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