ICU 55.1 55.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
calendar.h
Go to the documentation of this file.
1 /*
2 ********************************************************************************
3 * Copyright (C) 1997-2014, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
6 *
7 * File CALENDAR.H
8 *
9 * Modification History:
10 *
11 * Date Name Description
12 * 04/22/97 aliu Expanded and corrected comments and other header
13 * contents.
14 * 05/01/97 aliu Made equals(), before(), after() arguments const.
15 * 05/20/97 aliu Replaced fAreFieldsSet with fAreFieldsInSync and
16 * fAreAllFieldsSet.
17 * 07/27/98 stephen Sync up with JDK 1.2
18 * 11/15/99 weiv added YEAR_WOY and DOW_LOCAL
19 * to EDateFields
20 * 8/19/2002 srl Removed Javaisms
21 * 11/07/2003 srl Update, clean up documentation.
22 ********************************************************************************
23 */
24
25 #ifndef CALENDAR_H
26 #define CALENDAR_H
27
28 #include "unicode/utypes.h"
29
34 #if !UCONFIG_NO_FORMATTING
35
36 #include "unicode/uobject.h"
37 #include "unicode/locid.h"
38 #include "unicode/timezone.h"
39 #include "unicode/ucal.h"
40 #include "unicode/umisc.h"
41
42 U_NAMESPACE_BEGIN
43
44 class ICUServiceFactory;
45
49 typedef int32_t UFieldResolutionTable[12][8];
50
51 class BasicTimeZone;
200 class U_I18N_API Calendar : public UObject {
201 public:
202
209 enum EDateFields {
210 #ifndef U_HIDE_DEPRECATED_API
211 /*
212 * ERA may be defined on other platforms. To avoid any potential problems undefined it here.
213 */
214 #ifdef ERA
215 #undef ERA
216 #endif
217 ERA, // Example: 0..1
218 YEAR, // Example: 1..big number
219 MONTH, // Example: 0..11
220 WEEK_OF_YEAR, // Example: 1..53
221 WEEK_OF_MONTH, // Example: 1..4
222 DATE, // Example: 1..31
223 DAY_OF_YEAR, // Example: 1..365
224 DAY_OF_WEEK, // Example: 1..7
225 DAY_OF_WEEK_IN_MONTH, // Example: 1..4, may be specified as -1
226 AM_PM, // Example: 0..1
227 HOUR, // Example: 0..11
228 HOUR_OF_DAY, // Example: 0..23
229 MINUTE, // Example: 0..59
230 SECOND, // Example: 0..59
231 MILLISECOND, // Example: 0..999
232 ZONE_OFFSET, // Example: -12*U_MILLIS_PER_HOUR..12*U_MILLIS_PER_HOUR
233 DST_OFFSET, // Example: 0 or U_MILLIS_PER_HOUR
234 YEAR_WOY, // 'Y' Example: 1..big number - Year of Week of Year
235 DOW_LOCAL, // 'e' Example: 1..7 - Day of Week / Localized
236
237 EXTENDED_YEAR,
238 JULIAN_DAY,
239 MILLISECONDS_IN_DAY,
240 IS_LEAP_MONTH,
241
242 FIELD_COUNT = UCAL_FIELD_COUNT // See ucal.h for other fields.
243 #endif /* U_HIDE_DEPRECATED_API */
244 };
245
246 #ifndef U_HIDE_DEPRECATED_API
247
253 enum EDaysOfWeek {
254 SUNDAY = 1,
255 MONDAY,
256 TUESDAY,
257 WEDNESDAY,
258 THURSDAY,
259 FRIDAY,
260 SATURDAY
261 };
262
267 enum EMonths {
268 JANUARY,
269 FEBRUARY,
270 MARCH,
271 APRIL,
272 MAY,
273 JUNE,
274 JULY,
275 AUGUST,
276 SEPTEMBER,
277 OCTOBER,
278 NOVEMBER,
279 DECEMBER,
280 UNDECIMBER
281 };
282
287 enum EAmpm {
288 AM,
289 PM
290 };
291 #endif /* U_HIDE_DEPRECATED_API */
292
297 virtual ~Calendar();
298
305 virtual Calendar* clone(void) const = 0;
306
318 static Calendar* U_EXPORT2 createInstance(UErrorCode& success);
319
332 static Calendar* U_EXPORT2 createInstance(TimeZone* zoneToAdopt, UErrorCode& success);
333
345 static Calendar* U_EXPORT2 createInstance(const TimeZone& zone, UErrorCode& success);
346
357 static Calendar* U_EXPORT2 createInstance(const Locale& aLocale, UErrorCode& success);
358
372 static Calendar* U_EXPORT2 createInstance(TimeZone* zoneToAdopt, const Locale& aLocale, UErrorCode& success);
373
386 static Calendar* U_EXPORT2 createInstance(const TimeZone& zone, const Locale& aLocale, UErrorCode& success);
387
397 static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
398
399
416 static StringEnumeration* U_EXPORT2 getKeywordValuesForLocale(const char* key,
417 const Locale& locale, UBool commonlyUsed, UErrorCode& status);
418
426 static UDate U_EXPORT2 getNow(void);
427
441 inline UDate getTime(UErrorCode& status) const { return getTimeInMillis(status); }
442
453 inline void setTime(UDate date, UErrorCode& status) { setTimeInMillis(date, status); }
454
466 virtual UBool operator==(const Calendar& that) const;
467
476 UBool operator!=(const Calendar& that) const {return !operator==(that);}
477
488 virtual UBool isEquivalentTo(const Calendar& other) const;
489
504 UBool equals(const Calendar& when, UErrorCode& status) const;
505
519 UBool before(const Calendar& when, UErrorCode& status) const;
520
534 UBool after(const Calendar& when, UErrorCode& status) const;
535
556 virtual void add(EDateFields field, int32_t amount, UErrorCode& status);
557
578 virtual void add(UCalendarDateFields field, int32_t amount, UErrorCode& status);
579
580 #ifndef U_HIDE_DEPRECATED_API
581
613 inline void roll(EDateFields field, UBool up, UErrorCode& status);
614 #endif /* U_HIDE_DEPRECATED_API */
615
648 inline void roll(UCalendarDateFields field, UBool up, UErrorCode& status);
649
681 virtual void roll(EDateFields field, int32_t amount, UErrorCode& status);
682
714 virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode& status);
715
771 virtual int32_t fieldDifference(UDate when, EDateFields field, UErrorCode& status);
772
828 virtual int32_t fieldDifference(UDate when, UCalendarDateFields field, UErrorCode& status);
829
838 void adoptTimeZone(TimeZone* value);
839
847 void setTimeZone(const TimeZone& zone);
848
857 const TimeZone& getTimeZone(void) const;
858
867 TimeZone* orphanTimeZone(void);
868
877 virtual UBool inDaylightTime(UErrorCode& status) const = 0;
878
891 void setLenient(UBool lenient);
892
899 UBool isLenient(void) const;
900
921 void setRepeatedWallTimeOption(UCalendarWallTimeOption option);
922
932 UCalendarWallTimeOption getRepeatedWallTimeOption(void) const;
933
955 void setSkippedWallTimeOption(UCalendarWallTimeOption option);
956
967 UCalendarWallTimeOption getSkippedWallTimeOption(void) const;
968
969 #ifndef U_HIDE_DEPRECATED_API
970
976 void setFirstDayOfWeek(EDaysOfWeek value);
977 #endif /* U_HIDE_DEPRECATED_API */
978
985 void setFirstDayOfWeek(UCalendarDaysOfWeek value);
986
987 #ifndef U_HIDE_DEPRECATED_API
988
994 EDaysOfWeek getFirstDayOfWeek(void) const;
995 #endif /* U_HIDE_DEPRECATED_API */
996
1004 UCalendarDaysOfWeek getFirstDayOfWeek(UErrorCode &status) const;
1005
1015 void setMinimalDaysInFirstWeek(uint8_t value);
1016
1026 uint8_t getMinimalDaysInFirstWeek(void) const;
1027
1036 virtual int32_t getMinimum(EDateFields field) const;
1037
1046 virtual int32_t getMinimum(UCalendarDateFields field) const;
1047
1056 virtual int32_t getMaximum(EDateFields field) const;
1057
1066 virtual int32_t getMaximum(UCalendarDateFields field) const;
1067
1076 virtual int32_t getGreatestMinimum(EDateFields field) const;
1077
1086 virtual int32_t getGreatestMinimum(UCalendarDateFields field) const;
1087
1096 virtual int32_t getLeastMaximum(EDateFields field) const;
1097
1106 virtual int32_t getLeastMaximum(UCalendarDateFields field) const;
1107
1108 #ifndef U_HIDE_DEPRECATED_API
1109
1123 int32_t getActualMinimum(EDateFields field, UErrorCode& status) const;
1124 #endif /* U_HIDE_DEPRECATED_API */
1125
1140 virtual int32_t getActualMinimum(UCalendarDateFields field, UErrorCode& status) const;
1141
1142 #ifndef U_HIDE_DEPRECATED_API
1143
1159 int32_t getActualMaximum(EDateFields field, UErrorCode& status) const;
1160 #endif /* U_HIDE_DEPRECATED_API */
1161
1178 virtual int32_t getActualMaximum(UCalendarDateFields field, UErrorCode& status) const;
1179
1180 #ifndef U_HIDE_DEPRECATED_API
1181
1194 int32_t get(EDateFields field, UErrorCode& status) const;
1195 #endif /* U_HIDE_DEPRECATED_API */
1196
1210 int32_t get(UCalendarDateFields field, UErrorCode& status) const;
1211
1212 #ifndef U_HIDE_DEPRECATED_API
1213
1221 UBool isSet(EDateFields field) const;
1222 #endif /* U_HIDE_DEPRECATED_API */
1223
1232 UBool isSet(UCalendarDateFields field) const;
1233
1234 #ifndef U_HIDE_DEPRECATED_API
1235
1242 void set(EDateFields field, int32_t value);
1243 #endif /* U_HIDE_DEPRECATED_API */
1244
1252 void set(UCalendarDateFields field, int32_t value);
1253
1264 void set(int32_t year, int32_t month, int32_t date);
1265
1278 void set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute);
1279
1293 void set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute, int32_t second);
1294
1301 void clear(void);
1302
1303 #ifndef U_HIDE_DEPRECATED_API
1304
1312 void clear(EDateFields field);
1313 #endif /* U_HIDE_DEPRECATED_API */
1314
1323 void clear(UCalendarDateFields field);
1324
1340 virtual UClassID getDynamicClassID(void) const = 0;
1341
1374 virtual const char * getType() const = 0;
1375
1392 virtual UCalendarWeekdayType getDayOfWeekType(UCalendarDaysOfWeek dayOfWeek, UErrorCode &status) const;
1393
1408 virtual int32_t getWeekendTransition(UCalendarDaysOfWeek dayOfWeek, UErrorCode &status) const;
1409
1419 virtual UBool isWeekend(UDate date, UErrorCode &status) const;
1420
1428 virtual UBool isWeekend(void) const;
1429
1430 protected:
1431
1440 Calendar(UErrorCode& success);
1441
1448 Calendar(const Calendar& source);
1449
1456 Calendar& operator=(const Calendar& right);
1457
1468 Calendar(TimeZone* zone, const Locale& aLocale, UErrorCode& success);
1469
1479 Calendar(const TimeZone& zone, const Locale& aLocale, UErrorCode& success);
1480
1489 virtual void computeTime(UErrorCode& status);
1490
1502 virtual void computeFields(UErrorCode& status);
1503
1513 double getTimeInMillis(UErrorCode& status) const;
1514
1523 void setTimeInMillis( double millis, UErrorCode& status );
1524
1534 void complete(UErrorCode& status);
1535
1536 #ifndef U_HIDE_DEPRECATED_API
1537
1545 inline int32_t internalGet(EDateFields field) const {return fFields[field];}
1546 #endif /* U_HIDE_DEPRECATED_API */
1547
1548 #ifndef U_HIDE_INTERNAL_API
1549
1559 inline int32_t internalGet(UCalendarDateFields field, int32_t defaultValue) const {return fStamp[field]>kUnset ? fFields[field] : defaultValue;}
1560
1569 inline int32_t internalGet(UCalendarDateFields field) const {return fFields[field];}
1570 #endif /* U_HIDE_INTERNAL_API */
1571
1572 #ifndef U_HIDE_DEPRECATED_API
1573
1582 void internalSet(EDateFields field, int32_t value);
1583 #endif /* U_HIDE_DEPRECATED_API */
1584
1594 inline void internalSet(UCalendarDateFields field, int32_t value);
1595
1602 virtual void prepareGetActual(UCalendarDateFields field, UBool isMinimum, UErrorCode &status);
1603
1608 enum ELimitType {
1609 #ifndef U_HIDE_INTERNAL_API
1610 UCAL_LIMIT_MINIMUM = 0,
1611 UCAL_LIMIT_GREATEST_MINIMUM,
1612 UCAL_LIMIT_LEAST_MAXIMUM,
1613 UCAL_LIMIT_MAXIMUM,
1614 UCAL_LIMIT_COUNT
1615 #endif /* U_HIDE_INTERNAL_API */
1616 };
1617
1639 virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const = 0;
1640
1648 virtual int32_t getLimit(UCalendarDateFields field, ELimitType limitType) const;
1649
1650
1664 virtual int32_t handleComputeMonthStart(int32_t eyear, int32_t month,
1665 UBool useMonth) const = 0;
1666
1674 virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month) const ;
1675
1683 virtual int32_t handleGetYearLength(int32_t eyear) const;
1684
1685
1694 virtual int32_t handleGetExtendedYear() = 0;
1695
1704 virtual int32_t handleComputeJulianDay(UCalendarDateFields bestField);
1705
1714 virtual int32_t handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy);
1715
1724 virtual void validateField(UCalendarDateFields field, UErrorCode &status);
1725
1726 #ifndef U_HIDE_INTERNAL_API
1727
1733 int32_t computeJulianDay();
1734
1742 int32_t computeMillisInDay();
1743
1753 int32_t computeZoneOffset(double millis, int32_t millisInDay, UErrorCode &ec);
1754
1755
1764 int32_t newestStamp(UCalendarDateFields start, UCalendarDateFields end, int32_t bestSoFar) const;
1765
1771 enum {
1773 kResolveSTOP = -1,
1775 kResolveRemap = 32
1776 };
1777
1783 static const UFieldResolutionTable kDatePrecedence[];
1784
1790 static const UFieldResolutionTable kYearPrecedence[];
1791
1797 static const UFieldResolutionTable kDOWPrecedence[];
1798
1826 UCalendarDateFields resolveFields(const UFieldResolutionTable *precedenceTable);
1827 #endif /* U_HIDE_INTERNAL_API */
1828
1829
1833 virtual const UFieldResolutionTable* getFieldResolutionTable() const;
1834
1835 #ifndef U_HIDE_INTERNAL_API
1836
1841 UCalendarDateFields newerField(UCalendarDateFields defaultField, UCalendarDateFields alternateField) const;
1842 #endif /* U_HIDE_INTERNAL_API */
1843
1844
1845 private:
1854 int32_t getActualHelper(UCalendarDateFields field, int32_t startValue, int32_t endValue, UErrorCode &status) const;
1855
1856
1857 protected:
1862 UBool fIsTimeSet;
1863
1874 UBool fAreFieldsSet;
1875
1881 UBool fAreAllFieldsSet;
1882
1890 UBool fAreFieldsVirtuallySet;
1891
1898 UDate internalGetTime(void) const { return fTime; }
1899
1907 void internalSetTime(UDate time) { fTime = time; }
1908
1913 int32_t fFields[UCAL_FIELD_COUNT];
1914
1919 UBool fIsSet[UCAL_FIELD_COUNT];
1920
1924 enum {
1925 kUnset = 0,
1926 kInternallySet,
1927 kMinimumUserStamp
1928 };
1929
1936 int32_t fStamp[UCAL_FIELD_COUNT];
1937
1962 virtual void handleComputeFields(int32_t julianDay, UErrorCode &status);
1963
1964 #ifndef U_HIDE_INTERNAL_API
1965
1970 int32_t getGregorianYear() const {
1971 return fGregorianYear;
1972 }
1973
1979 int32_t getGregorianMonth() const {
1980 return fGregorianMonth;
1981 }
1982
1988 int32_t getGregorianDayOfYear() const {
1989 return fGregorianDayOfYear;
1990 }
1991
1997 int32_t getGregorianDayOfMonth() const {
1998 return fGregorianDayOfMonth;
1999 }
2000 #endif /* U_HIDE_INTERNAL_API */
2001
2008 virtual int32_t getDefaultMonthInYear(int32_t eyear) ;
2009
2010
2018 virtual int32_t getDefaultDayInMonth(int32_t eyear, int32_t month);
2019
2020 //-------------------------------------------------------------------------
2021 // Protected utility methods for use by subclasses. These are very handy
2022 // for implementing add, roll, and computeFields.
2023 //-------------------------------------------------------------------------
2024
2054 virtual void pinField(UCalendarDateFields field, UErrorCode& status);
2055
2099 int32_t weekNumber(int32_t desiredDay, int32_t dayOfPeriod, int32_t dayOfWeek);
2100
2101
2102 #ifndef U_HIDE_INTERNAL_API
2103
2133 inline int32_t weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek);
2134
2139 int32_t getLocalDOW();
2140 #endif /* U_HIDE_INTERNAL_API */
2141
2142 private:
2143
2147 int32_t fNextStamp;// = MINIMUM_USER_STAMP;
2148
2153 void recalculateStamp();
2154
2158 UDate fTime;
2159
2163 UBool fLenient;
2164
2169 TimeZone* fZone;
2170
2175 UCalendarWallTimeOption fRepeatedWallTime;
2176
2181 UCalendarWallTimeOption fSkippedWallTime;
2182
2191 UCalendarDaysOfWeek fFirstDayOfWeek;
2192 uint8_t fMinimalDaysInFirstWeek;
2193 UCalendarDaysOfWeek fWeekendOnset;
2194 int32_t fWeekendOnsetMillis;
2195 UCalendarDaysOfWeek fWeekendCease;
2196 int32_t fWeekendCeaseMillis;
2197
2208 void setWeekData(const Locale& desiredLocale, const char *type, UErrorCode& success);
2209
2219 void updateTime(UErrorCode& status);
2220
2226 int32_t fGregorianYear;
2227
2233 int32_t fGregorianMonth;
2234
2240 int32_t fGregorianDayOfYear;
2241
2247 int32_t fGregorianDayOfMonth;
2248
2249 /* calculations */
2250
2257 void computeGregorianAndDOWFields(int32_t julianDay, UErrorCode &ec);
2258
2259 protected:
2260
2268 void computeGregorianFields(int32_t julianDay, UErrorCode &ec);
2269
2270 private:
2271
2292 void computeWeekFields(UErrorCode &ec);
2293
2294
2303 void validateFields(UErrorCode &status);
2304
2313 void validateField(UCalendarDateFields field, int32_t min, int32_t max, UErrorCode& status);
2314
2315 protected:
2316 #ifndef U_HIDE_INTERNAL_API
2317
2326 static uint8_t julianDayToDayOfWeek(double julian);
2327 #endif /* U_HIDE_INTERNAL_API */
2328
2329 private:
2330 char validLocale[ULOC_FULLNAME_CAPACITY];
2331 char actualLocale[ULOC_FULLNAME_CAPACITY];
2332
2333 public:
2334 #if !UCONFIG_NO_SERVICE
2335
2339 #ifndef U_HIDE_INTERNAL_API
2340
2346 static StringEnumeration* getAvailableLocales(void);
2347
2361 static URegistryKey registerFactory(ICUServiceFactory* toAdopt, UErrorCode& status);
2362
2378 static UBool unregister(URegistryKey key, UErrorCode& status);
2379 #endif /* U_HIDE_INTERNAL_API */
2380
2385 friend class CalendarFactory;
2386
2391 friend class CalendarService;
2392
2397 friend class DefaultCalendarFactory;
2398 #endif /* !UCONFIG_NO_SERVICE */
2399
2404 virtual UBool haveDefaultCentury() const = 0;
2405
2410 virtual UDate defaultCenturyStart() const = 0;
2415 virtual int32_t defaultCenturyStartYear() const = 0;
2416
2423 Locale getLocale(ULocDataLocaleType type, UErrorCode &status) const;
2424
2430 virtual int32_t getRelatedYear(UErrorCode &status) const;
2431
2437 virtual void setRelatedYear(int32_t year);
2438
2439 #ifndef U_HIDE_INTERNAL_API
2440
2446 const char* getLocaleID(ULocDataLocaleType type, UErrorCode &status) const;
2447 #endif /* U_HIDE_INTERNAL_API */
2448
2449 private:
2454 BasicTimeZone* getBasicTimeZone() const;
2455
2463 UBool getImmediatePreviousZoneTransition(UDate base, UDate *transitionTime, UErrorCode& status) const;
2464
2465 public:
2466 #ifndef U_HIDE_INTERNAL_API
2467
2475 static Calendar * U_EXPORT2 makeInstance(
2476 const Locale &locale, UErrorCode &status);
2477
2488 static void U_EXPORT2 getCalendarTypeFromLocale(
2489 const Locale &locale,
2490 char *typeBuffer,
2491 int32_t typeBufferSize,
2492 UErrorCode &status);
2493 #endif /* U_HIDE_INTERNAL_API */
2494 };
2495
2496 // -------------------------------------
2497
2498 inline Calendar*
2499 Calendar::createInstance(TimeZone* zone, UErrorCode& errorCode)
2500 {
2501 // since the Locale isn't specified, use the default locale
2502 return createInstance(zone, Locale::getDefault(), errorCode);
2503 }
2504
2505 // -------------------------------------
2506
2507 inline void
2508 Calendar::roll(UCalendarDateFields field, UBool up, UErrorCode& status)
2509 {
2510 roll(field, (int32_t)(up ? +1 : -1), status);
2511 }
2512
2513 #ifndef U_HIDE_DEPRECATED_API
2514 inline void
2515 Calendar::roll(EDateFields field, UBool up, UErrorCode& status)
2516 {
2517 roll((UCalendarDateFields) field, up, status);
2518 }
2519 #endif /* U_HIDE_DEPRECATED_API */
2520
2521
2522 // -------------------------------------
2523
2529 inline void
2530 Calendar::internalSet(UCalendarDateFields field, int32_t value)
2531 {
2532 fFields[field] = value;
2533 fStamp[field] = kInternallySet;
2534 fIsSet[field] = TRUE; // Remove later
2535 }
2536
2537
2538 #ifndef U_HIDE_INTERNAL_API
2539 inline int32_t Calendar::weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek)
2540 {
2541 return weekNumber(dayOfPeriod, dayOfPeriod, dayOfWeek);
2542 }
2543 #endif /* U_HIDE_INTERNAL_API */
2544
2545 U_NAMESPACE_END
2546
2547 #endif /* #if !UCONFIG_NO_FORMATTING */
2548
2549 #endif // _CALENDAR