| 1 | // © 2016 and later: Unicode, Inc. and others. |
| 2 | // License & terms of use: http://www.unicode.org/copyright.html |
| 3 | /* |
| 4 | ****************************************************************************** |
| 5 | * Copyright (C) 2007-2014, International Business Machines Corporation |
| 6 | * and others. All Rights Reserved. |
| 7 | ****************************************************************************** |
| 8 | * |
| 9 | * File CHNSECAL.CPP |
| 10 | * |
| 11 | * Modification History: |
| 12 | * |
| 13 | * Date Name Description |
| 14 | * 9/18/2007 ajmacher ported from java ChineseCalendar |
| 15 | ***************************************************************************** |
| 16 | */ |
| 17 | |
| 18 | #include "chnsecal.h" |
| 19 | |
| 20 | #include <cstdint> |
| 21 | |
| 22 | #if !UCONFIG_NO_FORMATTING |
| 23 | |
| 24 | #include "umutex.h" |
| 25 | #include <float.h> |
| 26 | #include "gregoimp.h" // Math |
| 27 | #include "astro.h" // CalendarAstronomer and CalendarCache |
| 28 | #include "unicode/simpletz.h" |
| 29 | #include "uhash.h" |
| 30 | #include "ucln_in.h" |
| 31 | #include "cstring.h" |
| 32 | |
| 33 | // Debugging |
| 34 | #ifdef U_DEBUG_CHNSECAL |
| 35 | # include <stdio.h> |
| 36 | # include <stdarg.h> |
| 37 | static void debug_chnsecal_loc(const char *f, int32_t l) |
| 38 | { |
| 39 | fprintf(stderr, "%s:%d: ", f, l); |
| 40 | } |
| 41 | |
| 42 | static void debug_chnsecal_msg(const char *pat, ...) |
| 43 | { |
| 44 | va_list ap; |
| 45 | va_start(ap, pat); |
| 46 | vfprintf(stderr, pat, ap); |
| 47 | fflush(stderr); |
| 48 | } |
| 49 | // must use double parens, i.e.: U_DEBUG_CHNSECAL_MSG(("four is: %d",4)); |
| 50 | #define U_DEBUG_CHNSECAL_MSG(x) {debug_chnsecal_loc(__FILE__,__LINE__);debug_chnsecal_msg x;} |
| 51 | #else |
| 52 | #define U_DEBUG_CHNSECAL_MSG(x) |
| 53 | #endif |
| 54 | |
| 55 | |
| 56 | // Lazy Creation & Access synchronized by class CalendarCache with a mutex. |
| 57 | static icu::CalendarCache *gWinterSolsticeCache = nullptr; |
| 58 | static icu::CalendarCache *gNewYearCache = nullptr; |
| 59 | |
| 60 | static icu::TimeZone *gAstronomerTimeZone = nullptr; |
| 61 | static icu::UInitOnce gAstronomerTimeZoneInitOnce {}; |
| 62 | |
| 63 | /** |
| 64 | * The start year of the Chinese calendar, the 61st year of the reign |
| 65 | * of Huang Di. Some sources use the first year of his reign, |
| 66 | * resulting in EXTENDED_YEAR values 60 years greater and ERA (cycle) |
| 67 | * values one greater. |
| 68 | */ |
| 69 | static const int32_t CHINESE_EPOCH_YEAR = -2636; // Gregorian year |
| 70 | |
| 71 | /** |
| 72 | * The offset from GMT in milliseconds at which we perform astronomical |
| 73 | * computations. Some sources use a different historically accurate |
| 74 | * offset of GMT+7:45:40 for years before 1929; we do not do this. |
| 75 | */ |
| 76 | static const int32_t CHINA_OFFSET = 8 * kOneHour; |
| 77 | |
| 78 | /** |
| 79 | * Value to be added or subtracted from the local days of a new moon to |
| 80 | * get close to the next or prior new moon, but not cross it. Must be |
| 81 | * >= 1 and < CalendarAstronomer.SYNODIC_MONTH. |
| 82 | */ |
| 83 | static const int32_t SYNODIC_GAP = 25; |
| 84 | |
| 85 | |
| 86 | U_CDECL_BEGIN |
| 87 | static UBool calendar_chinese_cleanup() { |
| 88 | if (gWinterSolsticeCache) { |
| 89 | delete gWinterSolsticeCache; |
| 90 | gWinterSolsticeCache = nullptr; |
| 91 | } |
| 92 | if (gNewYearCache) { |
| 93 | delete gNewYearCache; |
| 94 | gNewYearCache = nullptr; |
| 95 | } |
| 96 | if (gAstronomerTimeZone) { |
| 97 | delete gAstronomerTimeZone; |
| 98 | gAstronomerTimeZone = nullptr; |
| 99 | } |
| 100 | gAstronomerTimeZoneInitOnce.reset(); |
| 101 | return true; |
| 102 | } |
| 103 | U_CDECL_END |
| 104 | |
| 105 | U_NAMESPACE_BEGIN |
| 106 | |
| 107 | |
| 108 | // Implementation of the ChineseCalendar class |
| 109 | |
| 110 | |
| 111 | //------------------------------------------------------------------------- |
| 112 | // Constructors... |
| 113 | //------------------------------------------------------------------------- |
| 114 | |
| 115 | |
| 116 | namespace { |
| 117 | |
| 118 | const TimeZone* getAstronomerTimeZone(); |
| 119 | int32_t newMoonNear(const TimeZone*, double, UBool, UErrorCode&); |
| 120 | int32_t newYear(const icu::ChineseCalendar::Setting&, int32_t, UErrorCode&); |
| 121 | UBool isLeapMonthBetween(const TimeZone*, int32_t, int32_t, UErrorCode&); |
| 122 | |
| 123 | } // namespace |
| 124 | |
| 125 | ChineseCalendar* ChineseCalendar::clone() const { |
| 126 | return new ChineseCalendar(*this); |
| 127 | } |
| 128 | |
| 129 | ChineseCalendar::ChineseCalendar(const Locale& aLocale, UErrorCode& success) |
| 130 | : Calendar(TimeZone::forLocaleOrDefault(aLocale), aLocale, success), |
| 131 | hasLeapMonthBetweenWinterSolstices(false) |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | ChineseCalendar::ChineseCalendar(const ChineseCalendar& other) : Calendar(other) { |
| 136 | hasLeapMonthBetweenWinterSolstices = other.hasLeapMonthBetweenWinterSolstices; |
| 137 | } |
| 138 | |
| 139 | ChineseCalendar::~ChineseCalendar() |
| 140 | { |
| 141 | } |
| 142 | |
| 143 | const char *ChineseCalendar::getType() const { |
| 144 | return "chinese"; |
| 145 | } |
| 146 | |
| 147 | namespace { // anonymous |
| 148 | |
| 149 | static void U_CALLCONV initAstronomerTimeZone() { |
| 150 | gAstronomerTimeZone = new SimpleTimeZone(CHINA_OFFSET, UNICODE_STRING_SIMPLE("CHINA_ZONE") ); |
| 151 | ucln_i18n_registerCleanup(UCLN_I18N_CHINESE_CALENDAR, calendar_chinese_cleanup); |
| 152 | } |
| 153 | |
| 154 | const TimeZone* getAstronomerTimeZone() { |
| 155 | umtx_initOnce(gAstronomerTimeZoneInitOnce, &initAstronomerTimeZone); |
| 156 | return gAstronomerTimeZone; |
| 157 | } |
| 158 | |
| 159 | } // namespace anonymous |
| 160 | |
| 161 | //------------------------------------------------------------------------- |
| 162 | // Minimum / Maximum access functions |
| 163 | //------------------------------------------------------------------------- |
| 164 | |
| 165 | |
| 166 | static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = { |
| 167 | // Minimum Greatest Least Maximum |
| 168 | // Minimum Maximum |
| 169 | { 1, 1, 83333, 83333}, // ERA |
| 170 | { 1, 1, 60, 60}, // YEAR |
| 171 | { 0, 0, 11, 11}, // MONTH |
| 172 | { 1, 1, 50, 55}, // WEEK_OF_YEAR |
| 173 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH |
| 174 | { 1, 1, 29, 30}, // DAY_OF_MONTH |
| 175 | { 1, 1, 353, 385}, // DAY_OF_YEAR |
| 176 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK |
| 177 | { -1, -1, 5, 5}, // DAY_OF_WEEK_IN_MONTH |
| 178 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM |
| 179 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR |
| 180 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY |
| 181 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE |
| 182 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND |
| 183 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND |
| 184 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET |
| 185 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET |
| 186 | { -5000000, -5000000, 5000000, 5000000}, // YEAR_WOY |
| 187 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL |
| 188 | { -5000000, -5000000, 5000000, 5000000}, // EXTENDED_YEAR |
| 189 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY |
| 190 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY |
| 191 | { 0, 0, 1, 1}, // IS_LEAP_MONTH |
| 192 | { 0, 0, 11, 12}, // ORDINAL_MONTH |
| 193 | }; |
| 194 | |
| 195 | |
| 196 | /** |
| 197 | * @draft ICU 2.4 |
| 198 | */ |
| 199 | int32_t ChineseCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const { |
| 200 | return LIMITS[field][limitType]; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | //---------------------------------------------------------------------- |
| 205 | // Calendar framework |
| 206 | //---------------------------------------------------------------------- |
| 207 | |
| 208 | /** |
| 209 | * Implement abstract Calendar method to return the extended year |
| 210 | * defined by the current fields. This will use either the ERA and |
| 211 | * YEAR field as the cycle and year-of-cycle, or the EXTENDED_YEAR |
| 212 | * field as the continuous year count, depending on which is newer. |
| 213 | * @stable ICU 2.8 |
| 214 | */ |
| 215 | int32_t ChineseCalendar::handleGetExtendedYear(UErrorCode& status) { |
| 216 | if (U_FAILURE(status)) { |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | int32_t year; |
| 221 | // if UCAL_EXTENDED_YEAR is not older than UCAL_ERA nor UCAL_YEAR |
| 222 | if (newerField(UCAL_EXTENDED_YEAR, newerField(UCAL_ERA, UCAL_YEAR)) == |
| 223 | UCAL_EXTENDED_YEAR) { |
| 224 | year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1 |
| 225 | } else { |
| 226 | // adjust to the instance specific epoch |
| 227 | int32_t cycle = internalGet(UCAL_ERA, 1); |
| 228 | year = internalGet(UCAL_YEAR, 1); |
| 229 | const Setting setting = getSetting(status); |
| 230 | if (U_FAILURE(status)) { |
| 231 | return 0; |
| 232 | } |
| 233 | // Handle int32 overflow calculation for |
| 234 | // year = year + (cycle-1) * 60 -(fEpochYear - CHINESE_EPOCH_YEAR) |
| 235 | if (uprv_add32_overflow(cycle, -1, &cycle) || // 0-based cycle |
| 236 | uprv_mul32_overflow(cycle, 60, &cycle) || |
| 237 | uprv_add32_overflow(year, cycle, &year) || |
| 238 | uprv_add32_overflow(year, -(setting.epochYear-CHINESE_EPOCH_YEAR), |
| 239 | &year)) { |
| 240 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 241 | return 0; |
| 242 | } |
| 243 | } |
| 244 | return year; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Override Calendar method to return the number of days in the given |
| 249 | * extended year and month. |
| 250 | * |
| 251 | * <p>Note: This method also reads the IS_LEAP_MONTH field to determine |
| 252 | * whether or not the given month is a leap month. |
| 253 | * @stable ICU 2.8 |
| 254 | */ |
| 255 | int32_t ChineseCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month, UErrorCode& status) const { |
| 256 | bool isLeapMonth = internalGet(UCAL_IS_LEAP_MONTH) == 1; |
| 257 | return handleGetMonthLengthWithLeap(extendedYear, month, isLeapMonth, status); |
| 258 | } |
| 259 | |
| 260 | int32_t ChineseCalendar::handleGetMonthLengthWithLeap(int32_t extendedYear, int32_t month, bool leap, UErrorCode& status) const { |
| 261 | const Setting setting = getSetting(status); |
| 262 | if (U_FAILURE(status)) { |
| 263 | return 0; |
| 264 | } |
| 265 | int32_t thisStart = handleComputeMonthStartWithLeap(extendedYear, month, leap, status); |
| 266 | if (U_FAILURE(status)) { |
| 267 | return 0; |
| 268 | } |
| 269 | thisStart = thisStart - |
| 270 | kEpochStartAsJulianDay + 1; // Julian day -> local days |
| 271 | int32_t nextStart = newMoonNear(setting.zoneAstroCalc, thisStart + SYNODIC_GAP, true, status); |
| 272 | return nextStart - thisStart; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Field resolution table that incorporates IS_LEAP_MONTH. |
| 277 | */ |
| 278 | const UFieldResolutionTable ChineseCalendar::CHINESE_DATE_PRECEDENCE[] = |
| 279 | { |
| 280 | { |
| 281 | { UCAL_DAY_OF_MONTH, kResolveSTOP }, |
| 282 | { UCAL_WEEK_OF_YEAR, UCAL_DAY_OF_WEEK, kResolveSTOP }, |
| 283 | { UCAL_WEEK_OF_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP }, |
| 284 | { UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP }, |
| 285 | { UCAL_WEEK_OF_YEAR, UCAL_DOW_LOCAL, kResolveSTOP }, |
| 286 | { UCAL_WEEK_OF_MONTH, UCAL_DOW_LOCAL, kResolveSTOP }, |
| 287 | { UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DOW_LOCAL, kResolveSTOP }, |
| 288 | { UCAL_DAY_OF_YEAR, kResolveSTOP }, |
| 289 | { kResolveRemap | UCAL_DAY_OF_MONTH, UCAL_IS_LEAP_MONTH, kResolveSTOP }, |
| 290 | { kResolveSTOP } |
| 291 | }, |
| 292 | { |
| 293 | { UCAL_WEEK_OF_YEAR, kResolveSTOP }, |
| 294 | { UCAL_WEEK_OF_MONTH, kResolveSTOP }, |
| 295 | { UCAL_DAY_OF_WEEK_IN_MONTH, kResolveSTOP }, |
| 296 | { kResolveRemap | UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP }, |
| 297 | { kResolveRemap | UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DOW_LOCAL, kResolveSTOP }, |
| 298 | { kResolveSTOP } |
| 299 | }, |
| 300 | {{kResolveSTOP}} |
| 301 | }; |
| 302 | |
| 303 | /** |
| 304 | * Override Calendar to add IS_LEAP_MONTH to the field resolution |
| 305 | * table. |
| 306 | * @stable ICU 2.8 |
| 307 | */ |
| 308 | const UFieldResolutionTable* ChineseCalendar::getFieldResolutionTable() const { |
| 309 | return CHINESE_DATE_PRECEDENCE; |
| 310 | } |
| 311 | |
| 312 | namespace { |
| 313 | |
| 314 | struct MonthInfo { |
| 315 | int32_t month; |
| 316 | int32_t ordinalMonth; |
| 317 | int32_t thisMoon; |
| 318 | bool isLeapMonth; |
| 319 | bool hasLeapMonthBetweenWinterSolstices; |
| 320 | }; |
| 321 | struct MonthInfo computeMonthInfo( |
| 322 | const icu::ChineseCalendar::Setting& setting, |
| 323 | int32_t gyear, int32_t days, UErrorCode& status); |
| 324 | |
| 325 | } // namespace |
| 326 | |
| 327 | /** |
| 328 | * Return the Julian day number of day before the first day of the |
| 329 | * given month in the given extended year. |
| 330 | * |
| 331 | * <p>Note: This method reads the IS_LEAP_MONTH field to determine |
| 332 | * whether the given month is a leap month. |
| 333 | * @param eyear the extended year |
| 334 | * @param month the zero-based month. The month is also determined |
| 335 | * by reading the IS_LEAP_MONTH field. |
| 336 | * @return the Julian day number of the day before the first |
| 337 | * day of the given month and year |
| 338 | * @stable ICU 2.8 |
| 339 | */ |
| 340 | int64_t ChineseCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool useMonth, UErrorCode& status) const { |
| 341 | bool isLeapMonth = false; |
| 342 | if (useMonth) { |
| 343 | isLeapMonth = internalGet(UCAL_IS_LEAP_MONTH) != 0; |
| 344 | } |
| 345 | return handleComputeMonthStartWithLeap(eyear, month, isLeapMonth, status); |
| 346 | } |
| 347 | |
| 348 | int64_t ChineseCalendar::handleComputeMonthStartWithLeap(int32_t eyear, int32_t month, bool isLeapMonth, UErrorCode& status) const { |
| 349 | if (U_FAILURE(status)) { |
| 350 | return 0; |
| 351 | } |
| 352 | // If the month is out of range, adjust it into range, and |
| 353 | // modify the extended year value accordingly. |
| 354 | if (month < 0 || month > 11) { |
| 355 | if (uprv_add32_overflow(eyear, ClockMath::floorDivide(month, 12, &month), &eyear)) { |
| 356 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 357 | return 0; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | const Setting setting = getSetting(status); |
| 362 | if (U_FAILURE(status)) { |
| 363 | return 0; |
| 364 | } |
| 365 | int32_t gyear; |
| 366 | if (uprv_add32_overflow(eyear, setting.epochYear - 1, &gyear)) { |
| 367 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | int32_t theNewYear = newYear(setting, gyear, status); |
| 372 | int32_t newMoon = newMoonNear(setting.zoneAstroCalc, theNewYear + month * 29, true, status); |
| 373 | if (U_FAILURE(status)) { |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | int32_t newMonthYear = Grego::dayToYear(newMoon, status); |
| 378 | |
| 379 | struct MonthInfo monthInfo = computeMonthInfo(setting, newMonthYear, newMoon, status); |
| 380 | if (U_FAILURE(status)) { |
| 381 | return 0; |
| 382 | } |
| 383 | if (month != monthInfo.month-1 || isLeapMonth != monthInfo.isLeapMonth) { |
| 384 | newMoon = newMoonNear(setting.zoneAstroCalc, newMoon + SYNODIC_GAP, true, status); |
| 385 | if (U_FAILURE(status)) { |
| 386 | return 0; |
| 387 | } |
| 388 | } |
| 389 | int32_t julianDay; |
| 390 | if (uprv_add32_overflow(newMoon-1, kEpochStartAsJulianDay, &julianDay)) { |
| 391 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | return julianDay; |
| 396 | } |
| 397 | |
| 398 | |
| 399 | /** |
| 400 | * Override Calendar to handle leap months properly. |
| 401 | * @stable ICU 2.8 |
| 402 | */ |
| 403 | void ChineseCalendar::add(UCalendarDateFields field, int32_t amount, UErrorCode& status) { |
| 404 | switch (field) { |
| 405 | case UCAL_MONTH: |
| 406 | case UCAL_ORDINAL_MONTH: |
| 407 | if (amount != 0) { |
| 408 | int32_t dom = get(UCAL_DAY_OF_MONTH, status); |
| 409 | if (U_FAILURE(status)) break; |
| 410 | int32_t day = get(UCAL_JULIAN_DAY, status) - kEpochStartAsJulianDay; // Get local day |
| 411 | if (U_FAILURE(status)) break; |
| 412 | int32_t moon = day - dom + 1; // New moon |
| 413 | offsetMonth(moon, dom, amount, status); |
| 414 | } |
| 415 | break; |
| 416 | default: |
| 417 | Calendar::add(field, amount, status); |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Override Calendar to handle leap months properly. |
| 424 | * @stable ICU 2.8 |
| 425 | */ |
| 426 | void ChineseCalendar::add(EDateFields field, int32_t amount, UErrorCode& status) { |
| 427 | add(static_cast<UCalendarDateFields>(field), amount, status); |
| 428 | } |
| 429 | |
| 430 | namespace { |
| 431 | |
| 432 | struct RollMonthInfo { |
| 433 | int32_t month; |
| 434 | int32_t newMoon; |
| 435 | int32_t thisMoon; |
| 436 | }; |
| 437 | |
| 438 | struct RollMonthInfo rollMonth(const TimeZone* timeZone, int32_t amount, int32_t day, int32_t month, int32_t dayOfMonth, |
| 439 | bool isLeapMonth, bool hasLeapMonthBetweenWinterSolstices, |
| 440 | UErrorCode& status) { |
| 441 | struct RollMonthInfo output = {0, 0, 0}; |
| 442 | if (U_FAILURE(status)) { |
| 443 | return output; |
| 444 | } |
| 445 | |
| 446 | output.thisMoon = day - dayOfMonth + 1; // New moon (start of this month) |
| 447 | |
| 448 | // Note throughout the following: Months 12 and 1 are never |
| 449 | // followed by a leap month (D&R p. 185). |
| 450 | |
| 451 | // Compute the adjusted month number m. This is zero-based |
| 452 | // value from 0..11 in a non-leap year, and from 0..12 in a |
| 453 | // leap year. |
| 454 | if (hasLeapMonthBetweenWinterSolstices) { // (member variable) |
| 455 | if (isLeapMonth) { |
| 456 | ++month; |
| 457 | } else { |
| 458 | // Check for a prior leap month. (In the |
| 459 | // following, month 0 is the first month of the |
| 460 | // year.) Month 0 is never followed by a leap |
| 461 | // month, and we know month m is not a leap month. |
| 462 | // moon1 will be the start of month 0 if there is |
| 463 | // no leap month between month 0 and month m; |
| 464 | // otherwise it will be the start of month 1. |
| 465 | int prevMoon = output.thisMoon - |
| 466 | static_cast<int>(CalendarAstronomer::SYNODIC_MONTH * (month - 0.5)); |
| 467 | prevMoon = newMoonNear(timeZone, prevMoon, true, status); |
| 468 | if (U_FAILURE(status)) { |
| 469 | return output; |
| 470 | } |
| 471 | if (isLeapMonthBetween(timeZone, prevMoon, output.thisMoon, status)) { |
| 472 | ++month; |
| 473 | } |
| 474 | if (U_FAILURE(status)) { |
| 475 | return output; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | // Now do the standard roll computation on month, with the |
| 480 | // allowed range of 0..n-1, where n is 12 or 13. |
| 481 | int32_t numberOfMonths = hasLeapMonthBetweenWinterSolstices ? 13 : 12; // Months in this year |
| 482 | if (uprv_add32_overflow(amount, month, &amount)) { |
| 483 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 484 | return output; |
| 485 | } |
| 486 | output.newMoon = amount % numberOfMonths; |
| 487 | if (output.newMoon < 0) { |
| 488 | output.newMoon += numberOfMonths; |
| 489 | } |
| 490 | output.month = month; |
| 491 | return output; |
| 492 | } |
| 493 | |
| 494 | } // namespace |
| 495 | |
| 496 | /** |
| 497 | * Override Calendar to handle leap months properly. |
| 498 | * @stable ICU 2.8 |
| 499 | */ |
| 500 | void ChineseCalendar::roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) { |
| 501 | switch (field) { |
| 502 | case UCAL_MONTH: |
| 503 | case UCAL_ORDINAL_MONTH: |
| 504 | if (amount != 0) { |
| 505 | const Setting setting = getSetting(status); |
| 506 | int32_t day = get(UCAL_JULIAN_DAY, status) - kEpochStartAsJulianDay; // Get local day |
| 507 | int32_t month = get(UCAL_MONTH, status); // 0-based month |
| 508 | int32_t dayOfMonth = get(UCAL_DAY_OF_MONTH, status); |
| 509 | bool isLeapMonth = get(UCAL_IS_LEAP_MONTH, status) == 1; |
| 510 | if (U_FAILURE(status)) break; |
| 511 | struct RollMonthInfo r = rollMonth( |
| 512 | setting.zoneAstroCalc, amount, day, month, dayOfMonth, isLeapMonth, |
| 513 | hasLeapMonthBetweenWinterSolstices, status); |
| 514 | if (U_FAILURE(status)) break; |
| 515 | if (r.newMoon != r.month) { |
| 516 | offsetMonth(r.thisMoon, dayOfMonth, r.newMoon - r.month, status); |
| 517 | } |
| 518 | } |
| 519 | break; |
| 520 | default: |
| 521 | Calendar::roll(field, amount, status); |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | void ChineseCalendar::roll(EDateFields field, int32_t amount, UErrorCode& status) { |
| 527 | roll(static_cast<UCalendarDateFields>(field), amount, status); |
| 528 | } |
| 529 | |
| 530 | |
| 531 | //------------------------------------------------------------------ |
| 532 | // Support methods and constants |
| 533 | //------------------------------------------------------------------ |
| 534 | |
| 535 | namespace { |
| 536 | /** |
| 537 | * Convert local days to UTC epoch milliseconds. |
| 538 | * This is not an accurate conversion in that getTimezoneOffset |
| 539 | * takes the milliseconds in GMT (not local time). In theory, more |
| 540 | * accurate algorithm can be implemented but practically we do not need |
| 541 | * to go through that complication as long as the historical timezone |
| 542 | * changes did not happen around the 'tricky' new moon (new moon around |
| 543 | * midnight). |
| 544 | * |
| 545 | * @param timeZone time zone for the Astro calculation. |
| 546 | * @param days days after January 1, 1970 0:00 in the astronomical base zone |
| 547 | * @return milliseconds after January 1, 1970 0:00 GMT |
| 548 | */ |
| 549 | double daysToMillis(const TimeZone* timeZone, double days, UErrorCode& status) { |
| 550 | if (U_FAILURE(status)) { |
| 551 | return 0; |
| 552 | } |
| 553 | double millis = days * kOneDay; |
| 554 | if (timeZone != nullptr) { |
| 555 | int32_t rawOffset, dstOffset; |
| 556 | timeZone->getOffset(millis, false, rawOffset, dstOffset, status); |
| 557 | if (U_FAILURE(status)) { |
| 558 | return 0; |
| 559 | } |
| 560 | return millis - static_cast<double>(rawOffset + dstOffset); |
| 561 | } |
| 562 | return millis - static_cast<double>(CHINA_OFFSET); |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Convert UTC epoch milliseconds to local days. |
| 567 | * @param timeZone time zone for the Astro calculation. |
| 568 | * @param millis milliseconds after January 1, 1970 0:00 GMT |
| 569 | * @return days after January 1, 1970 0:00 in the astronomical base zone |
| 570 | */ |
| 571 | double millisToDays(const TimeZone* timeZone, double millis, UErrorCode& status) { |
| 572 | if (U_FAILURE(status)) { |
| 573 | return 0; |
| 574 | } |
| 575 | if (timeZone != nullptr) { |
| 576 | int32_t rawOffset, dstOffset; |
| 577 | timeZone->getOffset(millis, false, rawOffset, dstOffset, status); |
| 578 | if (U_FAILURE(status)) { |
| 579 | return 0; |
| 580 | } |
| 581 | return ClockMath::floorDivide(millis + static_cast<double>(rawOffset + dstOffset), kOneDay); |
| 582 | } |
| 583 | return ClockMath::floorDivide(millis + static_cast<double>(CHINA_OFFSET), kOneDay); |
| 584 | } |
| 585 | |
| 586 | //------------------------------------------------------------------ |
| 587 | // Astronomical computations |
| 588 | //------------------------------------------------------------------ |
| 589 | |
| 590 | |
| 591 | /** |
| 592 | * Return the major solar term on or after December 15 of the given |
| 593 | * Gregorian year, that is, the winter solstice of the given year. |
| 594 | * Computations are relative to Asia/Shanghai time zone. |
| 595 | * @param setting setting (time zone and caches) for the Astro calculation. |
| 596 | * @param gyear a Gregorian year |
| 597 | * @return days after January 1, 1970 0:00 Asia/Shanghai of the |
| 598 | * winter solstice of the given year |
| 599 | */ |
| 600 | int32_t winterSolstice(const icu::ChineseCalendar::Setting& setting, |
| 601 | int32_t gyear, UErrorCode& status) { |
| 602 | if (U_FAILURE(status)) { |
| 603 | return 0; |
| 604 | } |
| 605 | const TimeZone* timeZone = setting.zoneAstroCalc; |
| 606 | |
| 607 | int32_t cacheValue = CalendarCache::get(setting.winterSolsticeCache, gyear, status); |
| 608 | if (U_FAILURE(status)) { |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | if (cacheValue == 0) { |
| 613 | // In books December 15 is used, but it fails for some years |
| 614 | // using our algorithms, e.g.: 1298 1391 1492 1553 1560. That |
| 615 | // is, winterSolstice(1298) starts search at Dec 14 08:00:00 |
| 616 | // PST 1298 with a final result of Dec 14 10:31:59 PST 1299. |
| 617 | double ms = daysToMillis(timeZone, Grego::fieldsToDay(gyear, UCAL_DECEMBER, 1), status); |
| 618 | if (U_FAILURE(status)) { |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | // Winter solstice is 270 degrees solar longitude aka Dongzhi |
| 623 | double days = millisToDays(timeZone, |
| 624 | CalendarAstronomer(ms) |
| 625 | .getSunTime(CalendarAstronomer::WINTER_SOLSTICE(), true), |
| 626 | status); |
| 627 | if (U_FAILURE(status)) { |
| 628 | return 0; |
| 629 | } |
| 630 | if (days < INT32_MIN || days > INT32_MAX) { |
| 631 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 632 | return 0; |
| 633 | } |
| 634 | cacheValue = static_cast<int32_t>(days); |
| 635 | CalendarCache::put(setting.winterSolsticeCache, gyear, cacheValue, status); |
| 636 | } |
| 637 | if(U_FAILURE(status)) { |
| 638 | cacheValue = 0; |
| 639 | } |
| 640 | return cacheValue; |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * Return the closest new moon to the given date, searching either |
| 645 | * forward or backward in time. |
| 646 | * @param timeZone time zone for the Astro calculation. |
| 647 | * @param days days after January 1, 1970 0:00 Asia/Shanghai |
| 648 | * @param after if true, search for a new moon on or after the given |
| 649 | * date; otherwise, search for a new moon before it |
| 650 | * @param status |
| 651 | * @return days after January 1, 1970 0:00 Asia/Shanghai of the nearest |
| 652 | * new moon after or before <code>days</code> |
| 653 | */ |
| 654 | int32_t newMoonNear(const TimeZone* timeZone, double days, UBool after, UErrorCode& status) { |
| 655 | if (U_FAILURE(status)) { |
| 656 | return 0; |
| 657 | } |
| 658 | double ms = daysToMillis(timeZone, days, status); |
| 659 | if (U_FAILURE(status)) { |
| 660 | return 0; |
| 661 | } |
| 662 | return static_cast<int32_t>(millisToDays( |
| 663 | timeZone, |
| 664 | CalendarAstronomer(ms) |
| 665 | .getMoonTime(CalendarAstronomer::NEW_MOON(), after), |
| 666 | status)); |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * Return the nearest integer number of synodic months between |
| 671 | * two dates. |
| 672 | * @param day1 days after January 1, 1970 0:00 Asia/Shanghai |
| 673 | * @param day2 days after January 1, 1970 0:00 Asia/Shanghai |
| 674 | * @return the nearest integer number of months between day1 and day2 |
| 675 | */ |
| 676 | int32_t synodicMonthsBetween(int32_t day1, int32_t day2) { |
| 677 | double roundme = ((day2 - day1) / CalendarAstronomer::SYNODIC_MONTH); |
| 678 | return static_cast<int32_t>(roundme + (roundme >= 0 ? .5 : -.5)); |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * Return the major solar term on or before a given date. This |
| 683 | * will be an integer from 1..12, with 1 corresponding to 330 degrees, |
| 684 | * 2 to 0 degrees, 3 to 30 degrees,..., and 12 to 300 degrees. |
| 685 | * @param timeZone time zone for the Astro calculation. |
| 686 | * @param days days after January 1, 1970 0:00 Asia/Shanghai |
| 687 | */ |
| 688 | int32_t majorSolarTerm(const TimeZone* timeZone, int32_t days, UErrorCode& status) { |
| 689 | if (U_FAILURE(status)) { |
| 690 | return 0; |
| 691 | } |
| 692 | // Compute (floor(solarLongitude / (pi/6)) + 2) % 12 |
| 693 | double ms = daysToMillis(timeZone, days, status); |
| 694 | if (U_FAILURE(status)) { |
| 695 | return 0; |
| 696 | } |
| 697 | int32_t term = ((static_cast<int32_t>(6 * CalendarAstronomer(ms) |
| 698 | .getSunLongitude() / CalendarAstronomer::PI)) + 2 ) % 12; |
| 699 | if (U_FAILURE(status)) { |
| 700 | return 0; |
| 701 | } |
| 702 | if (term < 1) { |
| 703 | term += 12; |
| 704 | } |
| 705 | return term; |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * Return true if the given month lacks a major solar term. |
| 710 | * @param timeZone time zone for the Astro calculation. |
| 711 | * @param newMoon days after January 1, 1970 0:00 Asia/Shanghai of a new |
| 712 | * moon |
| 713 | */ |
| 714 | UBool hasNoMajorSolarTerm(const TimeZone* timeZone, int32_t newMoon, UErrorCode& status) { |
| 715 | if (U_FAILURE(status)) { |
| 716 | return false; |
| 717 | } |
| 718 | int32_t term1 = majorSolarTerm(timeZone, newMoon, status); |
| 719 | int32_t term2 = majorSolarTerm( |
| 720 | timeZone, newMoonNear(timeZone, newMoon + SYNODIC_GAP, true, status), status); |
| 721 | if (U_FAILURE(status)) { |
| 722 | return false; |
| 723 | } |
| 724 | return term1 == term2; |
| 725 | } |
| 726 | |
| 727 | |
| 728 | //------------------------------------------------------------------ |
| 729 | // Time to fields |
| 730 | //------------------------------------------------------------------ |
| 731 | |
| 732 | /** |
| 733 | * Return true if there is a leap month on or after month newMoon1 and |
| 734 | * at or before month newMoon2. |
| 735 | * @param timeZone time zone for the Astro calculation. |
| 736 | * @param newMoon1 days after January 1, 1970 0:00 astronomical base zone |
| 737 | * of a new moon |
| 738 | * @param newMoon2 days after January 1, 1970 0:00 astronomical base zone |
| 739 | * of a new moon |
| 740 | */ |
| 741 | UBool isLeapMonthBetween(const TimeZone* timeZone, int32_t newMoon1, int32_t newMoon2, UErrorCode& status) { |
| 742 | if (U_FAILURE(status)) { |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | #ifdef U_DEBUG_CHNSECAL |
| 747 | // This is only needed to debug the timeOfAngle divergence bug. |
| 748 | // Remove this later. Liu 11/9/00 |
| 749 | if (synodicMonthsBetween(newMoon1, newMoon2) >= 50) { |
| 750 | U_DEBUG_CHNSECAL_MSG(( |
| 751 | "isLeapMonthBetween(%d, %d): Invalid parameters", newMoon1, newMoon2 |
| 752 | )); |
| 753 | } |
| 754 | #endif |
| 755 | |
| 756 | while (newMoon2 >= newMoon1) { |
| 757 | if (hasNoMajorSolarTerm(timeZone, newMoon2, status)) { |
| 758 | return true; |
| 759 | } |
| 760 | newMoon2 = newMoonNear(timeZone, newMoon2 - SYNODIC_GAP, false, status); |
| 761 | if (U_FAILURE(status)) { |
| 762 | return false; |
| 763 | } |
| 764 | } |
| 765 | return false; |
| 766 | } |
| 767 | |
| 768 | |
| 769 | /** |
| 770 | * Compute the information about the year. |
| 771 | * @param setting setting (time zone and caches) for the Astro calculation. |
| 772 | * @param gyear the Gregorian year of the given date |
| 773 | * @param days days after January 1, 1970 0:00 astronomical base zone |
| 774 | * of the date to compute fields for |
| 775 | * @return The MonthInfo result. |
| 776 | */ |
| 777 | struct MonthInfo computeMonthInfo( |
| 778 | const icu::ChineseCalendar::Setting& setting, |
| 779 | int32_t gyear, int32_t days, UErrorCode& status) { |
| 780 | struct MonthInfo output = {0, 0, 0, false, false}; |
| 781 | if (U_FAILURE(status)) { |
| 782 | return output; |
| 783 | } |
| 784 | // Find the winter solstices before and after the target date. |
| 785 | // These define the boundaries of this Chinese year, specifically, |
| 786 | // the position of month 11, which always contains the solstice. |
| 787 | // We want solsticeBefore <= date < solsticeAfter. |
| 788 | int32_t solsticeBefore; |
| 789 | int32_t solsticeAfter = winterSolstice(setting, gyear, status); |
| 790 | if (U_FAILURE(status)) { |
| 791 | return output; |
| 792 | } |
| 793 | if (days < solsticeAfter) { |
| 794 | solsticeBefore = winterSolstice(setting, gyear - 1, status); |
| 795 | } else { |
| 796 | solsticeBefore = solsticeAfter; |
| 797 | solsticeAfter = winterSolstice(setting, gyear + 1, status); |
| 798 | } |
| 799 | if (!(solsticeBefore <= days && days < solsticeAfter)) { |
| 800 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 801 | } |
| 802 | if (U_FAILURE(status)) { |
| 803 | return output; |
| 804 | } |
| 805 | |
| 806 | const TimeZone* timeZone = setting.zoneAstroCalc; |
| 807 | // Find the start of the month after month 11. This will be either |
| 808 | // the prior month 12 or leap month 11 (very rare). Also find the |
| 809 | // start of the following month 11. |
| 810 | int32_t firstMoon = newMoonNear(timeZone, solsticeBefore + 1, true, status); |
| 811 | int32_t lastMoon = newMoonNear(timeZone, solsticeAfter + 1, false, status); |
| 812 | if (U_FAILURE(status)) { |
| 813 | return output; |
| 814 | } |
| 815 | output.thisMoon = newMoonNear(timeZone, days + 1, false, status); // Start of this month |
| 816 | if (U_FAILURE(status)) { |
| 817 | return output; |
| 818 | } |
| 819 | output.hasLeapMonthBetweenWinterSolstices = synodicMonthsBetween(firstMoon, lastMoon) == 12; |
| 820 | |
| 821 | output.month = synodicMonthsBetween(firstMoon, output.thisMoon); |
| 822 | int32_t theNewYear = newYear(setting, gyear, status); |
| 823 | if (U_FAILURE(status)) { |
| 824 | return output; |
| 825 | } |
| 826 | if (days < theNewYear) { |
| 827 | theNewYear = newYear(setting, gyear-1, status); |
| 828 | if (U_FAILURE(status)) { |
| 829 | return output; |
| 830 | } |
| 831 | } |
| 832 | if (output.hasLeapMonthBetweenWinterSolstices && |
| 833 | isLeapMonthBetween(timeZone, firstMoon, output.thisMoon, status)) { |
| 834 | output.month--; |
| 835 | } |
| 836 | if (U_FAILURE(status)) { |
| 837 | return output; |
| 838 | } |
| 839 | if (output.month < 1) { |
| 840 | output.month += 12; |
| 841 | } |
| 842 | output.ordinalMonth = synodicMonthsBetween(theNewYear, output.thisMoon); |
| 843 | if (output.ordinalMonth < 0) { |
| 844 | output.ordinalMonth += 12; |
| 845 | } |
| 846 | output.isLeapMonth = output.hasLeapMonthBetweenWinterSolstices && |
| 847 | hasNoMajorSolarTerm(timeZone, output.thisMoon, status) && |
| 848 | !isLeapMonthBetween(timeZone, firstMoon, |
| 849 | newMoonNear(timeZone, output.thisMoon - SYNODIC_GAP, false, status), |
| 850 | status); |
| 851 | if (U_FAILURE(status)) { |
| 852 | return output; |
| 853 | } |
| 854 | return output; |
| 855 | } |
| 856 | |
| 857 | } // namespace |
| 858 | |
| 859 | /** |
| 860 | * Override Calendar to compute several fields specific to the Chinese |
| 861 | * calendar system. These are: |
| 862 | * |
| 863 | * <ul><li>ERA |
| 864 | * <li>YEAR |
| 865 | * <li>MONTH |
| 866 | * <li>DAY_OF_MONTH |
| 867 | * <li>DAY_OF_YEAR |
| 868 | * <li>EXTENDED_YEAR</ul> |
| 869 | * |
| 870 | * The DAY_OF_WEEK and DOW_LOCAL fields are already set when this |
| 871 | * method is called. The getGregorianXxx() methods return Gregorian |
| 872 | * calendar equivalents for the given Julian day. |
| 873 | * |
| 874 | * <p>Compute the ChineseCalendar-specific field IS_LEAP_MONTH. |
| 875 | * @stable ICU 2.8 |
| 876 | */ |
| 877 | void ChineseCalendar::handleComputeFields(int32_t julianDay, UErrorCode & status) { |
| 878 | if (U_FAILURE(status)) { |
| 879 | return; |
| 880 | } |
| 881 | int32_t days; |
| 882 | if (uprv_add32_overflow(julianDay, -kEpochStartAsJulianDay, &days)) { |
| 883 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 884 | return; |
| 885 | } |
| 886 | int32_t gyear = getGregorianYear(); |
| 887 | int32_t gmonth = getGregorianMonth(); |
| 888 | |
| 889 | const Setting setting = getSetting(status); |
| 890 | if (U_FAILURE(status)) { |
| 891 | return; |
| 892 | } |
| 893 | struct MonthInfo monthInfo = computeMonthInfo(setting, gyear, days, status); |
| 894 | if (U_FAILURE(status)) { |
| 895 | return; |
| 896 | } |
| 897 | hasLeapMonthBetweenWinterSolstices = monthInfo.hasLeapMonthBetweenWinterSolstices; |
| 898 | |
| 899 | // Extended year and cycle year is based on the epoch year |
| 900 | int32_t eyear = gyear - setting.epochYear; |
| 901 | int32_t cycle_year = gyear - CHINESE_EPOCH_YEAR; |
| 902 | if (monthInfo.month < 11 || |
| 903 | gmonth >= UCAL_JULY) { |
| 904 | eyear++; |
| 905 | cycle_year++; |
| 906 | } |
| 907 | int32_t dayOfMonth = days - monthInfo.thisMoon + 1; |
| 908 | |
| 909 | // 0->0,60 1->1,1 60->1,60 61->2,1 etc. |
| 910 | int32_t yearOfCycle; |
| 911 | int32_t cycle = ClockMath::floorDivide(cycle_year - 1, 60, &yearOfCycle); |
| 912 | |
| 913 | // Days will be before the first new year we compute if this |
| 914 | // date is in month 11, leap 11, 12. There is never a leap 12. |
| 915 | // New year computations are cached so this should be cheap in |
| 916 | // the long run. |
| 917 | int32_t theNewYear = newYear(setting, gyear, status); |
| 918 | if (U_FAILURE(status)) { |
| 919 | return; |
| 920 | } |
| 921 | if (days < theNewYear) { |
| 922 | theNewYear = newYear(setting, gyear-1, status); |
| 923 | } |
| 924 | if (U_FAILURE(status)) { |
| 925 | return; |
| 926 | } |
| 927 | cycle++; |
| 928 | yearOfCycle++; |
| 929 | int32_t dayOfYear = days - theNewYear + 1; |
| 930 | |
| 931 | int32_t minYear = this->handleGetLimit(UCAL_EXTENDED_YEAR, UCAL_LIMIT_MINIMUM); |
| 932 | if (eyear < minYear) { |
| 933 | if (!isLenient()) { |
| 934 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 935 | return; |
| 936 | } |
| 937 | eyear = minYear; |
| 938 | } |
| 939 | int32_t maxYear = this->handleGetLimit(UCAL_EXTENDED_YEAR, UCAL_LIMIT_MAXIMUM); |
| 940 | if (maxYear < eyear) { |
| 941 | if (!isLenient()) { |
| 942 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 943 | return; |
| 944 | } |
| 945 | eyear = maxYear; |
| 946 | } |
| 947 | |
| 948 | internalSet(UCAL_MONTH, monthInfo.month-1); // Convert from 1-based to 0-based |
| 949 | internalSet(UCAL_ORDINAL_MONTH, monthInfo.ordinalMonth); // Convert from 1-based to 0-based |
| 950 | internalSet(UCAL_IS_LEAP_MONTH, monthInfo.isLeapMonth?1:0); |
| 951 | |
| 952 | internalSet(UCAL_EXTENDED_YEAR, eyear); |
| 953 | internalSet(UCAL_ERA, cycle); |
| 954 | internalSet(UCAL_YEAR, yearOfCycle); |
| 955 | internalSet(UCAL_DAY_OF_MONTH, dayOfMonth); |
| 956 | internalSet(UCAL_DAY_OF_YEAR, dayOfYear); |
| 957 | } |
| 958 | |
| 959 | //------------------------------------------------------------------ |
| 960 | // Fields to time |
| 961 | //------------------------------------------------------------------ |
| 962 | |
| 963 | namespace { |
| 964 | |
| 965 | /** |
| 966 | * Return the Chinese new year of the given Gregorian year. |
| 967 | * @param setting setting (time zone and caches) for the Astro calculation. |
| 968 | * @param gyear a Gregorian year |
| 969 | * @return days after January 1, 1970 0:00 astronomical base zone of the |
| 970 | * Chinese new year of the given year (this will be a new moon) |
| 971 | */ |
| 972 | int32_t newYear(const icu::ChineseCalendar::Setting& setting, |
| 973 | int32_t gyear, UErrorCode& status) { |
| 974 | if (U_FAILURE(status)) { |
| 975 | return 0; |
| 976 | } |
| 977 | const TimeZone* timeZone = setting.zoneAstroCalc; |
| 978 | int32_t cacheValue = CalendarCache::get(setting.newYearCache, gyear, status); |
| 979 | if (U_FAILURE(status)) { |
| 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | if (cacheValue == 0) { |
| 984 | |
| 985 | int32_t solsticeBefore= winterSolstice(setting, gyear - 1, status); |
| 986 | int32_t solsticeAfter = winterSolstice(setting, gyear, status); |
| 987 | int32_t newMoon1 = newMoonNear(timeZone, solsticeBefore + 1, true, status); |
| 988 | int32_t newMoon2 = newMoonNear(timeZone, newMoon1 + SYNODIC_GAP, true, status); |
| 989 | int32_t newMoon11 = newMoonNear(timeZone, solsticeAfter + 1, false, status); |
| 990 | if (U_FAILURE(status)) { |
| 991 | return 0; |
| 992 | } |
| 993 | |
| 994 | if (synodicMonthsBetween(newMoon1, newMoon11) == 12 && |
| 995 | (hasNoMajorSolarTerm(timeZone, newMoon1, status) || |
| 996 | hasNoMajorSolarTerm(timeZone, newMoon2, status))) { |
| 997 | cacheValue = newMoonNear(timeZone, newMoon2 + SYNODIC_GAP, true, status); |
| 998 | } else { |
| 999 | cacheValue = newMoon2; |
| 1000 | } |
| 1001 | if (U_FAILURE(status)) { |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
| 1005 | CalendarCache::put(setting.newYearCache, gyear, cacheValue, status); |
| 1006 | } |
| 1007 | if(U_FAILURE(status)) { |
| 1008 | cacheValue = 0; |
| 1009 | } |
| 1010 | return cacheValue; |
| 1011 | } |
| 1012 | |
| 1013 | } // namespace |
| 1014 | |
| 1015 | /** |
| 1016 | * Adjust this calendar to be delta months before or after a given |
| 1017 | * start position, pinning the day of month if necessary. The start |
| 1018 | * position is given as a local days number for the start of the month |
| 1019 | * and a day-of-month. Used by add() and roll(). |
| 1020 | * @param newMoon the local days of the first day of the month of the |
| 1021 | * start position (days after January 1, 1970 0:00 Asia/Shanghai) |
| 1022 | * @param dayOfMonth the 1-based day-of-month of the start position |
| 1023 | * @param delta the number of months to move forward or backward from |
| 1024 | * the start position |
| 1025 | * @param status The status. |
| 1026 | */ |
| 1027 | void ChineseCalendar::offsetMonth(int32_t newMoon, int32_t dayOfMonth, int32_t delta, |
| 1028 | UErrorCode& status) { |
| 1029 | const Setting setting = getSetting(status); |
| 1030 | if (U_FAILURE(status)) { |
| 1031 | return; |
| 1032 | } |
| 1033 | |
| 1034 | // Move to the middle of the month before our target month. |
| 1035 | double value = newMoon; |
| 1036 | value += (CalendarAstronomer::SYNODIC_MONTH * |
| 1037 | (static_cast<double>(delta) - 0.5)); |
| 1038 | if (value < INT32_MIN || value > INT32_MAX) { |
| 1039 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 1040 | return; |
| 1041 | } |
| 1042 | newMoon = static_cast<int32_t>(value); |
| 1043 | |
| 1044 | // Search forward to the target month's new moon |
| 1045 | newMoon = newMoonNear(setting.zoneAstroCalc, newMoon, true, status); |
| 1046 | if (U_FAILURE(status)) { |
| 1047 | return; |
| 1048 | } |
| 1049 | |
| 1050 | // Find the target dayOfMonth |
| 1051 | int32_t jd; |
| 1052 | if (uprv_add32_overflow(newMoon, kEpochStartAsJulianDay - 1, &jd) || |
| 1053 | uprv_add32_overflow(jd, dayOfMonth, &jd)) { |
| 1054 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 1055 | return; |
| 1056 | } |
| 1057 | |
| 1058 | // Pin the dayOfMonth. In this calendar all months are 29 or 30 days |
| 1059 | // so pinning just means handling dayOfMonth 30. |
| 1060 | if (dayOfMonth > 29) { |
| 1061 | set(UCAL_JULIAN_DAY, jd-1); |
| 1062 | // TODO Fix this. We really shouldn't ever have to |
| 1063 | // explicitly call complete(). This is either a bug in |
| 1064 | // this method, in ChineseCalendar, or in |
| 1065 | // Calendar.getActualMaximum(). I suspect the last. |
| 1066 | complete(status); |
| 1067 | if (U_FAILURE(status)) return; |
| 1068 | if (getActualMaximum(UCAL_DAY_OF_MONTH, status) >= dayOfMonth) { |
| 1069 | if (U_FAILURE(status)) return; |
| 1070 | set(UCAL_JULIAN_DAY, jd); |
| 1071 | } |
| 1072 | } else { |
| 1073 | set(UCAL_JULIAN_DAY, jd); |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | constexpr uint32_t kChineseRelatedYearDiff = -2637; |
| 1078 | |
| 1079 | int32_t ChineseCalendar::getRelatedYear(UErrorCode &status) const |
| 1080 | { |
| 1081 | int32_t year = get(UCAL_EXTENDED_YEAR, status); |
| 1082 | if (U_FAILURE(status)) { |
| 1083 | return 0; |
| 1084 | } |
| 1085 | if (uprv_add32_overflow(year, kChineseRelatedYearDiff, &year)) { |
| 1086 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 1087 | return 0; |
| 1088 | } |
| 1089 | return year; |
| 1090 | } |
| 1091 | |
| 1092 | void ChineseCalendar::setRelatedYear(int32_t year) |
| 1093 | { |
| 1094 | // set extended year |
| 1095 | set(UCAL_EXTENDED_YEAR, year - kChineseRelatedYearDiff); |
| 1096 | } |
| 1097 | |
| 1098 | IMPL_SYSTEM_DEFAULT_CENTURY(ChineseCalendar, "@calendar=chinese") |
| 1099 | |
| 1100 | bool |
| 1101 | ChineseCalendar::inTemporalLeapYear(UErrorCode &status) const |
| 1102 | { |
| 1103 | int32_t days = getActualMaximum(UCAL_DAY_OF_YEAR, status); |
| 1104 | if (U_FAILURE(status)) return false; |
| 1105 | return days > 360; |
| 1106 | } |
| 1107 | |
| 1108 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ChineseCalendar) |
| 1109 | |
| 1110 | |
| 1111 | static const char * const gTemporalLeapMonthCodes[] = { |
| 1112 | "M01L", "M02L", "M03L", "M04L", "M05L", "M06L", |
| 1113 | "M07L", "M08L", "M09L", "M10L", "M11L", "M12L", nullptr |
| 1114 | }; |
| 1115 | |
| 1116 | const char* ChineseCalendar::getTemporalMonthCode(UErrorCode &status) const { |
| 1117 | // We need to call get, not internalGet, to force the calculation |
| 1118 | // from UCAL_ORDINAL_MONTH. |
| 1119 | int32_t is_leap = get(UCAL_IS_LEAP_MONTH, status); |
| 1120 | if (U_FAILURE(status)) return nullptr; |
| 1121 | if (is_leap != 0) { |
| 1122 | int32_t month = get(UCAL_MONTH, status); |
| 1123 | if (U_FAILURE(status)) return nullptr; |
| 1124 | return gTemporalLeapMonthCodes[month]; |
| 1125 | } |
| 1126 | return Calendar::getTemporalMonthCode(status); |
| 1127 | } |
| 1128 | |
| 1129 | void |
| 1130 | ChineseCalendar::setTemporalMonthCode(const char* code, UErrorCode& status ) |
| 1131 | { |
| 1132 | if (U_FAILURE(status)) return; |
| 1133 | int32_t len = static_cast<int32_t>(uprv_strlen(code)); |
| 1134 | if (len != 4 || code[0] != 'M' || code[3] != 'L') { |
| 1135 | set(UCAL_IS_LEAP_MONTH, 0); |
| 1136 | return Calendar::setTemporalMonthCode(code, status); |
| 1137 | } |
| 1138 | for (int m = 0; gTemporalLeapMonthCodes[m] != nullptr; m++) { |
| 1139 | if (uprv_strcmp(code, gTemporalLeapMonthCodes[m]) == 0) { |
| 1140 | set(UCAL_MONTH, m); |
| 1141 | set(UCAL_IS_LEAP_MONTH, 1); |
| 1142 | return; |
| 1143 | } |
| 1144 | } |
| 1145 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 1146 | } |
| 1147 | |
| 1148 | int32_t ChineseCalendar::internalGetMonth(UErrorCode& status) const { |
| 1149 | if (U_FAILURE(status)) { |
| 1150 | return 0; |
| 1151 | } |
| 1152 | if (resolveFields(kMonthPrecedence) == UCAL_MONTH) { |
| 1153 | return internalGet(UCAL_MONTH); |
| 1154 | } |
| 1155 | LocalPointer<Calendar> temp(this->clone()); |
| 1156 | temp->set(UCAL_MONTH, 0); |
| 1157 | temp->set(UCAL_IS_LEAP_MONTH, 0); |
| 1158 | temp->set(UCAL_DATE, 1); |
| 1159 | // Calculate the UCAL_MONTH and UCAL_IS_LEAP_MONTH by adding number of |
| 1160 | // months. |
| 1161 | temp->roll(UCAL_MONTH, internalGet(UCAL_ORDINAL_MONTH), status); |
| 1162 | if (U_FAILURE(status)) { |
| 1163 | return 0; |
| 1164 | } |
| 1165 | |
| 1166 | ChineseCalendar* nonConstThis = const_cast<ChineseCalendar*>(this); // cast away const |
| 1167 | nonConstThis->internalSet(UCAL_IS_LEAP_MONTH, temp->get(UCAL_IS_LEAP_MONTH, status)); |
| 1168 | int32_t month = temp->get(UCAL_MONTH, status); |
| 1169 | if (U_FAILURE(status)) { |
| 1170 | return 0; |
| 1171 | } |
| 1172 | nonConstThis->internalSet(UCAL_MONTH, month); |
| 1173 | return month; |
| 1174 | } |
| 1175 | |
| 1176 | int32_t ChineseCalendar::internalGetMonth(int32_t defaultValue, UErrorCode& status) const { |
| 1177 | if (U_FAILURE(status)) { |
| 1178 | return 0; |
| 1179 | } |
| 1180 | if (resolveFields(kMonthPrecedence) == UCAL_MONTH) { |
| 1181 | return internalGet(UCAL_MONTH, defaultValue); |
| 1182 | } |
| 1183 | return internalGetMonth(status); |
| 1184 | } |
| 1185 | |
| 1186 | ChineseCalendar::Setting ChineseCalendar::getSetting(UErrorCode&) const { |
| 1187 | return { |
| 1188 | CHINESE_EPOCH_YEAR, |
| 1189 | getAstronomerTimeZone(), |
| 1190 | &gWinterSolsticeCache, |
| 1191 | &gNewYearCache |
| 1192 | }; |
| 1193 | } |
| 1194 | |
| 1195 | int32_t |
| 1196 | ChineseCalendar::getActualMaximum(UCalendarDateFields field, UErrorCode& status) const |
| 1197 | { |
| 1198 | if (U_FAILURE(status)) { |
| 1199 | return 0; |
| 1200 | } |
| 1201 | if (field == UCAL_DATE) { |
| 1202 | LocalPointer<ChineseCalendar> cal(clone(), status); |
| 1203 | if(U_FAILURE(status)) { |
| 1204 | return 0; |
| 1205 | } |
| 1206 | cal->setLenient(true); |
| 1207 | cal->prepareGetActual(field,false,status); |
| 1208 | int32_t year = cal->get(UCAL_EXTENDED_YEAR, status); |
| 1209 | int32_t month = cal->get(UCAL_MONTH, status); |
| 1210 | bool leap = cal->get(UCAL_IS_LEAP_MONTH, status) != 0; |
| 1211 | return handleGetMonthLengthWithLeap(year, month, leap, status); |
| 1212 | } |
| 1213 | return Calendar::getActualMaximum(field, status); |
| 1214 | } |
| 1215 | |
| 1216 | U_NAMESPACE_END |
| 1217 | |
| 1218 | #endif |
| 1219 |