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

- Search for |

- File List
- Globals

# calendar.h

Go to the documentation of this file.

    00001 /*
    00002 ********************************************************************************
    00003 *   Copyright (C) 1997-2009, 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         ERA,                  // Example: 0..1
    00177         YEAR,                 // Example: 1..big number
    00178         MONTH,                // Example: 0..11
    00179         WEEK_OF_YEAR,         // Example: 1..53
    00180         WEEK_OF_MONTH,        // Example: 1..4
    00181         DATE,                 // Example: 1..31
    00182         DAY_OF_YEAR,          // Example: 1..365
    00183         DAY_OF_WEEK,          // Example: 1..7
    00184         DAY_OF_WEEK_IN_MONTH, // Example: 1..4, may be specified as -1
    00185         AM_PM,                // Example: 0..1
    00186         HOUR,                 // Example: 0..11
    00187         HOUR_OF_DAY,          // Example: 0..23
    00188         MINUTE,               // Example: 0..59
    00189         SECOND,               // Example: 0..59
    00190         MILLISECOND,          // Example: 0..999
    00191         ZONE_OFFSET,          // Example: -12*U_MILLIS_PER_HOUR..12*U_MILLIS_PER_HOUR
    00192         DST_OFFSET,           // Example: 0 or U_MILLIS_PER_HOUR
    00193         YEAR_WOY,             // 'Y' Example: 1..big number - Year of Week of Year
    00194         DOW_LOCAL,            // 'e' Example: 1..7 - Day of Week / Localized
    00195
    00196                 EXTENDED_YEAR,
    00197                 JULIAN_DAY,
    00198                 MILLISECONDS_IN_DAY,
    00199                 IS_LEAP_MONTH,
    00200
    00201         FIELD_COUNT = UCAL_FIELD_COUNT // See ucal.h for other fields.
    00202 #endif /* U_HIDE_DEPRECATED_API */
    00203     };
    00204
    00211     enum EDaysOfWeek {
    00212 #ifndef U_HIDE_DEPRECATED_API
    00213         SUNDAY = 1,
    00214         MONDAY,
    00215         TUESDAY,
    00216         WEDNESDAY,
    00217         THURSDAY,
    00218         FRIDAY,
    00219         SATURDAY
    00220 #endif /* U_HIDE_DEPRECATED_API */
    00221     };
    00222
    00227     enum EMonths {
    00228 #ifndef U_HIDE_DEPRECATED_API
    00229         JANUARY,
    00230         FEBRUARY,
    00231         MARCH,
    00232         APRIL,
    00233         MAY,
    00234         JUNE,
    00235         JULY,
    00236         AUGUST,
    00237         SEPTEMBER,
    00238         OCTOBER,
    00239         NOVEMBER,
    00240         DECEMBER,
    00241         UNDECIMBER
    00242 #endif /* U_HIDE_DEPRECATED_API */
    00243     };
    00244
    00249     enum EAmpm {
    00250 #ifndef U_HIDE_DEPRECATED_API
    00251         AM,
    00252         PM
    00253 #endif /* U_HIDE_DEPRECATED_API */
    00254     };
    00255
    00260     virtual ~Calendar();
    00261
    00268     virtual Calendar* clone(void) const = 0;
    00269
    00281     static Calendar* U_EXPORT2 createInstance(UErrorCode& success);
    00282
    00295     static Calendar* U_EXPORT2 createInstance(TimeZone* zoneToAdopt, UErrorCode& success);
    00296
    00308     static Calendar* U_EXPORT2 createInstance(const TimeZone& zone, UErrorCode& success);
    00309
    00320     static Calendar* U_EXPORT2 createInstance(const Locale& aLocale, UErrorCode& success);
    00321
    00335     static Calendar* U_EXPORT2 createInstance(TimeZone* zoneToAdopt, const Locale& aLocale, UErrorCode& success);
    00336
    00349     static Calendar* U_EXPORT2 createInstance(const TimeZone& zoneToAdopt, const Locale& aLocale, UErrorCode& success);
    00350
    00360     static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
    00361
    00362
    00379     static StringEnumeration* U_EXPORT2 getKeywordValuesForLocale(const char* key,
    00380                     const Locale& locale, UBool commonlyUsed, UErrorCode& status);
    00381
    00389     static UDate U_EXPORT2 getNow(void);
    00390
    00404     inline UDate getTime(UErrorCode& status) const { return getTimeInMillis(status); }
    00405
    00416     inline void setTime(UDate date, UErrorCode& status) { setTimeInMillis(date, status); }
    00417
    00429     virtual UBool operator==(const Calendar& that) const;
    00430
    00439     UBool operator!=(const Calendar& that) const {return !operator==(that);}
    00440
    00451     virtual UBool isEquivalentTo(const Calendar& other) const;
    00452
    00467     UBool equals(const Calendar& when, UErrorCode& status) const;
    00468
    00482     UBool before(const Calendar& when, UErrorCode& status) const;
    00483
    00497     UBool after(const Calendar& when, UErrorCode& status) const;
    00498
    00516     virtual void add(EDateFields field, int32_t amount, UErrorCode& status);
    00517
    00535     virtual void add(UCalendarDateFields field, int32_t amount, UErrorCode& status);
    00536
    00560     inline void roll(EDateFields field, UBool up, UErrorCode& status);
    00561
    00585     inline void roll(UCalendarDateFields field, UBool up, UErrorCode& status);
    00586
    00609     virtual void roll(EDateFields field, int32_t amount, UErrorCode& status);
    00610
    00633     virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode& status);
    00634
    00690     virtual int32_t fieldDifference(UDate when, EDateFields field, UErrorCode& status);
    00691
    00747     virtual int32_t fieldDifference(UDate when, UCalendarDateFields field, UErrorCode& status);
    00748
    00757     void adoptTimeZone(TimeZone* value);
    00758
    00766     void setTimeZone(const TimeZone& zone);
    00767
    00776     const TimeZone& getTimeZone(void) const;
    00777
    00786     TimeZone* orphanTimeZone(void);
    00787
    00796     virtual UBool inDaylightTime(UErrorCode& status) const = 0;
    00797
    00810     void setLenient(UBool lenient);
    00811
    00818     UBool isLenient(void) const;
    00819
    00826     void setFirstDayOfWeek(EDaysOfWeek value);
    00827
    00834     void setFirstDayOfWeek(UCalendarDaysOfWeek value);
    00835
    00842     EDaysOfWeek getFirstDayOfWeek(void) const;
    00843
    00851     UCalendarDaysOfWeek getFirstDayOfWeek(UErrorCode &status) const;
    00852
    00862     void setMinimalDaysInFirstWeek(uint8_t value);
    00863
    00873     uint8_t getMinimalDaysInFirstWeek(void) const;
    00874
    00883     virtual int32_t getMinimum(EDateFields field) const;
    00884
    00893     virtual int32_t getMinimum(UCalendarDateFields field) const;
    00894
    00903     virtual int32_t getMaximum(EDateFields field) const;
    00904
    00913     virtual int32_t getMaximum(UCalendarDateFields field) const;
    00914
    00923     virtual int32_t getGreatestMinimum(EDateFields field) const;
    00924
    00933     virtual int32_t getGreatestMinimum(UCalendarDateFields field) const;
    00934
    00943     virtual int32_t getLeastMaximum(EDateFields field) const;
    00944
    00953     virtual int32_t getLeastMaximum(UCalendarDateFields field) const;
    00954
    00969     int32_t getActualMinimum(EDateFields field, UErrorCode& status) const;
    00970
    00985     virtual int32_t getActualMinimum(UCalendarDateFields field, UErrorCode& status) const;
    00986
    01003     int32_t getActualMaximum(EDateFields field, UErrorCode& status) const;
    01004
    01021     virtual int32_t getActualMaximum(UCalendarDateFields field, UErrorCode& status) const;
    01022
    01036     int32_t get(EDateFields field, UErrorCode& status) const;
    01037
    01051     int32_t get(UCalendarDateFields field, UErrorCode& status) const;
    01052
    01061     UBool isSet(EDateFields field) const;
    01062
    01071     UBool isSet(UCalendarDateFields field) const;
    01072
    01080     void set(EDateFields field, int32_t value);
    01081
    01089     void set(UCalendarDateFields field, int32_t value);
    01090
    01101     void set(int32_t year, int32_t month, int32_t date);
    01102
    01115     void set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute);
    01116
    01130     void set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute, int32_t second);
    01131
    01138     void clear(void);
    01139
    01148     void clear(EDateFields field);
    01149
    01158     void clear(UCalendarDateFields field);
    01159
    01175     virtual UClassID getDynamicClassID(void) const = 0;
    01176
    01185     virtual const char * getType() const = 0;
    01186
    01187 protected:
    01188
    01197     Calendar(UErrorCode& success);
    01198
    01205     Calendar(const Calendar& source);
    01206
    01213     Calendar& operator=(const Calendar& right);
    01214
    01225     Calendar(TimeZone* zone, const Locale& aLocale, UErrorCode& success);
    01226
    01236     Calendar(const TimeZone& zone, const Locale& aLocale, UErrorCode& success);
    01237
    01246     virtual void computeTime(UErrorCode& status);
    01247
    01259     virtual void computeFields(UErrorCode& status);
    01260
    01270     double getTimeInMillis(UErrorCode& status) const;
    01271
    01280     void setTimeInMillis( double millis, UErrorCode& status );
    01281
    01291     void complete(UErrorCode& status);
    01292
    01301     inline int32_t internalGet(EDateFields field) const {return fFields[field];}
    01302
    01313     inline int32_t internalGet(UCalendarDateFields field, int32_t defaultValue) const {return fStamp[field]>kUnset ? fFields[field] : defaultValue;}
    01314
    01323     inline int32_t internalGet(UCalendarDateFields field) const {return fFields[field];}
    01324
    01334     void internalSet(EDateFields field, int32_t value);
    01335
    01345     inline void internalSet(UCalendarDateFields field, int32_t value);
    01346
    01353     virtual void prepareGetActual(UCalendarDateFields field, UBool isMinimum, UErrorCode &status);
    01354
    01359     enum ELimitType {
    01360       UCAL_LIMIT_MINIMUM = 0,
    01361       UCAL_LIMIT_GREATEST_MINIMUM,
    01362       UCAL_LIMIT_LEAST_MAXIMUM,
    01363       UCAL_LIMIT_MAXIMUM,
    01364       UCAL_LIMIT_COUNT
    01365     };
    01366
    01388     virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const = 0;
    01389
    01397     virtual int32_t getLimit(UCalendarDateFields field, ELimitType limitType) const;
    01398
    01399
    01413     virtual int32_t handleComputeMonthStart(int32_t eyear, int32_t month,
    01414                                                    UBool useMonth) const  = 0;
    01415
    01423     virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month) const ;
    01424
    01432     virtual int32_t handleGetYearLength(int32_t eyear) const;
    01433
    01434
    01443     virtual int32_t handleGetExtendedYear() = 0;
    01444
    01453     virtual int32_t handleComputeJulianDay(UCalendarDateFields bestField);
    01454
    01463     virtual int32_t handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy);
    01464
    01471     int32_t computeJulianDay();
    01472
    01480     int32_t computeMillisInDay();
    01481
    01491     int32_t computeZoneOffset(double millis, int32_t millisInDay, UErrorCode &ec);
    01492
    01493
    01502     int32_t newestStamp(UCalendarDateFields start, UCalendarDateFields end, int32_t bestSoFar) const;
    01503
    01509     enum {
    01511       kResolveSTOP = -1,
    01513       kResolveRemap = 32
    01514     };
    01515
    01521     static const UFieldResolutionTable kDatePrecedence[];
    01522
    01528     static const UFieldResolutionTable kYearPrecedence[];
    01529
    01535     static const UFieldResolutionTable kDOWPrecedence[];
    01536
    01564     UCalendarDateFields resolveFields(const UFieldResolutionTable *precedenceTable);
    01565
    01566
    01570     virtual const UFieldResolutionTable* getFieldResolutionTable() const;
    01571
    01577     UCalendarDateFields newerField(UCalendarDateFields defaultField, UCalendarDateFields alternateField) const;
    01578
    01579
    01580 private:
    01589     int32_t getActualHelper(UCalendarDateFields field, int32_t startValue, int32_t endValue, UErrorCode &status) const;
    01590
    01591
    01592 protected:
    01597     UBool      fIsTimeSet;
    01598
    01609     UBool      fAreFieldsSet;
    01610
    01616     UBool      fAreAllFieldsSet;
    01617
    01625     UBool fAreFieldsVirtuallySet;
    01626
    01633     UDate        internalGetTime(void) const     { return fTime; }
    01634
    01642     void        internalSetTime(UDate time)     { fTime = time; }
    01643
    01648     int32_t     fFields[UCAL_FIELD_COUNT];
    01649
    01654     UBool      fIsSet[UCAL_FIELD_COUNT];
    01655
    01659     enum {
    01660         kUnset                 = 0,
    01661         kInternallySet,
    01662         kMinimumUserStamp
    01663     };
    01664
    01671     int32_t        fStamp[UCAL_FIELD_COUNT];
    01672
    01697     virtual void handleComputeFields(int32_t julianDay, UErrorCode &status);
    01698
    01704     int32_t getGregorianYear() const {
    01705         return fGregorianYear;
    01706     }
    01707
    01713     int32_t getGregorianMonth() const {
    01714         return fGregorianMonth;
    01715     }
    01716
    01722     int32_t getGregorianDayOfYear() const {
    01723         return fGregorianDayOfYear;
    01724     }
    01725
    01731     int32_t getGregorianDayOfMonth() const {
    01732       return fGregorianDayOfMonth;
    01733     }
    01734
    01741     virtual int32_t getDefaultMonthInYear(int32_t eyear) ;
    01742
    01743
    01751     virtual int32_t getDefaultDayInMonth(int32_t eyear, int32_t month);
    01752
    01753     //-------------------------------------------------------------------------
    01754     // Protected utility methods for use by subclasses.  These are very handy
    01755     // for implementing add, roll, and computeFields.
    01756     //-------------------------------------------------------------------------
    01757
    01787     virtual void pinField(UCalendarDateFields field, UErrorCode& status);
    01788
    01832     int32_t weekNumber(int32_t desiredDay, int32_t dayOfPeriod, int32_t dayOfWeek);
    01833
    01834
    01865     inline int32_t weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek);
    01866
    01871     int32_t getLocalDOW();
    01872
    01873 private:
    01874
    01878     int32_t fNextStamp;// = MINIMUM_USER_STAMP;
    01879
    01883     UDate        fTime;
    01884
    01888     UBool      fLenient;
    01889
    01894     TimeZone*   fZone;
    01895
    01904     UCalendarDaysOfWeek fFirstDayOfWeek;
    01905     uint8_t     fMinimalDaysInFirstWeek;
    01906
    01917     void        setWeekCountData(const Locale& desiredLocale, const char *type, UErrorCode& success);
    01918
    01928     void updateTime(UErrorCode& status);
    01929
    01935     int32_t fGregorianYear;
    01936
    01942     int32_t fGregorianMonth;
    01943
    01949     int32_t fGregorianDayOfYear;
    01950
    01956     int32_t fGregorianDayOfMonth;
    01957
    01958     /* calculations */
    01959
    01966     void computeGregorianAndDOWFields(int32_t julianDay, UErrorCode &ec);
    01967
    01968         protected:
    01969
    01977     void computeGregorianFields(int32_t julianDay, UErrorCode &ec);
    01978
    01979         private:
    01980
    02001     void computeWeekFields(UErrorCode &ec);
    02002
    02003
    02012     void validateFields(UErrorCode &status);
    02013
    02022     virtual void validateField(UCalendarDateFields field, UErrorCode &status);
    02023
    02032     void validateField(UCalendarDateFields field, int32_t min, int32_t max, UErrorCode& status);
    02033
    02034  protected:
    02044     static uint8_t julianDayToDayOfWeek(double julian);
    02045
    02046  private:
    02047     char validLocale[ULOC_FULLNAME_CAPACITY];
    02048     char actualLocale[ULOC_FULLNAME_CAPACITY];
    02049
    02050  public:
    02051 #if !UCONFIG_NO_SERVICE
    02052
    02062     static StringEnumeration* getAvailableLocales(void);
    02063
    02072     static URegistryKey registerFactory(ICUServiceFactory* toAdopt, UErrorCode& status);
    02073
    02084     static UBool unregister(URegistryKey key, UErrorCode& status);
    02085
    02090     friend class CalendarFactory;
    02091
    02096     friend class CalendarService;
    02097
    02102     friend class DefaultCalendarFactory;
    02103 #endif /* !UCONFIG_NO_SERVICE */
    02104
    02109     virtual UBool haveDefaultCentury() const = 0;
    02110
    02115     virtual UDate defaultCenturyStart() const = 0;
    02120     virtual int32_t defaultCenturyStartYear() const = 0;
    02121
    02128     Locale getLocale(ULocDataLocaleType type, UErrorCode &status) const;
    02129
    02136     const char* getLocaleID(ULocDataLocaleType type, UErrorCode &status) const;
    02137
    02138 };
    02139
    02140 // -------------------------------------
    02141
    02142 inline Calendar*
    02143 Calendar::createInstance(TimeZone* zone, UErrorCode& errorCode)
    02144 {
    02145     // since the Locale isn't specified, use the default locale
    02146     return createInstance(zone, Locale::getDefault(), errorCode);
    02147 }
    02148
    02149 // -------------------------------------
    02150
    02151 inline void
    02152 Calendar::roll(UCalendarDateFields field, UBool up, UErrorCode& status)
    02153 {
    02154     roll(field, (int32_t)(up ? +1 : -1), status);
    02155 }
    02156
    02157 inline void
    02158 Calendar::roll(EDateFields field, UBool up, UErrorCode& status)
    02159 {
    02160     roll((UCalendarDateFields) field, up, status);
    02161 }
    02162
    02163
    02164 // -------------------------------------
    02165
    02171 inline void
    02172 Calendar::internalSet(UCalendarDateFields field, int32_t value)
    02173 {
    02174     fFields[field] = value;
    02175     fStamp[field] = kInternallySet;
    02176     fIsSet[field]     = TRUE; // Remove later
    02177 }
    02178
    02179 inline int32_t  Calendar::weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek)
    02180 {
    02181   return weekNumber(dayOfPeriod, dayOfPeriod, dayOfWeek);
    02182 }
    02183
    02184
    02185 U_NAMESPACE_END
    02186
    02187 #endif /* #if !UCONFIG_NO_FORMATTING */
    02188
    02189 #endif // _CALENDAR