Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | ***************************************************************************** |
michael@0 | 3 | * Copyright (C) 2007-2013, International Business Machines Corporation |
michael@0 | 4 | * and others. All Rights Reserved. |
michael@0 | 5 | ***************************************************************************** |
michael@0 | 6 | * |
michael@0 | 7 | * File CHNSECAL.H |
michael@0 | 8 | * |
michael@0 | 9 | * Modification History: |
michael@0 | 10 | * |
michael@0 | 11 | * Date Name Description |
michael@0 | 12 | * 9/18/2007 ajmacher ported from java ChineseCalendar |
michael@0 | 13 | ***************************************************************************** |
michael@0 | 14 | */ |
michael@0 | 15 | |
michael@0 | 16 | #ifndef CHNSECAL_H |
michael@0 | 17 | #define CHNSECAL_H |
michael@0 | 18 | |
michael@0 | 19 | #include "unicode/utypes.h" |
michael@0 | 20 | |
michael@0 | 21 | #if !UCONFIG_NO_FORMATTING |
michael@0 | 22 | |
michael@0 | 23 | #include "unicode/calendar.h" |
michael@0 | 24 | #include "unicode/timezone.h" |
michael@0 | 25 | |
michael@0 | 26 | U_NAMESPACE_BEGIN |
michael@0 | 27 | |
michael@0 | 28 | /** |
michael@0 | 29 | * <code>ChineseCalendar</code> is a concrete subclass of {@link Calendar} |
michael@0 | 30 | * that implements a traditional Chinese calendar. The traditional Chinese |
michael@0 | 31 | * calendar is a lunisolar calendar: Each month starts on a new moon, and |
michael@0 | 32 | * the months are numbered according to solar events, specifically, to |
michael@0 | 33 | * guarantee that month 11 always contains the winter solstice. In order |
michael@0 | 34 | * to accomplish this, leap months are inserted in certain years. Leap |
michael@0 | 35 | * months are numbered the same as the month they follow. The decision of |
michael@0 | 36 | * which month is a leap month depends on the relative movements of the sun |
michael@0 | 37 | * and moon. |
michael@0 | 38 | * |
michael@0 | 39 | * <p>This class defines one addition field beyond those defined by |
michael@0 | 40 | * <code>Calendar</code>: The <code>IS_LEAP_MONTH</code> field takes the |
michael@0 | 41 | * value of 0 for normal months, or 1 for leap months. |
michael@0 | 42 | * |
michael@0 | 43 | * <p>All astronomical computations are performed with respect to a time |
michael@0 | 44 | * zone of GMT+8:00 and a longitude of 120 degrees east. Although some |
michael@0 | 45 | * calendars implement a historically more accurate convention of using |
michael@0 | 46 | * Beijing's local longitude (116 degrees 25 minutes east) and time zone |
michael@0 | 47 | * (GMT+7:45:40) for dates before 1929, we do not implement this here. |
michael@0 | 48 | * |
michael@0 | 49 | * <p>Years are counted in two different ways in the Chinese calendar. The |
michael@0 | 50 | * first method is by sequential numbering from the 61st year of the reign |
michael@0 | 51 | * of Huang Di, 2637 BCE, which is designated year 1 on the Chinese |
michael@0 | 52 | * calendar. The second method uses 60-year cycles from the same starting |
michael@0 | 53 | * point, which is designated year 1 of cycle 1. In this class, the |
michael@0 | 54 | * <code>EXTENDED_YEAR</code> field contains the sequential year count. |
michael@0 | 55 | * The <code>ERA</code> field contains the cycle number, and the |
michael@0 | 56 | * <code>YEAR</code> field contains the year of the cycle, a value between |
michael@0 | 57 | * 1 and 60. |
michael@0 | 58 | * |
michael@0 | 59 | * <p>There is some variation in what is considered the starting point of |
michael@0 | 60 | * the calendar, with some sources starting in the first year of the reign |
michael@0 | 61 | * of Huang Di, rather than the 61st. This gives continuous year numbers |
michael@0 | 62 | * 60 years greater and cycle numbers one greater than what this class |
michael@0 | 63 | * implements. |
michael@0 | 64 | * |
michael@0 | 65 | * <p>Because <code>ChineseCalendar</code> defines an additional field and |
michael@0 | 66 | * redefines the way the <code>ERA</code> field is used, it requires a new |
michael@0 | 67 | * format class, <code>ChineseDateFormat</code>. As always, use the |
michael@0 | 68 | * methods <code>DateFormat.getXxxInstance(Calendar cal,...)</code> to |
michael@0 | 69 | * obtain a formatter for this calendar. |
michael@0 | 70 | * |
michael@0 | 71 | * <p>References:<ul> |
michael@0 | 72 | * |
michael@0 | 73 | * <li>Dershowitz and Reingold, <i>Calendrical Calculations</i>, |
michael@0 | 74 | * Cambridge University Press, 1997</li> |
michael@0 | 75 | * |
michael@0 | 76 | * <li>Helmer Aslaksen's |
michael@0 | 77 | * <a href="http://www.math.nus.edu.sg/aslaksen/calendar/chinese.shtml"> |
michael@0 | 78 | * Chinese Calendar page</a></li> |
michael@0 | 79 | * |
michael@0 | 80 | * <li>The <a href="http://www.tondering.dk/claus/calendar.html"> |
michael@0 | 81 | * Calendar FAQ</a></li> |
michael@0 | 82 | * |
michael@0 | 83 | * </ul> |
michael@0 | 84 | * |
michael@0 | 85 | * <p> |
michael@0 | 86 | * This class should only be subclassed to implement variants of the Chinese lunar calendar.</p> |
michael@0 | 87 | * <p> |
michael@0 | 88 | * ChineseCalendar usually should be instantiated using |
michael@0 | 89 | * {@link com.ibm.icu.util.Calendar#getInstance(ULocale)} passing in a <code>ULocale</code> |
michael@0 | 90 | * with the tag <code>"@calendar=chinese"</code>.</p> |
michael@0 | 91 | * |
michael@0 | 92 | * @see com.ibm.icu.text.ChineseDateFormat |
michael@0 | 93 | * @see com.ibm.icu.util.Calendar |
michael@0 | 94 | * @author Alan Liu |
michael@0 | 95 | * @internal |
michael@0 | 96 | */ |
michael@0 | 97 | class U_I18N_API ChineseCalendar : public Calendar { |
michael@0 | 98 | public: |
michael@0 | 99 | //------------------------------------------------------------------------- |
michael@0 | 100 | // Constructors... |
michael@0 | 101 | //------------------------------------------------------------------------- |
michael@0 | 102 | |
michael@0 | 103 | /** |
michael@0 | 104 | * Constructs a ChineseCalendar based on the current time in the default time zone |
michael@0 | 105 | * with the given locale. |
michael@0 | 106 | * |
michael@0 | 107 | * @param aLocale The given locale. |
michael@0 | 108 | * @param success Indicates the status of ChineseCalendar object construction. |
michael@0 | 109 | * Returns U_ZERO_ERROR if constructed successfully. |
michael@0 | 110 | * @internal |
michael@0 | 111 | */ |
michael@0 | 112 | ChineseCalendar(const Locale& aLocale, UErrorCode &success); |
michael@0 | 113 | |
michael@0 | 114 | protected: |
michael@0 | 115 | |
michael@0 | 116 | /** |
michael@0 | 117 | * Constructs a ChineseCalendar based on the current time in the default time zone |
michael@0 | 118 | * with the given locale, using the specified epoch year and time zone for |
michael@0 | 119 | * astronomical calculations. |
michael@0 | 120 | * |
michael@0 | 121 | * @param aLocale The given locale. |
michael@0 | 122 | * @param epochYear The epoch year to use for calculation. |
michael@0 | 123 | * @param zoneAstroCalc The TimeZone to use for astronomical calculations. If null, |
michael@0 | 124 | * will be set appropriately for Chinese calendar (UTC + 8:00). |
michael@0 | 125 | * @param success Indicates the status of ChineseCalendar object construction; |
michael@0 | 126 | * if successful, will not be changed to an error value. |
michael@0 | 127 | * @internal |
michael@0 | 128 | */ |
michael@0 | 129 | ChineseCalendar(const Locale& aLocale, int32_t epochYear, const TimeZone* zoneAstroCalc, UErrorCode &success); |
michael@0 | 130 | |
michael@0 | 131 | public: |
michael@0 | 132 | /** |
michael@0 | 133 | * Copy Constructor |
michael@0 | 134 | * @internal |
michael@0 | 135 | */ |
michael@0 | 136 | ChineseCalendar(const ChineseCalendar& other); |
michael@0 | 137 | |
michael@0 | 138 | /** |
michael@0 | 139 | * Destructor. |
michael@0 | 140 | * @internal |
michael@0 | 141 | */ |
michael@0 | 142 | virtual ~ChineseCalendar(); |
michael@0 | 143 | |
michael@0 | 144 | // clone |
michael@0 | 145 | virtual Calendar* clone() const; |
michael@0 | 146 | |
michael@0 | 147 | private: |
michael@0 | 148 | |
michael@0 | 149 | //------------------------------------------------------------------------- |
michael@0 | 150 | // Internal data.... |
michael@0 | 151 | //------------------------------------------------------------------------- |
michael@0 | 152 | |
michael@0 | 153 | UBool isLeapYear; |
michael@0 | 154 | int32_t fEpochYear; // Start year of this Chinese calendar instance. |
michael@0 | 155 | const TimeZone* fZoneAstroCalc; // Zone used for the astronomical calculation |
michael@0 | 156 | // of this Chinese calendar instance. |
michael@0 | 157 | |
michael@0 | 158 | //---------------------------------------------------------------------- |
michael@0 | 159 | // Calendar framework |
michael@0 | 160 | //---------------------------------------------------------------------- |
michael@0 | 161 | |
michael@0 | 162 | protected: |
michael@0 | 163 | virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const; |
michael@0 | 164 | virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month) const; |
michael@0 | 165 | virtual int32_t handleComputeMonthStart(int32_t eyear, int32_t month, UBool useMonth) const; |
michael@0 | 166 | virtual int32_t handleGetExtendedYear(); |
michael@0 | 167 | virtual void handleComputeFields(int32_t julianDay, UErrorCode &status); |
michael@0 | 168 | virtual const UFieldResolutionTable* getFieldResolutionTable() const; |
michael@0 | 169 | |
michael@0 | 170 | public: |
michael@0 | 171 | virtual void add(UCalendarDateFields field, int32_t amount, UErrorCode &status); |
michael@0 | 172 | virtual void add(EDateFields field, int32_t amount, UErrorCode &status); |
michael@0 | 173 | virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode &status); |
michael@0 | 174 | virtual void roll(EDateFields field, int32_t amount, UErrorCode &status); |
michael@0 | 175 | |
michael@0 | 176 | //---------------------------------------------------------------------- |
michael@0 | 177 | // Internal methods & astronomical calculations |
michael@0 | 178 | //---------------------------------------------------------------------- |
michael@0 | 179 | |
michael@0 | 180 | private: |
michael@0 | 181 | |
michael@0 | 182 | static const UFieldResolutionTable CHINESE_DATE_PRECEDENCE[]; |
michael@0 | 183 | |
michael@0 | 184 | double daysToMillis(double days) const; |
michael@0 | 185 | double millisToDays(double millis) const; |
michael@0 | 186 | virtual int32_t winterSolstice(int32_t gyear) const; |
michael@0 | 187 | virtual int32_t newMoonNear(double days, UBool after) const; |
michael@0 | 188 | virtual int32_t synodicMonthsBetween(int32_t day1, int32_t day2) const; |
michael@0 | 189 | virtual int32_t majorSolarTerm(int32_t days) const; |
michael@0 | 190 | virtual UBool hasNoMajorSolarTerm(int32_t newMoon) const; |
michael@0 | 191 | virtual UBool isLeapMonthBetween(int32_t newMoon1, int32_t newMoon2) const; |
michael@0 | 192 | virtual void computeChineseFields(int32_t days, int32_t gyear, |
michael@0 | 193 | int32_t gmonth, UBool setAllFields); |
michael@0 | 194 | virtual int32_t newYear(int32_t gyear) const; |
michael@0 | 195 | virtual void offsetMonth(int32_t newMoon, int32_t dom, int32_t delta); |
michael@0 | 196 | const TimeZone* getChineseCalZoneAstroCalc(void) const; |
michael@0 | 197 | |
michael@0 | 198 | // UObject stuff |
michael@0 | 199 | public: |
michael@0 | 200 | /** |
michael@0 | 201 | * @return The class ID for this object. All objects of a given class have the |
michael@0 | 202 | * same class ID. Objects of other classes have different class IDs. |
michael@0 | 203 | * @internal |
michael@0 | 204 | */ |
michael@0 | 205 | virtual UClassID getDynamicClassID(void) const; |
michael@0 | 206 | |
michael@0 | 207 | /** |
michael@0 | 208 | * Return the class ID for this class. This is useful only for comparing to a return |
michael@0 | 209 | * value from getDynamicClassID(). For example: |
michael@0 | 210 | * |
michael@0 | 211 | * Base* polymorphic_pointer = createPolymorphicObject(); |
michael@0 | 212 | * if (polymorphic_pointer->getDynamicClassID() == |
michael@0 | 213 | * Derived::getStaticClassID()) ... |
michael@0 | 214 | * |
michael@0 | 215 | * @return The class ID for all objects of this class. |
michael@0 | 216 | * @internal |
michael@0 | 217 | */ |
michael@0 | 218 | static UClassID U_EXPORT2 getStaticClassID(void); |
michael@0 | 219 | |
michael@0 | 220 | /** |
michael@0 | 221 | * return the calendar type, "chinese". |
michael@0 | 222 | * |
michael@0 | 223 | * @return calendar type |
michael@0 | 224 | * @internal |
michael@0 | 225 | */ |
michael@0 | 226 | virtual const char * getType() const; |
michael@0 | 227 | |
michael@0 | 228 | |
michael@0 | 229 | protected: |
michael@0 | 230 | /** |
michael@0 | 231 | * (Overrides Calendar) Return true if the current date for this Calendar is in |
michael@0 | 232 | * Daylight Savings Time. Recognizes DST_OFFSET, if it is set. |
michael@0 | 233 | * |
michael@0 | 234 | * @param status Fill-in parameter which receives the status of this operation. |
michael@0 | 235 | * @return True if the current date for this Calendar is in Daylight Savings Time, |
michael@0 | 236 | * false, otherwise. |
michael@0 | 237 | * @internal |
michael@0 | 238 | */ |
michael@0 | 239 | virtual UBool inDaylightTime(UErrorCode& status) const; |
michael@0 | 240 | |
michael@0 | 241 | |
michael@0 | 242 | /** |
michael@0 | 243 | * Returns TRUE because the Islamic Calendar does have a default century |
michael@0 | 244 | * @internal |
michael@0 | 245 | */ |
michael@0 | 246 | virtual UBool haveDefaultCentury() const; |
michael@0 | 247 | |
michael@0 | 248 | /** |
michael@0 | 249 | * Returns the date of the start of the default century |
michael@0 | 250 | * @return start of century - in milliseconds since epoch, 1970 |
michael@0 | 251 | * @internal |
michael@0 | 252 | */ |
michael@0 | 253 | virtual UDate defaultCenturyStart() const; |
michael@0 | 254 | |
michael@0 | 255 | /** |
michael@0 | 256 | * Returns the year in which the default century begins |
michael@0 | 257 | * @internal |
michael@0 | 258 | */ |
michael@0 | 259 | virtual int32_t defaultCenturyStartYear() const; |
michael@0 | 260 | |
michael@0 | 261 | private: // default century stuff. |
michael@0 | 262 | |
michael@0 | 263 | /** |
michael@0 | 264 | * Returns the beginning date of the 100-year window that dates |
michael@0 | 265 | * with 2-digit years are considered to fall within. |
michael@0 | 266 | */ |
michael@0 | 267 | UDate internalGetDefaultCenturyStart(void) const; |
michael@0 | 268 | |
michael@0 | 269 | /** |
michael@0 | 270 | * Returns the first year of the 100-year window that dates with |
michael@0 | 271 | * 2-digit years are considered to fall within. |
michael@0 | 272 | */ |
michael@0 | 273 | int32_t internalGetDefaultCenturyStartYear(void) const; |
michael@0 | 274 | |
michael@0 | 275 | ChineseCalendar(); // default constructor not implemented |
michael@0 | 276 | }; |
michael@0 | 277 | |
michael@0 | 278 | U_NAMESPACE_END |
michael@0 | 279 | |
michael@0 | 280 | #endif |
michael@0 | 281 | #endif |