| 1 | // © 2016 and later: Unicode, Inc. and others. |
| 2 | // License & terms of use: http://www.unicode.org/copyright.html |
| 3 | /* |
| 4 | ****************************************************************************** |
| 5 | * Copyright (C) 2013, International Business Machines Corporation |
| 6 | * and others. All Rights Reserved. |
| 7 | ****************************************************************************** |
| 8 | * |
| 9 | * File DANGICAL.CPP |
| 10 | ***************************************************************************** |
| 11 | */ |
| 12 | |
| 13 | #include "chnsecal.h" |
| 14 | #include "dangical.h" |
| 15 | |
| 16 | #if !UCONFIG_NO_FORMATTING |
| 17 | |
| 18 | #include "astro.h" // CalendarCache |
| 19 | #include "gregoimp.h" // Math |
| 20 | #include "uassert.h" |
| 21 | #include "ucln_in.h" |
| 22 | #include "umutex.h" |
| 23 | #include "unicode/rbtz.h" |
| 24 | #include "unicode/tzrule.h" |
| 25 | |
| 26 | // --- The cache -- |
| 27 | // Lazy Creation & Access synchronized by class CalendarCache with a mutex. |
| 28 | static icu::CalendarCache *gWinterSolsticeCache = nullptr; |
| 29 | static icu::CalendarCache *gNewYearCache = nullptr; |
| 30 | |
| 31 | // gAstronomerTimeZone |
| 32 | static icu::TimeZone *gAstronomerTimeZone = nullptr; |
| 33 | static icu::UInitOnce gAstronomerTimeZoneInitOnce {}; |
| 34 | |
| 35 | /** |
| 36 | * The start year of the Korean traditional calendar (Dan-gi) is the inaugural |
| 37 | * year of Dan-gun (BC 2333). |
| 38 | */ |
| 39 | static const int32_t DANGI_EPOCH_YEAR = -2332; // Gregorian year |
| 40 | |
| 41 | U_CDECL_BEGIN |
| 42 | static UBool calendar_dangi_cleanup() { |
| 43 | if (gWinterSolsticeCache) { |
| 44 | delete gWinterSolsticeCache; |
| 45 | gWinterSolsticeCache = nullptr; |
| 46 | } |
| 47 | if (gNewYearCache) { |
| 48 | delete gNewYearCache; |
| 49 | gNewYearCache = nullptr; |
| 50 | } |
| 51 | |
| 52 | if (gAstronomerTimeZone) { |
| 53 | delete gAstronomerTimeZone; |
| 54 | gAstronomerTimeZone = nullptr; |
| 55 | } |
| 56 | gAstronomerTimeZoneInitOnce.reset(); |
| 57 | return true; |
| 58 | } |
| 59 | U_CDECL_END |
| 60 | |
| 61 | U_NAMESPACE_BEGIN |
| 62 | |
| 63 | // Implementation of the DangiCalendar class |
| 64 | |
| 65 | //------------------------------------------------------------------------- |
| 66 | // Constructors... |
| 67 | //------------------------------------------------------------------------- |
| 68 | |
| 69 | const TimeZone* getAstronomerTimeZone(UErrorCode &status); |
| 70 | |
| 71 | DangiCalendar::DangiCalendar(const Locale& aLocale, UErrorCode& success) |
| 72 | : ChineseCalendar(aLocale, success) |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | DangiCalendar::DangiCalendar (const DangiCalendar& other) |
| 77 | : ChineseCalendar(other) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | DangiCalendar::~DangiCalendar() |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | DangiCalendar* |
| 86 | DangiCalendar::clone() const |
| 87 | { |
| 88 | return new DangiCalendar(*this); |
| 89 | } |
| 90 | |
| 91 | const char *DangiCalendar::getType() const { |
| 92 | return "dangi"; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * The time zone used for performing astronomical computations for |
| 97 | * Dangi calendar. In Korea various timezones have been used historically |
| 98 | * (cf. http://www.math.snu.ac.kr/~kye/others/lunar.html): |
| 99 | * |
| 100 | * - 1908/04/01: GMT+8 |
| 101 | * 1908/04/01 - 1911/12/31: GMT+8.5 |
| 102 | * 1912/01/01 - 1954/03/20: GMT+9 |
| 103 | * 1954/03/21 - 1961/08/09: GMT+8.5 |
| 104 | * 1961/08/10 - : GMT+9 |
| 105 | * |
| 106 | * Note that, in 1908-1911, the government did not apply the timezone change |
| 107 | * but used GMT+8. In addition, 1954-1961's timezone change does not affect |
| 108 | * the lunar date calculation. Therefore, the following simpler rule works: |
| 109 | * |
| 110 | * -1911: GMT+8 |
| 111 | * 1912-: GMT+9 |
| 112 | * |
| 113 | * Unfortunately, our astronomer's approximation doesn't agree with the |
| 114 | * references (http://www.math.snu.ac.kr/~kye/others/lunar.html and |
| 115 | * http://astro.kasi.re.kr/Life/ConvertSolarLunarForm.aspx?MenuID=115) |
| 116 | * in 1897/7/30. So the following ad hoc fix is used here: |
| 117 | * |
| 118 | * -1896: GMT+8 |
| 119 | * 1897: GMT+7 |
| 120 | * 1898-1911: GMT+8 |
| 121 | * 1912- : GMT+9 |
| 122 | */ |
| 123 | static void U_CALLCONV initAstronomerTimeZone(UErrorCode &status) { |
| 124 | U_ASSERT(gAstronomerTimeZone == nullptr); |
| 125 | const UDate millis1897[] = { static_cast<UDate>((1897 - 1970) * 365 * kOneDay) }; // some days of error is not a problem here |
| 126 | const UDate millis1898[] = { static_cast<UDate>((1898 - 1970) * 365 * kOneDay) }; // some days of error is not a problem here |
| 127 | const UDate millis1912[] = { static_cast<UDate>((1912 - 1970) * 365 * kOneDay) }; // this doesn't create an issue for 1911/12/20 |
| 128 | LocalPointer<InitialTimeZoneRule> initialTimeZone(new InitialTimeZoneRule( |
| 129 | UnicodeString(u"GMT+8"), 8*kOneHour, 0), status); |
| 130 | |
| 131 | LocalPointer<TimeZoneRule> rule1897(new TimeArrayTimeZoneRule( |
| 132 | UnicodeString(u"Korean 1897"), 7*kOneHour, 0, millis1897, 1, DateTimeRule::STANDARD_TIME), status); |
| 133 | |
| 134 | LocalPointer<TimeZoneRule> rule1898to1911(new TimeArrayTimeZoneRule( |
| 135 | UnicodeString(u"Korean 1898-1911"), 8*kOneHour, 0, millis1898, 1, DateTimeRule::STANDARD_TIME), status); |
| 136 | |
| 137 | LocalPointer<TimeZoneRule> ruleFrom1912(new TimeArrayTimeZoneRule( |
| 138 | UnicodeString(u"Korean 1912-"), 9*kOneHour, 0, millis1912, 1, DateTimeRule::STANDARD_TIME), status); |
| 139 | |
| 140 | LocalPointer<RuleBasedTimeZone> zone(new RuleBasedTimeZone( |
| 141 | UnicodeString(u"KOREA_ZONE"), initialTimeZone.orphan()), status); // adopts initialTimeZone |
| 142 | |
| 143 | if (U_FAILURE(status)) { |
| 144 | return; |
| 145 | } |
| 146 | zone->addTransitionRule(rule1897.orphan(), status); // adopts rule1897 |
| 147 | zone->addTransitionRule(rule1898to1911.orphan(), status); |
| 148 | zone->addTransitionRule(ruleFrom1912.orphan(), status); |
| 149 | zone->complete(status); |
| 150 | if (U_SUCCESS(status)) { |
| 151 | gAstronomerTimeZone = zone.orphan(); |
| 152 | } |
| 153 | ucln_i18n_registerCleanup(UCLN_I18N_DANGI_CALENDAR, calendar_dangi_cleanup); |
| 154 | } |
| 155 | |
| 156 | const TimeZone* getAstronomerTimeZone(UErrorCode &status) { |
| 157 | umtx_initOnce(gAstronomerTimeZoneInitOnce, &initAstronomerTimeZone, status); |
| 158 | return gAstronomerTimeZone; |
| 159 | } |
| 160 | |
| 161 | constexpr uint32_t kDangiRelatedYearDiff = -2333; |
| 162 | |
| 163 | int32_t DangiCalendar::getRelatedYear(UErrorCode &status) const |
| 164 | { |
| 165 | int32_t year = get(UCAL_EXTENDED_YEAR, status); |
| 166 | if (U_FAILURE(status)) { |
| 167 | return 0; |
| 168 | } |
| 169 | if (uprv_add32_overflow(year, kDangiRelatedYearDiff, &year)) { |
| 170 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 171 | return 0; |
| 172 | } |
| 173 | return year; |
| 174 | } |
| 175 | |
| 176 | void DangiCalendar::setRelatedYear(int32_t year) |
| 177 | { |
| 178 | // set extended year |
| 179 | set(UCAL_EXTENDED_YEAR, year - kDangiRelatedYearDiff); |
| 180 | } |
| 181 | |
| 182 | ChineseCalendar::Setting DangiCalendar::getSetting(UErrorCode& status) const { |
| 183 | return { DANGI_EPOCH_YEAR, |
| 184 | getAstronomerTimeZone(status), |
| 185 | &gWinterSolsticeCache, &gNewYearCache |
| 186 | }; |
| 187 | } |
| 188 | |
| 189 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DangiCalendar) |
| 190 | |
| 191 | U_NAMESPACE_END |
| 192 | |
| 193 | #endif |
| 194 |