]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ******************************************************************************* | |
374ca955 | 3 | * Copyright (C) 1997-2004, International Business Machines Corporation and * |
b75a7d8f A |
4 | * others. All Rights Reserved. * |
5 | ******************************************************************************* | |
6 | * | |
7 | * File GREGOCAL.CPP | |
8 | * | |
9 | * Modification History: | |
10 | * | |
11 | * Date Name Description | |
12 | * 02/05/97 clhuang Creation. | |
13 | * 03/28/97 aliu Made highly questionable fix to computeFields to | |
14 | * handle DST correctly. | |
15 | * 04/22/97 aliu Cleaned up code drastically. Added monthLength(). | |
16 | * Finished unimplemented parts of computeTime() for | |
17 | * week-based date determination. Removed quetionable | |
18 | * fix and wrote correct fix for computeFields() and | |
19 | * daylight time handling. Rewrote inDaylightTime() | |
20 | * and computeFields() to handle sensitive Daylight to | |
21 | * Standard time transitions correctly. | |
22 | * 05/08/97 aliu Added code review changes. Fixed isLeapYear() to | |
23 | * not cutover. | |
24 | * 08/12/97 aliu Added equivalentTo. Misc other fixes. Updated | |
25 | * add() from Java source. | |
26 | * 07/28/98 stephen Sync up with JDK 1.2 | |
27 | * 09/14/98 stephen Changed type of kOneDay, kOneWeek to double. | |
28 | * Fixed bug in roll() | |
29 | * 10/15/99 aliu Fixed j31, incorrect WEEK_OF_YEAR computation. | |
30 | * 10/15/99 aliu Fixed j32, cannot set date to Feb 29 2000 AD. | |
31 | * {JDK bug 4210209 4209272} | |
32 | * 11/15/99 weiv Added YEAR_WOY and DOW_LOCAL computation | |
33 | * to timeToFields method, updated kMinValues, kMaxValues & kLeastMaxValues | |
34 | * 12/09/99 aliu Fixed j81, calculation errors and roll bugs | |
35 | * in year of cutover. | |
36 | * 01/24/2000 aliu Revised computeJulianDay for YEAR YEAR_WOY WOY. | |
37 | ******************************************************************************** | |
38 | */ | |
39 | ||
40 | #include "unicode/utypes.h" | |
41 | #include <float.h> | |
42 | ||
43 | #if !UCONFIG_NO_FORMATTING | |
44 | ||
45 | #include "unicode/gregocal.h" | |
374ca955 | 46 | #include "gregoimp.h" |
b75a7d8f | 47 | #include "mutex.h" |
374ca955 | 48 | #include "uassert.h" |
b75a7d8f A |
49 | |
50 | // ***************************************************************************** | |
51 | // class GregorianCalendar | |
52 | // ***************************************************************************** | |
53 | ||
b75a7d8f A |
54 | /** |
55 | * Note that the Julian date used here is not a true Julian date, since | |
56 | * it is measured from midnight, not noon. This value is the Julian | |
57 | * day number of January 1, 1970 (Gregorian calendar) at noon UTC. [LIU] | |
58 | */ | |
b75a7d8f A |
59 | |
60 | static const int32_t kNumDays[] | |
61 | = {0,31,59,90,120,151,181,212,243,273,304,334}; // 0-based, for day-in-year | |
62 | static const int32_t kLeapNumDays[] | |
63 | = {0,31,60,91,121,152,182,213,244,274,305,335}; // 0-based, for day-in-year | |
64 | static const int32_t kMonthLength[] | |
65 | = {31,28,31,30,31,30,31,31,30,31,30,31}; // 0-based | |
66 | static const int32_t kLeapMonthLength[] | |
67 | = {31,29,31,30,31,30,31,31,30,31,30,31}; // 0-based | |
68 | ||
374ca955 A |
69 | // setTimeInMillis() limits the Julian day range to +/-7F000000. |
70 | // This would seem to limit the year range to: | |
71 | // ms=+183882168921600000 jd=7f000000 December 20, 5828963 AD | |
72 | // ms=-184303902528000000 jd=81000000 September 20, 5838270 BC | |
73 | // HOWEVER, CalendarRegressionTest/Test4167060 shows that the actual | |
74 | // range limit on the year field is smaller (~ +/-140000). [alan 3.0] | |
75 | ||
76 | static const int32_t kGregorianCalendarLimits[UCAL_FIELD_COUNT][4] = { | |
77 | // Minimum Greatest Least Maximum | |
78 | // Minimum Maximum | |
79 | { 0, 0, 1, 1 }, // ERA | |
80 | { 1, 1, 140742, 144683 }, // YEAR | |
81 | { 0, 0, 11, 11 }, // MONTH | |
82 | { 1, 1, 52, 53 }, // WEEK_OF_YEAR | |
83 | { 0, 0, 4, 6 }, // WEEK_OF_MONTH | |
84 | { 1, 1, 28, 31 }, // DAY_OF_MONTH | |
85 | { 1, 1, 365, 366 }, // DAY_OF_YEAR | |
86 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1},// DAY_OF_WEEK | |
87 | { -1, -1, 4, 6 }, // DAY_OF_WEEK_IN_MONTH | |
88 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1},// AM_PM | |
89 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1},// HOUR | |
90 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1},// HOUR_OF_DAY | |
91 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1},// MINUTE | |
92 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1},// SECOND | |
93 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1},// MILLISECOND | |
94 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1},// ZONE_OFFSET | |
95 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1},// DST_OFFSET | |
96 | { -140742, -140742, 140742, 144683 }, // YEAR_WOY | |
97 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1},// DOW_LOCAL | |
98 | { -140742, -140742, 140742, 144683 }, // EXTENDED_YEAR | |
99 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1},// JULIAN_DAY | |
100 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1} // MILLISECONDS_IN_DAY | |
101 | }; | |
b75a7d8f A |
102 | |
103 | /* | |
104 | * <pre> | |
105 | * Greatest Least | |
106 | * Field name Minimum Minimum Maximum Maximum | |
107 | * ---------- ------- ------- ------- ------- | |
108 | * ERA 0 0 1 1 | |
109 | * YEAR 1 1 140742 144683 | |
110 | * MONTH 0 0 11 11 | |
111 | * WEEK_OF_YEAR 1 1 52 53 | |
112 | * WEEK_OF_MONTH 0 0 4 6 | |
113 | * DAY_OF_MONTH 1 1 28 31 | |
114 | * DAY_OF_YEAR 1 1 365 366 | |
115 | * DAY_OF_WEEK 1 1 7 7 | |
116 | * DAY_OF_WEEK_IN_MONTH -1 -1 4 6 | |
117 | * AM_PM 0 0 1 1 | |
118 | * HOUR 0 0 11 11 | |
119 | * HOUR_OF_DAY 0 0 23 23 | |
120 | * MINUTE 0 0 59 59 | |
121 | * SECOND 0 0 59 59 | |
122 | * MILLISECOND 0 0 999 999 | |
123 | * ZONE_OFFSET -12* -12* 12* 12* | |
124 | * DST_OFFSET 0 0 1* 1* | |
125 | * YEAR_WOY 1 1 140742 144683 | |
126 | * DOW_LOCAL 1 1 7 7 | |
127 | * </pre> | |
128 | * (*) In units of one-hour | |
129 | */ | |
b75a7d8f | 130 | |
374ca955 A |
131 | #if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL) |
132 | #include <stdio.h> | |
133 | #endif | |
b75a7d8f A |
134 | |
135 | U_NAMESPACE_BEGIN | |
136 | ||
374ca955 | 137 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(GregorianCalendar) |
b75a7d8f A |
138 | |
139 | // 00:00:00 UTC, October 15, 1582, expressed in ms from the epoch. | |
140 | // Note that only Italy and other Catholic countries actually | |
141 | // observed this cutover. Most other countries followed in | |
142 | // the next few centuries, some as late as 1928. [LIU] | |
143 | // in Java, -12219292800000L | |
144 | //const UDate GregorianCalendar::kPapalCutover = -12219292800000L; | |
374ca955 | 145 | static const uint32_t kCutoverJulianDay = 2299161; |
b75a7d8f | 146 | static const UDate kPapalCutover = (2299161.0 - kEpochStartAsJulianDay) * U_MILLIS_PER_DAY; |
374ca955 | 147 | static const UDate kPapalCutoverJulian = (2299161.0 - kEpochStartAsJulianDay); |
b75a7d8f A |
148 | |
149 | // ------------------------------------- | |
150 | ||
151 | GregorianCalendar::GregorianCalendar(UErrorCode& status) | |
152 | : Calendar(TimeZone::createDefault(), Locale::getDefault(), status), | |
153 | fGregorianCutover(kPapalCutover), | |
374ca955 A |
154 | fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), |
155 | fIsGregorian(TRUE), fInvertGregorian(FALSE) | |
b75a7d8f A |
156 | { |
157 | setTimeInMillis(getNow(), status); | |
158 | } | |
159 | ||
160 | // ------------------------------------- | |
161 | ||
162 | GregorianCalendar::GregorianCalendar(TimeZone* zone, UErrorCode& status) | |
163 | : Calendar(zone, Locale::getDefault(), status), | |
164 | fGregorianCutover(kPapalCutover), | |
374ca955 A |
165 | fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), |
166 | fIsGregorian(TRUE), fInvertGregorian(FALSE) | |
b75a7d8f A |
167 | { |
168 | setTimeInMillis(getNow(), status); | |
169 | } | |
170 | ||
171 | // ------------------------------------- | |
172 | ||
173 | GregorianCalendar::GregorianCalendar(const TimeZone& zone, UErrorCode& status) | |
174 | : Calendar(zone, Locale::getDefault(), status), | |
175 | fGregorianCutover(kPapalCutover), | |
374ca955 A |
176 | fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), |
177 | fIsGregorian(TRUE), fInvertGregorian(FALSE) | |
b75a7d8f A |
178 | { |
179 | setTimeInMillis(getNow(), status); | |
180 | } | |
181 | ||
182 | // ------------------------------------- | |
183 | ||
184 | GregorianCalendar::GregorianCalendar(const Locale& aLocale, UErrorCode& status) | |
185 | : Calendar(TimeZone::createDefault(), aLocale, status), | |
186 | fGregorianCutover(kPapalCutover), | |
374ca955 A |
187 | fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), |
188 | fIsGregorian(TRUE), fInvertGregorian(FALSE) | |
b75a7d8f A |
189 | { |
190 | setTimeInMillis(getNow(), status); | |
191 | } | |
192 | ||
193 | // ------------------------------------- | |
194 | ||
195 | GregorianCalendar::GregorianCalendar(TimeZone* zone, const Locale& aLocale, | |
196 | UErrorCode& status) | |
197 | : Calendar(zone, aLocale, status), | |
198 | fGregorianCutover(kPapalCutover), | |
374ca955 A |
199 | fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), |
200 | fIsGregorian(TRUE), fInvertGregorian(FALSE) | |
b75a7d8f A |
201 | { |
202 | setTimeInMillis(getNow(), status); | |
203 | } | |
204 | ||
205 | // ------------------------------------- | |
206 | ||
207 | GregorianCalendar::GregorianCalendar(const TimeZone& zone, const Locale& aLocale, | |
208 | UErrorCode& status) | |
209 | : Calendar(zone, aLocale, status), | |
210 | fGregorianCutover(kPapalCutover), | |
374ca955 A |
211 | fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), |
212 | fIsGregorian(TRUE), fInvertGregorian(FALSE) | |
b75a7d8f A |
213 | { |
214 | setTimeInMillis(getNow(), status); | |
215 | } | |
216 | ||
217 | // ------------------------------------- | |
218 | ||
219 | GregorianCalendar::GregorianCalendar(int32_t year, int32_t month, int32_t date, | |
220 | UErrorCode& status) | |
221 | : Calendar(TimeZone::createDefault(), Locale::getDefault(), status), | |
222 | fGregorianCutover(kPapalCutover), | |
374ca955 A |
223 | fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), |
224 | fIsGregorian(TRUE), fInvertGregorian(FALSE) | |
b75a7d8f A |
225 | { |
226 | set(UCAL_ERA, AD); | |
227 | set(UCAL_YEAR, year); | |
228 | set(UCAL_MONTH, month); | |
229 | set(UCAL_DATE, date); | |
230 | } | |
231 | ||
232 | // ------------------------------------- | |
233 | ||
234 | GregorianCalendar::GregorianCalendar(int32_t year, int32_t month, int32_t date, | |
235 | int32_t hour, int32_t minute, UErrorCode& status) | |
236 | : Calendar(TimeZone::createDefault(), Locale::getDefault(), status), | |
237 | fGregorianCutover(kPapalCutover), | |
374ca955 A |
238 | fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), |
239 | fIsGregorian(TRUE), fInvertGregorian(FALSE) | |
b75a7d8f A |
240 | { |
241 | set(UCAL_ERA, AD); | |
242 | set(UCAL_YEAR, year); | |
243 | set(UCAL_MONTH, month); | |
244 | set(UCAL_DATE, date); | |
245 | set(UCAL_HOUR_OF_DAY, hour); | |
246 | set(UCAL_MINUTE, minute); | |
247 | } | |
248 | ||
249 | // ------------------------------------- | |
250 | ||
251 | GregorianCalendar::GregorianCalendar(int32_t year, int32_t month, int32_t date, | |
252 | int32_t hour, int32_t minute, int32_t second, | |
253 | UErrorCode& status) | |
254 | : Calendar(TimeZone::createDefault(), Locale::getDefault(), status), | |
255 | fGregorianCutover(kPapalCutover), | |
374ca955 A |
256 | fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), |
257 | fIsGregorian(TRUE), fInvertGregorian(FALSE) | |
b75a7d8f A |
258 | { |
259 | set(UCAL_ERA, AD); | |
260 | set(UCAL_YEAR, year); | |
261 | set(UCAL_MONTH, month); | |
262 | set(UCAL_DATE, date); | |
263 | set(UCAL_HOUR_OF_DAY, hour); | |
264 | set(UCAL_MINUTE, minute); | |
265 | set(UCAL_SECOND, second); | |
266 | } | |
267 | ||
268 | // ------------------------------------- | |
269 | ||
270 | GregorianCalendar::~GregorianCalendar() | |
271 | { | |
272 | } | |
273 | ||
274 | // ------------------------------------- | |
275 | ||
276 | GregorianCalendar::GregorianCalendar(const GregorianCalendar &source) | |
277 | : Calendar(source), | |
278 | fGregorianCutover(source.fGregorianCutover), | |
374ca955 A |
279 | fCutoverJulianDay(source.fCutoverJulianDay), fNormalizedGregorianCutover(source.fNormalizedGregorianCutover), fGregorianCutoverYear(source.fGregorianCutoverYear), |
280 | fIsGregorian(source.fIsGregorian), fInvertGregorian(source.fInvertGregorian) | |
b75a7d8f A |
281 | { |
282 | } | |
283 | ||
284 | // ------------------------------------- | |
285 | ||
286 | Calendar* GregorianCalendar::clone() const | |
287 | { | |
288 | return new GregorianCalendar(*this); | |
289 | } | |
290 | ||
291 | // ------------------------------------- | |
292 | ||
293 | GregorianCalendar & | |
294 | GregorianCalendar::operator=(const GregorianCalendar &right) | |
295 | { | |
296 | if (this != &right) | |
297 | { | |
298 | Calendar::operator=(right); | |
299 | fGregorianCutover = right.fGregorianCutover; | |
300 | fNormalizedGregorianCutover = right.fNormalizedGregorianCutover; | |
301 | fGregorianCutoverYear = right.fGregorianCutoverYear; | |
374ca955 | 302 | fCutoverJulianDay = right.fCutoverJulianDay; |
b75a7d8f A |
303 | } |
304 | return *this; | |
305 | } | |
306 | ||
307 | // ------------------------------------- | |
308 | ||
309 | UBool GregorianCalendar::isEquivalentTo(const Calendar& other) const | |
310 | { | |
311 | // Calendar override. | |
312 | return Calendar::isEquivalentTo(other) && | |
313 | fGregorianCutover == ((GregorianCalendar*)&other)->fGregorianCutover; | |
314 | } | |
315 | ||
316 | // ------------------------------------- | |
317 | ||
318 | void | |
319 | GregorianCalendar::setGregorianChange(UDate date, UErrorCode& status) | |
320 | { | |
321 | if (U_FAILURE(status)) | |
322 | return; | |
323 | ||
324 | fGregorianCutover = date; | |
325 | ||
326 | // Precompute two internal variables which we use to do the actual | |
327 | // cutover computations. These are the normalized cutover, which is the | |
328 | // midnight at or before the cutover, and the cutover year. The | |
329 | // normalized cutover is in pure date milliseconds; it contains no time | |
330 | // of day or timezone component, and it used to compare against other | |
331 | // pure date values. | |
374ca955 | 332 | int32_t cutoverDay = (int32_t)Math::floorDivide(fGregorianCutover, (double)kOneDay); |
b75a7d8f A |
333 | fNormalizedGregorianCutover = cutoverDay * kOneDay; |
334 | ||
335 | // Handle the rare case of numeric overflow. If the user specifies a | |
336 | // change of UDate(Long.MIN_VALUE), in order to get a pure Gregorian | |
337 | // calendar, then the epoch day is -106751991168, which when multiplied | |
338 | // by ONE_DAY gives 9223372036794351616 -- the negative value is too | |
339 | // large for 64 bits, and overflows into a positive value. We correct | |
340 | // this by using the next day, which for all intents is semantically | |
341 | // equivalent. | |
342 | if (cutoverDay < 0 && fNormalizedGregorianCutover > 0) { | |
343 | fNormalizedGregorianCutover = (cutoverDay + 1) * kOneDay; | |
344 | } | |
345 | ||
346 | // Normalize the year so BC values are represented as 0 and negative | |
347 | // values. | |
348 | GregorianCalendar *cal = new GregorianCalendar(getTimeZone(), status); | |
349 | /* test for NULL */ | |
350 | if (cal == 0) { | |
351 | status = U_MEMORY_ALLOCATION_ERROR; | |
352 | return; | |
353 | } | |
354 | if(U_FAILURE(status)) | |
355 | return; | |
356 | cal->setTime(date, status); | |
357 | fGregorianCutoverYear = cal->get(UCAL_YEAR, status); | |
358 | if (cal->get(UCAL_ERA, status) == BC) | |
359 | fGregorianCutoverYear = 1 - fGregorianCutoverYear; | |
374ca955 | 360 | fCutoverJulianDay = cutoverDay; |
b75a7d8f A |
361 | delete cal; |
362 | } | |
363 | ||
374ca955 A |
364 | |
365 | void GregorianCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) { | |
366 | int32_t eyear, month, dayOfMonth, dayOfYear; | |
367 | ||
368 | ||
369 | if(U_FAILURE(status)) { | |
370 | return; | |
371 | } | |
372 | ||
373 | #if defined (U_DEBUG_CAL) | |
374 | fprintf(stderr, "%s:%d: jd%d- (greg's %d)- [cut=%d]\n", | |
375 | __FILE__, __LINE__, julianDay, getGregorianDayOfYear(), fCutoverJulianDay); | |
376 | #endif | |
377 | ||
378 | ||
379 | if (julianDay >= fCutoverJulianDay) { | |
380 | month = getGregorianMonth(); | |
381 | dayOfMonth = getGregorianDayOfMonth(); | |
382 | dayOfYear = getGregorianDayOfYear(); | |
383 | eyear = getGregorianYear(); | |
384 | } else { | |
385 | // The Julian epoch day (not the same as Julian Day) | |
386 | // is zero on Saturday December 30, 0 (Gregorian). | |
387 | int32_t julianEpochDay = julianDay - (kJan1_1JulianDay - 2); | |
388 | eyear = (int32_t) Math::floorDivide(4*julianEpochDay + 1464, 1461); | |
389 | ||
390 | // Compute the Julian calendar day number for January 1, eyear | |
391 | int32_t january1 = 365*(eyear-1) + Math::floorDivide(eyear-1, (int32_t)4); | |
392 | dayOfYear = (julianEpochDay - january1); // 0-based | |
393 | ||
394 | // Julian leap years occurred historically every 4 years starting | |
395 | // with 8 AD. Before 8 AD the spacing is irregular; every 3 years | |
396 | // from 45 BC to 9 BC, and then none until 8 AD. However, we don't | |
397 | // implement this historical detail; instead, we implement the | |
398 | // computatinally cleaner proleptic calendar, which assumes | |
399 | // consistent 4-year cycles throughout time. | |
400 | UBool isLeap = ((eyear&0x3) == 0); // equiv. to (eyear%4 == 0) | |
401 | ||
402 | // Common Julian/Gregorian calculation | |
403 | int32_t correction = 0; | |
404 | int32_t march1 = isLeap ? 60 : 59; // zero-based DOY for March 1 | |
405 | if (dayOfYear >= march1) { | |
406 | correction = isLeap ? 1 : 2; | |
407 | } | |
408 | month = (12 * (dayOfYear + correction) + 6) / 367; // zero-based month | |
409 | dayOfMonth = dayOfYear - (isLeap?kLeapNumDays[month]:kNumDays[month]) + 1; // one-based DOM | |
410 | ++dayOfYear; | |
411 | #if defined (U_DEBUG_CAL) | |
412 | // fprintf(stderr, "%d - %d[%d] + 1\n", dayOfYear, isLeap?kLeapNumDays[month]:kNumDays[month], month ); | |
413 | // fprintf(stderr, "%s:%d: greg's HCF %d -> %d/%d/%d not %d/%d/%d\n", | |
414 | // __FILE__, __LINE__,julianDay, | |
415 | // eyear,month,dayOfMonth, | |
416 | // getGregorianYear(), getGregorianMonth(), getGregorianDayOfMonth() ); | |
417 | fprintf(stderr, "%s:%d: doy %d (greg's %d)- [cut=%d]\n", | |
418 | __FILE__, __LINE__, dayOfYear, getGregorianDayOfYear(), fCutoverJulianDay); | |
419 | #endif | |
420 | ||
421 | } | |
422 | ||
423 | // [j81] if we are after the cutover in its year, shift the day of the year | |
424 | if((eyear == fGregorianCutoverYear) && (julianDay >= fCutoverJulianDay)) { | |
425 | //from handleComputeMonthStart | |
426 | int32_t gregShift = Grego::gregorianShift(eyear); | |
427 | #if defined (U_DEBUG_CAL) | |
428 | fprintf(stderr, "%s:%d: gregorian shift %d ::: doy%d => %d [cut=%d]\n", | |
429 | __FILE__, __LINE__,gregShift, dayOfYear, dayOfYear+gregShift, fCutoverJulianDay); | |
430 | #endif | |
431 | dayOfYear += gregShift; | |
432 | } | |
433 | ||
434 | internalSet(UCAL_MONTH, month); | |
435 | internalSet(UCAL_DAY_OF_MONTH, dayOfMonth); | |
436 | internalSet(UCAL_DAY_OF_YEAR, dayOfYear); | |
437 | internalSet(UCAL_EXTENDED_YEAR, eyear); | |
438 | int32_t era = AD; | |
439 | if (eyear < 1) { | |
440 | era = BC; | |
441 | eyear = 1 - eyear; | |
442 | } | |
443 | internalSet(UCAL_ERA, era); | |
444 | internalSet(UCAL_YEAR, eyear); | |
445 | } | |
446 | ||
447 | ||
b75a7d8f A |
448 | // ------------------------------------- |
449 | ||
450 | UDate | |
451 | GregorianCalendar::getGregorianChange() const | |
452 | { | |
453 | return fGregorianCutover; | |
454 | } | |
455 | ||
456 | // ------------------------------------- | |
457 | ||
458 | UBool | |
459 | GregorianCalendar::isLeapYear(int32_t year) const | |
460 | { | |
374ca955 A |
461 | // MSVC complains bitterly if we try to use Grego::isLeapYear here |
462 | // NOTE: year&0x3 == year%4 | |
b75a7d8f | 463 | return (year >= fGregorianCutoverYear ? |
374ca955 A |
464 | (((year&0x3) == 0) && ((year%100 != 0) || (year%400 == 0))) : // Gregorian |
465 | ((year&0x3) == 0)); // Julian | |
b75a7d8f A |
466 | } |
467 | ||
b75a7d8f A |
468 | // ------------------------------------- |
469 | ||
374ca955 | 470 | int32_t GregorianCalendar::handleComputeJulianDay(UCalendarDateFields bestField) |
b75a7d8f | 471 | { |
374ca955 | 472 | fInvertGregorian = FALSE; |
b75a7d8f | 473 | |
374ca955 A |
474 | int32_t jd = Calendar::handleComputeJulianDay(bestField); |
475 | ||
476 | if((bestField == UCAL_WEEK_OF_YEAR) && // if we are doing WOY calculations, we are counting relative to Jan 1 *julian* | |
477 | (internalGet(UCAL_EXTENDED_YEAR)==fGregorianCutoverYear) && | |
478 | jd >= fCutoverJulianDay) { | |
479 | fInvertGregorian = TRUE; // So that the Julian Jan 1 will be used in handleComputeMonthStart | |
480 | return Calendar::handleComputeJulianDay(bestField); | |
481 | } | |
b75a7d8f | 482 | |
b75a7d8f | 483 | |
374ca955 A |
484 | // The following check handles portions of the cutover year BEFORE the |
485 | // cutover itself happens. | |
486 | //if ((fIsGregorian==TRUE) != (jd >= fCutoverJulianDay)) { /* cutoverJulianDay)) { */ | |
487 | if ((fIsGregorian==TRUE) != (jd >= fCutoverJulianDay)) { /* cutoverJulianDay)) { */ | |
488 | #if defined (U_DEBUG_CAL) | |
489 | fprintf(stderr, "%s:%d: jd [invert] %d\n", | |
490 | __FILE__, __LINE__, jd); | |
491 | #endif | |
492 | fInvertGregorian = TRUE; | |
493 | jd = Calendar::handleComputeJulianDay(bestField); | |
494 | #if defined (U_DEBUG_CAL) | |
495 | fprintf(stderr, "%s:%d: fIsGregorian %s, fInvertGregorian %s - ", | |
496 | __FILE__, __LINE__,fIsGregorian?"T":"F", fInvertGregorian?"T":"F"); | |
497 | fprintf(stderr, " jd NOW %d\n", | |
498 | jd); | |
499 | #endif | |
500 | } else { | |
501 | #if defined (U_DEBUG_CAL) | |
502 | fprintf(stderr, "%s:%d: jd [==] %d - %sfIsGregorian %sfInvertGregorian, %d\n", | |
503 | __FILE__, __LINE__, jd, fIsGregorian?"T":"F", fInvertGregorian?"T":"F", bestField); | |
504 | #endif | |
505 | } | |
506 | ||
507 | if(fIsGregorian && (internalGet(UCAL_EXTENDED_YEAR) == fGregorianCutoverYear)) { | |
508 | int32_t gregShift = Grego::gregorianShift(internalGet(UCAL_EXTENDED_YEAR)); | |
509 | if (bestField == UCAL_DAY_OF_YEAR) { | |
510 | #if defined (U_DEBUG_CAL) | |
511 | fprintf(stderr, "%s:%d: [DOY%d] gregorian shift of JD %d += %d\n", | |
512 | __FILE__, __LINE__, fFields[bestField],jd, gregShift); | |
513 | #endif | |
514 | jd -= gregShift; | |
515 | } else if ( bestField == UCAL_WEEK_OF_MONTH ) { | |
516 | int32_t weekShift = 14; | |
517 | #if defined (U_DEBUG_CAL) | |
518 | fprintf(stderr, "%s:%d: [WOY/WOM] gregorian week shift of %d += %d\n", | |
519 | __FILE__, __LINE__, jd, weekShift); | |
520 | #endif | |
521 | jd += weekShift; // shift by weeks for week based fields. | |
b75a7d8f | 522 | } |
374ca955 A |
523 | } |
524 | ||
525 | return jd; | |
526 | } | |
b75a7d8f | 527 | |
374ca955 | 528 | int32_t GregorianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, |
b75a7d8f | 529 | |
374ca955 A |
530 | UBool /* useMonth */) const |
531 | { | |
532 | GregorianCalendar *nonConstThis = (GregorianCalendar*)this; // cast away const | |
b75a7d8f | 533 | |
374ca955 A |
534 | // If the month is out of range, adjust it into range, and |
535 | // modify the extended year value accordingly. | |
536 | if (month < 0 || month > 11) { | |
537 | eyear += Math::floorDivide(month, 12, month); | |
538 | } | |
b75a7d8f | 539 | |
374ca955 A |
540 | UBool isLeap = eyear%4 == 0; |
541 | int32_t y = eyear-1; | |
542 | int32_t julianDay = 365*y + Math::floorDivide(y, 4) + (kJan1_1JulianDay - 3); | |
543 | ||
544 | nonConstThis->fIsGregorian = (eyear >= fGregorianCutoverYear); | |
545 | #if defined (U_DEBUG_CAL) | |
546 | fprintf(stderr, "%s:%d: (hcms%d/%d) fIsGregorian %s, fInvertGregorian %s\n", | |
547 | __FILE__, __LINE__, eyear,month, fIsGregorian?"T":"F", fInvertGregorian?"T":"F"); | |
548 | #endif | |
549 | if (fInvertGregorian) { | |
550 | nonConstThis->fIsGregorian = !fIsGregorian; | |
b75a7d8f | 551 | } |
374ca955 A |
552 | if (fIsGregorian) { |
553 | isLeap = isLeap && ((eyear%100 != 0) || (eyear%400 == 0)); | |
554 | // Add 2 because Gregorian calendar starts 2 days after | |
555 | // Julian calendar | |
556 | int32_t gregShift = Grego::gregorianShift(eyear); | |
557 | #if defined (U_DEBUG_CAL) | |
558 | fprintf(stderr, "%s:%d: (hcms%d/%d) gregorian shift of %d += %d\n", | |
559 | __FILE__, __LINE__, eyear, month, julianDay, gregShift); | |
560 | #endif | |
561 | julianDay += gregShift; | |
b75a7d8f A |
562 | } |
563 | ||
374ca955 A |
564 | // At this point julianDay indicates the day BEFORE the first |
565 | // day of January 1, <eyear> of either the Julian or Gregorian | |
566 | // calendar. | |
b75a7d8f | 567 | |
374ca955 A |
568 | if (month != 0) { |
569 | julianDay += isLeap?kLeapNumDays[month]:kNumDays[month]; | |
570 | } | |
b75a7d8f | 571 | |
374ca955 | 572 | return julianDay; |
b75a7d8f A |
573 | } |
574 | ||
374ca955 | 575 | int32_t GregorianCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const |
b75a7d8f | 576 | { |
374ca955 A |
577 | return isLeapYear(extendedYear) ? kLeapMonthLength[month] : kMonthLength[month]; |
578 | } | |
579 | ||
580 | int32_t GregorianCalendar::handleGetYearLength(int32_t eyear) const { | |
581 | return isLeapYear(eyear) ? 366 : 365; | |
b75a7d8f A |
582 | } |
583 | ||
b75a7d8f A |
584 | |
585 | int32_t | |
586 | GregorianCalendar::monthLength(int32_t month) const | |
587 | { | |
374ca955 A |
588 | int32_t year = internalGet(UCAL_EXTENDED_YEAR); |
589 | return handleGetMonthLength(year, month); | |
b75a7d8f A |
590 | } |
591 | ||
592 | // ------------------------------------- | |
593 | ||
594 | int32_t | |
595 | GregorianCalendar::monthLength(int32_t month, int32_t year) const | |
596 | { | |
597 | return isLeapYear(year) ? kLeapMonthLength[month] : kMonthLength[month]; | |
598 | } | |
599 | ||
600 | // ------------------------------------- | |
601 | ||
602 | int32_t | |
603 | GregorianCalendar::yearLength(int32_t year) const | |
604 | { | |
605 | return isLeapYear(year) ? 366 : 365; | |
606 | } | |
607 | ||
608 | // ------------------------------------- | |
609 | ||
610 | int32_t | |
611 | GregorianCalendar::yearLength() const | |
612 | { | |
613 | return isLeapYear(internalGet(UCAL_YEAR)) ? 366 : 365; | |
614 | } | |
615 | ||
616 | // ------------------------------------- | |
617 | ||
b75a7d8f A |
618 | /** |
619 | * After adjustments such as add(MONTH), add(YEAR), we don't want the | |
620 | * month to jump around. E.g., we don't want Jan 31 + 1 month to go to Mar | |
621 | * 3, we want it to go to Feb 28. Adjustments which might run into this | |
622 | * problem call this method to retain the proper month. | |
623 | */ | |
624 | void | |
625 | GregorianCalendar::pinDayOfMonth() | |
626 | { | |
627 | int32_t monthLen = monthLength(internalGet(UCAL_MONTH)); | |
628 | int32_t dom = internalGet(UCAL_DATE); | |
629 | if(dom > monthLen) | |
630 | set(UCAL_DATE, monthLen); | |
631 | } | |
632 | ||
633 | // ------------------------------------- | |
634 | ||
635 | ||
636 | UBool | |
637 | GregorianCalendar::validateFields() const | |
638 | { | |
639 | for (int32_t field = 0; field < UCAL_FIELD_COUNT; field++) { | |
640 | // Ignore DATE and DAY_OF_YEAR which are handled below | |
641 | if (field != UCAL_DATE && | |
642 | field != UCAL_DAY_OF_YEAR && | |
643 | isSet((UCalendarDateFields)field) && | |
644 | ! boundsCheck(internalGet((UCalendarDateFields)field), (UCalendarDateFields)field)) | |
645 | return FALSE; | |
646 | } | |
647 | ||
648 | // Values differ in Least-Maximum and Maximum should be handled | |
649 | // specially. | |
650 | if (isSet(UCAL_DATE)) { | |
651 | int32_t date = internalGet(UCAL_DATE); | |
652 | if (date < getMinimum(UCAL_DATE) || | |
653 | date > monthLength(internalGet(UCAL_MONTH))) { | |
654 | return FALSE; | |
655 | } | |
656 | } | |
657 | ||
658 | if (isSet(UCAL_DAY_OF_YEAR)) { | |
659 | int32_t days = internalGet(UCAL_DAY_OF_YEAR); | |
660 | if (days < 1 || days > yearLength()) { | |
661 | return FALSE; | |
662 | } | |
663 | } | |
664 | ||
665 | // Handle DAY_OF_WEEK_IN_MONTH, which must not have the value zero. | |
666 | // We've checked against minimum and maximum above already. | |
667 | if (isSet(UCAL_DAY_OF_WEEK_IN_MONTH) && | |
668 | 0 == internalGet(UCAL_DAY_OF_WEEK_IN_MONTH)) { | |
669 | return FALSE; | |
670 | } | |
671 | ||
672 | return TRUE; | |
673 | } | |
674 | ||
675 | // ------------------------------------- | |
676 | ||
677 | UBool | |
678 | GregorianCalendar::boundsCheck(int32_t value, UCalendarDateFields field) const | |
679 | { | |
680 | return value >= getMinimum(field) && value <= getMaximum(field); | |
681 | } | |
682 | ||
683 | // ------------------------------------- | |
684 | ||
685 | UDate | |
686 | GregorianCalendar::getEpochDay(UErrorCode& status) | |
687 | { | |
688 | complete(status); | |
689 | // Divide by 1000 (convert to seconds) in order to prevent overflow when | |
690 | // dealing with UDate(Long.MIN_VALUE) and UDate(Long.MAX_VALUE). | |
691 | double wallSec = internalGetTime()/1000 + (internalGet(UCAL_ZONE_OFFSET) + internalGet(UCAL_DST_OFFSET))/1000; | |
692 | ||
374ca955 | 693 | return Math::floorDivide(wallSec, kOneDay/1000.0); |
b75a7d8f A |
694 | } |
695 | ||
696 | // ------------------------------------- | |
697 | ||
b75a7d8f A |
698 | |
699 | // ------------------------------------- | |
700 | ||
701 | /** | |
702 | * Compute the julian day number of the day BEFORE the first day of | |
703 | * January 1, year 1 of the given calendar. If julianDay == 0, it | |
704 | * specifies (Jan. 1, 1) - 1, in whatever calendar we are using (Julian | |
705 | * or Gregorian). | |
706 | */ | |
707 | double GregorianCalendar::computeJulianDayOfYear(UBool isGregorian, | |
708 | int32_t year, UBool& isLeap) { | |
709 | isLeap = year%4 == 0; | |
710 | int32_t y = year - 1; | |
374ca955 | 711 | double julianDay = 365.0*y + Math::floorDivide(y, 4) + (kJan1_1JulianDay - 3); |
b75a7d8f A |
712 | |
713 | if (isGregorian) { | |
714 | isLeap = isLeap && ((year%100 != 0) || (year%400 == 0)); | |
715 | // Add 2 because Gregorian calendar starts 2 days after Julian calendar | |
374ca955 | 716 | julianDay += Grego::gregorianShift(year); |
b75a7d8f A |
717 | } |
718 | ||
719 | return julianDay; | |
720 | } | |
721 | ||
374ca955 A |
722 | // /** |
723 | // * Compute the day of week, relative to the first day of week, from | |
724 | // * 0..6, of the current DOW_LOCAL or DAY_OF_WEEK fields. This is | |
725 | // * equivalent to get(DOW_LOCAL) - 1. | |
726 | // */ | |
727 | // int32_t GregorianCalendar::computeRelativeDOW() const { | |
728 | // int32_t relDow = 0; | |
729 | // if (fStamp[UCAL_DOW_LOCAL] > fStamp[UCAL_DAY_OF_WEEK]) { | |
730 | // relDow = internalGet(UCAL_DOW_LOCAL) - 1; // 1-based | |
731 | // } else if (fStamp[UCAL_DAY_OF_WEEK] != kUnset) { | |
732 | // relDow = internalGet(UCAL_DAY_OF_WEEK) - getFirstDayOfWeek(); | |
733 | // if (relDow < 0) relDow += 7; | |
734 | // } | |
735 | // return relDow; | |
736 | // } | |
737 | ||
738 | // /** | |
739 | // * Compute the day of week, relative to the first day of week, | |
740 | // * from 0..6 of the given julian day. | |
741 | // */ | |
742 | // int32_t GregorianCalendar::computeRelativeDOW(double julianDay) const { | |
743 | // int32_t relDow = julianDayToDayOfWeek(julianDay) - getFirstDayOfWeek(); | |
744 | // if (relDow < 0) { | |
745 | // relDow += 7; | |
746 | // } | |
747 | // return relDow; | |
748 | // } | |
749 | ||
750 | // /** | |
751 | // * Compute the DOY using the WEEK_OF_YEAR field and the julian day | |
752 | // * of the day BEFORE January 1 of a year (a return value from | |
753 | // * computeJulianDayOfYear). | |
754 | // */ | |
755 | // int32_t GregorianCalendar::computeDOYfromWOY(double julianDayOfYear) const { | |
756 | // // Compute DOY from day of week plus week of year | |
757 | ||
758 | // // Find the day of the week for the first of this year. This | |
759 | // // is zero-based, with 0 being the locale-specific first day of | |
760 | // // the week. Add 1 to get first day of year. | |
761 | // int32_t fdy = computeRelativeDOW(julianDayOfYear + 1); | |
762 | ||
763 | // return | |
764 | // // Compute doy of first (relative) DOW of WOY 1 | |
765 | // (((7 - fdy) < getMinimalDaysInFirstWeek()) | |
766 | // ? (8 - fdy) : (1 - fdy)) | |
b75a7d8f | 767 | |
374ca955 A |
768 | // // Adjust for the week number. |
769 | // + (7 * (internalGet(UCAL_WEEK_OF_YEAR) - 1)) | |
b75a7d8f | 770 | |
374ca955 A |
771 | // // Adjust for the DOW |
772 | // + computeRelativeDOW(); | |
773 | // } | |
b75a7d8f A |
774 | |
775 | // ------------------------------------- | |
776 | ||
777 | double | |
778 | GregorianCalendar::millisToJulianDay(UDate millis) | |
779 | { | |
374ca955 | 780 | return (double)kEpochStartAsJulianDay + Math::floorDivide(millis, (double)kOneDay); |
b75a7d8f A |
781 | } |
782 | ||
783 | // ------------------------------------- | |
784 | ||
785 | UDate | |
786 | GregorianCalendar::julianDayToMillis(double julian) | |
787 | { | |
788 | return (UDate) ((julian - kEpochStartAsJulianDay) * (double) kOneDay); | |
789 | } | |
790 | ||
b75a7d8f A |
791 | // ------------------------------------- |
792 | ||
793 | int32_t | |
794 | GregorianCalendar::aggregateStamp(int32_t stamp_a, int32_t stamp_b) | |
795 | { | |
796 | return (((stamp_a != kUnset && stamp_b != kUnset) | |
797 | ? uprv_max(stamp_a, stamp_b) | |
374ca955 | 798 | : (int32_t)kUnset)); |
b75a7d8f A |
799 | } |
800 | ||
801 | // ------------------------------------- | |
802 | ||
803 | /** | |
804 | * Roll a field by a signed amount. | |
805 | * Note: This will be made public later. [LIU] | |
806 | */ | |
807 | ||
808 | void | |
809 | GregorianCalendar::roll(EDateFields field, int32_t amount, UErrorCode& status) { | |
810 | roll((UCalendarDateFields) field, amount, status); | |
811 | } | |
812 | ||
813 | void | |
814 | GregorianCalendar::roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) | |
815 | { | |
374ca955 A |
816 | if((amount == 0) || U_FAILURE(status)) { |
817 | return; | |
818 | } | |
b75a7d8f | 819 | |
374ca955 A |
820 | // J81 processing. (gregorian cutover) |
821 | UBool inCutoverMonth = FALSE; | |
822 | int32_t cMonthLen=0; // 'c' for cutover; in days | |
823 | int32_t cDayOfMonth=0; // no discontinuity: [0, cMonthLen) | |
824 | double cMonthStart=0.0; // in ms | |
b75a7d8f | 825 | |
374ca955 A |
826 | // Common code - see if we're in the cutover month of the cutover year |
827 | if(get(UCAL_EXTENDED_YEAR, status) == fGregorianCutoverYear) { | |
828 | switch (field) { | |
829 | case UCAL_DAY_OF_MONTH: | |
830 | case UCAL_WEEK_OF_MONTH: | |
831 | { | |
832 | int32_t max = monthLength(internalGet(UCAL_MONTH)); | |
833 | UDate t = internalGetTime(); | |
b75a7d8f | 834 | // We subtract 1 from the DAY_OF_MONTH to make it zero-based, and an |
374ca955 | 835 | // additional 10 if we are after the cutover. Thus the monthStart |
b75a7d8f | 836 | // value will be correct iff we actually are in the cutover month. |
374ca955 | 837 | cDayOfMonth = internalGet(UCAL_DAY_OF_MONTH) - ((t >= fGregorianCutover) ? 10 : 0); |
b75a7d8f | 838 | cMonthStart = t - ((cDayOfMonth - 1) * kOneDay); |
b75a7d8f A |
839 | // A month containing the cutover is 10 days shorter. |
840 | if ((cMonthStart < fGregorianCutover) && | |
841 | (cMonthStart + (cMonthLen=(max-10))*kOneDay >= fGregorianCutover)) { | |
374ca955 | 842 | inCutoverMonth = TRUE; |
b75a7d8f | 843 | } |
374ca955 | 844 | } |
b75a7d8f | 845 | default: |
374ca955 | 846 | ; |
b75a7d8f | 847 | } |
374ca955 | 848 | } |
b75a7d8f | 849 | |
374ca955 A |
850 | switch (field) { |
851 | case UCAL_WEEK_OF_YEAR: { | |
852 | // Unlike WEEK_OF_MONTH, WEEK_OF_YEAR never shifts the day of the | |
853 | // week. Also, rolling the week of the year can have seemingly | |
854 | // strange effects simply because the year of the week of year | |
855 | // may be different from the calendar year. For example, the | |
856 | // date Dec 28, 1997 is the first day of week 1 of 1998 (if | |
857 | // weeks start on Sunday and the minimal days in first week is | |
858 | // <= 3). | |
859 | int32_t woy = get(UCAL_WEEK_OF_YEAR, status); | |
860 | // Get the ISO year, which matches the week of year. This | |
861 | // may be one year before or after the calendar year. | |
862 | int32_t isoYear = get(UCAL_YEAR_WOY, status); | |
863 | int32_t isoDoy = internalGet(UCAL_DAY_OF_YEAR); | |
864 | if (internalGet(UCAL_MONTH) == UCAL_JANUARY) { | |
865 | if (woy >= 52) { | |
866 | isoDoy += handleGetYearLength(isoYear); | |
867 | } | |
868 | } else { | |
869 | if (woy == 1) { | |
870 | isoDoy -= handleGetYearLength(isoYear - 1); | |
871 | } | |
872 | } | |
873 | woy += amount; | |
874 | // Do fast checks to avoid unnecessary computation: | |
875 | if (woy < 1 || woy > 52) { | |
876 | // Determine the last week of the ISO year. | |
877 | // We do this using the standard formula we use | |
878 | // everywhere in this file. If we can see that the | |
879 | // days at the end of the year are going to fall into | |
880 | // week 1 of the next year, we drop the last week by | |
881 | // subtracting 7 from the last day of the year. | |
882 | int32_t lastDoy = handleGetYearLength(isoYear); | |
883 | int32_t lastRelDow = (lastDoy - isoDoy + internalGet(UCAL_DAY_OF_WEEK) - | |
884 | getFirstDayOfWeek()) % 7; | |
885 | if (lastRelDow < 0) lastRelDow += 7; | |
886 | if ((6 - lastRelDow) >= getMinimalDaysInFirstWeek()) lastDoy -= 7; | |
887 | int32_t lastWoy = weekNumber(lastDoy, lastRelDow + 1); | |
888 | woy = ((woy + lastWoy - 1) % lastWoy) + 1; | |
889 | } | |
890 | set(UCAL_WEEK_OF_YEAR, woy); | |
891 | set(UCAL_YEAR_WOY,isoYear); | |
892 | return; | |
893 | } | |
b75a7d8f | 894 | |
374ca955 A |
895 | case UCAL_DAY_OF_MONTH: |
896 | if( !inCutoverMonth ) { | |
897 | Calendar::roll(field, amount, status); | |
898 | return; | |
899 | } else { | |
900 | // [j81] 1582 special case for DOM | |
901 | // The default computation works except when the current month | |
902 | // contains the Gregorian cutover. We handle this special case | |
903 | // here. [j81 - aliu] | |
904 | double monthLen = cMonthLen * kOneDay; | |
905 | double msIntoMonth = uprv_fmod(internalGetTime() - cMonthStart + | |
906 | amount * kOneDay, monthLen); | |
907 | if (msIntoMonth < 0) { | |
908 | msIntoMonth += monthLen; | |
909 | } | |
910 | #if defined (U_DEBUG_CAL) | |
911 | fprintf(stderr, "%s:%d: roll DOM %d -> %.0lf ms \n", | |
912 | __FILE__, __LINE__,amount, cMonthLen, cMonthStart+msIntoMonth); | |
913 | #endif | |
914 | setTimeInMillis(cMonthStart + msIntoMonth, status); | |
915 | return; | |
916 | } | |
b75a7d8f | 917 | |
374ca955 A |
918 | case UCAL_WEEK_OF_MONTH: |
919 | if( !inCutoverMonth ) { | |
920 | Calendar::roll(field, amount, status); | |
921 | return; | |
922 | } else { | |
923 | #if defined (U_DEBUG_CAL) | |
924 | fprintf(stderr, "%s:%d: roll WOM %d ??????????????????? \n", | |
925 | __FILE__, __LINE__,amount); | |
926 | #endif | |
927 | // NOTE: following copied from the old | |
928 | // GregorianCalendar::roll( WEEK_OF_MONTH ) code | |
929 | ||
930 | // This is tricky, because during the roll we may have to shift | |
931 | // to a different day of the week. For example: | |
932 | ||
933 | // s m t w r f s | |
934 | // 1 2 3 4 5 | |
935 | // 6 7 8 9 10 11 12 | |
936 | ||
937 | // When rolling from the 6th or 7th back one week, we go to the | |
938 | // 1st (assuming that the first partial week counts). The same | |
939 | // thing happens at the end of the month. | |
940 | ||
941 | // The other tricky thing is that we have to figure out whether | |
942 | // the first partial week actually counts or not, based on the | |
943 | // minimal first days in the week. And we have to use the | |
944 | // correct first day of the week to delineate the week | |
945 | // boundaries. | |
946 | ||
947 | // Here's our algorithm. First, we find the real boundaries of | |
948 | // the month. Then we discard the first partial week if it | |
949 | // doesn't count in this locale. Then we fill in the ends with | |
950 | // phantom days, so that the first partial week and the last | |
951 | // partial week are full weeks. We then have a nice square | |
952 | // block of weeks. We do the usual rolling within this block, | |
953 | // as is done elsewhere in this method. If we wind up on one of | |
954 | // the phantom days that we added, we recognize this and pin to | |
955 | // the first or the last day of the month. Easy, eh? | |
956 | ||
957 | // Another wrinkle: To fix jitterbug 81, we have to make all this | |
958 | // work in the oddball month containing the Gregorian cutover. | |
959 | // This month is 10 days shorter than usual, and also contains | |
960 | // a discontinuity in the days; e.g., the default cutover month | |
961 | // is Oct 1582, and goes from day of month 4 to day of month 15. | |
962 | ||
963 | // Normalize the DAY_OF_WEEK so that 0 is the first day of the week | |
964 | // in this locale. We have dow in 0..6. | |
965 | int32_t dow = internalGet(UCAL_DAY_OF_WEEK) - getFirstDayOfWeek(); | |
966 | if (dow < 0) | |
967 | dow += 7; | |
968 | ||
969 | // Find the day of month, compensating for cutover discontinuity. | |
970 | int32_t dom = cDayOfMonth; | |
971 | ||
972 | // Find the day of the week (normalized for locale) for the first | |
973 | // of the month. | |
974 | int32_t fdm = (dow - dom + 1) % 7; | |
975 | if (fdm < 0) | |
976 | fdm += 7; | |
977 | ||
978 | // Get the first day of the first full week of the month, | |
979 | // including phantom days, if any. Figure out if the first week | |
980 | // counts or not; if it counts, then fill in phantom days. If | |
981 | // not, advance to the first real full week (skip the partial week). | |
982 | int32_t start; | |
983 | if ((7 - fdm) < getMinimalDaysInFirstWeek()) | |
984 | start = 8 - fdm; // Skip the first partial week | |
985 | else | |
986 | start = 1 - fdm; // This may be zero or negative | |
987 | ||
988 | // Get the day of the week (normalized for locale) for the last | |
989 | // day of the month. | |
990 | int32_t monthLen = cMonthLen; | |
991 | int32_t ldm = (monthLen - dom + dow) % 7; | |
992 | // We know monthLen >= DAY_OF_MONTH so we skip the += 7 step here. | |
993 | ||
994 | // Get the limit day for the blocked-off rectangular month; that | |
995 | // is, the day which is one past the last day of the month, | |
996 | // after the month has already been filled in with phantom days | |
997 | // to fill out the last week. This day has a normalized DOW of 0. | |
998 | int32_t limit = monthLen + 7 - ldm; | |
999 | ||
1000 | // Now roll between start and (limit - 1). | |
1001 | int32_t gap = limit - start; | |
1002 | int32_t newDom = (dom + amount*7 - start) % gap; | |
1003 | if (newDom < 0) | |
1004 | newDom += gap; | |
1005 | newDom += start; | |
1006 | ||
1007 | // Finally, pin to the real start and end of the month. | |
1008 | if (newDom < 1) | |
1009 | newDom = 1; | |
1010 | if (newDom > monthLen) | |
1011 | newDom = monthLen; | |
1012 | ||
1013 | // Set the DAY_OF_MONTH. We rely on the fact that this field | |
1014 | // takes precedence over everything else (since all other fields | |
1015 | // are also set at this point). If this fact changes (if the | |
1016 | // disambiguation algorithm changes) then we will have to unset | |
1017 | // the appropriate fields here so that DAY_OF_MONTH is attended | |
1018 | // to. | |
1019 | ||
1020 | // If we are in the cutover month, manipulate ms directly. Don't do | |
1021 | // this in general because it doesn't work across DST boundaries | |
1022 | // (details, details). This takes care of the discontinuity. | |
1023 | setTimeInMillis(cMonthStart + (newDom-1)*kOneDay, status); | |
1024 | return; | |
1025 | } | |
1026 | ||
1027 | default: | |
1028 | Calendar::roll(field, amount, status); | |
1029 | return; | |
1030 | } | |
b75a7d8f A |
1031 | } |
1032 | ||
1033 | // ------------------------------------- | |
b75a7d8f | 1034 | |
b75a7d8f | 1035 | |
374ca955 A |
1036 | /** |
1037 | * Return the minimum value that this field could have, given the current date. | |
1038 | * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum(). | |
1039 | * @param field the time field. | |
1040 | * @return the minimum value that this field could have, given the current date. | |
1041 | * @deprecated ICU 2.6. Use getActualMinimum(UCalendarDateFields field) instead. | |
1042 | */ | |
1043 | int32_t GregorianCalendar::getActualMinimum(EDateFields field) const | |
b75a7d8f | 1044 | { |
374ca955 | 1045 | return getMinimum((UCalendarDateFields)field); |
b75a7d8f A |
1046 | } |
1047 | ||
374ca955 | 1048 | int32_t GregorianCalendar::getActualMinimum(EDateFields field, UErrorCode& /* status */) const |
b75a7d8f | 1049 | { |
374ca955 | 1050 | return getMinimum((UCalendarDateFields)field); |
b75a7d8f A |
1051 | } |
1052 | ||
374ca955 A |
1053 | /** |
1054 | * Return the minimum value that this field could have, given the current date. | |
1055 | * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum(). | |
1056 | * @param field the time field. | |
1057 | * @return the minimum value that this field could have, given the current date. | |
1058 | * @draft ICU 2.6. | |
1059 | */ | |
1060 | int32_t GregorianCalendar::getActualMinimum(UCalendarDateFields field, UErrorCode& /* status */) const | |
b75a7d8f | 1061 | { |
374ca955 | 1062 | return getMinimum(field); |
b75a7d8f A |
1063 | } |
1064 | ||
b75a7d8f | 1065 | |
374ca955 | 1066 | // ------------------------------------ |
b75a7d8f | 1067 | |
374ca955 A |
1068 | /** |
1069 | * Old year limits were least max 292269054, max 292278994. | |
1070 | */ | |
b75a7d8f | 1071 | |
374ca955 A |
1072 | /** |
1073 | * @stable ICU 2.0 | |
1074 | */ | |
1075 | int32_t GregorianCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const { | |
1076 | return kGregorianCalendarLimits[field][limitType]; | |
b75a7d8f A |
1077 | } |
1078 | ||
374ca955 A |
1079 | /** |
1080 | * Return the maximum value that this field could have, given the current date. | |
1081 | * For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual | |
1082 | * maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for a Hebrew calendar, | |
1083 | * for some years the actual maximum for MONTH is 12, and for others 13. | |
1084 | * @stable ICU 2.0 | |
1085 | */ | |
1086 | int32_t GregorianCalendar::getActualMaximum(UCalendarDateFields field, UErrorCode& status) const | |
b75a7d8f A |
1087 | { |
1088 | /* It is a known limitation that the code here (and in getActualMinimum) | |
374ca955 A |
1089 | * won't behave properly at the extreme limits of GregorianCalendar's |
1090 | * representable range (except for the code that handles the YEAR | |
1091 | * field). That's because the ends of the representable range are at | |
1092 | * odd spots in the year. For calendars with the default Gregorian | |
1093 | * cutover, these limits are Sun Dec 02 16:47:04 GMT 292269055 BC to Sun | |
1094 | * Aug 17 07:12:55 GMT 292278994 AD, somewhat different for non-GMT | |
1095 | * zones. As a result, if the calendar is set to Aug 1 292278994 AD, | |
1096 | * the actual maximum of DAY_OF_MONTH is 17, not 30. If the date is Mar | |
1097 | * 31 in that year, the actual maximum month might be Jul, whereas is | |
1098 | * the date is Mar 15, the actual maximum might be Aug -- depending on | |
1099 | * the precise semantics that are desired. Similar considerations | |
1100 | * affect all fields. Nonetheless, this effect is sufficiently arcane | |
1101 | * that we permit it, rather than complicating the code to handle such | |
1102 | * intricacies. - liu 8/20/98 | |
1103 | ||
1104 | * UPDATE: No longer true, since we have pulled in the limit values on | |
1105 | * the year. - Liu 11/6/00 */ | |
b75a7d8f A |
1106 | |
1107 | switch (field) { | |
b75a7d8f A |
1108 | |
1109 | case UCAL_YEAR: | |
b75a7d8f | 1110 | /* The year computation is no different, in principle, from the |
374ca955 A |
1111 | * others, however, the range of possible maxima is large. In |
1112 | * addition, the way we know we've exceeded the range is different. | |
1113 | * For these reasons, we use the special case code below to handle | |
1114 | * this field. | |
1115 | * | |
1116 | * The actual maxima for YEAR depend on the type of calendar: | |
1117 | * | |
1118 | * Gregorian = May 17, 292275056 BC - Aug 17, 292278994 AD | |
1119 | * Julian = Dec 2, 292269055 BC - Jan 3, 292272993 AD | |
1120 | * Hybrid = Dec 2, 292269055 BC - Aug 17, 292278994 AD | |
1121 | * | |
1122 | * We know we've exceeded the maximum when either the month, date, | |
1123 | * time, or era changes in response to setting the year. We don't | |
1124 | * check for month, date, and time here because the year and era are | |
1125 | * sufficient to detect an invalid year setting. NOTE: If code is | |
1126 | * added to check the month and date in the future for some reason, | |
1127 | * Feb 29 must be allowed to shift to Mar 1 when setting the year. | |
1128 | */ | |
b75a7d8f | 1129 | { |
374ca955 A |
1130 | if(U_FAILURE(status)) return 0; |
1131 | Calendar *cal = clone(); | |
1132 | if(!cal) { | |
1133 | status = U_MEMORY_ALLOCATION_ERROR; | |
1134 | return 0; | |
1135 | } | |
1136 | ||
b75a7d8f A |
1137 | cal->setLenient(TRUE); |
1138 | ||
1139 | int32_t era = cal->get(UCAL_ERA, status); | |
b75a7d8f | 1140 | UDate d = cal->getTime(status); |
b75a7d8f A |
1141 | |
1142 | /* Perform a binary search, with the invariant that lowGood is a | |
374ca955 A |
1143 | * valid year, and highBad is an out of range year. |
1144 | */ | |
1145 | int32_t lowGood = kGregorianCalendarLimits[UCAL_YEAR][1]; | |
1146 | int32_t highBad = kGregorianCalendarLimits[UCAL_YEAR][2]+1; | |
1147 | while ((lowGood + 1) < highBad) { | |
b75a7d8f | 1148 | int32_t y = (lowGood + highBad) / 2; |
374ca955 A |
1149 | cal->set(UCAL_YEAR, y); |
1150 | if (cal->get(UCAL_YEAR, status) == y && cal->get(UCAL_ERA, status) == era) { | |
b75a7d8f | 1151 | lowGood = y; |
374ca955 | 1152 | } else { |
b75a7d8f A |
1153 | highBad = y; |
1154 | cal->setTime(d, status); // Restore original fields | |
1155 | } | |
1156 | } | |
1157 | ||
1158 | delete cal; | |
1159 | return lowGood; | |
1160 | } | |
1161 | ||
b75a7d8f | 1162 | default: |
374ca955 | 1163 | return Calendar::getActualMaximum(field,status); |
b75a7d8f A |
1164 | } |
1165 | } | |
1166 | ||
374ca955 A |
1167 | |
1168 | int32_t GregorianCalendar::handleGetExtendedYear() { | |
1169 | int32_t year = kEpochYear; | |
1170 | switch(resolveFields(kYearPrecedence)) { | |
1171 | case UCAL_EXTENDED_YEAR: | |
1172 | year = internalGet(UCAL_EXTENDED_YEAR, kEpochYear); | |
1173 | break; | |
1174 | ||
1175 | case UCAL_YEAR: | |
1176 | { | |
1177 | // The year defaults to the epoch start, the era to AD | |
1178 | int32_t era = internalGet(UCAL_ERA, AD); | |
1179 | if (era == BC) { | |
1180 | year = 1 - internalGet(UCAL_YEAR, 1); // Convert to extended year | |
1181 | } else { | |
1182 | year = internalGet(UCAL_YEAR, kEpochYear); | |
1183 | } | |
1184 | } | |
1185 | break; | |
1186 | ||
1187 | case UCAL_YEAR_WOY: | |
1188 | year = handleGetExtendedYearFromWeekFields(internalGet(UCAL_YEAR_WOY), internalGet(UCAL_WEEK_OF_YEAR)); | |
1189 | #if defined (U_DEBUG_CAL) | |
1190 | // if(internalGet(UCAL_YEAR_WOY) != year) { | |
1191 | fprintf(stderr, "%s:%d: hGEYFWF[%d,%d] -> %d\n", | |
1192 | __FILE__, __LINE__,internalGet(UCAL_YEAR_WOY),internalGet(UCAL_WEEK_OF_YEAR),year); | |
1193 | //} | |
1194 | #endif | |
1195 | break; | |
1196 | ||
1197 | default: | |
1198 | year = kEpochYear; | |
1199 | } | |
1200 | return year; | |
1201 | } | |
1202 | ||
1203 | int32_t GregorianCalendar::handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy) | |
1204 | { | |
1205 | // convert year to extended form | |
1206 | int32_t era = internalGet(UCAL_ERA, AD); | |
1207 | if(era == BC) { | |
1208 | yearWoy = 1 - yearWoy; | |
1209 | } | |
1210 | return Calendar::handleGetExtendedYearFromWeekFields(yearWoy, woy); | |
1211 | } | |
1212 | ||
1213 | ||
b75a7d8f A |
1214 | // ------------------------------------- |
1215 | ||
1216 | UBool | |
1217 | GregorianCalendar::inDaylightTime(UErrorCode& status) const | |
1218 | { | |
1219 | if (U_FAILURE(status) || !getTimeZone().useDaylightTime()) | |
1220 | return FALSE; | |
1221 | ||
1222 | // Force an update of the state of the Calendar. | |
1223 | ((GregorianCalendar*)this)->complete(status); // cast away const | |
1224 | ||
1225 | return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE); | |
1226 | } | |
1227 | ||
1228 | // ------------------------------------- | |
1229 | ||
1230 | /** | |
1231 | * Return the ERA. We need a special method for this because the | |
1232 | * default ERA is AD, but a zero (unset) ERA is BC. | |
1233 | */ | |
1234 | int32_t | |
1235 | GregorianCalendar::internalGetEra() const { | |
374ca955 | 1236 | return isSet(UCAL_ERA) ? internalGet(UCAL_ERA) : (int32_t)AD; |
b75a7d8f A |
1237 | } |
1238 | ||
1239 | const char * | |
1240 | GregorianCalendar::getType() const { | |
1241 | //static const char kGregorianType = "gregorian"; | |
1242 | ||
1243 | return "gregorian"; | |
1244 | } | |
1245 | ||
374ca955 | 1246 | const UDate GregorianCalendar::fgSystemDefaultCentury = DBL_MIN; |
b75a7d8f A |
1247 | const int32_t GregorianCalendar::fgSystemDefaultCenturyYear = -1; |
1248 | ||
1249 | UDate GregorianCalendar::fgSystemDefaultCenturyStart = DBL_MIN; | |
1250 | int32_t GregorianCalendar::fgSystemDefaultCenturyStartYear = -1; | |
1251 | ||
1252 | ||
1253 | UBool GregorianCalendar::haveDefaultCentury() const | |
1254 | { | |
1255 | return TRUE; | |
1256 | } | |
1257 | ||
1258 | UDate GregorianCalendar::defaultCenturyStart() const | |
1259 | { | |
1260 | return internalGetDefaultCenturyStart(); | |
1261 | } | |
1262 | ||
1263 | int32_t GregorianCalendar::defaultCenturyStartYear() const | |
1264 | { | |
1265 | return internalGetDefaultCenturyStartYear(); | |
1266 | } | |
1267 | ||
1268 | UDate | |
1269 | GregorianCalendar::internalGetDefaultCenturyStart() const | |
1270 | { | |
1271 | // lazy-evaluate systemDefaultCenturyStart | |
1272 | UBool needsUpdate; | |
1273 | { | |
1274 | Mutex m; | |
374ca955 | 1275 | needsUpdate = (fgSystemDefaultCenturyStart == fgSystemDefaultCentury); |
b75a7d8f A |
1276 | } |
1277 | ||
1278 | if (needsUpdate) { | |
1279 | initializeSystemDefaultCentury(); | |
1280 | } | |
1281 | ||
1282 | // use defaultCenturyStart unless it's the flag value; | |
1283 | // then use systemDefaultCenturyStart | |
1284 | ||
1285 | return fgSystemDefaultCenturyStart; | |
1286 | } | |
1287 | ||
1288 | int32_t | |
1289 | GregorianCalendar::internalGetDefaultCenturyStartYear() const | |
1290 | { | |
1291 | // lazy-evaluate systemDefaultCenturyStartYear | |
1292 | UBool needsUpdate; | |
1293 | { | |
1294 | Mutex m; | |
374ca955 | 1295 | needsUpdate = (fgSystemDefaultCenturyStart == fgSystemDefaultCentury); |
b75a7d8f A |
1296 | } |
1297 | ||
1298 | if (needsUpdate) { | |
1299 | initializeSystemDefaultCentury(); | |
1300 | } | |
1301 | ||
1302 | // use defaultCenturyStart unless it's the flag value; | |
1303 | // then use systemDefaultCenturyStartYear | |
1304 | ||
1305 | return fgSystemDefaultCenturyStartYear; | |
1306 | } | |
1307 | ||
1308 | void | |
1309 | GregorianCalendar::initializeSystemDefaultCentury() | |
1310 | { | |
1311 | // initialize systemDefaultCentury and systemDefaultCenturyYear based | |
1312 | // on the current time. They'll be set to 80 years before | |
1313 | // the current time. | |
1314 | // No point in locking as it should be idempotent. | |
374ca955 | 1315 | if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury) |
b75a7d8f A |
1316 | { |
1317 | UErrorCode status = U_ZERO_ERROR; | |
1318 | Calendar *calendar = new GregorianCalendar(status); | |
1319 | if (calendar != NULL && U_SUCCESS(status)) | |
1320 | { | |
1321 | calendar->setTime(Calendar::getNow(), status); | |
1322 | calendar->add(UCAL_YEAR, -80, status); | |
1323 | ||
1324 | UDate newStart = calendar->getTime(status); | |
1325 | int32_t newYear = calendar->get(UCAL_YEAR, status); | |
1326 | { | |
1327 | Mutex m; | |
1328 | fgSystemDefaultCenturyStart = newStart; | |
1329 | fgSystemDefaultCenturyStartYear = newYear; | |
1330 | } | |
1331 | delete calendar; | |
1332 | } | |
1333 | // We have no recourse upon failure unless we want to propagate the failure | |
1334 | // out. | |
1335 | } | |
1336 | } | |
1337 | ||
1338 | ||
1339 | U_NAMESPACE_END | |
1340 | ||
1341 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
1342 | ||
1343 | //eof |