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