calendar.h

Go to the documentation of this file.
00001 /*
00002 ********************************************************************************
00003 *   Copyright (C) 1997-2012, 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 
00051 class BasicTimeZone;
00200 class U_I18N_API Calendar : public UObject {
00201 public:
00202 
00209     enum EDateFields {
00210 #ifndef U_HIDE_DEPRECATED_API
00211 /*
00212  * ERA may be defined on other platforms. To avoid any potential problems undefined it here.
00213  */
00214 #ifdef ERA
00215 #undef ERA
00216 #endif
00217         ERA,                  // Example: 0..1
00218         YEAR,                 // Example: 1..big number
00219         MONTH,                // Example: 0..11
00220         WEEK_OF_YEAR,         // Example: 1..53
00221         WEEK_OF_MONTH,        // Example: 1..4
00222         DATE,                 // Example: 1..31
00223         DAY_OF_YEAR,          // Example: 1..365
00224         DAY_OF_WEEK,          // Example: 1..7
00225         DAY_OF_WEEK_IN_MONTH, // Example: 1..4, may be specified as -1
00226         AM_PM,                // Example: 0..1
00227         HOUR,                 // Example: 0..11
00228         HOUR_OF_DAY,          // Example: 0..23
00229         MINUTE,               // Example: 0..59
00230         SECOND,               // Example: 0..59
00231         MILLISECOND,          // Example: 0..999
00232         ZONE_OFFSET,          // Example: -12*U_MILLIS_PER_HOUR..12*U_MILLIS_PER_HOUR
00233         DST_OFFSET,           // Example: 0 or U_MILLIS_PER_HOUR
00234         YEAR_WOY,             // 'Y' Example: 1..big number - Year of Week of Year
00235         DOW_LOCAL,            // 'e' Example: 1..7 - Day of Week / Localized
00236         
00237         EXTENDED_YEAR,
00238         JULIAN_DAY,
00239         MILLISECONDS_IN_DAY,
00240         IS_LEAP_MONTH,
00241 
00242         FIELD_COUNT = UCAL_FIELD_COUNT // See ucal.h for other fields.
00243 #endif /* U_HIDE_DEPRECATED_API */
00244     };
00245 
00246 #ifndef U_HIDE_DEPRECATED_API
00247 
00253     enum EDaysOfWeek {
00254         SUNDAY = 1,
00255         MONDAY,
00256         TUESDAY,
00257         WEDNESDAY,
00258         THURSDAY,
00259         FRIDAY,
00260         SATURDAY
00261     };
00262 
00267     enum EMonths {
00268         JANUARY,
00269         FEBRUARY,
00270         MARCH,
00271         APRIL,
00272         MAY,
00273         JUNE,
00274         JULY,
00275         AUGUST,
00276         SEPTEMBER,
00277         OCTOBER,
00278         NOVEMBER,
00279         DECEMBER,
00280         UNDECIMBER
00281     };
00282 
00287     enum EAmpm {
00288         AM,
00289         PM
00290     };
00291 #endif  /* U_HIDE_DEPRECATED_API */
00292 
00297     virtual ~Calendar();
00298 
00305     virtual Calendar* clone(void) const = 0;
00306 
00318     static Calendar* U_EXPORT2 createInstance(UErrorCode& success);
00319 
00332     static Calendar* U_EXPORT2 createInstance(TimeZone* zoneToAdopt, UErrorCode& success);
00333 
00345     static Calendar* U_EXPORT2 createInstance(const TimeZone& zone, UErrorCode& success);
00346 
00357     static Calendar* U_EXPORT2 createInstance(const Locale& aLocale, UErrorCode& success);
00358 
00372     static Calendar* U_EXPORT2 createInstance(TimeZone* zoneToAdopt, const Locale& aLocale, UErrorCode& success);
00373 
00386     static Calendar* U_EXPORT2 createInstance(const TimeZone& zone, const Locale& aLocale, UErrorCode& success);
00387 
00397     static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
00398 
00399 
00416     static StringEnumeration* U_EXPORT2 getKeywordValuesForLocale(const char* key,
00417                     const Locale& locale, UBool commonlyUsed, UErrorCode& status);
00418 
00426     static UDate U_EXPORT2 getNow(void);
00427 
00441     inline UDate getTime(UErrorCode& status) const { return getTimeInMillis(status); }
00442 
00453     inline void setTime(UDate date, UErrorCode& status) { setTimeInMillis(date, status); }
00454 
00466     virtual UBool operator==(const Calendar& that) const;
00467 
00476     UBool operator!=(const Calendar& that) const {return !operator==(that);}
00477 
00488     virtual UBool isEquivalentTo(const Calendar& other) const;
00489 
00504     UBool equals(const Calendar& when, UErrorCode& status) const;
00505 
00519     UBool before(const Calendar& when, UErrorCode& status) const;
00520 
00534     UBool after(const Calendar& when, UErrorCode& status) const;
00535 
00556     virtual void add(EDateFields field, int32_t amount, UErrorCode& status);
00557 
00578     virtual void add(UCalendarDateFields field, int32_t amount, UErrorCode& status);
00579 
00580 #ifndef U_HIDE_DEPRECATED_API
00581 
00613     inline void roll(EDateFields field, UBool up, UErrorCode& status);
00614 #endif  /* U_HIDE_DEPRECATED_API */
00615 
00648     inline void roll(UCalendarDateFields field, UBool up, UErrorCode& status);
00649 
00681     virtual void roll(EDateFields field, int32_t amount, UErrorCode& status);
00682 
00714     virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode& status);
00715 
00771     virtual int32_t fieldDifference(UDate when, EDateFields field, UErrorCode& status);
00772 
00828     virtual int32_t fieldDifference(UDate when, UCalendarDateFields field, UErrorCode& status);
00829 
00838     void adoptTimeZone(TimeZone* value);
00839 
00847     void setTimeZone(const TimeZone& zone);
00848 
00857     const TimeZone& getTimeZone(void) const;
00858 
00867     TimeZone* orphanTimeZone(void);
00868 
00877     virtual UBool inDaylightTime(UErrorCode& status) const = 0;
00878 
00891     void setLenient(UBool lenient);
00892 
00899     UBool isLenient(void) const;
00900 
00901 #ifndef U_HIDE_DRAFT_API
00902 
00922     void setRepeatedWallTimeOption(UCalendarWallTimeOption option);
00923 
00933     UCalendarWallTimeOption getRepeatedWallTimeOption(void) const;
00934 
00956     void setSkippedWallTimeOption(UCalendarWallTimeOption option);
00957 
00968     UCalendarWallTimeOption getSkippedWallTimeOption(void) const;
00969 #endif  /* U_HIDE_DRAFT_API */
00970 
00971 #ifndef U_HIDE_DEPRECATED_API
00972 
00978     void setFirstDayOfWeek(EDaysOfWeek value);
00979 #endif  /* U_HIDE_DEPRECATED_API */
00980 
00987     void setFirstDayOfWeek(UCalendarDaysOfWeek value);
00988 
00989 #ifndef U_HIDE_DEPRECATED_API
00990 
00996     EDaysOfWeek getFirstDayOfWeek(void) const;
00997 #endif  /* U_HIDE_DEPRECATED_API */
00998 
01006     UCalendarDaysOfWeek getFirstDayOfWeek(UErrorCode &status) const;
01007 
01017     void setMinimalDaysInFirstWeek(uint8_t value);
01018 
01028     uint8_t getMinimalDaysInFirstWeek(void) const;
01029 
01038     virtual int32_t getMinimum(EDateFields field) const;
01039 
01048     virtual int32_t getMinimum(UCalendarDateFields field) const;
01049 
01058     virtual int32_t getMaximum(EDateFields field) const;
01059 
01068     virtual int32_t getMaximum(UCalendarDateFields field) const;
01069 
01078     virtual int32_t getGreatestMinimum(EDateFields field) const;
01079 
01088     virtual int32_t getGreatestMinimum(UCalendarDateFields field) const;
01089 
01098     virtual int32_t getLeastMaximum(EDateFields field) const;
01099 
01108     virtual int32_t getLeastMaximum(UCalendarDateFields field) const;
01109 
01110 #ifndef U_HIDE_DEPRECATED_API
01111 
01125     int32_t getActualMinimum(EDateFields field, UErrorCode& status) const;
01126 #endif  /* U_HIDE_DEPRECATED_API */
01127 
01142     virtual int32_t getActualMinimum(UCalendarDateFields field, UErrorCode& status) const;
01143 
01144 #ifndef U_HIDE_DEPRECATED_API
01145 
01161     int32_t getActualMaximum(EDateFields field, UErrorCode& status) const;
01162 #endif  /* U_HIDE_DEPRECATED_API */
01163 
01180     virtual int32_t getActualMaximum(UCalendarDateFields field, UErrorCode& status) const;
01181 
01182 #ifndef U_HIDE_DEPRECATED_API
01183 
01196     int32_t get(EDateFields field, UErrorCode& status) const;
01197 #endif  /* U_HIDE_DEPRECATED_API */
01198 
01212     int32_t get(UCalendarDateFields field, UErrorCode& status) const;
01213 
01214 #ifndef U_HIDE_DEPRECATED_API
01215 
01223     UBool isSet(EDateFields field) const;
01224 #endif  /* U_HIDE_DEPRECATED_API */
01225 
01234     UBool isSet(UCalendarDateFields field) const;
01235 
01236 #ifndef U_HIDE_DEPRECATED_API
01237 
01244     void set(EDateFields field, int32_t value);
01245 #endif  /* U_HIDE_DEPRECATED_API */
01246 
01254     void set(UCalendarDateFields field, int32_t value);
01255 
01266     void set(int32_t year, int32_t month, int32_t date);
01267 
01280     void set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute);
01281 
01295     void set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute, int32_t second);
01296 
01303     void clear(void);
01304 
01305 #ifndef U_HIDE_DEPRECATED_API
01306 
01314     void clear(EDateFields field);
01315 #endif  /* U_HIDE_DEPRECATED_API */
01316 
01325     void clear(UCalendarDateFields field);
01326 
01342     virtual UClassID getDynamicClassID(void) const = 0;
01343 
01376     virtual const char * getType() const = 0;
01377 
01393     virtual UCalendarWeekdayType getDayOfWeekType(UCalendarDaysOfWeek dayOfWeek, UErrorCode &status) const;
01394 
01409     virtual int32_t getWeekendTransition(UCalendarDaysOfWeek dayOfWeek, UErrorCode &status) const;
01410 
01420     virtual UBool isWeekend(UDate date, UErrorCode &status) const;
01421 
01429     virtual UBool isWeekend(void) const;
01430 
01431 protected:
01432 
01441     Calendar(UErrorCode& success);
01442 
01449     Calendar(const Calendar& source);
01450 
01457     Calendar& operator=(const Calendar& right);
01458 
01469     Calendar(TimeZone* zone, const Locale& aLocale, UErrorCode& success);
01470 
01480     Calendar(const TimeZone& zone, const Locale& aLocale, UErrorCode& success);
01481 
01490     virtual void computeTime(UErrorCode& status);
01491 
01503     virtual void computeFields(UErrorCode& status);
01504 
01514     double getTimeInMillis(UErrorCode& status) const;
01515 
01524     void setTimeInMillis( double millis, UErrorCode& status );
01525 
01535     void complete(UErrorCode& status);
01536 
01537 #ifndef U_HIDE_DEPRECATED_API
01538 
01546     inline int32_t internalGet(EDateFields field) const {return fFields[field];}
01547 #endif  /* U_HIDE_DEPRECATED_API */
01548 
01549 #ifndef U_HIDE_INTERNAL_API
01550 
01560     inline int32_t internalGet(UCalendarDateFields field, int32_t defaultValue) const {return fStamp[field]>kUnset ? fFields[field] : defaultValue;}
01561 
01570     inline int32_t internalGet(UCalendarDateFields field) const {return fFields[field];}
01571 #endif  /* U_HIDE_INTERNAL_API */
01572 
01573 #ifndef U_HIDE_DEPRECATED_API
01574 
01583     void internalSet(EDateFields field, int32_t value);
01584 #endif  /* U_HIDE_DEPRECATED_API */
01585 
01595     inline void internalSet(UCalendarDateFields field, int32_t value);
01596 
01603     virtual void prepareGetActual(UCalendarDateFields field, UBool isMinimum, UErrorCode &status);
01604 
01609     enum ELimitType {
01610       UCAL_LIMIT_MINIMUM = 0,
01611       UCAL_LIMIT_GREATEST_MINIMUM,
01612       UCAL_LIMIT_LEAST_MAXIMUM,
01613       UCAL_LIMIT_MAXIMUM,
01614       UCAL_LIMIT_COUNT
01615     };
01616 
01638     virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const = 0;
01639 
01647     virtual int32_t getLimit(UCalendarDateFields field, ELimitType limitType) const;
01648 
01649 
01663     virtual int32_t handleComputeMonthStart(int32_t eyear, int32_t month,
01664                                                    UBool useMonth) const  = 0;
01665 
01673     virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month) const ;
01674 
01682     virtual int32_t handleGetYearLength(int32_t eyear) const;
01683 
01684 
01693     virtual int32_t handleGetExtendedYear() = 0;
01694 
01703     virtual int32_t handleComputeJulianDay(UCalendarDateFields bestField);
01704 
01713     virtual int32_t handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy);
01714 
01715 #ifndef U_HIDE_INTERNAL_API
01716 
01722     int32_t computeJulianDay();
01723 
01731     int32_t computeMillisInDay();
01732 
01742     int32_t computeZoneOffset(double millis, int32_t millisInDay, UErrorCode &ec);
01743 
01744 
01753     int32_t newestStamp(UCalendarDateFields start, UCalendarDateFields end, int32_t bestSoFar) const;
01754 
01760     enum {
01762       kResolveSTOP = -1,
01764       kResolveRemap = 32
01765     };
01766 
01772     static const UFieldResolutionTable kDatePrecedence[];
01773 
01779     static const UFieldResolutionTable kYearPrecedence[];
01780 
01786     static const UFieldResolutionTable kDOWPrecedence[];
01787 
01815     UCalendarDateFields resolveFields(const UFieldResolutionTable *precedenceTable);
01816 #endif  /* U_HIDE_INTERNAL_API */
01817 
01818 
01822     virtual const UFieldResolutionTable* getFieldResolutionTable() const;
01823 
01824 #ifndef U_HIDE_INTERNAL_API
01825 
01830     UCalendarDateFields newerField(UCalendarDateFields defaultField, UCalendarDateFields alternateField) const;
01831 #endif  /* U_HIDE_INTERNAL_API */
01832 
01833 
01834 private:
01843     int32_t getActualHelper(UCalendarDateFields field, int32_t startValue, int32_t endValue, UErrorCode &status) const;
01844 
01845 
01846 protected:
01851     UBool      fIsTimeSet;
01852 
01863     UBool      fAreFieldsSet;
01864 
01870     UBool      fAreAllFieldsSet;
01871 
01879     UBool fAreFieldsVirtuallySet;
01880 
01887     UDate        internalGetTime(void) const     { return fTime; }
01888 
01896     void        internalSetTime(UDate time)     { fTime = time; }
01897 
01902     int32_t     fFields[UCAL_FIELD_COUNT];
01903 
01908     UBool      fIsSet[UCAL_FIELD_COUNT];
01909 
01913     enum {
01914         kUnset                 = 0,
01915         kInternallySet,
01916         kMinimumUserStamp
01917     };
01918 
01925     int32_t        fStamp[UCAL_FIELD_COUNT];
01926 
01951     virtual void handleComputeFields(int32_t julianDay, UErrorCode &status);
01952 
01953 #ifndef U_HIDE_INTERNAL_API
01954 
01959     int32_t getGregorianYear() const {
01960         return fGregorianYear;
01961     }
01962 
01968     int32_t getGregorianMonth() const {
01969         return fGregorianMonth;
01970     }
01971 
01977     int32_t getGregorianDayOfYear() const {
01978         return fGregorianDayOfYear;
01979     }
01980 
01986     int32_t getGregorianDayOfMonth() const {
01987       return fGregorianDayOfMonth;
01988     }
01989 #endif  /* U_HIDE_INTERNAL_API */
01990 
01997     virtual int32_t getDefaultMonthInYear(int32_t eyear) ;
01998 
01999 
02007     virtual int32_t getDefaultDayInMonth(int32_t eyear, int32_t month);
02008 
02009     //-------------------------------------------------------------------------
02010     // Protected utility methods for use by subclasses.  These are very handy
02011     // for implementing add, roll, and computeFields.
02012     //-------------------------------------------------------------------------
02013 
02043     virtual void pinField(UCalendarDateFields field, UErrorCode& status);
02044 
02088     int32_t weekNumber(int32_t desiredDay, int32_t dayOfPeriod, int32_t dayOfWeek);
02089 
02090 
02091 #ifndef U_HIDE_INTERNAL_API
02092 
02122     inline int32_t weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek);
02123 
02128     int32_t getLocalDOW();
02129 #endif  /* U_HIDE_INTERNAL_API */
02130 
02131 private:
02132 
02136     int32_t fNextStamp;// = MINIMUM_USER_STAMP;
02137 
02142     void recalculateStamp();
02143 
02147     UDate        fTime;
02148 
02152     UBool      fLenient;
02153 
02158     TimeZone*   fZone;
02159 
02164     UCalendarWallTimeOption fRepeatedWallTime;
02165 
02170     UCalendarWallTimeOption fSkippedWallTime;
02171 
02180     UCalendarDaysOfWeek fFirstDayOfWeek;
02181     uint8_t     fMinimalDaysInFirstWeek;
02182     UCalendarDaysOfWeek fWeekendOnset;
02183     int32_t fWeekendOnsetMillis;
02184     UCalendarDaysOfWeek fWeekendCease;
02185     int32_t fWeekendCeaseMillis;
02186 
02197     void        setWeekData(const Locale& desiredLocale, const char *type, UErrorCode& success);
02198 
02208     void updateTime(UErrorCode& status);
02209 
02215     int32_t fGregorianYear;
02216 
02222     int32_t fGregorianMonth;
02223 
02229     int32_t fGregorianDayOfYear;
02230 
02236     int32_t fGregorianDayOfMonth;
02237 
02238     /* calculations */
02239 
02246     void computeGregorianAndDOWFields(int32_t julianDay, UErrorCode &ec);
02247 
02248 protected:
02249 
02257     void computeGregorianFields(int32_t julianDay, UErrorCode &ec);
02258 
02259 private:
02260 
02281     void computeWeekFields(UErrorCode &ec);
02282 
02283 
02292     void validateFields(UErrorCode &status);
02293 
02302     virtual void validateField(UCalendarDateFields field, UErrorCode &status);
02303 
02312     void validateField(UCalendarDateFields field, int32_t min, int32_t max, UErrorCode& status);
02313 
02314  protected:
02315 #ifndef U_HIDE_INTERNAL_API
02316 
02325     static uint8_t julianDayToDayOfWeek(double julian);
02326 #endif  /* U_HIDE_INTERNAL_API */
02327 
02328  private:
02329     char validLocale[ULOC_FULLNAME_CAPACITY];
02330     char actualLocale[ULOC_FULLNAME_CAPACITY];
02331 
02332  public:
02333 #if !UCONFIG_NO_SERVICE
02334 
02338 #ifndef U_HIDE_INTERNAL_API
02339 
02345     static StringEnumeration* getAvailableLocales(void);
02346 
02355     static URegistryKey registerFactory(ICUServiceFactory* toAdopt, UErrorCode& status);
02356 
02367     static UBool unregister(URegistryKey key, UErrorCode& status);
02368 #endif  /* U_HIDE_INTERNAL_API */
02369 
02374     friend class CalendarFactory;
02375 
02380     friend class CalendarService;
02381 
02386     friend class DefaultCalendarFactory;
02387 #endif /* !UCONFIG_NO_SERVICE */
02388 
02393     virtual UBool haveDefaultCentury() const = 0;
02394 
02399     virtual UDate defaultCenturyStart() const = 0;
02404     virtual int32_t defaultCenturyStartYear() const = 0;
02405 
02412     Locale getLocale(ULocDataLocaleType type, UErrorCode &status) const;
02413 
02414 #ifndef U_HIDE_INTERNAL_API
02415 
02421     const char* getLocaleID(ULocDataLocaleType type, UErrorCode &status) const;
02422 #endif  /* U_HIDE_INTERNAL_API */
02423 
02424 private:
02429     BasicTimeZone* getBasicTimeZone() const;
02430 };
02431 
02432 // -------------------------------------
02433 
02434 inline Calendar*
02435 Calendar::createInstance(TimeZone* zone, UErrorCode& errorCode)
02436 {
02437     // since the Locale isn't specified, use the default locale
02438     return createInstance(zone, Locale::getDefault(), errorCode);
02439 }
02440 
02441 // -------------------------------------
02442 
02443 inline void
02444 Calendar::roll(UCalendarDateFields field, UBool up, UErrorCode& status)
02445 {
02446     roll(field, (int32_t)(up ? +1 : -1), status);
02447 }
02448 
02449 #ifndef U_HIDE_DEPRECATED_API
02450 inline void
02451 Calendar::roll(EDateFields field, UBool up, UErrorCode& status)
02452 {
02453     roll((UCalendarDateFields) field, up, status);
02454 }
02455 #endif
02456 
02457 
02458 // -------------------------------------
02459 
02465 inline void
02466 Calendar::internalSet(UCalendarDateFields field, int32_t value)
02467 {
02468     fFields[field] = value;
02469     fStamp[field] = kInternallySet;
02470     fIsSet[field]     = TRUE; // Remove later
02471 }
02472 
02473 
02474 #ifndef U_HIDE_INTERNAL_API
02475 inline int32_t  Calendar::weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek)
02476 {
02477   return weekNumber(dayOfPeriod, dayOfPeriod, dayOfWeek);
02478 }
02479 #endif
02480 
02481 U_NAMESPACE_END
02482 
02483 #endif /* #if !UCONFIG_NO_FORMATTING */
02484 
02485 #endif // _CALENDAR

Generated on 25 Nov 2014 for ICU 50.1.2 by  doxygen 1.4.7