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

- File List
- Globals

# calendar.h

Go to the documentation of this file.

    00001 /*
    00002 ********************************************************************************
    00003 *   Copyright (C) 1997-2010, International Business Machines
    00004 *   Corporation and others.  All Rights Reserved.
    00005 ********************************************************************************
    00006 *
    00007 * File CALENDAR.H
    00008 *
    00009 * Modification History:
    00010 *
    00011 *   Date        Name        Description
    00012 *   04/22/97    aliu        Expanded and corrected comments and other header
    00013 *                           contents.
    00014 *   05/01/97    aliu        Made equals(), before(), after() arguments const.
    00015 *   05/20/97    aliu        Replaced fAreFieldsSet with fAreFieldsInSync and
    00016 *                           fAreAllFieldsSet.
    00017 *   07/27/98    stephen     Sync up with JDK 1.2
    00018 *   11/15/99    weiv        added YEAR_WOY and DOW_LOCAL
    00019 *                           to EDateFields
    00020 *    8/19/2002  srl         Removed Javaisms
    00021 *   11/07/2003  srl         Update, clean up documentation.
    00022 ********************************************************************************
    00023 */
    00024
    00025 #ifndef CALENDAR_H
    00026 #define CALENDAR_H
    00027
    00028 #include "unicode/utypes.h"
    00029
    00034 #if !UCONFIG_NO_FORMATTING
    00035
    00036 #include "unicode/uobject.h"
    00037 #include "unicode/locid.h"
    00038 #include "unicode/timezone.h"
    00039 #include "unicode/ucal.h"
    00040 #include "unicode/umisc.h"
    00041
    00042 U_NAMESPACE_BEGIN
    00043
    00044 class ICUServiceFactory;
    00045
    00049 typedef int32_t UFieldResolutionTable[12][8];
    00050
    00165 class U_I18N_API Calendar : public UObject {
    00166 public:
    00167
    00174     enum EDateFields {
    00175 #ifndef U_HIDE_DEPRECATED_API
    00176 /*
    00177  * ERA may be defined on other platforms. To avoid any potential problems undefined it here.
    00178  */
    00179 #ifdef ERA
    00180 #undef ERA
    00181 #endif
    00182         ERA,                  // Example: 0..1
    00183         YEAR,                 // Example: 1..big number
    00184         MONTH,                // Example: 0..11
    00185         WEEK_OF_YEAR,         // Example: 1..53
    00186         WEEK_OF_MONTH,        // Example: 1..4
    00187         DATE,                 // Example: 1..31
    00188         DAY_OF_YEAR,          // Example: 1..365
    00189         DAY_OF_WEEK,          // Example: 1..7
    00190         DAY_OF_WEEK_IN_MONTH, // Example: 1..4, may be specified as -1
    00191         AM_PM,                // Example: 0..1
    00192         HOUR,                 // Example: 0..11
    00193         HOUR_OF_DAY,          // Example: 0..23
    00194         MINUTE,               // Example: 0..59
    00195         SECOND,               // Example: 0..59
    00196         MILLISECOND,          // Example: 0..999
    00197         ZONE_OFFSET,          // Example: -12*U_MILLIS_PER_HOUR..12*U_MILLIS_PER_HOUR
    00198         DST_OFFSET,           // Example: 0 or U_MILLIS_PER_HOUR
    00199         YEAR_WOY,             // 'Y' Example: 1..big number - Year of Week of Year
    00200         DOW_LOCAL,            // 'e' Example: 1..7 - Day of Week / Localized
    00201
    00202         EXTENDED_YEAR,
    00203         JULIAN_DAY,
    00204         MILLISECONDS_IN_DAY,
    00205         IS_LEAP_MONTH,
    00206
    00207         FIELD_COUNT = UCAL_FIELD_COUNT // See ucal.h for other fields.
    00208 #endif /* U_HIDE_DEPRECATED_API */
    00209     };
    00210
    00217     enum EDaysOfWeek {
    00218 #ifndef U_HIDE_DEPRECATED_API
    00219         SUNDAY = 1,
    00220         MONDAY,
    00221         TUESDAY,
    00222         WEDNESDAY,
    00223         THURSDAY,
    00224         FRIDAY,
    00225         SATURDAY
    00226 #endif /* U_HIDE_DEPRECATED_API */
    00227     };
    00228
    00233     enum EMonths {
    00234 #ifndef U_HIDE_DEPRECATED_API
    00235         JANUARY,
    00236         FEBRUARY,
    00237         MARCH,
    00238         APRIL,
    00239         MAY,
    00240         JUNE,
    00241         JULY,
    00242         AUGUST,
    00243         SEPTEMBER,
    00244         OCTOBER,
    00245         NOVEMBER,
    00246         DECEMBER,
    00247         UNDECIMBER
    00248 #endif /* U_HIDE_DEPRECATED_API */
    00249     };
    00250
    00255     enum EAmpm {
    00256 #ifndef U_HIDE_DEPRECATED_API
    00257         AM,
    00258         PM
    00259 #endif /* U_HIDE_DEPRECATED_API */
    00260     };
    00261
    00266     virtual ~Calendar();
    00267
    00274     virtual Calendar* clone(void) const = 0;
    00275
    00287     static Calendar* U_EXPORT2 createInstance(UErrorCode& success);
    00288
    00301     static Calendar* U_EXPORT2 createInstance(TimeZone* zoneToAdopt, UErrorCode& success);
    00302
    00314     static Calendar* U_EXPORT2 createInstance(const TimeZone& zone, UErrorCode& success);
    00315
    00326     static Calendar* U_EXPORT2 createInstance(const Locale& aLocale, UErrorCode& success);
    00327
    00341     static Calendar* U_EXPORT2 createInstance(TimeZone* zoneToAdopt, const Locale& aLocale, UErrorCode& success);
    00342
    00355     static Calendar* U_EXPORT2 createInstance(const TimeZone& zoneToAdopt, const Locale& aLocale, UErrorCode& success);
    00356
    00366     static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
    00367
    00368
    00385     static StringEnumeration* U_EXPORT2 getKeywordValuesForLocale(const char* key,
    00386                     const Locale& locale, UBool commonlyUsed, UErrorCode& status);
    00387
    00395     static UDate U_EXPORT2 getNow(void);
    00396
    00410     inline UDate getTime(UErrorCode& status) const { return getTimeInMillis(status); }
    00411
    00422     inline void setTime(UDate date, UErrorCode& status) { setTimeInMillis(date, status); }
    00423
    00435     virtual UBool operator==(const Calendar& that) const;
    00436
    00445     UBool operator!=(const Calendar& that) const {return !operator==(that);}
    00446
    00457     virtual UBool isEquivalentTo(const Calendar& other) const;
    00458
    00473     UBool equals(const Calendar& when, UErrorCode& status) const;
    00474
    00488     UBool before(const Calendar& when, UErrorCode& status) const;
    00489
    00503     UBool after(const Calendar& when, UErrorCode& status) const;
    00504
    00522     virtual void add(EDateFields field, int32_t amount, UErrorCode& status);
    00523
    00541     virtual void add(UCalendarDateFields field, int32_t amount, UErrorCode& status);
    00542
    00566     inline void roll(EDateFields field, UBool up, UErrorCode& status);
    00567
    00591     inline void roll(UCalendarDateFields field, UBool up, UErrorCode& status);
    00592
    00615     virtual void roll(EDateFields field, int32_t amount, UErrorCode& status);
    00616
    00639     virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode& status);
    00640
    00696     virtual int32_t fieldDifference(UDate when, EDateFields field, UErrorCode& status);
    00697
    00753     virtual int32_t fieldDifference(UDate when, UCalendarDateFields field, UErrorCode& status);
    00754
    00763     void adoptTimeZone(TimeZone* value);
    00764
    00772     void setTimeZone(const TimeZone& zone);
    00773
    00782     const TimeZone& getTimeZone(void) const;
    00783
    00792     TimeZone* orphanTimeZone(void);
    00793
    00802     virtual UBool inDaylightTime(UErrorCode& status) const = 0;
    00803
    00816     void setLenient(UBool lenient);
    00817
    00824     UBool isLenient(void) const;
    00825
    00832     void setFirstDayOfWeek(EDaysOfWeek value);
    00833
    00840     void setFirstDayOfWeek(UCalendarDaysOfWeek value);
    00841
    00848     EDaysOfWeek getFirstDayOfWeek(void) const;
    00849
    00857     UCalendarDaysOfWeek getFirstDayOfWeek(UErrorCode &status) const;
    00858
    00868     void setMinimalDaysInFirstWeek(uint8_t value);
    00869
    00879     uint8_t getMinimalDaysInFirstWeek(void) const;
    00880
    00889     virtual int32_t getMinimum(EDateFields field) const;
    00890
    00899     virtual int32_t getMinimum(UCalendarDateFields field) const;
    00900
    00909     virtual int32_t getMaximum(EDateFields field) const;
    00910
    00919     virtual int32_t getMaximum(UCalendarDateFields field) const;
    00920
    00929     virtual int32_t getGreatestMinimum(EDateFields field) const;
    00930
    00939     virtual int32_t getGreatestMinimum(UCalendarDateFields field) const;
    00940
    00949     virtual int32_t getLeastMaximum(EDateFields field) const;
    00950
    00959     virtual int32_t getLeastMaximum(UCalendarDateFields field) const;
    00960
    00975     int32_t getActualMinimum(EDateFields field, UErrorCode& status) const;
    00976
    00991     virtual int32_t getActualMinimum(UCalendarDateFields field, UErrorCode& status) const;
    00992
    01009     int32_t getActualMaximum(EDateFields field, UErrorCode& status) const;
    01010
    01027     virtual int32_t getActualMaximum(UCalendarDateFields field, UErrorCode& status) const;
    01028
    01042     int32_t get(EDateFields field, UErrorCode& status) const;
    01043
    01057     int32_t get(UCalendarDateFields field, UErrorCode& status) const;
    01058
    01067     UBool isSet(EDateFields field) const;
    01068
    01077     UBool isSet(UCalendarDateFields field) const;
    01078
    01086     void set(EDateFields field, int32_t value);
    01087
    01095     void set(UCalendarDateFields field, int32_t value);
    01096
    01107     void set(int32_t year, int32_t month, int32_t date);
    01108
    01121     void set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute);
    01122
    01136     void set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute, int32_t second);
    01137
    01144     void clear(void);
    01145
    01154     void clear(EDateFields field);
    01155
    01164     void clear(UCalendarDateFields field);
    01165
    01181     virtual UClassID getDynamicClassID(void) const = 0;
    01182
    01191     virtual const char * getType() const = 0;
    01192
    01208     virtual UCalendarWeekdayType getDayOfWeekType(UCalendarDaysOfWeek dayOfWeek, UErrorCode &status) const;
    01209
    01224     virtual int32_t getWeekendTransition(UCalendarDaysOfWeek dayOfWeek, UErrorCode &status) const;
    01225
    01235     virtual UBool isWeekend(UDate date, UErrorCode &status) const;
    01236
    01244     virtual UBool isWeekend(void) const;
    01245
    01246 protected:
    01247
    01256     Calendar(UErrorCode& success);
    01257
    01264     Calendar(const Calendar& source);
    01265
    01272     Calendar& operator=(const Calendar& right);
    01273
    01284     Calendar(TimeZone* zone, const Locale& aLocale, UErrorCode& success);
    01285
    01295     Calendar(const TimeZone& zone, const Locale& aLocale, UErrorCode& success);
    01296
    01305     virtual void computeTime(UErrorCode& status);
    01306
    01318     virtual void computeFields(UErrorCode& status);
    01319
    01329     double getTimeInMillis(UErrorCode& status) const;
    01330
    01339     void setTimeInMillis( double millis, UErrorCode& status );
    01340
    01350     void complete(UErrorCode& status);
    01351
    01360     inline int32_t internalGet(EDateFields field) const {return fFields[field];}
    01361
    01372     inline int32_t internalGet(UCalendarDateFields field, int32_t defaultValue) const {return fStamp[field]>kUnset ? fFields[field] : defaultValue;}
    01373
    01382     inline int32_t internalGet(UCalendarDateFields field) const {return fFields[field];}
    01383
    01393     void internalSet(EDateFields field, int32_t value);
    01394
    01404     inline void internalSet(UCalendarDateFields field, int32_t value);
    01405
    01412     virtual void prepareGetActual(UCalendarDateFields field, UBool isMinimum, UErrorCode &status);
    01413
    01418     enum ELimitType {
    01419       UCAL_LIMIT_MINIMUM = 0,
    01420       UCAL_LIMIT_GREATEST_MINIMUM,
    01421       UCAL_LIMIT_LEAST_MAXIMUM,
    01422       UCAL_LIMIT_MAXIMUM,
    01423       UCAL_LIMIT_COUNT
    01424     };
    01425
    01447     virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const = 0;
    01448
    01456     virtual int32_t getLimit(UCalendarDateFields field, ELimitType limitType) const;
    01457
    01458
    01472     virtual int32_t handleComputeMonthStart(int32_t eyear, int32_t month,
    01473                                                    UBool useMonth) const  = 0;
    01474
    01482     virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month) const ;
    01483
    01491     virtual int32_t handleGetYearLength(int32_t eyear) const;
    01492
    01493
    01502     virtual int32_t handleGetExtendedYear() = 0;
    01503
    01512     virtual int32_t handleComputeJulianDay(UCalendarDateFields bestField);
    01513
    01522     virtual int32_t handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy);
    01523
    01530     int32_t computeJulianDay();
    01531
    01539     int32_t computeMillisInDay();
    01540
    01550     int32_t computeZoneOffset(double millis, int32_t millisInDay, UErrorCode &ec);
    01551
    01552
    01561     int32_t newestStamp(UCalendarDateFields start, UCalendarDateFields end, int32_t bestSoFar) const;
    01562
    01568     enum {
    01570       kResolveSTOP = -1,
    01572       kResolveRemap = 32
    01573     };
    01574
    01580     static const UFieldResolutionTable kDatePrecedence[];
    01581
    01587     static const UFieldResolutionTable kYearPrecedence[];
    01588
    01594     static const UFieldResolutionTable kDOWPrecedence[];
    01595
    01623     UCalendarDateFields resolveFields(const UFieldResolutionTable *precedenceTable);
    01624
    01625
    01629     virtual const UFieldResolutionTable* getFieldResolutionTable() const;
    01630
    01636     UCalendarDateFields newerField(UCalendarDateFields defaultField, UCalendarDateFields alternateField) const;
    01637
    01638
    01639 private:
    01648     int32_t getActualHelper(UCalendarDateFields field, int32_t startValue, int32_t endValue, UErrorCode &status) const;
    01649
    01650
    01651 protected:
    01656     UBool      fIsTimeSet;
    01657
    01668     UBool      fAreFieldsSet;
    01669
    01675     UBool      fAreAllFieldsSet;
    01676
    01684     UBool fAreFieldsVirtuallySet;
    01685
    01692     UDate        internalGetTime(void) const     { return fTime; }
    01693
    01701     void        internalSetTime(UDate time)     { fTime = time; }
    01702
    01707     int32_t     fFields[UCAL_FIELD_COUNT];
    01708
    01713     UBool      fIsSet[UCAL_FIELD_COUNT];
    01714
    01718     enum {
    01719         kUnset                 = 0,
    01720         kInternallySet,
    01721         kMinimumUserStamp
    01722     };
    01723
    01730     int32_t        fStamp[UCAL_FIELD_COUNT];
    01731
    01756     virtual void handleComputeFields(int32_t julianDay, UErrorCode &status);
    01757
    01763     int32_t getGregorianYear() const {
    01764         return fGregorianYear;
    01765     }
    01766
    01772     int32_t getGregorianMonth() const {
    01773         return fGregorianMonth;
    01774     }
    01775
    01781     int32_t getGregorianDayOfYear() const {
    01782         return fGregorianDayOfYear;
    01783     }
    01784
    01790     int32_t getGregorianDayOfMonth() const {
    01791       return fGregorianDayOfMonth;
    01792     }
    01793
    01800     virtual int32_t getDefaultMonthInYear(int32_t eyear) ;
    01801
    01802
    01810     virtual int32_t getDefaultDayInMonth(int32_t eyear, int32_t month);
    01811
    01812     //-------------------------------------------------------------------------
    01813     // Protected utility methods for use by subclasses.  These are very handy
    01814     // for implementing add, roll, and computeFields.
    01815     //-------------------------------------------------------------------------
    01816
    01846     virtual void pinField(UCalendarDateFields field, UErrorCode& status);
    01847
    01891     int32_t weekNumber(int32_t desiredDay, int32_t dayOfPeriod, int32_t dayOfWeek);
    01892
    01893
    01924     inline int32_t weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek);
    01925
    01930     int32_t getLocalDOW();
    01931
    01932 private:
    01933
    01937     int32_t fNextStamp;// = MINIMUM_USER_STAMP;
    01938
    01942     UDate        fTime;
    01943
    01947     UBool      fLenient;
    01948
    01953     TimeZone*   fZone;
    01954
    01963     UCalendarDaysOfWeek fFirstDayOfWeek;
    01964     uint8_t     fMinimalDaysInFirstWeek;
    01965     UCalendarDaysOfWeek fWeekendOnset;
    01966     int32_t fWeekendOnsetMillis;
    01967     UCalendarDaysOfWeek fWeekendCease;
    01968     int32_t fWeekendCeaseMillis;
    01969
    01980     void        setWeekData(const Locale& desiredLocale, const char *type, UErrorCode& success);
    01981
    01991     void updateTime(UErrorCode& status);
    01992
    01998     int32_t fGregorianYear;
    01999
    02005     int32_t fGregorianMonth;
    02006
    02012     int32_t fGregorianDayOfYear;
    02013
    02019     int32_t fGregorianDayOfMonth;
    02020
    02021     /* calculations */
    02022
    02029     void computeGregorianAndDOWFields(int32_t julianDay, UErrorCode &ec);
    02030
    02031 protected:
    02032
    02040     void computeGregorianFields(int32_t julianDay, UErrorCode &ec);
    02041
    02042 private:
    02043
    02064     void computeWeekFields(UErrorCode &ec);
    02065
    02066
    02075     void validateFields(UErrorCode &status);
    02076
    02085     virtual void validateField(UCalendarDateFields field, UErrorCode &status);
    02086
    02095     void validateField(UCalendarDateFields field, int32_t min, int32_t max, UErrorCode& status);
    02096
    02097  protected:
    02107     static uint8_t julianDayToDayOfWeek(double julian);
    02108
    02109  private:
    02110     char validLocale[ULOC_FULLNAME_CAPACITY];
    02111     char actualLocale[ULOC_FULLNAME_CAPACITY];
    02112
    02113  public:
    02114 #if !UCONFIG_NO_SERVICE
    02115
    02125     static StringEnumeration* getAvailableLocales(void);
    02126
    02135     static URegistryKey registerFactory(ICUServiceFactory* toAdopt, UErrorCode& status);
    02136
    02147     static UBool unregister(URegistryKey key, UErrorCode& status);
    02148
    02153     friend class CalendarFactory;
    02154
    02159     friend class CalendarService;
    02160
    02165     friend class DefaultCalendarFactory;
    02166 #endif /* !UCONFIG_NO_SERVICE */
    02167
    02172     virtual UBool haveDefaultCentury() const = 0;
    02173
    02178     virtual UDate defaultCenturyStart() const = 0;
    02183     virtual int32_t defaultCenturyStartYear() const = 0;
    02184
    02191     Locale getLocale(ULocDataLocaleType type, UErrorCode &status) const;
    02192
    02199     const char* getLocaleID(ULocDataLocaleType type, UErrorCode &status) const;
    02200
    02201 };
    02202
    02203 // -------------------------------------
    02204
    02205 inline Calendar*
    02206 Calendar::createInstance(TimeZone* zone, UErrorCode& errorCode)
    02207 {
    02208     // since the Locale isn't specified, use the default locale
    02209     return createInstance(zone, Locale::getDefault(), errorCode);
    02210 }
    02211
    02212 // -------------------------------------
    02213
    02214 inline void
    02215 Calendar::roll(UCalendarDateFields field, UBool up, UErrorCode& status)
    02216 {
    02217     roll(field, (int32_t)(up ? +1 : -1), status);
    02218 }
    02219
    02220 inline void
    02221 Calendar::roll(EDateFields field, UBool up, UErrorCode& status)
    02222 {
    02223     roll((UCalendarDateFields) field, up, status);
    02224 }
    02225
    02226
    02227 // -------------------------------------
    02228
    02234 inline void
    02235 Calendar::internalSet(UCalendarDateFields field, int32_t value)
    02236 {
    02237     fFields[field] = value;
    02238     fStamp[field] = kInternallySet;
    02239     fIsSet[field]     = TRUE; // Remove later
    02240 }
    02241
    02242 inline int32_t  Calendar::weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek)
    02243 {
    02244   return weekNumber(dayOfPeriod, dayOfPeriod, dayOfWeek);
    02245 }
    02246
    02247
    02248 U_NAMESPACE_END
    02249
    02250 #endif /* #if !UCONFIG_NO_FORMATTING */
    02251
    02252 #endif // _CALENDAR

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines