Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | ****************************************************************************** |
michael@0 | 3 | * Copyright (C) 2013, International Business Machines Corporation |
michael@0 | 4 | * and others. All Rights Reserved. |
michael@0 | 5 | ****************************************************************************** |
michael@0 | 6 | * |
michael@0 | 7 | * File DANGICAL.CPP |
michael@0 | 8 | ***************************************************************************** |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include "chnsecal.h" |
michael@0 | 12 | #include "dangical.h" |
michael@0 | 13 | |
michael@0 | 14 | #if !UCONFIG_NO_FORMATTING |
michael@0 | 15 | |
michael@0 | 16 | #include "gregoimp.h" // Math |
michael@0 | 17 | #include "uassert.h" |
michael@0 | 18 | #include "ucln_in.h" |
michael@0 | 19 | #include "umutex.h" |
michael@0 | 20 | #include "unicode/rbtz.h" |
michael@0 | 21 | #include "unicode/tzrule.h" |
michael@0 | 22 | |
michael@0 | 23 | // --- The cache -- |
michael@0 | 24 | static icu::TimeZone *gDangiCalendarZoneAstroCalc = NULL; |
michael@0 | 25 | static icu::UInitOnce gDangiCalendarInitOnce = U_INITONCE_INITIALIZER; |
michael@0 | 26 | |
michael@0 | 27 | /** |
michael@0 | 28 | * The start year of the Korean traditional calendar (Dan-gi) is the inaugural |
michael@0 | 29 | * year of Dan-gun (BC 2333). |
michael@0 | 30 | */ |
michael@0 | 31 | static const int32_t DANGI_EPOCH_YEAR = -2332; // Gregorian year |
michael@0 | 32 | |
michael@0 | 33 | U_CDECL_BEGIN |
michael@0 | 34 | static UBool calendar_dangi_cleanup(void) { |
michael@0 | 35 | if (gDangiCalendarZoneAstroCalc) { |
michael@0 | 36 | delete gDangiCalendarZoneAstroCalc; |
michael@0 | 37 | gDangiCalendarZoneAstroCalc = NULL; |
michael@0 | 38 | } |
michael@0 | 39 | gDangiCalendarInitOnce.reset(); |
michael@0 | 40 | return TRUE; |
michael@0 | 41 | } |
michael@0 | 42 | U_CDECL_END |
michael@0 | 43 | |
michael@0 | 44 | U_NAMESPACE_BEGIN |
michael@0 | 45 | |
michael@0 | 46 | // Implementation of the DangiCalendar class |
michael@0 | 47 | |
michael@0 | 48 | //------------------------------------------------------------------------- |
michael@0 | 49 | // Constructors... |
michael@0 | 50 | //------------------------------------------------------------------------- |
michael@0 | 51 | |
michael@0 | 52 | DangiCalendar::DangiCalendar(const Locale& aLocale, UErrorCode& success) |
michael@0 | 53 | : ChineseCalendar(aLocale, DANGI_EPOCH_YEAR, getDangiCalZoneAstroCalc(), success) |
michael@0 | 54 | { |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | DangiCalendar::DangiCalendar (const DangiCalendar& other) |
michael@0 | 58 | : ChineseCalendar(other) |
michael@0 | 59 | { |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | DangiCalendar::~DangiCalendar() |
michael@0 | 63 | { |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | Calendar* |
michael@0 | 67 | DangiCalendar::clone() const |
michael@0 | 68 | { |
michael@0 | 69 | return new DangiCalendar(*this); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | const char *DangiCalendar::getType() const { |
michael@0 | 73 | return "dangi"; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | /** |
michael@0 | 77 | * The time zone used for performing astronomical computations for |
michael@0 | 78 | * Dangi calendar. In Korea various timezones have been used historically |
michael@0 | 79 | * (cf. http://www.math.snu.ac.kr/~kye/others/lunar.html): |
michael@0 | 80 | * |
michael@0 | 81 | * - 1908/04/01: GMT+8 |
michael@0 | 82 | * 1908/04/01 - 1911/12/31: GMT+8.5 |
michael@0 | 83 | * 1912/01/01 - 1954/03/20: GMT+9 |
michael@0 | 84 | * 1954/03/21 - 1961/08/09: GMT+8.5 |
michael@0 | 85 | * 1961/08/10 - : GMT+9 |
michael@0 | 86 | * |
michael@0 | 87 | * Note that, in 1908-1911, the government did not apply the timezone change |
michael@0 | 88 | * but used GMT+8. In addition, 1954-1961's timezone change does not affect |
michael@0 | 89 | * the lunar date calculation. Therefore, the following simpler rule works: |
michael@0 | 90 | * |
michael@0 | 91 | * -1911: GMT+8 |
michael@0 | 92 | * 1912-: GMT+9 |
michael@0 | 93 | * |
michael@0 | 94 | * Unfortunately, our astronomer's approximation doesn't agree with the |
michael@0 | 95 | * references (http://www.math.snu.ac.kr/~kye/others/lunar.html and |
michael@0 | 96 | * http://astro.kasi.re.kr/Life/ConvertSolarLunarForm.aspx?MenuID=115) |
michael@0 | 97 | * in 1897/7/30. So the following ad hoc fix is used here: |
michael@0 | 98 | * |
michael@0 | 99 | * -1896: GMT+8 |
michael@0 | 100 | * 1897: GMT+7 |
michael@0 | 101 | * 1898-1911: GMT+8 |
michael@0 | 102 | * 1912- : GMT+9 |
michael@0 | 103 | */ |
michael@0 | 104 | static void U_CALLCONV initDangiCalZoneAstroCalc(void) { |
michael@0 | 105 | U_ASSERT(gDangiCalendarZoneAstroCalc == NULL); |
michael@0 | 106 | const UDate millis1897[] = { (UDate)((1897 - 1970) * 365 * kOneDay) }; // some days of error is not a problem here |
michael@0 | 107 | const UDate millis1898[] = { (UDate)((1898 - 1970) * 365 * kOneDay) }; // some days of error is not a problem here |
michael@0 | 108 | const UDate millis1912[] = { (UDate)((1912 - 1970) * 365 * kOneDay) }; // this doesn't create an issue for 1911/12/20 |
michael@0 | 109 | InitialTimeZoneRule* initialTimeZone = new InitialTimeZoneRule(UNICODE_STRING_SIMPLE("GMT+8"), 8*kOneHour, 0); |
michael@0 | 110 | TimeZoneRule* rule1897 = new TimeArrayTimeZoneRule(UNICODE_STRING_SIMPLE("Korean 1897"), 7*kOneHour, 0, millis1897, 1, DateTimeRule::STANDARD_TIME); |
michael@0 | 111 | TimeZoneRule* rule1898to1911 = new TimeArrayTimeZoneRule(UNICODE_STRING_SIMPLE("Korean 1898-1911"), 8*kOneHour, 0, millis1898, 1, DateTimeRule::STANDARD_TIME); |
michael@0 | 112 | TimeZoneRule* ruleFrom1912 = new TimeArrayTimeZoneRule(UNICODE_STRING_SIMPLE("Korean 1912-"), 9*kOneHour, 0, millis1912, 1, DateTimeRule::STANDARD_TIME); |
michael@0 | 113 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 114 | RuleBasedTimeZone* dangiCalZoneAstroCalc = new RuleBasedTimeZone(UNICODE_STRING_SIMPLE("KOREA_ZONE"), initialTimeZone); // adopts initialTimeZone |
michael@0 | 115 | dangiCalZoneAstroCalc->addTransitionRule(rule1897, status); // adopts rule1897 |
michael@0 | 116 | dangiCalZoneAstroCalc->addTransitionRule(rule1898to1911, status); |
michael@0 | 117 | dangiCalZoneAstroCalc->addTransitionRule(ruleFrom1912, status); |
michael@0 | 118 | dangiCalZoneAstroCalc->complete(status); |
michael@0 | 119 | if (U_SUCCESS(status)) { |
michael@0 | 120 | gDangiCalendarZoneAstroCalc = dangiCalZoneAstroCalc; |
michael@0 | 121 | } else { |
michael@0 | 122 | delete dangiCalZoneAstroCalc; |
michael@0 | 123 | gDangiCalendarZoneAstroCalc = NULL; |
michael@0 | 124 | } |
michael@0 | 125 | ucln_i18n_registerCleanup(UCLN_I18N_DANGI_CALENDAR, calendar_dangi_cleanup); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | const TimeZone* DangiCalendar::getDangiCalZoneAstroCalc(void) const { |
michael@0 | 129 | umtx_initOnce(gDangiCalendarInitOnce, &initDangiCalZoneAstroCalc); |
michael@0 | 130 | return gDangiCalendarZoneAstroCalc; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | |
michael@0 | 134 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DangiCalendar) |
michael@0 | 135 | |
michael@0 | 136 | U_NAMESPACE_END |
michael@0 | 137 | |
michael@0 | 138 | #endif |
michael@0 | 139 |