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