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