michael@0: /* michael@0: ****************************************************************************** michael@0: * Copyright (C) 2003-2013, International Business Machines Corporation michael@0: * and others. All Rights Reserved. michael@0: ****************************************************************************** michael@0: * michael@0: * File HEBRWCAL.CPP michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 12/03/2003 srl ported from java HebrewCalendar michael@0: ***************************************************************************** michael@0: */ michael@0: michael@0: #include "hebrwcal.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "umutex.h" michael@0: #include michael@0: #include "gregoimp.h" // Math michael@0: #include "astro.h" // CalendarAstronomer michael@0: #include "uhash.h" michael@0: #include "ucln_in.h" michael@0: michael@0: // Hebrew Calendar implementation michael@0: michael@0: /** michael@0: * The absolute date, in milliseconds since 1/1/1970 AD, Gregorian, michael@0: * of the start of the Hebrew calendar. In order to keep this calendar's michael@0: * time of day in sync with that of the Gregorian calendar, we use michael@0: * midnight, rather than sunset the day before. michael@0: */ michael@0: //static const double EPOCH_MILLIS = -180799862400000.; // 1/1/1 HY michael@0: michael@0: static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = { michael@0: // Minimum Greatest Least Maximum michael@0: // Minimum Maximum michael@0: { 0, 0, 0, 0}, // ERA michael@0: { -5000000, -5000000, 5000000, 5000000}, // YEAR michael@0: { 0, 0, 12, 12}, // MONTH michael@0: { 1, 1, 51, 56}, // WEEK_OF_YEAR michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH michael@0: { 1, 1, 29, 30}, // DAY_OF_MONTH michael@0: { 1, 1, 353, 385}, // DAY_OF_YEAR michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK michael@0: { -1, -1, 5, 5}, // DAY_OF_WEEK_IN_MONTH michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET michael@0: { -5000000, -5000000, 5000000, 5000000}, // YEAR_WOY michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL michael@0: { -5000000, -5000000, 5000000, 5000000}, // EXTENDED_YEAR michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH michael@0: }; michael@0: michael@0: /** michael@0: * The lengths of the Hebrew months. This is complicated, because there michael@0: * are three different types of years, or six if you count leap years. michael@0: * Due to the rules for postponing the start of the year to avoid having michael@0: * certain holidays fall on the sabbath, the year can end up being three michael@0: * different lengths, called "deficient", "normal", and "complete". michael@0: */ michael@0: static const int8_t MONTH_LENGTH[][3] = { michael@0: // Deficient Normal Complete michael@0: { 30, 30, 30 }, //Tishri michael@0: { 29, 29, 30 }, //Heshvan michael@0: { 29, 30, 30 }, //Kislev michael@0: { 29, 29, 29 }, //Tevet michael@0: { 30, 30, 30 }, //Shevat michael@0: { 30, 30, 30 }, //Adar I (leap years only) michael@0: { 29, 29, 29 }, //Adar michael@0: { 30, 30, 30 }, //Nisan michael@0: { 29, 29, 29 }, //Iyar michael@0: { 30, 30, 30 }, //Sivan michael@0: { 29, 29, 29 }, //Tammuz michael@0: { 30, 30, 30 }, //Av michael@0: { 29, 29, 29 }, //Elul michael@0: }; michael@0: michael@0: /** michael@0: * The cumulative # of days to the end of each month in a non-leap year michael@0: * Although this can be calculated from the MONTH_LENGTH table, michael@0: * keeping it around separately makes some calculations a lot faster michael@0: */ michael@0: michael@0: static const int16_t MONTH_START[][3] = { michael@0: // Deficient Normal Complete michael@0: { 0, 0, 0 }, // (placeholder) michael@0: { 30, 30, 30 }, // Tishri michael@0: { 59, 59, 60 }, // Heshvan michael@0: { 88, 89, 90 }, // Kislev michael@0: { 117, 118, 119 }, // Tevet michael@0: { 147, 148, 149 }, // Shevat michael@0: { 147, 148, 149 }, // (Adar I placeholder) michael@0: { 176, 177, 178 }, // Adar michael@0: { 206, 207, 208 }, // Nisan michael@0: { 235, 236, 237 }, // Iyar michael@0: { 265, 266, 267 }, // Sivan michael@0: { 294, 295, 296 }, // Tammuz michael@0: { 324, 325, 326 }, // Av michael@0: { 353, 354, 355 }, // Elul michael@0: }; michael@0: michael@0: /** michael@0: * The cumulative # of days to the end of each month in a leap year michael@0: */ michael@0: static const int16_t LEAP_MONTH_START[][3] = { michael@0: // Deficient Normal Complete michael@0: { 0, 0, 0 }, // (placeholder) michael@0: { 30, 30, 30 }, // Tishri michael@0: { 59, 59, 60 }, // Heshvan michael@0: { 88, 89, 90 }, // Kislev michael@0: { 117, 118, 119 }, // Tevet michael@0: { 147, 148, 149 }, // Shevat michael@0: { 177, 178, 179 }, // Adar I michael@0: { 206, 207, 208 }, // Adar II michael@0: { 236, 237, 238 }, // Nisan michael@0: { 265, 266, 267 }, // Iyar michael@0: { 295, 296, 297 }, // Sivan michael@0: { 324, 325, 326 }, // Tammuz michael@0: { 354, 355, 356 }, // Av michael@0: { 383, 384, 385 }, // Elul michael@0: }; michael@0: michael@0: static icu::CalendarCache *gCache = NULL; michael@0: michael@0: U_CDECL_BEGIN michael@0: static UBool calendar_hebrew_cleanup(void) { michael@0: delete gCache; michael@0: gCache = NULL; michael@0: return TRUE; michael@0: } michael@0: U_CDECL_END michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: //------------------------------------------------------------------------- michael@0: // Constructors... michael@0: //------------------------------------------------------------------------- michael@0: michael@0: /** michael@0: * Constructs a default HebrewCalendar using the current time michael@0: * in the default time zone with the default locale. michael@0: * @internal michael@0: */ michael@0: HebrewCalendar::HebrewCalendar(const Locale& aLocale, UErrorCode& success) michael@0: : Calendar(TimeZone::createDefault(), aLocale, success) michael@0: michael@0: { michael@0: setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly. michael@0: } michael@0: michael@0: michael@0: HebrewCalendar::~HebrewCalendar() { michael@0: } michael@0: michael@0: const char *HebrewCalendar::getType() const { michael@0: return "hebrew"; michael@0: } michael@0: michael@0: Calendar* HebrewCalendar::clone() const { michael@0: return new HebrewCalendar(*this); michael@0: } michael@0: michael@0: HebrewCalendar::HebrewCalendar(const HebrewCalendar& other) : Calendar(other) { michael@0: } michael@0: michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Rolling and adding functions overridden from Calendar michael@0: // michael@0: // These methods call through to the default implementation in IBMCalendar michael@0: // for most of the fields and only handle the unusual ones themselves. michael@0: //------------------------------------------------------------------------- michael@0: michael@0: /** michael@0: * Add a signed amount to a specified field, using this calendar's rules. michael@0: * For example, to add three days to the current date, you can call michael@0: * add(Calendar.DATE, 3). michael@0: *

michael@0: * When adding to certain fields, the values of other fields may conflict and michael@0: * need to be changed. For example, when adding one to the {@link #MONTH MONTH} field michael@0: * for the date "30 Av 5758", the {@link #DAY_OF_MONTH DAY_OF_MONTH} field michael@0: * must be adjusted so that the result is "29 Elul 5758" rather than the invalid michael@0: * "30 Elul 5758". michael@0: *

michael@0: * This method is able to add to michael@0: * all fields except for {@link #ERA ERA}, {@link #DST_OFFSET DST_OFFSET}, michael@0: * and {@link #ZONE_OFFSET ZONE_OFFSET}. michael@0: *

michael@0: * Note: You should always use {@link #roll roll} and add rather michael@0: * than attempting to perform arithmetic operations directly on the fields michael@0: * of a HebrewCalendar. Since the {@link #MONTH MONTH} field behaves michael@0: * discontinuously in non-leap years, simple arithmetic can give invalid results. michael@0: *

michael@0: * @param field the time field. michael@0: * @param amount the amount to add to the field. michael@0: * michael@0: * @exception IllegalArgumentException if the field is invalid or refers michael@0: * to a field that cannot be handled by this method. michael@0: * @internal michael@0: */ michael@0: void HebrewCalendar::add(UCalendarDateFields field, int32_t amount, UErrorCode& status) michael@0: { michael@0: if(U_FAILURE(status)) { michael@0: return; michael@0: } michael@0: switch (field) { michael@0: case UCAL_MONTH: michael@0: { michael@0: // We can't just do a set(MONTH, get(MONTH) + amount). The michael@0: // reason is ADAR_1. Suppose amount is +2 and we land in michael@0: // ADAR_1 -- then we have to bump to ADAR_2 aka ADAR. But michael@0: // if amount is -2 and we land in ADAR_1, then we have to michael@0: // bump the other way -- down to SHEVAT. - Alan 11/00 michael@0: int32_t month = get(UCAL_MONTH, status); michael@0: int32_t year = get(UCAL_YEAR, status); michael@0: UBool acrossAdar1; michael@0: if (amount > 0) { michael@0: acrossAdar1 = (month < ADAR_1); // started before ADAR_1? michael@0: month += amount; michael@0: for (;;) { michael@0: if (acrossAdar1 && month>=ADAR_1 && !isLeapYear(year)) { michael@0: ++month; michael@0: } michael@0: if (month <= ELUL) { michael@0: break; michael@0: } michael@0: month -= ELUL+1; michael@0: ++year; michael@0: acrossAdar1 = TRUE; michael@0: } michael@0: } else { michael@0: acrossAdar1 = (month > ADAR_1); // started after ADAR_1? michael@0: month += amount; michael@0: for (;;) { michael@0: if (acrossAdar1 && month<=ADAR_1 && !isLeapYear(year)) { michael@0: --month; michael@0: } michael@0: if (month >= 0) { michael@0: break; michael@0: } michael@0: month += ELUL+1; michael@0: --year; michael@0: acrossAdar1 = TRUE; michael@0: } michael@0: } michael@0: set(UCAL_MONTH, month); michael@0: set(UCAL_YEAR, year); michael@0: pinField(UCAL_DAY_OF_MONTH, status); michael@0: break; michael@0: } michael@0: michael@0: default: michael@0: Calendar::add(field, amount, status); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * @deprecated ICU 2.6 use UCalendarDateFields instead of EDateFields michael@0: */ michael@0: void HebrewCalendar::add(EDateFields field, int32_t amount, UErrorCode& status) michael@0: { michael@0: add((UCalendarDateFields)field, amount, status); michael@0: } michael@0: michael@0: /** michael@0: * Rolls (up/down) a specified amount time on the given field. For michael@0: * example, to roll the current date up by three days, you can call michael@0: * roll(Calendar.DATE, 3). If the michael@0: * field is rolled past its maximum allowable value, it will "wrap" back michael@0: * to its minimum and continue rolling. michael@0: * For example, calling roll(Calendar.DATE, 10) michael@0: * on a Hebrew calendar set to "25 Av 5758" will result in the date "5 Av 5758". michael@0: *

michael@0: * When rolling certain fields, the values of other fields may conflict and michael@0: * need to be changed. For example, when rolling the {@link #MONTH MONTH} field michael@0: * upward by one for the date "30 Av 5758", the {@link #DAY_OF_MONTH DAY_OF_MONTH} field michael@0: * must be adjusted so that the result is "29 Elul 5758" rather than the invalid michael@0: * "30 Elul". michael@0: *

michael@0: * This method is able to roll michael@0: * all fields except for {@link #ERA ERA}, {@link #DST_OFFSET DST_OFFSET}, michael@0: * and {@link #ZONE_OFFSET ZONE_OFFSET}. Subclasses may, of course, add support for michael@0: * additional fields in their overrides of roll. michael@0: *

michael@0: * Note: You should always use roll and {@link #add add} rather michael@0: * than attempting to perform arithmetic operations directly on the fields michael@0: * of a HebrewCalendar. Since the {@link #MONTH MONTH} field behaves michael@0: * discontinuously in non-leap years, simple arithmetic can give invalid results. michael@0: *

michael@0: * @param field the time field. michael@0: * @param amount the amount by which the field should be rolled. michael@0: * michael@0: * @exception IllegalArgumentException if the field is invalid or refers michael@0: * to a field that cannot be handled by this method. michael@0: * @internal michael@0: */ michael@0: void HebrewCalendar::roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) michael@0: { michael@0: if(U_FAILURE(status)) { michael@0: return; michael@0: } michael@0: switch (field) { michael@0: case UCAL_MONTH: michael@0: { michael@0: int32_t month = get(UCAL_MONTH, status); michael@0: int32_t year = get(UCAL_YEAR, status); michael@0: michael@0: UBool leapYear = isLeapYear(year); michael@0: int32_t yearLength = monthsInYear(year); michael@0: int32_t newMonth = month + (amount % yearLength); michael@0: // michael@0: // If it's not a leap year and we're rolling past the missing month michael@0: // of ADAR_1, we need to roll an extra month to make up for it. michael@0: // michael@0: if (!leapYear) { michael@0: if (amount > 0 && month < ADAR_1 && newMonth >= ADAR_1) { michael@0: newMonth++; michael@0: } else if (amount < 0 && month > ADAR_1 && newMonth <= ADAR_1) { michael@0: newMonth--; michael@0: } michael@0: } michael@0: set(UCAL_MONTH, (newMonth + 13) % 13); michael@0: pinField(UCAL_DAY_OF_MONTH, status); michael@0: return; michael@0: } michael@0: default: michael@0: Calendar::roll(field, amount, status); michael@0: } michael@0: } michael@0: michael@0: void HebrewCalendar::roll(EDateFields field, int32_t amount, UErrorCode& status) { michael@0: roll((UCalendarDateFields)field, amount, status); michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Support methods michael@0: //------------------------------------------------------------------------- michael@0: michael@0: // Hebrew date calculations are performed in terms of days, hours, and michael@0: // "parts" (or halakim), which are 1/1080 of an hour, or 3 1/3 seconds. michael@0: static const int32_t HOUR_PARTS = 1080; michael@0: static const int32_t DAY_PARTS = 24*HOUR_PARTS; michael@0: michael@0: // An approximate value for the length of a lunar month. michael@0: // It is used to calculate the approximate year and month of a given michael@0: // absolute date. michael@0: static const int32_t MONTH_DAYS = 29; michael@0: static const int32_t MONTH_FRACT = 12*HOUR_PARTS + 793; michael@0: static const int32_t MONTH_PARTS = MONTH_DAYS*DAY_PARTS + MONTH_FRACT; michael@0: michael@0: // The time of the new moon (in parts) on 1 Tishri, year 1 (the epoch) michael@0: // counting from noon on the day before. BAHARAD is an abbreviation of michael@0: // Bet (Monday), Hey (5 hours from sunset), Resh-Daled (204). michael@0: static const int32_t BAHARAD = 11*HOUR_PARTS + 204; michael@0: michael@0: /** michael@0: * Finds the day # of the first day in the given Hebrew year. michael@0: * To do this, we want to calculate the time of the Tishri 1 new moon michael@0: * in that year. michael@0: *

michael@0: * The algorithm here is similar to ones described in a number of michael@0: * references, including: michael@0: *

michael@0: */ michael@0: int32_t HebrewCalendar::startOfYear(int32_t year, UErrorCode &status) michael@0: { michael@0: ucln_i18n_registerCleanup(UCLN_I18N_HEBREW_CALENDAR, calendar_hebrew_cleanup); michael@0: int32_t day = CalendarCache::get(&gCache, year, status); michael@0: michael@0: if (day == 0) { michael@0: int32_t months = (235 * year - 234) / 19; // # of months before year michael@0: michael@0: int64_t frac = (int64_t)months * MONTH_FRACT + BAHARAD; // Fractional part of day # michael@0: day = months * 29 + (int32_t)(frac / DAY_PARTS); // Whole # part of calculation michael@0: frac = frac % DAY_PARTS; // Time of day michael@0: michael@0: int32_t wd = (day % 7); // Day of week (0 == Monday) michael@0: michael@0: if (wd == 2 || wd == 4 || wd == 6) { michael@0: // If the 1st is on Sun, Wed, or Fri, postpone to the next day michael@0: day += 1; michael@0: wd = (day % 7); michael@0: } michael@0: if (wd == 1 && frac > 15*HOUR_PARTS+204 && !isLeapYear(year) ) { michael@0: // If the new moon falls after 3:11:20am (15h204p from the previous noon) michael@0: // on a Tuesday and it is not a leap year, postpone by 2 days. michael@0: // This prevents 356-day years. michael@0: day += 2; michael@0: } michael@0: else if (wd == 0 && frac > 21*HOUR_PARTS+589 && isLeapYear(year-1) ) { michael@0: // If the new moon falls after 9:32:43 1/3am (21h589p from yesterday noon) michael@0: // on a Monday and *last* year was a leap year, postpone by 1 day. michael@0: // Prevents 382-day years. michael@0: day += 1; michael@0: } michael@0: CalendarCache::put(&gCache, year, day, status); michael@0: } michael@0: return day; michael@0: } michael@0: michael@0: /** michael@0: * Find the day of the week for a given day michael@0: * michael@0: * @param day The # of days since the start of the Hebrew calendar, michael@0: * 1-based (i.e. 1/1/1 AM is day 1). michael@0: */ michael@0: int32_t HebrewCalendar::absoluteDayToDayOfWeek(int32_t day) michael@0: { michael@0: // We know that 1/1/1 AM is a Monday, which makes the math easy... michael@0: return (day % 7) + 1; michael@0: } michael@0: michael@0: /** michael@0: * Returns the the type of a given year. michael@0: * 0 "Deficient" year with 353 or 383 days michael@0: * 1 "Normal" year with 354 or 384 days michael@0: * 2 "Complete" year with 355 or 385 days michael@0: */ michael@0: int32_t HebrewCalendar::yearType(int32_t year) const michael@0: { michael@0: int32_t yearLength = handleGetYearLength(year); michael@0: michael@0: if (yearLength > 380) { michael@0: yearLength -= 30; // Subtract length of leap month. michael@0: } michael@0: michael@0: int type = 0; michael@0: michael@0: switch (yearLength) { michael@0: case 353: michael@0: type = 0; break; michael@0: case 354: michael@0: type = 1; break; michael@0: case 355: michael@0: type = 2; break; michael@0: default: michael@0: //throw new RuntimeException("Illegal year length " + yearLength + " in year " + year); michael@0: type = 1; michael@0: } michael@0: return type; michael@0: } michael@0: michael@0: /** michael@0: * Determine whether a given Hebrew year is a leap year michael@0: * michael@0: * The rule here is that if (year % 19) == 0, 3, 6, 8, 11, 14, or 17. michael@0: * The formula below performs the same test, believe it or not. michael@0: */ michael@0: UBool HebrewCalendar::isLeapYear(int32_t year) { michael@0: //return (year * 12 + 17) % 19 >= 12; michael@0: int32_t x = (year*12 + 17) % 19; michael@0: return x >= ((x < 0) ? -7 : 12); michael@0: } michael@0: michael@0: int32_t HebrewCalendar::monthsInYear(int32_t year) { michael@0: return isLeapYear(year) ? 13 : 12; michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Calendar framework michael@0: //------------------------------------------------------------------------- michael@0: michael@0: /** michael@0: * @internal michael@0: */ michael@0: int32_t HebrewCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const { michael@0: return LIMITS[field][limitType]; michael@0: } michael@0: michael@0: /** michael@0: * Returns the length of the given month in the given year michael@0: * @internal michael@0: */ michael@0: int32_t HebrewCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const { michael@0: // Resolve out-of-range months. This is necessary in order to michael@0: // obtain the correct year. We correct to michael@0: // a 12- or 13-month year (add/subtract 12 or 13, depending michael@0: // on the year) but since we _always_ number from 0..12, and michael@0: // the leap year determines whether or not month 5 (Adar 1) michael@0: // is present, we allow 0..12 in any given year. michael@0: while (month < 0) { michael@0: month += monthsInYear(--extendedYear); michael@0: } michael@0: // Careful: allow 0..12 in all years michael@0: while (month > 12) { michael@0: month -= monthsInYear(extendedYear++); michael@0: } michael@0: michael@0: switch (month) { michael@0: case HESHVAN: michael@0: case KISLEV: michael@0: // These two month lengths can vary michael@0: return MONTH_LENGTH[month][yearType(extendedYear)]; michael@0: michael@0: default: michael@0: // The rest are a fixed length michael@0: return MONTH_LENGTH[month][0]; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Returns the number of days in the given Hebrew year michael@0: * @internal michael@0: */ michael@0: int32_t HebrewCalendar::handleGetYearLength(int32_t eyear) const { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: return startOfYear(eyear+1, status) - startOfYear(eyear, status); michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Functions for converting from milliseconds to field values michael@0: //------------------------------------------------------------------------- michael@0: michael@0: /** michael@0: * Subclasses may override this method to compute several fields michael@0: * specific to each calendar system. These are: michael@0: * michael@0: * michael@0: * michael@0: * Subclasses can refer to the DAY_OF_WEEK and DOW_LOCAL fields, michael@0: * which will be set when this method is called. Subclasses can michael@0: * also call the getGregorianXxx() methods to obtain Gregorian michael@0: * calendar equivalents for the given Julian day. michael@0: * michael@0: *

In addition, subclasses should compute any subclass-specific michael@0: * fields, that is, fields from BASE_FIELD_COUNT to michael@0: * getFieldCount() - 1. michael@0: * @internal michael@0: */ michael@0: void HebrewCalendar::handleComputeFields(int32_t julianDay, UErrorCode &status) { michael@0: int32_t d = julianDay - 347997; michael@0: double m = ((d * (double)DAY_PARTS)/ (double) MONTH_PARTS); // Months (approx) michael@0: int32_t year = (int32_t)( ((19. * m + 234.) / 235.) + 1.); // Years (approx) michael@0: int32_t ys = startOfYear(year, status); // 1st day of year michael@0: int32_t dayOfYear = (d - ys); michael@0: michael@0: // Because of the postponement rules, it's possible to guess wrong. Fix it. michael@0: while (dayOfYear < 1) { michael@0: year--; michael@0: ys = startOfYear(year, status); michael@0: dayOfYear = (d - ys); michael@0: } michael@0: michael@0: // Now figure out which month we're in, and the date within that month michael@0: int32_t type = yearType(year); michael@0: UBool isLeap = isLeapYear(year); michael@0: michael@0: int32_t month = 0; michael@0: int32_t momax = sizeof(MONTH_START) / (3 * sizeof(MONTH_START[0][0])); michael@0: while (month < momax && dayOfYear > ( isLeap ? LEAP_MONTH_START[month][type] : MONTH_START[month][type] ) ) { michael@0: month++; michael@0: } michael@0: if (month >= momax || month<=0) { michael@0: // TODO: I found dayOfYear could be out of range when michael@0: // a large value is set to julianDay. I patched startOfYear michael@0: // to reduce the chace, but it could be still reproduced either michael@0: // by startOfYear or other places. For now, we check michael@0: // the month is in valid range to avoid out of array index michael@0: // access problem here. However, we need to carefully review michael@0: // the calendar implementation to check the extreme limit of michael@0: // each calendar field and the code works well for any values michael@0: // in the valid value range. -yoshito michael@0: status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return; michael@0: } michael@0: month--; michael@0: int dayOfMonth = dayOfYear - (isLeap ? LEAP_MONTH_START[month][type] : MONTH_START[month][type]); michael@0: michael@0: internalSet(UCAL_ERA, 0); michael@0: internalSet(UCAL_YEAR, year); michael@0: internalSet(UCAL_EXTENDED_YEAR, year); michael@0: internalSet(UCAL_MONTH, month); michael@0: internalSet(UCAL_DAY_OF_MONTH, dayOfMonth); michael@0: internalSet(UCAL_DAY_OF_YEAR, dayOfYear); michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Functions for converting from field values to milliseconds michael@0: //------------------------------------------------------------------------- michael@0: michael@0: /** michael@0: * @internal michael@0: */ michael@0: int32_t HebrewCalendar::handleGetExtendedYear() { michael@0: int32_t year; michael@0: if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { michael@0: year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1 michael@0: } else { michael@0: year = internalGet(UCAL_YEAR, 1); // Default to year 1 michael@0: } michael@0: return year; michael@0: } michael@0: michael@0: /** michael@0: * Return JD of start of given month/year. michael@0: * @internal michael@0: */ michael@0: int32_t HebrewCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /*useMonth*/) const { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: // Resolve out-of-range months. This is necessary in order to michael@0: // obtain the correct year. We correct to michael@0: // a 12- or 13-month year (add/subtract 12 or 13, depending michael@0: // on the year) but since we _always_ number from 0..12, and michael@0: // the leap year determines whether or not month 5 (Adar 1) michael@0: // is present, we allow 0..12 in any given year. michael@0: while (month < 0) { michael@0: month += monthsInYear(--eyear); michael@0: } michael@0: // Careful: allow 0..12 in all years michael@0: while (month > 12) { michael@0: month -= monthsInYear(eyear++); michael@0: } michael@0: michael@0: int32_t day = startOfYear(eyear, status); michael@0: michael@0: if(U_FAILURE(status)) { michael@0: return 0; michael@0: } michael@0: michael@0: if (month != 0) { michael@0: if (isLeapYear(eyear)) { michael@0: day += LEAP_MONTH_START[month][yearType(eyear)]; michael@0: } else { michael@0: day += MONTH_START[month][yearType(eyear)]; michael@0: } michael@0: } michael@0: michael@0: return (int) (day + 347997); michael@0: } michael@0: michael@0: UBool michael@0: HebrewCalendar::inDaylightTime(UErrorCode& status) const michael@0: { michael@0: // copied from GregorianCalendar michael@0: if (U_FAILURE(status) || !getTimeZone().useDaylightTime()) michael@0: return FALSE; michael@0: michael@0: // Force an update of the state of the Calendar. michael@0: ((HebrewCalendar*)this)->complete(status); // cast away const michael@0: michael@0: return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE); michael@0: } michael@0: michael@0: /** michael@0: * The system maintains a static default century start date and Year. They are michael@0: * initialized the first time they are used. Once the system default century date michael@0: * and year are set, they do not change. michael@0: */ michael@0: static UDate gSystemDefaultCenturyStart = DBL_MIN; michael@0: static int32_t gSystemDefaultCenturyStartYear = -1; michael@0: static icu::UInitOnce gSystemDefaultCenturyInit = U_INITONCE_INITIALIZER; michael@0: michael@0: UBool HebrewCalendar::haveDefaultCentury() const michael@0: { michael@0: return TRUE; michael@0: } michael@0: michael@0: static void U_CALLCONV initializeSystemDefaultCentury() michael@0: { michael@0: // initialize systemDefaultCentury and systemDefaultCenturyYear based michael@0: // on the current time. They'll be set to 80 years before michael@0: // the current time. michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: HebrewCalendar calendar(Locale("@calendar=hebrew"),status); michael@0: if (U_SUCCESS(status)) { michael@0: calendar.setTime(Calendar::getNow(), status); michael@0: calendar.add(UCAL_YEAR, -80, status); michael@0: michael@0: gSystemDefaultCenturyStart = calendar.getTime(status); michael@0: gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status); michael@0: } michael@0: // We have no recourse upon failure unless we want to propagate the failure michael@0: // out. michael@0: } michael@0: michael@0: michael@0: UDate HebrewCalendar::defaultCenturyStart() const { michael@0: // lazy-evaluate systemDefaultCenturyStart michael@0: umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); michael@0: return gSystemDefaultCenturyStart; michael@0: } michael@0: michael@0: int32_t HebrewCalendar::defaultCenturyStartYear() const { michael@0: // lazy-evaluate systemDefaultCenturyStartYear michael@0: umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); michael@0: return gSystemDefaultCenturyStartYear; michael@0: } michael@0: michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(HebrewCalendar) michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif // UCONFIG_NO_FORMATTING michael@0: