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

- Search for |

- File List
- Globals

# calendar.h

Go to the documentation of this file.

    00001 /*
    00002 ********************************************************************************
    00003 *   Copyright (C) 1997-2006, 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         FIELD_COUNT = UCAL_FIELD_COUNT // See ucal.h for other fields.
    00197 #endif /* U_HIDE_DEPRECATED_API */
    00198     };
    00199
    00206     enum EDaysOfWeek {
    00207 #ifndef U_HIDE_DEPRECATED_API
    00208         SUNDAY = 1,
    00209         MONDAY,
    00210         TUESDAY,
    00211         WEDNESDAY,
    00212         THURSDAY,
    00213         FRIDAY,
    00214         SATURDAY
    00215 #endif /* U_HIDE_DEPRECATED_API */
    00216     };
    00217
    00222     enum EMonths {
    00223 #ifndef U_HIDE_DEPRECATED_API
    00224         JANUARY,
    00225         FEBRUARY,
    00226         MARCH,
    00227         APRIL,
    00228         MAY,
    00229         JUNE,
    00230         JULY,
    00231         AUGUST,
    00232         SEPTEMBER,
    00233         OCTOBER,
    00234         NOVEMBER,
    00235         DECEMBER,
    00236         UNDECIMBER
    00237 #endif /* U_HIDE_DEPRECATED_API */
    00238     };
    00239
    00244     enum EAmpm {
    00245 #ifndef U_HIDE_DEPRECATED_API
    00246         AM,
    00247         PM
    00248 #endif /* U_HIDE_DEPRECATED_API */
    00249     };
    00250
    00255     virtual ~Calendar();
    00256
    00263     virtual Calendar* clone(void) const = 0;
    00264
    00276     static Calendar* U_EXPORT2 createInstance(UErrorCode& success);
    00277
    00290     static Calendar* U_EXPORT2 createInstance(TimeZone* zoneToAdopt, UErrorCode& success);
    00291
    00303     static Calendar* U_EXPORT2 createInstance(const TimeZone& zone, UErrorCode& success);
    00304
    00315     static Calendar* U_EXPORT2 createInstance(const Locale& aLocale, UErrorCode& success);
    00316
    00330     static Calendar* U_EXPORT2 createInstance(TimeZone* zoneToAdopt, const Locale& aLocale, UErrorCode& success);
    00331
    00344     static Calendar* U_EXPORT2 createInstance(const TimeZone& zoneToAdopt, const Locale& aLocale, UErrorCode& success);
    00345
    00355     static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
    00356
    00364     static UDate U_EXPORT2 getNow(void);
    00365
    00379     inline UDate getTime(UErrorCode& status) const { return getTimeInMillis(status); }
    00380
    00391     inline void setTime(UDate date, UErrorCode& status) { setTimeInMillis(date, status); }
    00392
    00404     virtual UBool operator==(const Calendar& that) const;
    00405
    00414     UBool operator!=(const Calendar& that) const {return !operator==(that);}
    00415
    00426     virtual UBool isEquivalentTo(const Calendar& other) const;
    00427
    00442     UBool equals(const Calendar& when, UErrorCode& status) const;
    00443
    00457     UBool before(const Calendar& when, UErrorCode& status) const;
    00458
    00472     UBool after(const Calendar& when, UErrorCode& status) const;
    00473
    00491     virtual void add(EDateFields field, int32_t amount, UErrorCode& status);
    00492
    00510     virtual void add(UCalendarDateFields field, int32_t amount, UErrorCode& status);
    00511
    00535     inline void roll(EDateFields field, UBool up, UErrorCode& status);
    00536
    00560     inline void roll(UCalendarDateFields field, UBool up, UErrorCode& status);
    00561
    00584     virtual void roll(EDateFields field, int32_t amount, UErrorCode& status);
    00585
    00608     virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode& status);
    00609
    00665     virtual int32_t fieldDifference(UDate when, EDateFields field, UErrorCode& status);
    00666
    00722     virtual int32_t fieldDifference(UDate when, UCalendarDateFields field, UErrorCode& status);
    00723
    00732     void adoptTimeZone(TimeZone* value);
    00733
    00741     void setTimeZone(const TimeZone& zone);
    00742
    00751     const TimeZone& getTimeZone(void) const;
    00752
    00761     TimeZone* orphanTimeZone(void);
    00762
    00771     virtual UBool inDaylightTime(UErrorCode& status) const = 0;
    00772
    00785     void setLenient(UBool lenient);
    00786
    00793     UBool isLenient(void) const;
    00794
    00801     void setFirstDayOfWeek(EDaysOfWeek value);
    00802
    00809     void setFirstDayOfWeek(UCalendarDaysOfWeek value);
    00810
    00817     EDaysOfWeek getFirstDayOfWeek(void) const;
    00818
    00826     UCalendarDaysOfWeek getFirstDayOfWeek(UErrorCode &status) const;
    00827
    00837     void setMinimalDaysInFirstWeek(uint8_t value);
    00838
    00848     uint8_t getMinimalDaysInFirstWeek(void) const;
    00849
    00858     virtual int32_t getMinimum(EDateFields field) const;
    00859
    00868     virtual int32_t getMinimum(UCalendarDateFields field) const;
    00869
    00878     virtual int32_t getMaximum(EDateFields field) const;
    00879
    00888     virtual int32_t getMaximum(UCalendarDateFields field) const;
    00889
    00898     virtual int32_t getGreatestMinimum(EDateFields field) const;
    00899
    00908     virtual int32_t getGreatestMinimum(UCalendarDateFields field) const;
    00909
    00918     virtual int32_t getLeastMaximum(EDateFields field) const;
    00919
    00928     virtual int32_t getLeastMaximum(UCalendarDateFields field) const;
    00929
    00944     int32_t getActualMinimum(EDateFields field, UErrorCode& status) const;
    00945
    00960     int32_t getActualMinimum(UCalendarDateFields field, UErrorCode& status) const;
    00961
    00978     int32_t getActualMaximum(EDateFields field, UErrorCode& status) const;
    00979
    00996     int32_t getActualMaximum(UCalendarDateFields field, UErrorCode& status) const;
    00997
    01011     int32_t get(EDateFields field, UErrorCode& status) const;
    01012
    01026     int32_t get(UCalendarDateFields field, UErrorCode& status) const;
    01027
    01036     UBool isSet(EDateFields field) const;
    01037
    01046     UBool isSet(UCalendarDateFields field) const;
    01047
    01055     void set(EDateFields field, int32_t value);
    01056
    01064     void set(UCalendarDateFields field, int32_t value);
    01065
    01076     void set(int32_t year, int32_t month, int32_t date);
    01077
    01090     void set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute);
    01091
    01105     void set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute, int32_t second);
    01106
    01113     void clear(void);
    01114
    01123     void clear(EDateFields field);
    01124
    01133     void clear(UCalendarDateFields field);
    01134
    01150     virtual UClassID getDynamicClassID(void) const = 0;
    01151
    01160     virtual const char * getType() const = 0;
    01161
    01162 protected:
    01163
    01172     Calendar(UErrorCode& success);
    01173
    01180     Calendar(const Calendar& source);
    01181
    01188     Calendar& operator=(const Calendar& right);
    01189
    01200     Calendar(TimeZone* zone, const Locale& aLocale, UErrorCode& success);
    01201
    01211     Calendar(const TimeZone& zone, const Locale& aLocale, UErrorCode& success);
    01212
    01221     virtual void computeTime(UErrorCode& status);
    01222
    01234     virtual void computeFields(UErrorCode& status);
    01235
    01245     double getTimeInMillis(UErrorCode& status) const;
    01246
    01255     void setTimeInMillis( double millis, UErrorCode& status );
    01256
    01266     void complete(UErrorCode& status);
    01267
    01276     inline int32_t internalGet(EDateFields field) const {return fFields[field];}
    01277
    01288     inline int32_t internalGet(UCalendarDateFields field, int32_t defaultValue) const {return fStamp[field]>kUnset ? fFields[field] : defaultValue;}
    01289
    01298     inline int32_t internalGet(UCalendarDateFields field) const {return fFields[field];}
    01299
    01309     void internalSet(EDateFields field, int32_t value);
    01310
    01320     inline void internalSet(UCalendarDateFields field, int32_t value);
    01321
    01328     virtual void prepareGetActual(UCalendarDateFields field, UBool isMinimum, UErrorCode &status);
    01329
    01334     enum ELimitType {
    01335       UCAL_LIMIT_MINIMUM = 0,
    01336       UCAL_LIMIT_GREATEST_MINIMUM,
    01337       UCAL_LIMIT_LEAST_MAXIMUM,
    01338       UCAL_LIMIT_MAXIMUM,
    01339       UCAL_LIMIT_COUNT
    01340     };
    01341
    01363     virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const = 0;
    01364
    01372     virtual int32_t getLimit(UCalendarDateFields field, ELimitType limitType) const;
    01373
    01374
    01388     virtual int32_t handleComputeMonthStart(int32_t eyear, int32_t month,
    01389                                                    UBool useMonth) const  = 0;
    01390
    01398     virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month) const ;
    01399
    01407     virtual int32_t handleGetYearLength(int32_t eyear) const;
    01408
    01409
    01418     virtual int32_t handleGetExtendedYear() = 0;
    01419
    01428     virtual int32_t handleComputeJulianDay(UCalendarDateFields bestField);
    01429
    01438     virtual int32_t handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy);
    01439
    01446     int32_t computeJulianDay();
    01447
    01455     int32_t computeMillisInDay();
    01456
    01466     int32_t computeZoneOffset(double millis, int32_t millisInDay, UErrorCode &ec);
    01467
    01468
    01477     int32_t newestStamp(UCalendarDateFields start, UCalendarDateFields end, int32_t bestSoFar) const;
    01478
    01484     enum {
    01486       kResolveSTOP = -1,
    01488       kResolveRemap = 32
    01489     };
    01490
    01496     static const UFieldResolutionTable kDatePrecedence[];
    01497
    01503     static const UFieldResolutionTable kYearPrecedence[];
    01504
    01510     static const UFieldResolutionTable kDOWPrecedence[];
    01511
    01539     UCalendarDateFields resolveFields(const UFieldResolutionTable *precedenceTable);
    01540
    01541
    01545     virtual const UFieldResolutionTable* getFieldResolutionTable() const;
    01546
    01552     UCalendarDateFields newerField(UCalendarDateFields defaultField, UCalendarDateFields alternateField) const;
    01553
    01554
    01555 private:
    01564     int32_t getActualHelper(UCalendarDateFields field, int32_t startValue, int32_t endValue, UErrorCode &status) const;
    01565
    01566
    01567 protected:
    01572     UBool      fIsTimeSet;
    01573
    01584     UBool      fAreFieldsSet;
    01585
    01591     UBool      fAreAllFieldsSet;
    01592
    01600     UBool fAreFieldsVirtuallySet;
    01601
    01608     UDate        internalGetTime(void) const     { return fTime; }
    01609
    01617     void        internalSetTime(UDate time)     { fTime = time; }
    01618
    01623     int32_t     fFields[UCAL_FIELD_COUNT];
    01624
    01629     UBool      fIsSet[UCAL_FIELD_COUNT];
    01630
    01634     enum {
    01635         kUnset                 = 0,
    01636         kInternallySet,
    01637         kMinimumUserStamp
    01638     };
    01639
    01646     int32_t        fStamp[UCAL_FIELD_COUNT];
    01647
    01672     virtual void handleComputeFields(int32_t julianDay, UErrorCode &status);
    01673
    01679     int32_t getGregorianYear() const {
    01680         return fGregorianYear;
    01681     }
    01682
    01688     int32_t getGregorianMonth() const {
    01689         return fGregorianMonth;
    01690     }
    01691
    01697     int32_t getGregorianDayOfYear() const {
    01698         return fGregorianDayOfYear;
    01699     }
    01700
    01706     int32_t getGregorianDayOfMonth() const {
    01707       return fGregorianDayOfMonth;
    01708     }
    01709
    01716     virtual int32_t getDefaultMonthInYear() ;
    01717
    01718
    01724     virtual int32_t getDefaultDayInMonth(int32_t /*month*/);
    01725
    01726     //-------------------------------------------------------------------------
    01727     // Protected utility methods for use by subclasses.  These are very handy
    01728     // for implementing add, roll, and computeFields.
    01729     //-------------------------------------------------------------------------
    01730
    01760     virtual void pinField(UCalendarDateFields field, UErrorCode& status);
    01761
    01805     int32_t weekNumber(int32_t desiredDay, int32_t dayOfPeriod, int32_t dayOfWeek);
    01806
    01807
    01838     inline int32_t weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek);
    01839
    01844     int32_t getLocalDOW();
    01845
    01846 private:
    01847
    01851     int32_t fNextStamp;// = MINIMUM_USER_STAMP;
    01852
    01856     UDate        fTime;
    01857
    01861     UBool      fLenient;
    01862
    01867     TimeZone*   fZone;
    01868
    01877     UCalendarDaysOfWeek fFirstDayOfWeek;
    01878     uint8_t     fMinimalDaysInFirstWeek;
    01879
    01890     void        setWeekCountData(const Locale& desiredLocale, const char *type, UErrorCode& success);
    01891
    01901     void updateTime(UErrorCode& status);
    01902
    01908     int32_t fGregorianYear;
    01909
    01915     int32_t fGregorianMonth;
    01916
    01922     int32_t fGregorianDayOfYear;
    01923
    01929     int32_t fGregorianDayOfMonth;
    01930
    01931     /* calculations */
    01932
    01939     void computeGregorianAndDOWFields(int32_t julianDay, UErrorCode &ec);
    01940
    01951     void computeGregorianFields(int32_t julianDay, UErrorCode &ec);
    01952
    01973     void computeWeekFields(UErrorCode &ec);
    01974
    01975
    01984     void validateFields(UErrorCode &status);
    01985
    01994     virtual void validateField(UCalendarDateFields field, UErrorCode &status);
    01995
    02004     void validateField(UCalendarDateFields field, int32_t min, int32_t max, UErrorCode& status);
    02005
    02006  protected:
    02016     static uint8_t julianDayToDayOfWeek(double julian);
    02017
    02018  private:
    02019     char validLocale[ULOC_FULLNAME_CAPACITY];
    02020     char actualLocale[ULOC_FULLNAME_CAPACITY];
    02021
    02022  public:
    02023 #if !UCONFIG_NO_SERVICE
    02024
    02034     static StringEnumeration* getAvailableLocales(void);
    02035
    02044     static URegistryKey registerFactory(ICUServiceFactory* toAdopt, UErrorCode& status);
    02045
    02056     static UBool unregister(URegistryKey key, UErrorCode& status);
    02057
    02062     friend class CalendarFactory;
    02063
    02068     friend class CalendarService;
    02069
    02074     friend class DefaultCalendarFactory;
    02075 #endif /* !UCONFIG_NO_SERVICE */
    02076
    02081     virtual UBool haveDefaultCentury() const = 0;
    02082
    02087     virtual UDate defaultCenturyStart() const = 0;
    02092     virtual int32_t defaultCenturyStartYear() const = 0;
    02093
    02100     Locale getLocale(ULocDataLocaleType type, UErrorCode &status) const;
    02101
    02108     const char* getLocaleID(ULocDataLocaleType type, UErrorCode &status) const;
    02109
    02110 };
    02111
    02112 // -------------------------------------
    02113
    02114 inline Calendar*
    02115 Calendar::createInstance(TimeZone* zone, UErrorCode& errorCode)
    02116 {
    02117     // since the Locale isn't specified, use the default locale
    02118     return createInstance(zone, Locale::getDefault(), errorCode);
    02119 }
    02120
    02121 // -------------------------------------
    02122
    02123 inline void
    02124 Calendar::roll(UCalendarDateFields field, UBool up, UErrorCode& status)
    02125 {
    02126     roll(field, (int32_t)(up ? +1 : -1), status);
    02127 }
    02128
    02129 inline void
    02130 Calendar::roll(EDateFields field, UBool up, UErrorCode& status)
    02131 {
    02132     roll((UCalendarDateFields) field, up, status);
    02133 }
    02134
    02135
    02136 // -------------------------------------
    02137
    02143 inline void
    02144 Calendar::internalSet(UCalendarDateFields field, int32_t value)
    02145 {
    02146     fFields[field] = value;
    02147     fStamp[field] = kInternallySet;
    02148     fIsSet[field]     = TRUE; // Remove later
    02149 }
    02150
    02151 inline int32_t  Calendar::weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek)
    02152 {
    02153   return weekNumber(dayOfPeriod, dayOfPeriod, dayOfWeek);
    02154 }
    02155
    02156
    02157 U_NAMESPACE_END
    02158
    02159 #endif /* #if !UCONFIG_NO_FORMATTING */
    02160
    02161 #endif // _CALENDAR