Main Page | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

calendar.h

00001 /* 00002 ******************************************************************************** 00003 * Copyright (C) 1997-2003, 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 00030 #if !UCONFIG_NO_FORMATTING 00031 00032 #include "unicode/uobject.h" 00033 #include "unicode/locid.h" 00034 #include "unicode/timezone.h" 00035 #include "unicode/ucal.h" 00036 00037 U_NAMESPACE_BEGIN 00038 00039 class ICUServiceFactory; 00040 00044 typedef const void* URegistryKey; 00045 00049 typedef int32_t UFieldResolutionTable[12][8]; 00050 00165 class U_I18N_API Calendar : public UObject { 00166 public: 00167 00174 enum EDateFields { 00175 ERA, // Example: 0..1 00176 YEAR, // Example: 1..big number 00177 MONTH, // Example: 0..11 00178 WEEK_OF_YEAR, // Example: 1..53 00179 WEEK_OF_MONTH, // Example: 1..4 00180 DATE, // Example: 1..31 00181 DAY_OF_YEAR, // Example: 1..365 00182 DAY_OF_WEEK, // Example: 1..7 00183 DAY_OF_WEEK_IN_MONTH, // Example: 1..4, may be specified as -1 00184 AM_PM, // Example: 0..1 00185 HOUR, // Example: 0..11 00186 HOUR_OF_DAY, // Example: 0..23 00187 MINUTE, // Example: 0..59 00188 SECOND, // Example: 0..59 00189 MILLISECOND, // Example: 0..999 00190 ZONE_OFFSET, // Example: -12*U_MILLIS_PER_HOUR..12*U_MILLIS_PER_HOUR 00191 DST_OFFSET, // Example: 0 or U_MILLIS_PER_HOUR 00192 YEAR_WOY, // 'Y' Example: 1..big number - Year of Week of Year 00193 DOW_LOCAL, // 'e' Example: 1..7 - Day of Week / Localized 00194 00195 FIELD_COUNT = UCAL_FIELD_COUNT // See ucal.h for other fields. 00196 }; 00197 00204 enum EDaysOfWeek { 00205 SUNDAY = 1, 00206 MONDAY, 00207 TUESDAY, 00208 WEDNESDAY, 00209 THURSDAY, 00210 FRIDAY, 00211 SATURDAY 00212 }; 00213 00218 enum EMonths { 00219 JANUARY, 00220 FEBRUARY, 00221 MARCH, 00222 APRIL, 00223 MAY, 00224 JUNE, 00225 JULY, 00226 AUGUST, 00227 SEPTEMBER, 00228 OCTOBER, 00229 NOVEMBER, 00230 DECEMBER, 00231 UNDECIMBER 00232 }; 00233 00238 enum EAmpm { 00239 AM, 00240 PM 00241 }; 00242 00247 virtual ~Calendar(); 00248 00255 virtual Calendar* clone(void) const = 0; 00256 00268 static Calendar* createInstance(UErrorCode& success); 00269 00282 static Calendar* createInstance(TimeZone* zoneToAdopt, UErrorCode& success); 00283 00295 static Calendar* createInstance(const TimeZone& zone, UErrorCode& success); 00296 00307 static Calendar* createInstance(const Locale& aLocale, UErrorCode& success); 00308 00322 static Calendar* createInstance(TimeZone* zoneToAdopt, const Locale& aLocale, UErrorCode& success); 00323 00336 static Calendar* createInstance(const TimeZone& zoneToAdopt, const Locale& aLocale, UErrorCode& success); 00337 00347 static const Locale* getAvailableLocales(int32_t& count); 00348 00356 static UDate getNow(void); 00357 00371 inline UDate getTime(UErrorCode& status) const { return getTimeInMillis(status); } 00372 00383 inline void setTime(UDate date, UErrorCode& status) { setTimeInMillis(date, status); } 00384 00396 virtual UBool operator==(const Calendar& that) const; 00397 00406 UBool operator!=(const Calendar& that) const {return !operator==(that);} 00407 00418 virtual UBool isEquivalentTo(const Calendar& other) const; 00419 00434 UBool equals(const Calendar& when, UErrorCode& status) const; 00435 00449 UBool before(const Calendar& when, UErrorCode& status) const; 00450 00464 UBool after(const Calendar& when, UErrorCode& status) const; 00465 00483 virtual void add(EDateFields field, int32_t amount, UErrorCode& status); 00484 00502 virtual void add(UCalendarDateFields field, int32_t amount, UErrorCode& status); 00503 00527 inline void roll(EDateFields field, UBool up, UErrorCode& status); 00528 00552 inline void roll(UCalendarDateFields field, UBool up, UErrorCode& status); 00553 00576 virtual void roll(EDateFields field, int32_t amount, UErrorCode& status); 00577 00600 virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode& status); 00601 00657 virtual int32_t fieldDifference(UDate when, EDateFields field, UErrorCode& status); 00658 00714 virtual int32_t fieldDifference(UDate when, UCalendarDateFields field, UErrorCode& status); 00715 00724 void adoptTimeZone(TimeZone* value); 00725 00733 void setTimeZone(const TimeZone& zone); 00734 00743 const TimeZone& getTimeZone(void) const; 00744 00753 TimeZone* orphanTimeZone(void); 00754 00763 virtual UBool inDaylightTime(UErrorCode& status) const = 0; 00764 00777 void setLenient(UBool lenient); 00778 00785 UBool isLenient(void) const; 00786 00793 void setFirstDayOfWeek(EDaysOfWeek value); 00794 00801 void setFirstDayOfWeek(UCalendarDaysOfWeek value); 00802 00809 EDaysOfWeek getFirstDayOfWeek(void) const; 00810 00818 UCalendarDaysOfWeek getFirstDayOfWeek(UErrorCode &status) const; 00819 00829 void setMinimalDaysInFirstWeek(uint8_t value); 00830 00840 uint8_t getMinimalDaysInFirstWeek(void) const; 00841 00850 virtual int32_t getMinimum(EDateFields field) const; 00851 00860 virtual int32_t getMinimum(UCalendarDateFields field) const; 00861 00870 virtual int32_t getMaximum(EDateFields field) const; 00871 00880 virtual int32_t getMaximum(UCalendarDateFields field) const; 00881 00890 virtual int32_t getGreatestMinimum(EDateFields field) const; 00891 00900 virtual int32_t getGreatestMinimum(UCalendarDateFields field) const; 00901 00910 virtual int32_t getLeastMaximum(EDateFields field) const; 00911 00920 virtual int32_t getLeastMaximum(UCalendarDateFields field) const; 00921 00936 int32_t getActualMinimum(EDateFields field, UErrorCode& status) const; 00937 00952 int32_t getActualMinimum(UCalendarDateFields field, UErrorCode& status) const; 00953 00970 int32_t getActualMaximum(EDateFields field, UErrorCode& status) const; 00971 00988 int32_t getActualMaximum(UCalendarDateFields field, UErrorCode& status) const; 00989 01003 int32_t get(EDateFields field, UErrorCode& status) const; 01004 01018 int32_t get(UCalendarDateFields field, UErrorCode& status) const; 01019 01028 UBool isSet(EDateFields field) const; 01029 01038 UBool isSet(UCalendarDateFields field) const; 01039 01047 void set(EDateFields field, int32_t value); 01048 01056 void set(UCalendarDateFields field, int32_t value); 01057 01068 void set(int32_t year, int32_t month, int32_t date); 01069 01082 void set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute); 01083 01097 void set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute, int32_t second); 01098 01105 void clear(void); 01106 01115 void clear(EDateFields field); 01116 01125 void clear(UCalendarDateFields field); 01126 01142 virtual UClassID getDynamicClassID(void) const = 0; 01143 01152 virtual const char * getType() const = 0; 01153 01154 protected: 01155 01164 Calendar(UErrorCode& success); 01165 01172 Calendar(const Calendar& source); 01173 01180 Calendar& operator=(const Calendar& right); 01181 01192 Calendar(TimeZone* zone, const Locale& aLocale, UErrorCode& success); 01193 01203 Calendar(const TimeZone& zone, const Locale& aLocale, UErrorCode& success); 01204 01213 virtual void computeTime(UErrorCode& status); 01214 01226 virtual void computeFields(UErrorCode& status); 01227 01237 double getTimeInMillis(UErrorCode& status) const; 01238 01247 void setTimeInMillis( double millis, UErrorCode& status ); 01248 01258 void complete(UErrorCode& status); 01259 01268 inline int32_t internalGet(EDateFields field) const {return fFields[field];} 01269 01280 inline int32_t internalGet(UCalendarDateFields field, int32_t defaultValue) const {return fStamp[field]>kUnset ? fFields[field] : defaultValue;} 01281 01290 inline int32_t internalGet(UCalendarDateFields field) const {return fFields[field];} 01291 01301 void internalSet(EDateFields field, int32_t value); 01302 01312 inline void internalSet(UCalendarDateFields field, int32_t value); 01313 01320 virtual void prepareGetActual(UCalendarDateFields field, UBool isMinimum, UErrorCode &status); 01321 01326 enum ELimitType { 01327 UCAL_LIMIT_MINIMUM = 0, 01328 UCAL_LIMIT_GREATEST_MINIMUM, 01329 UCAL_LIMIT_LEAST_MAXIMUM, 01330 UCAL_LIMIT_MAXIMUM, 01331 UCAL_LIMIT_COUNT 01332 }; 01333 01355 virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const = 0; 01356 01364 virtual int32_t getLimit(UCalendarDateFields field, ELimitType limitType) const; 01365 01366 01380 virtual int32_t handleComputeMonthStart(int32_t eyear, int32_t month, 01381 UBool useMonth) const = 0; 01382 01390 virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month) const ; 01391 01399 virtual int32_t handleGetYearLength(int32_t eyear) const; 01400 01401 01410 virtual int32_t handleGetExtendedYear() = 0; 01411 01420 virtual int32_t handleComputeJulianDay(UCalendarDateFields bestField); 01421 01430 virtual int32_t handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy); 01431 01438 int32_t computeJulianDay(); 01439 01447 int32_t computeMillisInDay(); 01448 01456 int32_t computeZoneOffset(double millis, int32_t millisInDay, UErrorCode &ec); 01457 01458 01467 int32_t newestStamp(UCalendarDateFields start, UCalendarDateFields end, int32_t bestSoFar) const; 01468 01474 enum { 01476 kResolveSTOP = -1, 01478 kResolveRemap = 32 01479 }; 01480 01486 static const UFieldResolutionTable kDatePrecedence[]; 01487 01493 static const UFieldResolutionTable kYearPrecedence[]; 01494 01500 static const UFieldResolutionTable kDOWPrecedence[]; 01501 01529 UCalendarDateFields resolveFields(const UFieldResolutionTable *precedenceTable); 01530 01531 virtual const UFieldResolutionTable* getFieldResolutionTable() const; 01532 01538 UCalendarDateFields newerField(UCalendarDateFields defaultField, UCalendarDateFields alternateField) const; 01539 01540 01541 private: 01550 int32_t getActualHelper(UCalendarDateFields field, int32_t startValue, int32_t endValue, UErrorCode &status) const; 01551 01552 01553 protected: 01558 UBool fIsTimeSet; 01559 01570 UBool fAreFieldsSet; 01571 01577 UBool fAreAllFieldsSet; 01578 01585 UDate internalGetTime(void) const { return fTime; } 01586 01594 void internalSetTime(UDate time) { fTime = time; } 01595 01600 int32_t fFields[UCAL_FIELD_COUNT]; 01601 01606 UBool fIsSet[UCAL_FIELD_COUNT]; 01607 01611 enum { 01612 kUnset = 0, 01613 kInternallySet, 01614 kMinimumUserStamp 01615 }; 01616 01623 int32_t fStamp[UCAL_FIELD_COUNT]; 01624 01649 virtual void handleComputeFields(int32_t julianDay, UErrorCode &status); 01650 01657 int32_t getGregorianYear() const { 01658 return fGregorianYear; 01659 } 01660 01667 int32_t getGregorianMonth() const { 01668 return fGregorianMonth; 01669 } 01670 01677 int32_t getGregorianDayOfYear() const { 01678 return fGregorianDayOfYear; 01679 } 01680 01687 int32_t getGregorianDayOfMonth() const { 01688 return fGregorianDayOfMonth; 01689 } 01690 01697 virtual int32_t getDefaultMonthInYear() ; 01698 01699 01705 virtual int32_t getDefaultDayInMonth(int32_t /*month*/); 01706 01707 //------------------------------------------------------------------------- 01708 // Protected utility methods for use by subclasses. These are very handy 01709 // for implementing add, roll, and computeFields. 01710 //------------------------------------------------------------------------- 01711 01739 virtual void pinField(UCalendarDateFields field, UErrorCode& status); 01740 01782 int32_t weekNumber(int32_t desiredDay, int32_t dayOfPeriod, int32_t dayOfWeek); 01783 01784 01815 inline int32_t weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek); 01816 01821 int32_t getLocalDOW(); 01822 01823 private: 01824 01828 int32_t fNextStamp;// = MINIMUM_USER_STAMP; 01829 01833 UDate fTime; 01834 01838 UBool fLenient; 01839 01844 TimeZone* fZone; 01845 01854 UCalendarDaysOfWeek fFirstDayOfWeek; 01855 uint8_t fMinimalDaysInFirstWeek; 01856 01866 void setWeekCountData(const Locale& desiredLocale, UErrorCode& success); 01867 01877 void updateTime(UErrorCode& status); 01878 01882 static const char kDateTimeElements[]; 01883 01887 static const char kDefaultCalendar[]; 01888 01889 01894 int32_t fGregorianYear; 01895 01900 int32_t fGregorianMonth; 01901 01906 int32_t fGregorianDayOfYear; 01907 01912 int32_t fGregorianDayOfMonth; 01913 01914 /* calculations */ 01915 01922 void computeGregorianAndDOWFields(int32_t julianDay, UErrorCode &ec); 01923 01934 void computeGregorianFields(int32_t julianDay, UErrorCode &ec); 01935 01956 void computeWeekFields(UErrorCode &ec); 01957 01958 01967 void validateFields(UErrorCode &status); 01968 01977 virtual void validateField(UCalendarDateFields field, UErrorCode &status); 01978 01987 void validateField(UCalendarDateFields field, int32_t min, int32_t max, UErrorCode& status); 01988 01989 protected: 01999 static uint8_t julianDayToDayOfWeek(double julian); 02000 02001 private: 02002 char validLocale[ULOC_FULLNAME_CAPACITY]; 02003 char actualLocale[ULOC_FULLNAME_CAPACITY]; 02004 02005 public: 02016 static StringEnumeration* getAvailableLocales(void); 02017 02026 static URegistryKey registerFactory(ICUServiceFactory* toAdopt, UErrorCode& status); 02027 02038 static UBool unregister(URegistryKey key, UErrorCode& status); 02039 02044 friend class CalendarFactory; 02045 02050 friend class CalendarService; 02051 02056 friend class DefaultCalendarFactory; 02057 02062 virtual UBool haveDefaultCentury() const = 0; 02063 02068 virtual UDate defaultCenturyStart() const = 0; 02073 virtual int32_t defaultCenturyStartYear() const = 0; 02074 02081 Locale getLocale(ULocDataLocaleType type, UErrorCode &status) const; 02082 02089 const char* getLocaleID(ULocDataLocaleType type, UErrorCode &status) const; 02090 02091 }; 02092 02093 // ------------------------------------- 02094 02095 inline Calendar* 02096 Calendar::createInstance(TimeZone* zone, UErrorCode& errorCode) 02097 { 02098 // since the Locale isn't specified, use the default locale 02099 return createInstance(zone, Locale::getDefault(), errorCode); 02100 } 02101 02102 // ------------------------------------- 02103 02104 inline void 02105 Calendar::roll(UCalendarDateFields field, UBool up, UErrorCode& status) 02106 { 02107 roll(field, (int32_t)(up ? +1 : -1), status); 02108 } 02109 02110 inline void 02111 Calendar::roll(EDateFields field, UBool up, UErrorCode& status) 02112 { 02113 roll((UCalendarDateFields) field, up, status); 02114 } 02115 02116 02117 // ------------------------------------- 02118 02124 inline void 02125 Calendar::internalSet(UCalendarDateFields field, int32_t value) 02126 { 02127 fFields[field] = value; 02128 fStamp[field] = kInternallySet; 02129 fIsSet[field] = TRUE; // Remove later 02130 } 02131 02132 inline void 02133 Calendar::internalSet(EDateFields field, int32_t value) 02134 { 02135 internalSet((UCalendarDateFields) field, value); 02136 } 02137 02138 inline uint8_t Calendar::julianDayToDayOfWeek(double julian) 02139 { 02140 // If julian is negative, then julian%7 will be negative, so we adjust 02141 // accordingly. We add 1 because Julian day 0 is Monday. 02142 int8_t dayOfWeek = (int8_t) uprv_fmod(julian + 1, 7); 02143 02144 uint8_t result = (uint8_t)(dayOfWeek + ((dayOfWeek < 0) ? (7+UCAL_SUNDAY ) : UCAL_SUNDAY)); 02145 return result; 02146 } 02147 02148 inline int32_t Calendar::weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek) 02149 { 02150 return weekNumber(dayOfPeriod, dayOfPeriod, dayOfWeek); 02151 } 02152 02153 02154 U_NAMESPACE_END 02155 02156 #endif /* #if !UCONFIG_NO_FORMATTING */ 02157 02158 #endif // _CALENDAR

Generated on Wed Sep 15 17:18:08 2004 for ICU 2.8 by doxygen 1.3.8