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