/src/icu/source/i18n/chnsecal.cpp
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | // © 2016 and later: Unicode, Inc. and others.  | 
2  |  | // License & terms of use: http://www.unicode.org/copyright.html  | 
3  |  | /*  | 
4  |  |  ******************************************************************************  | 
5  |  |  * Copyright (C) 2007-2014, International Business Machines Corporation  | 
6  |  |  * and others. All Rights Reserved.  | 
7  |  |  ******************************************************************************  | 
8  |  |  *  | 
9  |  |  * File CHNSECAL.CPP  | 
10  |  |  *  | 
11  |  |  * Modification History:  | 
12  |  |  *  | 
13  |  |  *   Date        Name        Description  | 
14  |  |  *   9/18/2007  ajmacher         ported from java ChineseCalendar  | 
15  |  |  *****************************************************************************  | 
16  |  |  */  | 
17  |  |  | 
18  |  | #include "chnsecal.h"  | 
19  |  |  | 
20  |  | #if !UCONFIG_NO_FORMATTING  | 
21  |  |  | 
22  |  | #include "umutex.h"  | 
23  |  | #include <float.h>  | 
24  |  | #include "gregoimp.h" // Math  | 
25  |  | #include "astro.h" // CalendarAstronomer  | 
26  |  | #include "unicode/simpletz.h"  | 
27  |  | #include "uhash.h"  | 
28  |  | #include "ucln_in.h"  | 
29  |  |  | 
30  |  | // Debugging  | 
31  |  | #ifdef U_DEBUG_CHNSECAL  | 
32  |  | # include <stdio.h>  | 
33  |  | # include <stdarg.h>  | 
34  |  | static void debug_chnsecal_loc(const char *f, int32_t l)  | 
35  |  | { | 
36  |  |     fprintf(stderr, "%s:%d: ", f, l);  | 
37  |  | }  | 
38  |  |  | 
39  |  | static void debug_chnsecal_msg(const char *pat, ...)  | 
40  |  | { | 
41  |  |     va_list ap;  | 
42  |  |     va_start(ap, pat);  | 
43  |  |     vfprintf(stderr, pat, ap);  | 
44  |  |     fflush(stderr);  | 
45  |  | }  | 
46  |  | // must use double parens, i.e.:  U_DEBUG_CHNSECAL_MSG(("four is: %d",4)); | 
47  |  | #define U_DEBUG_CHNSECAL_MSG(x) {debug_chnsecal_loc(__FILE__,__LINE__);debug_chnsecal_msg x;} | 
48  |  | #else  | 
49  |  | #define U_DEBUG_CHNSECAL_MSG(x)  | 
50  |  | #endif  | 
51  |  |  | 
52  |  |  | 
53  |  | // --- The cache --  | 
54  |  | static icu::UMutex astroLock;  | 
55  |  | static icu::CalendarAstronomer *gChineseCalendarAstro = NULL;  | 
56  |  |  | 
57  |  | // Lazy Creation & Access synchronized by class CalendarCache with a mutex.  | 
58  |  | static icu::CalendarCache *gChineseCalendarWinterSolsticeCache = NULL;  | 
59  |  | static icu::CalendarCache *gChineseCalendarNewYearCache = NULL;  | 
60  |  |  | 
61  |  | static icu::TimeZone *gChineseCalendarZoneAstroCalc = NULL;  | 
62  |  | static icu::UInitOnce gChineseCalendarZoneAstroCalcInitOnce = U_INITONCE_INITIALIZER;  | 
63  |  |  | 
64  |  | /**  | 
65  |  |  * The start year of the Chinese calendar, the 61st year of the reign  | 
66  |  |  * of Huang Di.  Some sources use the first year of his reign,  | 
67  |  |  * resulting in EXTENDED_YEAR values 60 years greater and ERA (cycle)  | 
68  |  |  * values one greater.  | 
69  |  |  */  | 
70  |  | static const int32_t CHINESE_EPOCH_YEAR = -2636; // Gregorian year  | 
71  |  |  | 
72  |  | /**  | 
73  |  |  * The offset from GMT in milliseconds at which we perform astronomical  | 
74  |  |  * computations.  Some sources use a different historically accurate  | 
75  |  |  * offset of GMT+7:45:40 for years before 1929; we do not do this.  | 
76  |  |  */  | 
77  |  | static const int32_t CHINA_OFFSET = 8 * kOneHour;  | 
78  |  |  | 
79  |  | /**  | 
80  |  |  * Value to be added or subtracted from the local days of a new moon to  | 
81  |  |  * get close to the next or prior new moon, but not cross it.  Must be  | 
82  |  |  * >= 1 and < CalendarAstronomer.SYNODIC_MONTH.  | 
83  |  |  */  | 
84  |  | static const int32_t SYNODIC_GAP = 25;  | 
85  |  |  | 
86  |  |  | 
87  |  | U_CDECL_BEGIN  | 
88  | 0  | static UBool calendar_chinese_cleanup(void) { | 
89  | 0  |     if (gChineseCalendarAstro) { | 
90  | 0  |         delete gChineseCalendarAstro;  | 
91  | 0  |         gChineseCalendarAstro = NULL;  | 
92  | 0  |     }  | 
93  | 0  |     if (gChineseCalendarWinterSolsticeCache) { | 
94  | 0  |         delete gChineseCalendarWinterSolsticeCache;  | 
95  | 0  |         gChineseCalendarWinterSolsticeCache = NULL;  | 
96  | 0  |     }  | 
97  | 0  |     if (gChineseCalendarNewYearCache) { | 
98  | 0  |         delete gChineseCalendarNewYearCache;  | 
99  | 0  |         gChineseCalendarNewYearCache = NULL;  | 
100  | 0  |     }  | 
101  | 0  |     if (gChineseCalendarZoneAstroCalc) { | 
102  | 0  |         delete gChineseCalendarZoneAstroCalc;  | 
103  | 0  |         gChineseCalendarZoneAstroCalc = NULL;  | 
104  | 0  |     }  | 
105  | 0  |     gChineseCalendarZoneAstroCalcInitOnce.reset();  | 
106  | 0  |     return TRUE;  | 
107  | 0  | }  | 
108  |  | U_CDECL_END  | 
109  |  |  | 
110  |  | U_NAMESPACE_BEGIN  | 
111  |  |  | 
112  |  |  | 
113  |  | // Implementation of the ChineseCalendar class  | 
114  |  |  | 
115  |  |  | 
116  |  | //-------------------------------------------------------------------------  | 
117  |  | // Constructors...  | 
118  |  | //-------------------------------------------------------------------------  | 
119  |  |  | 
120  |  |  | 
121  | 0  | ChineseCalendar* ChineseCalendar::clone() const { | 
122  | 0  |     return new ChineseCalendar(*this);  | 
123  | 0  | }  | 
124  |  |  | 
125  |  | ChineseCalendar::ChineseCalendar(const Locale& aLocale, UErrorCode& success)  | 
126  | 0  | :   Calendar(TimeZone::forLocaleOrDefault(aLocale), aLocale, success),  | 
127  | 0  |     isLeapYear(FALSE),  | 
128  | 0  |     fEpochYear(CHINESE_EPOCH_YEAR),  | 
129  | 0  |     fZoneAstroCalc(getChineseCalZoneAstroCalc())  | 
130  | 0  | { | 
131  | 0  |     setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.  | 
132  | 0  | }  | 
133  |  |  | 
134  |  | ChineseCalendar::ChineseCalendar(const Locale& aLocale, int32_t epochYear,  | 
135  |  |                                 const TimeZone* zoneAstroCalc, UErrorCode &success)  | 
136  | 0  | :   Calendar(TimeZone::forLocaleOrDefault(aLocale), aLocale, success),  | 
137  | 0  |     isLeapYear(FALSE),  | 
138  | 0  |     fEpochYear(epochYear),  | 
139  | 0  |     fZoneAstroCalc(zoneAstroCalc)  | 
140  | 0  | { | 
141  | 0  |     setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.  | 
142  | 0  | }  | 
143  |  |  | 
144  | 0  | ChineseCalendar::ChineseCalendar(const ChineseCalendar& other) : Calendar(other) { | 
145  | 0  |     isLeapYear = other.isLeapYear;  | 
146  | 0  |     fEpochYear = other.fEpochYear;  | 
147  | 0  |     fZoneAstroCalc = other.fZoneAstroCalc;  | 
148  | 0  | }  | 
149  |  |  | 
150  |  | ChineseCalendar::~ChineseCalendar()  | 
151  | 0  | { | 
152  | 0  | }  | 
153  |  |  | 
154  | 0  | const char *ChineseCalendar::getType() const {  | 
155  | 0  |     return "chinese";  | 
156  | 0  | }  | 
157  |  |  | 
158  | 0  | static void U_CALLCONV initChineseCalZoneAstroCalc() { | 
159  | 0  |     gChineseCalendarZoneAstroCalc = new SimpleTimeZone(CHINA_OFFSET, UNICODE_STRING_SIMPLE("CHINA_ZONE") ); | 
160  | 0  |     ucln_i18n_registerCleanup(UCLN_I18N_CHINESE_CALENDAR, calendar_chinese_cleanup);  | 
161  | 0  | }  | 
162  |  |  | 
163  | 0  | const TimeZone* ChineseCalendar::getChineseCalZoneAstroCalc(void) const { | 
164  | 0  |     umtx_initOnce(gChineseCalendarZoneAstroCalcInitOnce, &initChineseCalZoneAstroCalc);  | 
165  | 0  |     return gChineseCalendarZoneAstroCalc;  | 
166  | 0  | }  | 
167  |  |  | 
168  |  | //-------------------------------------------------------------------------  | 
169  |  | // Minimum / Maximum access functions  | 
170  |  | //-------------------------------------------------------------------------  | 
171  |  |  | 
172  |  |  | 
173  |  | static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = { | 
174  |  |     // Minimum  Greatest     Least    Maximum  | 
175  |  |     //           Minimum   Maximum  | 
176  |  |     {        1,        1,    83333,    83333}, // ERA | 
177  |  |     {        1,        1,       60,       60}, // YEAR | 
178  |  |     {        0,        0,       11,       11}, // MONTH | 
179  |  |     {        1,        1,       50,       55}, // WEEK_OF_YEAR | 
180  |  |     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH | 
181  |  |     {        1,        1,       29,       30}, // DAY_OF_MONTH | 
182  |  |     {        1,        1,      353,      385}, // DAY_OF_YEAR | 
183  |  |     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK | 
184  |  |     {       -1,       -1,        5,        5}, // DAY_OF_WEEK_IN_MONTH | 
185  |  |     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM | 
186  |  |     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR | 
187  |  |     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY | 
188  |  |     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE | 
189  |  |     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND | 
190  |  |     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND | 
191  |  |     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET | 
192  |  |     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET | 
193  |  |     { -5000000, -5000000,  5000000,  5000000}, // YEAR_WOY | 
194  |  |     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL | 
195  |  |     { -5000000, -5000000,  5000000,  5000000}, // EXTENDED_YEAR | 
196  |  |     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY | 
197  |  |     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY | 
198  |  |     {        0,        0,        1,        1}, // IS_LEAP_MONTH | 
199  |  | };  | 
200  |  |  | 
201  |  |  | 
202  |  | /**  | 
203  |  | * @draft ICU 2.4  | 
204  |  | */  | 
205  | 0  | int32_t ChineseCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const { | 
206  | 0  |     return LIMITS[field][limitType];  | 
207  | 0  | }  | 
208  |  |  | 
209  |  |  | 
210  |  | //----------------------------------------------------------------------  | 
211  |  | // Calendar framework  | 
212  |  | //----------------------------------------------------------------------  | 
213  |  |  | 
214  |  | /**  | 
215  |  |  * Implement abstract Calendar method to return the extended year  | 
216  |  |  * defined by the current fields.  This will use either the ERA and  | 
217  |  |  * YEAR field as the cycle and year-of-cycle, or the EXTENDED_YEAR  | 
218  |  |  * field as the continuous year count, depending on which is newer.  | 
219  |  |  * @stable ICU 2.8  | 
220  |  |  */  | 
221  | 0  | int32_t ChineseCalendar::handleGetExtendedYear() { | 
222  | 0  |     int32_t year;  | 
223  | 0  |     if (newestStamp(UCAL_ERA, UCAL_YEAR, kUnset) <= fStamp[UCAL_EXTENDED_YEAR]) { | 
224  | 0  |         year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1  | 
225  | 0  |     } else { | 
226  | 0  |         int32_t cycle = internalGet(UCAL_ERA, 1) - 1; // 0-based cycle  | 
227  |  |         // adjust to the instance specific epoch  | 
228  | 0  |         year = cycle * 60 + internalGet(UCAL_YEAR, 1) - (fEpochYear - CHINESE_EPOCH_YEAR);  | 
229  | 0  |     }  | 
230  | 0  |     return year;  | 
231  | 0  | }  | 
232  |  |  | 
233  |  | /**  | 
234  |  |  * Override Calendar method to return the number of days in the given  | 
235  |  |  * extended year and month.  | 
236  |  |  *  | 
237  |  |  * <p>Note: This method also reads the IS_LEAP_MONTH field to determine  | 
238  |  |  * whether or not the given month is a leap month.  | 
239  |  |  * @stable ICU 2.8  | 
240  |  |  */  | 
241  | 0  | int32_t ChineseCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const { | 
242  | 0  |     int32_t thisStart = handleComputeMonthStart(extendedYear, month, TRUE) -  | 
243  | 0  |         kEpochStartAsJulianDay + 1; // Julian day -> local days  | 
244  | 0  |     int32_t nextStart = newMoonNear(thisStart + SYNODIC_GAP, TRUE);  | 
245  | 0  |     return nextStart - thisStart;  | 
246  | 0  | }  | 
247  |  |  | 
248  |  | /**  | 
249  |  |  * Override Calendar to compute several fields specific to the Chinese  | 
250  |  |  * calendar system.  These are:  | 
251  |  |  *  | 
252  |  |  * <ul><li>ERA  | 
253  |  |  * <li>YEAR  | 
254  |  |  * <li>MONTH  | 
255  |  |  * <li>DAY_OF_MONTH  | 
256  |  |  * <li>DAY_OF_YEAR  | 
257  |  |  * <li>EXTENDED_YEAR</ul>  | 
258  |  |  *   | 
259  |  |  * The DAY_OF_WEEK and DOW_LOCAL fields are already set when this  | 
260  |  |  * method is called.  The getGregorianXxx() methods return Gregorian  | 
261  |  |  * calendar equivalents for the given Julian day.  | 
262  |  |  *  | 
263  |  |  * <p>Compute the ChineseCalendar-specific field IS_LEAP_MONTH.  | 
264  |  |  * @stable ICU 2.8  | 
265  |  |  */  | 
266  | 0  | void ChineseCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/) { | 
267  |  | 
  | 
268  | 0  |     computeChineseFields(julianDay - kEpochStartAsJulianDay, // local days  | 
269  | 0  |                          getGregorianYear(), getGregorianMonth(),  | 
270  | 0  |                          TRUE); // set all fields  | 
271  | 0  | }  | 
272  |  |  | 
273  |  | /**  | 
274  |  |  * Field resolution table that incorporates IS_LEAP_MONTH.  | 
275  |  |  */  | 
276  |  | const UFieldResolutionTable ChineseCalendar::CHINESE_DATE_PRECEDENCE[] =  | 
277  |  | { | 
278  |  |     { | 
279  |  |         { UCAL_DAY_OF_MONTH, kResolveSTOP }, | 
280  |  |         { UCAL_WEEK_OF_YEAR, UCAL_DAY_OF_WEEK, kResolveSTOP }, | 
281  |  |         { UCAL_WEEK_OF_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP }, | 
282  |  |         { UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP }, | 
283  |  |         { UCAL_WEEK_OF_YEAR, UCAL_DOW_LOCAL, kResolveSTOP }, | 
284  |  |         { UCAL_WEEK_OF_MONTH, UCAL_DOW_LOCAL, kResolveSTOP }, | 
285  |  |         { UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DOW_LOCAL, kResolveSTOP }, | 
286  |  |         { UCAL_DAY_OF_YEAR, kResolveSTOP }, | 
287  |  |         { kResolveRemap | UCAL_DAY_OF_MONTH, UCAL_IS_LEAP_MONTH, kResolveSTOP }, | 
288  |  |         { kResolveSTOP } | 
289  |  |     },  | 
290  |  |     { | 
291  |  |         { UCAL_WEEK_OF_YEAR, kResolveSTOP }, | 
292  |  |         { UCAL_WEEK_OF_MONTH, kResolveSTOP }, | 
293  |  |         { UCAL_DAY_OF_WEEK_IN_MONTH, kResolveSTOP }, | 
294  |  |         { kResolveRemap | UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP }, | 
295  |  |         { kResolveRemap | UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DOW_LOCAL, kResolveSTOP }, | 
296  |  |         { kResolveSTOP } | 
297  |  |     },  | 
298  |  |     {{kResolveSTOP}} | 
299  |  | };  | 
300  |  |  | 
301  |  | /**  | 
302  |  |  * Override Calendar to add IS_LEAP_MONTH to the field resolution  | 
303  |  |  * table.  | 
304  |  |  * @stable ICU 2.8  | 
305  |  |  */  | 
306  | 0  | const UFieldResolutionTable* ChineseCalendar::getFieldResolutionTable() const { | 
307  | 0  |     return CHINESE_DATE_PRECEDENCE;  | 
308  | 0  | }  | 
309  |  |  | 
310  |  | /**  | 
311  |  |  * Return the Julian day number of day before the first day of the  | 
312  |  |  * given month in the given extended year.  | 
313  |  |  *   | 
314  |  |  * <p>Note: This method reads the IS_LEAP_MONTH field to determine  | 
315  |  |  * whether the given month is a leap month.  | 
316  |  |  * @param eyear the extended year  | 
317  |  |  * @param month the zero-based month.  The month is also determined  | 
318  |  |  * by reading the IS_LEAP_MONTH field.  | 
319  |  |  * @return the Julian day number of the day before the first  | 
320  |  |  * day of the given month and year  | 
321  |  |  * @stable ICU 2.8  | 
322  |  |  */  | 
323  | 0  | int32_t ChineseCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool useMonth) const { | 
324  |  | 
  | 
325  | 0  |     ChineseCalendar *nonConstThis = (ChineseCalendar*)this; // cast away const  | 
326  |  |  | 
327  |  |     // If the month is out of range, adjust it into range, and  | 
328  |  |     // modify the extended year value accordingly.  | 
329  | 0  |     if (month < 0 || month > 11) { | 
330  | 0  |         double m = month;  | 
331  | 0  |         eyear += (int32_t)ClockMath::floorDivide(m, 12.0, m);  | 
332  | 0  |         month = (int32_t)m;  | 
333  | 0  |     }  | 
334  |  | 
  | 
335  | 0  |     int32_t gyear = eyear + fEpochYear - 1; // Gregorian year  | 
336  | 0  |     int32_t theNewYear = newYear(gyear);  | 
337  | 0  |     int32_t newMoon = newMoonNear(theNewYear + month * 29, TRUE);  | 
338  |  |       | 
339  | 0  |     int32_t julianDay = newMoon + kEpochStartAsJulianDay;  | 
340  |  |  | 
341  |  |     // Save fields for later restoration  | 
342  | 0  |     int32_t saveMonth = internalGet(UCAL_MONTH);  | 
343  | 0  |     int32_t saveIsLeapMonth = internalGet(UCAL_IS_LEAP_MONTH);  | 
344  |  |  | 
345  |  |     // Ignore IS_LEAP_MONTH field if useMonth is false  | 
346  | 0  |     int32_t isLeapMonth = useMonth ? saveIsLeapMonth : 0;  | 
347  |  | 
  | 
348  | 0  |     UErrorCode status = U_ZERO_ERROR;  | 
349  | 0  |     nonConstThis->computeGregorianFields(julianDay, status);  | 
350  | 0  |     if (U_FAILURE(status))  | 
351  | 0  |         return 0;  | 
352  |  |       | 
353  |  |     // This will modify the MONTH and IS_LEAP_MONTH fields (only)  | 
354  | 0  |     nonConstThis->computeChineseFields(newMoon, getGregorianYear(),  | 
355  | 0  |                          getGregorianMonth(), FALSE);          | 
356  |  | 
  | 
357  | 0  |     if (month != internalGet(UCAL_MONTH) ||  | 
358  | 0  |         isLeapMonth != internalGet(UCAL_IS_LEAP_MONTH)) { | 
359  | 0  |         newMoon = newMoonNear(newMoon + SYNODIC_GAP, TRUE);  | 
360  | 0  |         julianDay = newMoon + kEpochStartAsJulianDay;  | 
361  | 0  |     }  | 
362  |  | 
  | 
363  | 0  |     nonConstThis->internalSet(UCAL_MONTH, saveMonth);  | 
364  | 0  |     nonConstThis->internalSet(UCAL_IS_LEAP_MONTH, saveIsLeapMonth);  | 
365  |  | 
  | 
366  | 0  |     return julianDay - 1;  | 
367  | 0  | }  | 
368  |  |  | 
369  |  |  | 
370  |  | /**  | 
371  |  |  * Override Calendar to handle leap months properly.  | 
372  |  |  * @stable ICU 2.8  | 
373  |  |  */  | 
374  | 0  | void ChineseCalendar::add(UCalendarDateFields field, int32_t amount, UErrorCode& status) { | 
375  | 0  |     switch (field) { | 
376  | 0  |     case UCAL_MONTH:  | 
377  | 0  |         if (amount != 0) { | 
378  | 0  |             int32_t dom = get(UCAL_DAY_OF_MONTH, status);  | 
379  | 0  |             if (U_FAILURE(status)) break;  | 
380  | 0  |             int32_t day = get(UCAL_JULIAN_DAY, status) - kEpochStartAsJulianDay; // Get local day  | 
381  | 0  |             if (U_FAILURE(status)) break;  | 
382  | 0  |             int32_t moon = day - dom + 1; // New moon   | 
383  | 0  |             offsetMonth(moon, dom, amount);  | 
384  | 0  |         }  | 
385  | 0  |         break;  | 
386  | 0  |     default:  | 
387  | 0  |         Calendar::add(field, amount, status);  | 
388  | 0  |         break;  | 
389  | 0  |     }  | 
390  | 0  | }  | 
391  |  |  | 
392  |  | /**  | 
393  |  |  * Override Calendar to handle leap months properly.  | 
394  |  |  * @stable ICU 2.8  | 
395  |  |  */  | 
396  | 0  | void ChineseCalendar::add(EDateFields field, int32_t amount, UErrorCode& status) { | 
397  | 0  |     add((UCalendarDateFields)field, amount, status);  | 
398  | 0  | }  | 
399  |  |  | 
400  |  | /**  | 
401  |  |  * Override Calendar to handle leap months properly.  | 
402  |  |  * @stable ICU 2.8  | 
403  |  |  */  | 
404  | 0  | void ChineseCalendar::roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) { | 
405  | 0  |     switch (field) { | 
406  | 0  |     case UCAL_MONTH:  | 
407  | 0  |         if (amount != 0) { | 
408  | 0  |             int32_t dom = get(UCAL_DAY_OF_MONTH, status);  | 
409  | 0  |             if (U_FAILURE(status)) break;  | 
410  | 0  |             int32_t day = get(UCAL_JULIAN_DAY, status) - kEpochStartAsJulianDay; // Get local day  | 
411  | 0  |             if (U_FAILURE(status)) break;  | 
412  | 0  |             int32_t moon = day - dom + 1; // New moon (start of this month)  | 
413  |  |  | 
414  |  |             // Note throughout the following:  Months 12 and 1 are never  | 
415  |  |             // followed by a leap month (D&R p. 185).  | 
416  |  |  | 
417  |  |             // Compute the adjusted month number m.  This is zero-based  | 
418  |  |             // value from 0..11 in a non-leap year, and from 0..12 in a  | 
419  |  |             // leap year.  | 
420  | 0  |             int32_t m = get(UCAL_MONTH, status); // 0-based month  | 
421  | 0  |             if (U_FAILURE(status)) break;  | 
422  | 0  |             if (isLeapYear) { // (member variable) | 
423  | 0  |                 if (get(UCAL_IS_LEAP_MONTH, status) == 1) { | 
424  | 0  |                     ++m;  | 
425  | 0  |                 } else { | 
426  |  |                     // Check for a prior leap month.  (In the  | 
427  |  |                     // following, month 0 is the first month of the  | 
428  |  |                     // year.)  Month 0 is never followed by a leap  | 
429  |  |                     // month, and we know month m is not a leap month.  | 
430  |  |                     // moon1 will be the start of month 0 if there is  | 
431  |  |                     // no leap month between month 0 and month m;  | 
432  |  |                     // otherwise it will be the start of month 1.  | 
433  | 0  |                     int moon1 = moon -  | 
434  | 0  |                         (int) (CalendarAstronomer::SYNODIC_MONTH * (m - 0.5));  | 
435  | 0  |                     moon1 = newMoonNear(moon1, TRUE);  | 
436  | 0  |                     if (isLeapMonthBetween(moon1, moon)) { | 
437  | 0  |                         ++m;  | 
438  | 0  |                     }  | 
439  | 0  |                 }  | 
440  | 0  |                 if (U_FAILURE(status)) break;  | 
441  | 0  |             }  | 
442  |  |  | 
443  |  |             // Now do the standard roll computation on m, with the  | 
444  |  |             // allowed range of 0..n-1, where n is 12 or 13.  | 
445  | 0  |             int32_t n = isLeapYear ? 13 : 12; // Months in this year  | 
446  | 0  |             int32_t newM = (m + amount) % n;  | 
447  | 0  |             if (newM < 0) { | 
448  | 0  |                 newM += n;  | 
449  | 0  |             }  | 
450  |  | 
  | 
451  | 0  |             if (newM != m) { | 
452  | 0  |                 offsetMonth(moon, dom, newM - m);  | 
453  | 0  |             }  | 
454  | 0  |         }  | 
455  | 0  |         break;  | 
456  | 0  |     default:  | 
457  | 0  |         Calendar::roll(field, amount, status);  | 
458  | 0  |         break;  | 
459  | 0  |     }  | 
460  | 0  | }  | 
461  |  |  | 
462  | 0  | void ChineseCalendar::roll(EDateFields field, int32_t amount, UErrorCode& status) { | 
463  | 0  |     roll((UCalendarDateFields)field, amount, status);  | 
464  | 0  | }  | 
465  |  |  | 
466  |  |  | 
467  |  | //------------------------------------------------------------------  | 
468  |  | // Support methods and constants  | 
469  |  | //------------------------------------------------------------------  | 
470  |  |  | 
471  |  | /**  | 
472  |  |  * Convert local days to UTC epoch milliseconds.  | 
473  |  |  * This is not an accurate conversion in that getTimezoneOffset   | 
474  |  |  * takes the milliseconds in GMT (not local time). In theory, more   | 
475  |  |  * accurate algorithm can be implemented but practically we do not need   | 
476  |  |  * to go through that complication as long as the historical timezone   | 
477  |  |  * changes did not happen around the 'tricky' new moon (new moon around   | 
478  |  |  * midnight).   | 
479  |  |  *    | 
480  |  |  * @param days days after January 1, 1970 0:00 in the astronomical base zone  | 
481  |  |  * @return milliseconds after January 1, 1970 0:00 GMT  | 
482  |  |  */  | 
483  | 0  | double ChineseCalendar::daysToMillis(double days) const { | 
484  | 0  |     double millis = days * (double)kOneDay;  | 
485  | 0  |     if (fZoneAstroCalc != NULL) { | 
486  | 0  |         int32_t rawOffset, dstOffset;  | 
487  | 0  |         UErrorCode status = U_ZERO_ERROR;  | 
488  | 0  |         fZoneAstroCalc->getOffset(millis, FALSE, rawOffset, dstOffset, status);  | 
489  | 0  |         if (U_SUCCESS(status)) { | 
490  | 0  |           return millis - (double)(rawOffset + dstOffset);  | 
491  | 0  |         }  | 
492  | 0  |     }  | 
493  | 0  |     return millis - (double)CHINA_OFFSET;  | 
494  | 0  | }  | 
495  |  |  | 
496  |  | /**  | 
497  |  |  * Convert UTC epoch milliseconds to local days.  | 
498  |  |  * @param millis milliseconds after January 1, 1970 0:00 GMT  | 
499  |  |  * @return days after January 1, 1970 0:00 in the astronomical base zone  | 
500  |  |  */  | 
501  | 0  | double ChineseCalendar::millisToDays(double millis) const { | 
502  | 0  |     if (fZoneAstroCalc != NULL) { | 
503  | 0  |         int32_t rawOffset, dstOffset;  | 
504  | 0  |         UErrorCode status = U_ZERO_ERROR;  | 
505  | 0  |         fZoneAstroCalc->getOffset(millis, FALSE, rawOffset, dstOffset, status);  | 
506  | 0  |         if (U_SUCCESS(status)) { | 
507  | 0  |           return ClockMath::floorDivide(millis + (double)(rawOffset + dstOffset), kOneDay);  | 
508  | 0  |         }  | 
509  | 0  |     }  | 
510  | 0  |     return ClockMath::floorDivide(millis + (double)CHINA_OFFSET, kOneDay);  | 
511  | 0  | }  | 
512  |  |  | 
513  |  | //------------------------------------------------------------------  | 
514  |  | // Astronomical computations  | 
515  |  | //------------------------------------------------------------------  | 
516  |  |  | 
517  |  |  | 
518  |  | /**  | 
519  |  |  * Return the major solar term on or after December 15 of the given  | 
520  |  |  * Gregorian year, that is, the winter solstice of the given year.  | 
521  |  |  * Computations are relative to Asia/Shanghai time zone.  | 
522  |  |  * @param gyear a Gregorian year  | 
523  |  |  * @return days after January 1, 1970 0:00 Asia/Shanghai of the  | 
524  |  |  * winter solstice of the given year  | 
525  |  |  */  | 
526  | 0  | int32_t ChineseCalendar::winterSolstice(int32_t gyear) const { | 
527  |  | 
  | 
528  | 0  |     UErrorCode status = U_ZERO_ERROR;  | 
529  | 0  |     int32_t cacheValue = CalendarCache::get(&gChineseCalendarWinterSolsticeCache, gyear, status);  | 
530  |  | 
  | 
531  | 0  |     if (cacheValue == 0) { | 
532  |  |         // In books December 15 is used, but it fails for some years  | 
533  |  |         // using our algorithms, e.g.: 1298 1391 1492 1553 1560.  That  | 
534  |  |         // is, winterSolstice(1298) starts search at Dec 14 08:00:00  | 
535  |  |         // PST 1298 with a final result of Dec 14 10:31:59 PST 1299.  | 
536  | 0  |         double ms = daysToMillis(Grego::fieldsToDay(gyear, UCAL_DECEMBER, 1));  | 
537  |  | 
  | 
538  | 0  |         umtx_lock(&astroLock);  | 
539  | 0  |         if(gChineseCalendarAstro == NULL) { | 
540  | 0  |             gChineseCalendarAstro = new CalendarAstronomer();  | 
541  | 0  |             ucln_i18n_registerCleanup(UCLN_I18N_CHINESE_CALENDAR, calendar_chinese_cleanup);  | 
542  | 0  |         }  | 
543  | 0  |         gChineseCalendarAstro->setTime(ms);  | 
544  | 0  |         UDate solarLong = gChineseCalendarAstro->getSunTime(CalendarAstronomer::WINTER_SOLSTICE(), TRUE);  | 
545  | 0  |         umtx_unlock(&astroLock);  | 
546  |  |  | 
547  |  |         // Winter solstice is 270 degrees solar longitude aka Dongzhi  | 
548  | 0  |         cacheValue = (int32_t)millisToDays(solarLong);  | 
549  | 0  |         CalendarCache::put(&gChineseCalendarWinterSolsticeCache, gyear, cacheValue, status);  | 
550  | 0  |     }  | 
551  | 0  |     if(U_FAILURE(status)) { | 
552  | 0  |         cacheValue = 0;  | 
553  | 0  |     }  | 
554  | 0  |     return cacheValue;  | 
555  | 0  | }  | 
556  |  |  | 
557  |  | /**  | 
558  |  |  * Return the closest new moon to the given date, searching either  | 
559  |  |  * forward or backward in time.  | 
560  |  |  * @param days days after January 1, 1970 0:00 Asia/Shanghai  | 
561  |  |  * @param after if true, search for a new moon on or after the given  | 
562  |  |  * date; otherwise, search for a new moon before it  | 
563  |  |  * @return days after January 1, 1970 0:00 Asia/Shanghai of the nearest  | 
564  |  |  * new moon after or before <code>days</code>  | 
565  |  |  */  | 
566  | 0  | int32_t ChineseCalendar::newMoonNear(double days, UBool after) const { | 
567  |  |       | 
568  | 0  |     umtx_lock(&astroLock);  | 
569  | 0  |     if(gChineseCalendarAstro == NULL) { | 
570  | 0  |         gChineseCalendarAstro = new CalendarAstronomer();  | 
571  | 0  |         ucln_i18n_registerCleanup(UCLN_I18N_CHINESE_CALENDAR, calendar_chinese_cleanup);  | 
572  | 0  |     }  | 
573  | 0  |     gChineseCalendarAstro->setTime(daysToMillis(days));  | 
574  | 0  |     UDate newMoon = gChineseCalendarAstro->getMoonTime(CalendarAstronomer::NEW_MOON(), after);  | 
575  | 0  |     umtx_unlock(&astroLock);  | 
576  |  |       | 
577  | 0  |     return (int32_t) millisToDays(newMoon);  | 
578  | 0  | }  | 
579  |  |  | 
580  |  | /**  | 
581  |  |  * Return the nearest integer number of synodic months between  | 
582  |  |  * two dates.  | 
583  |  |  * @param day1 days after January 1, 1970 0:00 Asia/Shanghai  | 
584  |  |  * @param day2 days after January 1, 1970 0:00 Asia/Shanghai  | 
585  |  |  * @return the nearest integer number of months between day1 and day2  | 
586  |  |  */  | 
587  | 0  | int32_t ChineseCalendar::synodicMonthsBetween(int32_t day1, int32_t day2) const { | 
588  | 0  |     double roundme = ((day2 - day1) / CalendarAstronomer::SYNODIC_MONTH);  | 
589  | 0  |     return (int32_t) (roundme + (roundme >= 0 ? .5 : -.5));  | 
590  | 0  | }  | 
591  |  |  | 
592  |  | /**  | 
593  |  |  * Return the major solar term on or before a given date.  This  | 
594  |  |  * will be an integer from 1..12, with 1 corresponding to 330 degrees,  | 
595  |  |  * 2 to 0 degrees, 3 to 30 degrees,..., and 12 to 300 degrees.  | 
596  |  |  * @param days days after January 1, 1970 0:00 Asia/Shanghai  | 
597  |  |  */  | 
598  | 0  | int32_t ChineseCalendar::majorSolarTerm(int32_t days) const { | 
599  |  |       | 
600  | 0  |     umtx_lock(&astroLock);  | 
601  | 0  |     if(gChineseCalendarAstro == NULL) { | 
602  | 0  |         gChineseCalendarAstro = new CalendarAstronomer();  | 
603  | 0  |         ucln_i18n_registerCleanup(UCLN_I18N_CHINESE_CALENDAR, calendar_chinese_cleanup);  | 
604  | 0  |     }  | 
605  | 0  |     gChineseCalendarAstro->setTime(daysToMillis(days));  | 
606  | 0  |     UDate solarLongitude = gChineseCalendarAstro->getSunLongitude();  | 
607  | 0  |     umtx_unlock(&astroLock);  | 
608  |  |  | 
609  |  |     // Compute (floor(solarLongitude / (pi/6)) + 2) % 12  | 
610  | 0  |     int32_t term = ( ((int32_t)(6 * solarLongitude / CalendarAstronomer::PI)) + 2 ) % 12;  | 
611  | 0  |     if (term < 1) { | 
612  | 0  |         term += 12;  | 
613  | 0  |     }  | 
614  | 0  |     return term;  | 
615  | 0  | }  | 
616  |  |  | 
617  |  | /**  | 
618  |  |  * Return true if the given month lacks a major solar term.  | 
619  |  |  * @param newMoon days after January 1, 1970 0:00 Asia/Shanghai of a new  | 
620  |  |  * moon  | 
621  |  |  */  | 
622  | 0  | UBool ChineseCalendar::hasNoMajorSolarTerm(int32_t newMoon) const { | 
623  | 0  |     return majorSolarTerm(newMoon) ==  | 
624  | 0  |         majorSolarTerm(newMoonNear(newMoon + SYNODIC_GAP, TRUE));  | 
625  | 0  | }  | 
626  |  |  | 
627  |  |  | 
628  |  | //------------------------------------------------------------------  | 
629  |  | // Time to fields  | 
630  |  | //------------------------------------------------------------------  | 
631  |  |  | 
632  |  | /**  | 
633  |  |  * Return true if there is a leap month on or after month newMoon1 and  | 
634  |  |  * at or before month newMoon2.  | 
635  |  |  * @param newMoon1 days after January 1, 1970 0:00 astronomical base zone  | 
636  |  |  * of a new moon  | 
637  |  |  * @param newMoon2 days after January 1, 1970 0:00 astronomical base zone  | 
638  |  |  * of a new moon  | 
639  |  |  */  | 
640  | 0  | UBool ChineseCalendar::isLeapMonthBetween(int32_t newMoon1, int32_t newMoon2) const { | 
641  |  | 
  | 
642  |  | #ifdef U_DEBUG_CHNSECAL  | 
643  |  |     // This is only needed to debug the timeOfAngle divergence bug.  | 
644  |  |     // Remove this later. Liu 11/9/00  | 
645  |  |     if (synodicMonthsBetween(newMoon1, newMoon2) >= 50) { | 
646  |  |         U_DEBUG_CHNSECAL_MSG((  | 
647  |  |             "isLeapMonthBetween(%d, %d): Invalid parameters", newMoon1, newMoon2  | 
648  |  |             ));  | 
649  |  |     }  | 
650  |  | #endif  | 
651  |  | 
  | 
652  | 0  |     return (newMoon2 >= newMoon1) &&  | 
653  | 0  |         (isLeapMonthBetween(newMoon1, newMoonNear(newMoon2 - SYNODIC_GAP, FALSE)) ||  | 
654  | 0  |          hasNoMajorSolarTerm(newMoon2));  | 
655  | 0  | }  | 
656  |  |  | 
657  |  | /**  | 
658  |  |  * Compute fields for the Chinese calendar system.  This method can  | 
659  |  |  * either set all relevant fields, as required by  | 
660  |  |  * <code>handleComputeFields()</code>, or it can just set the MONTH and  | 
661  |  |  * IS_LEAP_MONTH fields, as required by  | 
662  |  |  * <code>handleComputeMonthStart()</code>.  | 
663  |  |  *  | 
664  |  |  * <p>As a side effect, this method sets {@link #isLeapYear}. | 
665  |  |  * @param days days after January 1, 1970 0:00 astronomical base zone  | 
666  |  |  * of the date to compute fields for  | 
667  |  |  * @param gyear the Gregorian year of the given date  | 
668  |  |  * @param gmonth the Gregorian month of the given date  | 
669  |  |  * @param setAllFields if true, set the EXTENDED_YEAR, ERA, YEAR,  | 
670  |  |  * DAY_OF_MONTH, and DAY_OF_YEAR fields.  In either case set the MONTH  | 
671  |  |  * and IS_LEAP_MONTH fields.  | 
672  |  |  */  | 
673  |  | void ChineseCalendar::computeChineseFields(int32_t days, int32_t gyear, int32_t gmonth,  | 
674  | 0  |                                   UBool setAllFields) { | 
675  |  |  | 
676  |  |     // Find the winter solstices before and after the target date.  | 
677  |  |     // These define the boundaries of this Chinese year, specifically,  | 
678  |  |     // the position of month 11, which always contains the solstice.  | 
679  |  |     // We want solsticeBefore <= date < solsticeAfter.  | 
680  | 0  |     int32_t solsticeBefore;  | 
681  | 0  |     int32_t solsticeAfter = winterSolstice(gyear);  | 
682  | 0  |     if (days < solsticeAfter) { | 
683  | 0  |         solsticeBefore = winterSolstice(gyear - 1);  | 
684  | 0  |     } else { | 
685  | 0  |         solsticeBefore = solsticeAfter;  | 
686  | 0  |         solsticeAfter = winterSolstice(gyear + 1);  | 
687  | 0  |     }  | 
688  |  |  | 
689  |  |     // Find the start of the month after month 11.  This will be either  | 
690  |  |     // the prior month 12 or leap month 11 (very rare).  Also find the  | 
691  |  |     // start of the following month 11.  | 
692  | 0  |     int32_t firstMoon = newMoonNear(solsticeBefore + 1, TRUE);  | 
693  | 0  |     int32_t lastMoon = newMoonNear(solsticeAfter + 1, FALSE);  | 
694  | 0  |     int32_t thisMoon = newMoonNear(days + 1, FALSE); // Start of this month  | 
695  |  |     // Note: isLeapYear is a member variable  | 
696  | 0  |     isLeapYear = synodicMonthsBetween(firstMoon, lastMoon) == 12;  | 
697  |  | 
  | 
698  | 0  |     int32_t month = synodicMonthsBetween(firstMoon, thisMoon);  | 
699  | 0  |     if (isLeapYear && isLeapMonthBetween(firstMoon, thisMoon)) { | 
700  | 0  |         month--;  | 
701  | 0  |     }  | 
702  | 0  |     if (month < 1) { | 
703  | 0  |         month += 12;  | 
704  | 0  |     }  | 
705  |  | 
  | 
706  | 0  |     UBool isLeapMonth = isLeapYear &&  | 
707  | 0  |         hasNoMajorSolarTerm(thisMoon) &&  | 
708  | 0  |         !isLeapMonthBetween(firstMoon, newMoonNear(thisMoon - SYNODIC_GAP, FALSE));  | 
709  |  | 
  | 
710  | 0  |     internalSet(UCAL_MONTH, month-1); // Convert from 1-based to 0-based  | 
711  | 0  |     internalSet(UCAL_IS_LEAP_MONTH, isLeapMonth?1:0);  | 
712  |  | 
  | 
713  | 0  |     if (setAllFields) { | 
714  |  |  | 
715  |  |         // Extended year and cycle year is based on the epoch year  | 
716  |  |           | 
717  | 0  |         int32_t extended_year = gyear - fEpochYear;  | 
718  | 0  |         int cycle_year = gyear - CHINESE_EPOCH_YEAR;  | 
719  | 0  |         if (month < 11 ||  | 
720  | 0  |             gmonth >= UCAL_JULY) { | 
721  | 0  |             extended_year++;  | 
722  | 0  |             cycle_year++;  | 
723  | 0  |         }  | 
724  | 0  |         int32_t dayOfMonth = days - thisMoon + 1;  | 
725  |  | 
  | 
726  | 0  |         internalSet(UCAL_EXTENDED_YEAR, extended_year);  | 
727  |  |  | 
728  |  |         // 0->0,60  1->1,1  60->1,60  61->2,1  etc.  | 
729  | 0  |         int32_t yearOfCycle;  | 
730  | 0  |         int32_t cycle = ClockMath::floorDivide(cycle_year - 1, 60, yearOfCycle);  | 
731  | 0  |         internalSet(UCAL_ERA, cycle + 1);  | 
732  | 0  |         internalSet(UCAL_YEAR, yearOfCycle + 1);  | 
733  |  | 
  | 
734  | 0  |         internalSet(UCAL_DAY_OF_MONTH, dayOfMonth);  | 
735  |  |  | 
736  |  |         // Days will be before the first new year we compute if this  | 
737  |  |         // date is in month 11, leap 11, 12.  There is never a leap 12.  | 
738  |  |         // New year computations are cached so this should be cheap in  | 
739  |  |         // the long run.  | 
740  | 0  |         int32_t theNewYear = newYear(gyear);  | 
741  | 0  |         if (days < theNewYear) { | 
742  | 0  |             theNewYear = newYear(gyear-1);  | 
743  | 0  |         }  | 
744  | 0  |         internalSet(UCAL_DAY_OF_YEAR, days - theNewYear + 1);  | 
745  | 0  |     }  | 
746  | 0  | }  | 
747  |  |  | 
748  |  |  | 
749  |  | //------------------------------------------------------------------  | 
750  |  | // Fields to time  | 
751  |  | //------------------------------------------------------------------  | 
752  |  |  | 
753  |  | /**  | 
754  |  |  * Return the Chinese new year of the given Gregorian year.  | 
755  |  |  * @param gyear a Gregorian year  | 
756  |  |  * @return days after January 1, 1970 0:00 astronomical base zone of the  | 
757  |  |  * Chinese new year of the given year (this will be a new moon)  | 
758  |  |  */  | 
759  | 0  | int32_t ChineseCalendar::newYear(int32_t gyear) const { | 
760  | 0  |     UErrorCode status = U_ZERO_ERROR;  | 
761  | 0  |     int32_t cacheValue = CalendarCache::get(&gChineseCalendarNewYearCache, gyear, status);  | 
762  |  | 
  | 
763  | 0  |     if (cacheValue == 0) { | 
764  |  | 
  | 
765  | 0  |         int32_t solsticeBefore= winterSolstice(gyear - 1);  | 
766  | 0  |         int32_t solsticeAfter = winterSolstice(gyear);  | 
767  | 0  |         int32_t newMoon1 = newMoonNear(solsticeBefore + 1, TRUE);  | 
768  | 0  |         int32_t newMoon2 = newMoonNear(newMoon1 + SYNODIC_GAP, TRUE);  | 
769  | 0  |         int32_t newMoon11 = newMoonNear(solsticeAfter + 1, FALSE);  | 
770  |  |           | 
771  | 0  |         if (synodicMonthsBetween(newMoon1, newMoon11) == 12 &&  | 
772  | 0  |             (hasNoMajorSolarTerm(newMoon1) || hasNoMajorSolarTerm(newMoon2))) { | 
773  | 0  |             cacheValue = newMoonNear(newMoon2 + SYNODIC_GAP, TRUE);  | 
774  | 0  |         } else { | 
775  | 0  |             cacheValue = newMoon2;  | 
776  | 0  |         }  | 
777  |  | 
  | 
778  | 0  |         CalendarCache::put(&gChineseCalendarNewYearCache, gyear, cacheValue, status);  | 
779  | 0  |     }  | 
780  | 0  |     if(U_FAILURE(status)) { | 
781  | 0  |         cacheValue = 0;  | 
782  | 0  |     }  | 
783  | 0  |     return cacheValue;  | 
784  | 0  | }  | 
785  |  |  | 
786  |  | /**  | 
787  |  |  * Adjust this calendar to be delta months before or after a given  | 
788  |  |  * start position, pinning the day of month if necessary.  The start  | 
789  |  |  * position is given as a local days number for the start of the month  | 
790  |  |  * and a day-of-month.  Used by add() and roll().  | 
791  |  |  * @param newMoon the local days of the first day of the month of the  | 
792  |  |  * start position (days after January 1, 1970 0:00 Asia/Shanghai)  | 
793  |  |  * @param dom the 1-based day-of-month of the start position  | 
794  |  |  * @param delta the number of months to move forward or backward from  | 
795  |  |  * the start position  | 
796  |  |  */  | 
797  | 0  | void ChineseCalendar::offsetMonth(int32_t newMoon, int32_t dom, int32_t delta) { | 
798  | 0  |     UErrorCode status = U_ZERO_ERROR;  | 
799  |  |  | 
800  |  |     // Move to the middle of the month before our target month.  | 
801  | 0  |     newMoon += (int32_t) (CalendarAstronomer::SYNODIC_MONTH * (delta - 0.5));  | 
802  |  |  | 
803  |  |     // Search forward to the target month's new moon  | 
804  | 0  |     newMoon = newMoonNear(newMoon, TRUE);  | 
805  |  |  | 
806  |  |     // Find the target dom  | 
807  | 0  |     int32_t jd = newMoon + kEpochStartAsJulianDay - 1 + dom;  | 
808  |  |  | 
809  |  |     // Pin the dom.  In this calendar all months are 29 or 30 days  | 
810  |  |     // so pinning just means handling dom 30.  | 
811  | 0  |     if (dom > 29) { | 
812  | 0  |         set(UCAL_JULIAN_DAY, jd-1);  | 
813  |  |         // TODO Fix this.  We really shouldn't ever have to  | 
814  |  |         // explicitly call complete().  This is either a bug in  | 
815  |  |         // this method, in ChineseCalendar, or in  | 
816  |  |         // Calendar.getActualMaximum().  I suspect the last.  | 
817  | 0  |         complete(status);  | 
818  | 0  |         if (U_FAILURE(status)) return;  | 
819  | 0  |         if (getActualMaximum(UCAL_DAY_OF_MONTH, status) >= dom) { | 
820  | 0  |             if (U_FAILURE(status)) return;  | 
821  | 0  |             set(UCAL_JULIAN_DAY, jd);  | 
822  | 0  |         }  | 
823  | 0  |     } else { | 
824  | 0  |         set(UCAL_JULIAN_DAY, jd);  | 
825  | 0  |     }  | 
826  | 0  | }  | 
827  |  |  | 
828  |  |  | 
829  |  | UBool  | 
830  |  | ChineseCalendar::inDaylightTime(UErrorCode& status) const  | 
831  | 0  | { | 
832  |  |     // copied from GregorianCalendar  | 
833  | 0  |     if (U_FAILURE(status) || !getTimeZone().useDaylightTime())   | 
834  | 0  |         return FALSE;  | 
835  |  |  | 
836  |  |     // Force an update of the state of the Calendar.  | 
837  | 0  |     ((ChineseCalendar*)this)->complete(status); // cast away const  | 
838  |  | 
  | 
839  | 0  |     return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE);  | 
840  | 0  | }  | 
841  |  |  | 
842  |  | // default century  | 
843  |  |  | 
844  |  | static UDate     gSystemDefaultCenturyStart       = DBL_MIN;  | 
845  |  | static int32_t   gSystemDefaultCenturyStartYear   = -1;  | 
846  |  | static icu::UInitOnce gSystemDefaultCenturyInitOnce = U_INITONCE_INITIALIZER;  | 
847  |  |  | 
848  |  |  | 
849  |  | UBool ChineseCalendar::haveDefaultCentury() const  | 
850  | 0  | { | 
851  | 0  |     return TRUE;  | 
852  | 0  | }  | 
853  |  |  | 
854  |  | UDate ChineseCalendar::defaultCenturyStart() const  | 
855  | 0  | { | 
856  | 0  |     return internalGetDefaultCenturyStart();  | 
857  | 0  | }  | 
858  |  |  | 
859  |  | int32_t ChineseCalendar::defaultCenturyStartYear() const  | 
860  | 0  | { | 
861  | 0  |     return internalGetDefaultCenturyStartYear();  | 
862  | 0  | }  | 
863  |  |  | 
864  |  | static void U_CALLCONV initializeSystemDefaultCentury()  | 
865  | 0  | { | 
866  |  |     // initialize systemDefaultCentury and systemDefaultCenturyYear based  | 
867  |  |     // on the current time.  They'll be set to 80 years before  | 
868  |  |     // the current time.  | 
869  | 0  |     UErrorCode status = U_ZERO_ERROR;  | 
870  | 0  |     ChineseCalendar calendar(Locale("@calendar=chinese"),status); | 
871  | 0  |     if (U_SUCCESS(status)) { | 
872  | 0  |         calendar.setTime(Calendar::getNow(), status);  | 
873  | 0  |         calendar.add(UCAL_YEAR, -80, status);  | 
874  | 0  |         gSystemDefaultCenturyStart     = calendar.getTime(status);  | 
875  | 0  |         gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);  | 
876  | 0  |     }  | 
877  |  |     // We have no recourse upon failure unless we want to propagate the failure  | 
878  |  |     // out.  | 
879  | 0  | }  | 
880  |  |  | 
881  |  | UDate  | 
882  |  | ChineseCalendar::internalGetDefaultCenturyStart() const  | 
883  | 0  | { | 
884  |  |     // lazy-evaluate systemDefaultCenturyStart  | 
885  | 0  |     umtx_initOnce(gSystemDefaultCenturyInitOnce, &initializeSystemDefaultCentury);  | 
886  | 0  |     return gSystemDefaultCenturyStart;  | 
887  | 0  | }  | 
888  |  |  | 
889  |  | int32_t  | 
890  |  | ChineseCalendar::internalGetDefaultCenturyStartYear() const  | 
891  | 0  | { | 
892  |  |     // lazy-evaluate systemDefaultCenturyStartYear  | 
893  | 0  |     umtx_initOnce(gSystemDefaultCenturyInitOnce, &initializeSystemDefaultCentury);  | 
894  | 0  |     return    gSystemDefaultCenturyStartYear;  | 
895  | 0  | }  | 
896  |  |  | 
897  |  | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ChineseCalendar)  | 
898  |  |  | 
899  |  | U_NAMESPACE_END  | 
900  |  |  | 
901  |  | #endif  | 
902  |  |  |