]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/hebrwcal.cpp
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / i18n / hebrwcal.cpp
1 /*
2 ******************************************************************************
3 * Copyright (C) 2003-2006, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ******************************************************************************
6 *
7 * File HEBRWCAL.CPP
8 *
9 * Modification History:
10 *
11 * Date Name Description
12 * 12/03/2003 srl ported from java HebrewCalendar
13 *****************************************************************************
14 */
15
16 #include "hebrwcal.h"
17
18 #if !UCONFIG_NO_FORMATTING
19
20 #include "mutex.h"
21 #include <float.h>
22 #include "gregoimp.h" // Math
23 #include "astro.h" // CalendarAstronomer
24 #include "uhash.h"
25 #include "ucln_in.h"
26
27 // Hebrew Calendar implementation
28
29 /**
30 * The absolute date, in milliseconds since 1/1/1970 AD, Gregorian,
31 * of the start of the Hebrew calendar. In order to keep this calendar's
32 * time of day in sync with that of the Gregorian calendar, we use
33 * midnight, rather than sunset the day before.
34 */
35 //static const double EPOCH_MILLIS = -180799862400000.; // 1/1/1 HY
36
37 static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = {
38 // Minimum Greatest Least Maximum
39 // Minimum Maximum
40 { 0, 0, 0, 0 }, // ERA
41 { 1, 1, 5000000, 5000000 }, // YEAR
42 { 0, 0, 12, 12 }, // MONTH
43 { 1, 1, 51, 56 }, // WEEK_OF_YEAR
44 { 0, 0, 5, 6 }, // WEEK_OF_MONTH
45 { 1, 1, 29, 30 }, // DAY_OF_MONTH
46 { 1, 1, 353, 385 }, // DAY_OF_YEAR
47 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK
48 { -1, -1, 4, 6 }, // DAY_OF_WEEK_IN_MONTH
49 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1/* */}, // AM_PM
50 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR
51 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY
52 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE
53 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND
54 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND
55 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET
56 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET
57 { -5000001, -5000001, 5000001, 5000001 }, // YEAR_WOY
58 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL
59 { -5000000, -5000000, 5000000, 5000000 }, // EXTENDED_YEAR
60 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY
61 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY
62 };
63
64 /**
65 * The lengths of the Hebrew months. This is complicated, because there
66 * are three different types of years, or six if you count leap years.
67 * Due to the rules for postponing the start of the year to avoid having
68 * certain holidays fall on the sabbath, the year can end up being three
69 * different lengths, called "deficient", "normal", and "complete".
70 */
71 static const int32_t MONTH_LENGTH[][3] = {
72 // Deficient Normal Complete
73 { 30, 30, 30 }, //Tishri
74 { 29, 29, 30 }, //Heshvan
75 { 29, 30, 30 }, //Kislev
76 { 29, 29, 29 }, //Tevet
77 { 30, 30, 30 }, //Shevat
78 { 30, 30, 30 }, //Adar I (leap years only)
79 { 29, 29, 29 }, //Adar
80 { 30, 30, 30 }, //Nisan
81 { 29, 29, 29 }, //Iyar
82 { 30, 30, 30 }, //Sivan
83 { 29, 29, 29 }, //Tammuz
84 { 30, 30, 30 }, //Av
85 { 29, 29, 29 }, //Elul
86 };
87
88 /**
89 * The cumulative # of days to the end of each month in a non-leap year
90 * Although this can be calculated from the MONTH_LENGTH table,
91 * keeping it around separately makes some calculations a lot faster
92 */
93
94 static const int32_t MONTH_START[][3] = {
95 // Deficient Normal Complete
96 { 0, 0, 0 }, // (placeholder)
97 { 30, 30, 30 }, // Tishri
98 { 59, 59, 60 }, // Heshvan
99 { 88, 89, 90 }, // Kislev
100 { 117, 118, 119 }, // Tevet
101 { 147, 148, 149 }, // Shevat
102 { 147, 148, 149 }, // (Adar I placeholder)
103 { 176, 177, 178 }, // Adar
104 { 206, 207, 208 }, // Nisan
105 { 235, 236, 237 }, // Iyar
106 { 265, 266, 267 }, // Sivan
107 { 294, 295, 296 }, // Tammuz
108 { 324, 325, 326 }, // Av
109 { 353, 354, 355 }, // Elul
110 };
111
112 /**
113 * The cumulative # of days to the end of each month in a leap year
114 */
115 static const int32_t LEAP_MONTH_START[][3] = {
116 // Deficient Normal Complete
117 { 0, 0, 0 }, // (placeholder)
118 { 30, 30, 30 }, // Tishri
119 { 59, 59, 60 }, // Heshvan
120 { 88, 89, 90 }, // Kislev
121 { 117, 118, 119 }, // Tevet
122 { 147, 148, 149 }, // Shevat
123 { 177, 178, 179 }, // Adar I
124 { 206, 207, 208 }, // Adar II
125 { 236, 237, 238 }, // Nisan
126 { 265, 266, 267 }, // Iyar
127 { 295, 296, 297 }, // Sivan
128 { 324, 325, 326 }, // Tammuz
129 { 354, 355, 356 }, // Av
130 { 383, 384, 385 }, // Elul
131 };
132
133 static CalendarCache *gCache = NULL;
134
135 U_CDECL_BEGIN
136 static UBool calendar_hebrew_cleanup(void) {
137 delete gCache;
138 gCache = NULL;
139 return TRUE;
140 }
141 U_CDECL_END
142
143 U_NAMESPACE_BEGIN
144 //-------------------------------------------------------------------------
145 // Constructors...
146 //-------------------------------------------------------------------------
147
148 /**
149 * Constructs a default <code>HebrewCalendar</code> using the current time
150 * in the default time zone with the default locale.
151 * @internal
152 */
153 HebrewCalendar::HebrewCalendar(const Locale& aLocale, UErrorCode& success)
154 : Calendar(TimeZone::createDefault(), aLocale, success)
155
156 {
157 setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
158 }
159
160
161 HebrewCalendar::~HebrewCalendar() {
162 }
163
164 const char *HebrewCalendar::getType() const {
165 return "hebrew";
166 }
167
168 Calendar* HebrewCalendar::clone() const {
169 return new HebrewCalendar(*this);
170 }
171
172 HebrewCalendar::HebrewCalendar(const HebrewCalendar& other) : Calendar(other) {
173 }
174
175
176 //-------------------------------------------------------------------------
177 // Rolling and adding functions overridden from Calendar
178 //
179 // These methods call through to the default implementation in IBMCalendar
180 // for most of the fields and only handle the unusual ones themselves.
181 //-------------------------------------------------------------------------
182
183 /**
184 * Add a signed amount to a specified field, using this calendar's rules.
185 * For example, to add three days to the current date, you can call
186 * <code>add(Calendar.DATE, 3)</code>.
187 * <p>
188 * When adding to certain fields, the values of other fields may conflict and
189 * need to be changed. For example, when adding one to the {@link #MONTH MONTH} field
190 * for the date "30 Av 5758", the {@link #DAY_OF_MONTH DAY_OF_MONTH} field
191 * must be adjusted so that the result is "29 Elul 5758" rather than the invalid
192 * "30 Elul 5758".
193 * <p>
194 * This method is able to add to
195 * all fields except for {@link #ERA ERA}, {@link #DST_OFFSET DST_OFFSET},
196 * and {@link #ZONE_OFFSET ZONE_OFFSET}.
197 * <p>
198 * <b>Note:</b> You should always use {@link #roll roll} and add rather
199 * than attempting to perform arithmetic operations directly on the fields
200 * of a <tt>HebrewCalendar</tt>. Since the {@link #MONTH MONTH} field behaves
201 * discontinuously in non-leap years, simple arithmetic can give invalid results.
202 * <p>
203 * @param field the time field.
204 * @param amount the amount to add to the field.
205 *
206 * @exception IllegalArgumentException if the field is invalid or refers
207 * to a field that cannot be handled by this method.
208 * @internal
209 */
210 void HebrewCalendar::add(UCalendarDateFields field, int32_t amount, UErrorCode& status)
211 {
212 if(U_FAILURE(status)) {
213 return;
214 }
215 switch (field) {
216 case UCAL_MONTH:
217 {
218 // We can't just do a set(MONTH, get(MONTH) + amount). The
219 // reason is ADAR_1. Suppose amount is +2 and we land in
220 // ADAR_1 -- then we have to bump to ADAR_2 aka ADAR. But
221 // if amount is -2 and we land in ADAR_1, then we have to
222 // bump the other way -- down to SHEVAT. - Alan 11/00
223 int32_t month = get(UCAL_MONTH, status);
224 int32_t year = get(UCAL_YEAR, status);
225 UBool acrossAdar1;
226 if (amount > 0) {
227 acrossAdar1 = (month < ADAR_1); // started before ADAR_1?
228 month += amount;
229 for (;;) {
230 if (acrossAdar1 && month>=ADAR_1 && !isLeapYear(year)) {
231 ++month;
232 }
233 if (month <= ELUL) {
234 break;
235 }
236 month -= ELUL+1;
237 ++year;
238 acrossAdar1 = TRUE;
239 }
240 } else {
241 acrossAdar1 = (month > ADAR_1); // started after ADAR_1?
242 month += amount;
243 for (;;) {
244 if (acrossAdar1 && month<=ADAR_1 && !isLeapYear(year)) {
245 --month;
246 }
247 if (month >= 0) {
248 break;
249 }
250 month += ELUL+1;
251 --year;
252 acrossAdar1 = TRUE;
253 }
254 }
255 set(UCAL_MONTH, month);
256 set(UCAL_YEAR, year);
257 pinField(UCAL_DAY_OF_MONTH, status);
258 break;
259 }
260
261 default:
262 Calendar::add(field, amount, status);
263 break;
264 }
265 }
266
267 /**
268 * @deprecated ICU 2.6 use UCalendarDateFields instead of EDateFields
269 */
270 void HebrewCalendar::add(EDateFields field, int32_t amount, UErrorCode& status)
271 {
272 add((UCalendarDateFields)field, amount, status);
273 }
274
275 /**
276 * Rolls (up/down) a specified amount time on the given field. For
277 * example, to roll the current date up by three days, you can call
278 * <code>roll(Calendar.DATE, 3)</code>. If the
279 * field is rolled past its maximum allowable value, it will "wrap" back
280 * to its minimum and continue rolling.
281 * For example, calling <code>roll(Calendar.DATE, 10)</code>
282 * on a Hebrew calendar set to "25 Av 5758" will result in the date "5 Av 5758".
283 * <p>
284 * When rolling certain fields, the values of other fields may conflict and
285 * need to be changed. For example, when rolling the {@link #MONTH MONTH} field
286 * upward by one for the date "30 Av 5758", the {@link #DAY_OF_MONTH DAY_OF_MONTH} field
287 * must be adjusted so that the result is "29 Elul 5758" rather than the invalid
288 * "30 Elul".
289 * <p>
290 * This method is able to roll
291 * all fields except for {@link #ERA ERA}, {@link #DST_OFFSET DST_OFFSET},
292 * and {@link #ZONE_OFFSET ZONE_OFFSET}. Subclasses may, of course, add support for
293 * additional fields in their overrides of <code>roll</code>.
294 * <p>
295 * <b>Note:</b> You should always use roll and {@link #add add} rather
296 * than attempting to perform arithmetic operations directly on the fields
297 * of a <tt>HebrewCalendar</tt>. Since the {@link #MONTH MONTH} field behaves
298 * discontinuously in non-leap years, simple arithmetic can give invalid results.
299 * <p>
300 * @param field the time field.
301 * @param amount the amount by which the field should be rolled.
302 *
303 * @exception IllegalArgumentException if the field is invalid or refers
304 * to a field that cannot be handled by this method.
305 * @internal
306 */
307 void HebrewCalendar::roll(UCalendarDateFields field, int32_t amount, UErrorCode& status)
308 {
309 if(U_FAILURE(status)) {
310 return;
311 }
312 switch (field) {
313 case UCAL_MONTH:
314 {
315 int32_t month = get(UCAL_MONTH, status);
316 int32_t year = get(UCAL_YEAR, status);
317
318 UBool leapYear = isLeapYear(year);
319 int32_t yearLength = monthsInYear(year);
320 int32_t newMonth = month + (amount % yearLength);
321 //
322 // If it's not a leap year and we're rolling past the missing month
323 // of ADAR_1, we need to roll an extra month to make up for it.
324 //
325 if (!leapYear) {
326 if (amount > 0 && month < ADAR_1 && newMonth >= ADAR_1) {
327 newMonth++;
328 } else if (amount < 0 && month > ADAR_1 && newMonth <= ADAR_1) {
329 newMonth--;
330 }
331 }
332 set(UCAL_MONTH, (newMonth + 13) % 13);
333 pinField(UCAL_DAY_OF_MONTH, status);
334 return;
335 }
336 default:
337 Calendar::roll(field, amount, status);
338 }
339 }
340
341 void HebrewCalendar::roll(EDateFields field, int32_t amount, UErrorCode& status) {
342 roll((UCalendarDateFields)field, amount, status);
343 }
344
345 //-------------------------------------------------------------------------
346 // Support methods
347 //-------------------------------------------------------------------------
348
349 // Hebrew date calculations are performed in terms of days, hours, and
350 // "parts" (or halakim), which are 1/1080 of an hour, or 3 1/3 seconds.
351 static const int32_t HOUR_PARTS = 1080;
352 static const int32_t DAY_PARTS = 24*HOUR_PARTS;
353
354 // An approximate value for the length of a lunar month.
355 // It is used to calculate the approximate year and month of a given
356 // absolute date.
357 static const int32_t MONTH_DAYS = 29;
358 static const int32_t MONTH_FRACT = 12*HOUR_PARTS + 793;
359 static const int32_t MONTH_PARTS = MONTH_DAYS*DAY_PARTS + MONTH_FRACT;
360
361 // The time of the new moon (in parts) on 1 Tishri, year 1 (the epoch)
362 // counting from noon on the day before. BAHARAD is an abbreviation of
363 // Bet (Monday), Hey (5 hours from sunset), Resh-Daled (204).
364 static const int32_t BAHARAD = 11*HOUR_PARTS + 204;
365
366 /**
367 * Finds the day # of the first day in the given Hebrew year.
368 * To do this, we want to calculate the time of the Tishri 1 new moon
369 * in that year.
370 * <p>
371 * The algorithm here is similar to ones described in a number of
372 * references, including:
373 * <ul>
374 * <li>"Calendrical Calculations", by Nachum Dershowitz & Edward Reingold,
375 * Cambridge University Press, 1997, pages 85-91.
376 *
377 * <li>Hebrew Calendar Science and Myths,
378 * <a href="http://www.geocities.com/Athens/1584/">
379 * http://www.geocities.com/Athens/1584/</a>
380 *
381 * <li>The Calendar FAQ,
382 * <a href="http://www.faqs.org/faqs/calendars/faq/">
383 * http://www.faqs.org/faqs/calendars/faq/</a>
384 * </ul>
385 */
386 int32_t HebrewCalendar::startOfYear(int32_t year, UErrorCode &status)
387 {
388 ucln_i18n_registerCleanup(UCLN_I18N_HEBREW_CALENDAR, calendar_hebrew_cleanup);
389 int32_t day = CalendarCache::get(&gCache, year, status);
390
391 if (day == 0) {
392 int32_t months = (235 * year - 234) / 19; // # of months before year
393
394 int32_t frac = months * MONTH_FRACT + BAHARAD; // Fractional part of day #
395 day = months * 29 + (frac / DAY_PARTS); // Whole # part of calculation
396 frac = frac % DAY_PARTS; // Time of day
397
398 int32_t wd = (day % 7); // Day of week (0 == Monday)
399
400 if (wd == 2 || wd == 4 || wd == 6) {
401 // If the 1st is on Sun, Wed, or Fri, postpone to the next day
402 day += 1;
403 wd = (day % 7);
404 }
405 if (wd == 1 && frac > 15*HOUR_PARTS+204 && !isLeapYear(year) ) {
406 // If the new moon falls after 3:11:20am (15h204p from the previous noon)
407 // on a Tuesday and it is not a leap year, postpone by 2 days.
408 // This prevents 356-day years.
409 day += 2;
410 }
411 else if (wd == 0 && frac > 21*HOUR_PARTS+589 && isLeapYear(year-1) ) {
412 // If the new moon falls after 9:32:43 1/3am (21h589p from yesterday noon)
413 // on a Monday and *last* year was a leap year, postpone by 1 day.
414 // Prevents 382-day years.
415 day += 1;
416 }
417 CalendarCache::put(&gCache, year, day, status);
418 }
419 return day;
420 }
421
422 /**
423 * Find the day of the week for a given day
424 *
425 * @param day The # of days since the start of the Hebrew calendar,
426 * 1-based (i.e. 1/1/1 AM is day 1).
427 */
428 int32_t HebrewCalendar::absoluteDayToDayOfWeek(int32_t day)
429 {
430 // We know that 1/1/1 AM is a Monday, which makes the math easy...
431 return (day % 7) + 1;
432 }
433
434 /**
435 * Returns the the type of a given year.
436 * 0 "Deficient" year with 353 or 383 days
437 * 1 "Normal" year with 354 or 384 days
438 * 2 "Complete" year with 355 or 385 days
439 */
440 int32_t HebrewCalendar::yearType(int32_t year) const
441 {
442 int32_t yearLength = handleGetYearLength(year);
443
444 if (yearLength > 380) {
445 yearLength -= 30; // Subtract length of leap month.
446 }
447
448 int type = 0;
449
450 switch (yearLength) {
451 case 353:
452 type = 0; break;
453 case 354:
454 type = 1; break;
455 case 355:
456 type = 2; break;
457 default:
458 //throw new RuntimeException("Illegal year length " + yearLength + " in year " + year);
459 type = 1;
460 }
461 return type;
462 }
463
464 /**
465 * Determine whether a given Hebrew year is a leap year
466 *
467 * The rule here is that if (year % 19) == 0, 3, 6, 8, 11, 14, or 17.
468 * The formula below performs the same test, believe it or not.
469 */
470 UBool HebrewCalendar::isLeapYear(int32_t year) {
471 //return (year * 12 + 17) % 19 >= 12;
472 int32_t x = (year*12 + 17) % 19;
473 return x >= ((x < 0) ? -7 : 12);
474 }
475
476 int32_t HebrewCalendar::monthsInYear(int32_t year) {
477 return isLeapYear(year) ? 13 : 12;
478 }
479
480 //-------------------------------------------------------------------------
481 // Calendar framework
482 //-------------------------------------------------------------------------
483
484 /**
485 * @internal
486 */
487 int32_t HebrewCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const {
488 return LIMITS[field][limitType];
489 }
490
491 /**
492 * Returns the length of the given month in the given year
493 * @internal
494 */
495 int32_t HebrewCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const {
496 switch (month) {
497 case HESHVAN:
498 case KISLEV:
499 // These two month lengths can vary
500 return MONTH_LENGTH[month][yearType(extendedYear)];
501
502 default:
503 // The rest are a fixed length
504 return MONTH_LENGTH[month][0];
505 }
506 }
507
508 /**
509 * Returns the number of days in the given Hebrew year
510 * @internal
511 */
512 int32_t HebrewCalendar::handleGetYearLength(int32_t eyear) const {
513 UErrorCode status = U_ZERO_ERROR;
514 return startOfYear(eyear+1, status) - startOfYear(eyear, status);
515 }
516
517 //-------------------------------------------------------------------------
518 // Functions for converting from milliseconds to field values
519 //-------------------------------------------------------------------------
520
521 /**
522 * Subclasses may override this method to compute several fields
523 * specific to each calendar system. These are:
524 *
525 * <ul><li>ERA
526 * <li>YEAR
527 * <li>MONTH
528 * <li>DAY_OF_MONTH
529 * <li>DAY_OF_YEAR
530 * <li>EXTENDED_YEAR</ul>
531 *
532 * Subclasses can refer to the DAY_OF_WEEK and DOW_LOCAL fields,
533 * which will be set when this method is called. Subclasses can
534 * also call the getGregorianXxx() methods to obtain Gregorian
535 * calendar equivalents for the given Julian day.
536 *
537 * <p>In addition, subclasses should compute any subclass-specific
538 * fields, that is, fields from BASE_FIELD_COUNT to
539 * getFieldCount() - 1.
540 * @internal
541 */
542 void HebrewCalendar::handleComputeFields(int32_t julianDay, UErrorCode &status) {
543 int32_t d = julianDay - 347997;
544 double m = ((d * (double)DAY_PARTS)/ (double) MONTH_PARTS); // Months (approx)
545 int32_t year = (int32_t)( ((19. * m + 234.) / 235.) + 1.); // Years (approx)
546 int32_t ys = startOfYear(year, status); // 1st day of year
547 int32_t dayOfYear = (d - ys);
548
549 // Because of the postponement rules, it's possible to guess wrong. Fix it.
550 while (dayOfYear < 1) {
551 year--;
552 ys = startOfYear(year, status);
553 dayOfYear = (d - ys);
554 }
555
556 // Now figure out which month we're in, and the date within that month
557 int32_t type = yearType(year);
558 UBool isLeap = isLeapYear(year);
559
560 int32_t month = 0;
561 while (dayOfYear > ( isLeap ? LEAP_MONTH_START[month][type] : MONTH_START[month][type] ) ) {
562 month++;
563 }
564 month--;
565 int dayOfMonth = dayOfYear - (isLeap ? LEAP_MONTH_START[month][type] : MONTH_START[month][type]);
566
567 internalSet(UCAL_ERA, 0);
568 internalSet(UCAL_YEAR, year);
569 internalSet(UCAL_EXTENDED_YEAR, year);
570 internalSet(UCAL_MONTH, month);
571 internalSet(UCAL_DAY_OF_MONTH, dayOfMonth);
572 internalSet(UCAL_DAY_OF_YEAR, dayOfYear);
573 }
574
575 //-------------------------------------------------------------------------
576 // Functions for converting from field values to milliseconds
577 //-------------------------------------------------------------------------
578
579 /**
580 * @internal
581 */
582 int32_t HebrewCalendar::handleGetExtendedYear() {
583 int32_t year;
584 if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
585 year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
586 } else {
587 year = internalGet(UCAL_YEAR, 1); // Default to year 1
588 }
589 return year;
590 }
591
592 /**
593 * Return JD of start of given month/year.
594 * @internal
595 */
596 int32_t HebrewCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /*useMonth*/) const {
597 UErrorCode status = U_ZERO_ERROR;
598 // Resolve out-of-range months. This is necessary in order to
599 // obtain the correct year. We correct to
600 // a 12- or 13-month year (add/subtract 12 or 13, depending
601 // on the year) but since we _always_ number from 0..12, and
602 // the leap year determines whether or not month 5 (Adar 1)
603 // is present, we allow 0..12 in any given year.
604 while (month < 0) {
605 month += monthsInYear(--eyear);
606 }
607 // Careful: allow 0..12 in all years
608 while (month > 12) {
609 month -= monthsInYear(eyear++);
610 }
611
612 int32_t day = startOfYear(eyear, status);
613
614 if(U_FAILURE(status)) {
615 return 0;
616 }
617
618 if (month != 0) {
619 if (isLeapYear(eyear)) {
620 day += LEAP_MONTH_START[month][yearType(eyear)];
621 } else {
622 day += MONTH_START[month][yearType(eyear)];
623 }
624 }
625
626 return (int) (day + 347997);
627 }
628
629 UBool
630 HebrewCalendar::inDaylightTime(UErrorCode& status) const
631 {
632 // copied from GregorianCalendar
633 if (U_FAILURE(status) || !getTimeZone().useDaylightTime())
634 return FALSE;
635
636 // Force an update of the state of the Calendar.
637 ((HebrewCalendar*)this)->complete(status); // cast away const
638
639 return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE);
640 }
641
642 // default century
643 const UDate HebrewCalendar::fgSystemDefaultCentury = DBL_MIN;
644 const int32_t HebrewCalendar::fgSystemDefaultCenturyYear = -1;
645
646 UDate HebrewCalendar::fgSystemDefaultCenturyStart = DBL_MIN;
647 int32_t HebrewCalendar::fgSystemDefaultCenturyStartYear = -1;
648
649
650 UBool HebrewCalendar::haveDefaultCentury() const
651 {
652 return TRUE;
653 }
654
655 UDate HebrewCalendar::defaultCenturyStart() const
656 {
657 return internalGetDefaultCenturyStart();
658 }
659
660 int32_t HebrewCalendar::defaultCenturyStartYear() const
661 {
662 return internalGetDefaultCenturyStartYear();
663 }
664
665 UDate
666 HebrewCalendar::internalGetDefaultCenturyStart() const
667 {
668 // lazy-evaluate systemDefaultCenturyStart
669 UBool needsUpdate;
670 {
671 Mutex m;
672 needsUpdate = (fgSystemDefaultCenturyStart == fgSystemDefaultCentury);
673 }
674
675 if (needsUpdate) {
676 initializeSystemDefaultCentury();
677 }
678
679 // use defaultCenturyStart unless it's the flag value;
680 // then use systemDefaultCenturyStart
681
682 return fgSystemDefaultCenturyStart;
683 }
684
685 int32_t
686 HebrewCalendar::internalGetDefaultCenturyStartYear() const
687 {
688 // lazy-evaluate systemDefaultCenturyStartYear
689 UBool needsUpdate;
690 {
691 Mutex m;
692 needsUpdate = (fgSystemDefaultCenturyStart == fgSystemDefaultCentury);
693 }
694
695 if (needsUpdate) {
696 initializeSystemDefaultCentury();
697 }
698
699 // use defaultCenturyStart unless it's the flag value;
700 // then use systemDefaultCenturyStartYear
701
702 return fgSystemDefaultCenturyStartYear;
703 }
704
705 void
706 HebrewCalendar::initializeSystemDefaultCentury()
707 {
708 // initialize systemDefaultCentury and systemDefaultCenturyYear based
709 // on the current time. They'll be set to 80 years before
710 // the current time.
711 // No point in locking as it should be idempotent.
712 if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
713 {
714 UErrorCode status = U_ZERO_ERROR;
715 HebrewCalendar calendar(Locale("@calendar=hebrew"),status);
716 if (U_SUCCESS(status))
717 {
718 calendar.setTime(Calendar::getNow(), status);
719 calendar.add(UCAL_YEAR, -80, status);
720 UDate newStart = calendar.getTime(status);
721 int32_t newYear = calendar.get(UCAL_YEAR, status);
722 {
723 Mutex m;
724 fgSystemDefaultCenturyStart = newStart;
725 fgSystemDefaultCenturyStartYear = newYear;
726 }
727 }
728 // We have no recourse upon failure unless we want to propagate the failure
729 // out.
730 }
731 }
732
733 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(HebrewCalendar)
734
735 U_NAMESPACE_END
736
737 #endif // UCONFIG_NO_FORMATTING
738