michael@0: /* michael@0: ****************************************************************************** michael@0: * Copyright (C) 2013, International Business Machines Corporation michael@0: * and others. All Rights Reserved. michael@0: ****************************************************************************** michael@0: * michael@0: * File DANGICAL.CPP michael@0: ***************************************************************************** michael@0: */ michael@0: michael@0: #include "chnsecal.h" michael@0: #include "dangical.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "gregoimp.h" // Math michael@0: #include "uassert.h" michael@0: #include "ucln_in.h" michael@0: #include "umutex.h" michael@0: #include "unicode/rbtz.h" michael@0: #include "unicode/tzrule.h" michael@0: michael@0: // --- The cache -- michael@0: static icu::TimeZone *gDangiCalendarZoneAstroCalc = NULL; michael@0: static icu::UInitOnce gDangiCalendarInitOnce = U_INITONCE_INITIALIZER; michael@0: michael@0: /** michael@0: * The start year of the Korean traditional calendar (Dan-gi) is the inaugural michael@0: * year of Dan-gun (BC 2333). michael@0: */ michael@0: static const int32_t DANGI_EPOCH_YEAR = -2332; // Gregorian year michael@0: michael@0: U_CDECL_BEGIN michael@0: static UBool calendar_dangi_cleanup(void) { michael@0: if (gDangiCalendarZoneAstroCalc) { michael@0: delete gDangiCalendarZoneAstroCalc; michael@0: gDangiCalendarZoneAstroCalc = NULL; michael@0: } michael@0: gDangiCalendarInitOnce.reset(); michael@0: return TRUE; michael@0: } michael@0: U_CDECL_END michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: // Implementation of the DangiCalendar class michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Constructors... michael@0: //------------------------------------------------------------------------- michael@0: michael@0: DangiCalendar::DangiCalendar(const Locale& aLocale, UErrorCode& success) michael@0: : ChineseCalendar(aLocale, DANGI_EPOCH_YEAR, getDangiCalZoneAstroCalc(), success) michael@0: { michael@0: } michael@0: michael@0: DangiCalendar::DangiCalendar (const DangiCalendar& other) michael@0: : ChineseCalendar(other) michael@0: { michael@0: } michael@0: michael@0: DangiCalendar::~DangiCalendar() michael@0: { michael@0: } michael@0: michael@0: Calendar* michael@0: DangiCalendar::clone() const michael@0: { michael@0: return new DangiCalendar(*this); michael@0: } michael@0: michael@0: const char *DangiCalendar::getType() const { michael@0: return "dangi"; michael@0: } michael@0: michael@0: /** michael@0: * The time zone used for performing astronomical computations for michael@0: * Dangi calendar. In Korea various timezones have been used historically michael@0: * (cf. http://www.math.snu.ac.kr/~kye/others/lunar.html): michael@0: * michael@0: * - 1908/04/01: GMT+8 michael@0: * 1908/04/01 - 1911/12/31: GMT+8.5 michael@0: * 1912/01/01 - 1954/03/20: GMT+9 michael@0: * 1954/03/21 - 1961/08/09: GMT+8.5 michael@0: * 1961/08/10 - : GMT+9 michael@0: * michael@0: * Note that, in 1908-1911, the government did not apply the timezone change michael@0: * but used GMT+8. In addition, 1954-1961's timezone change does not affect michael@0: * the lunar date calculation. Therefore, the following simpler rule works: michael@0: * michael@0: * -1911: GMT+8 michael@0: * 1912-: GMT+9 michael@0: * michael@0: * Unfortunately, our astronomer's approximation doesn't agree with the michael@0: * references (http://www.math.snu.ac.kr/~kye/others/lunar.html and michael@0: * http://astro.kasi.re.kr/Life/ConvertSolarLunarForm.aspx?MenuID=115) michael@0: * in 1897/7/30. So the following ad hoc fix is used here: michael@0: * michael@0: * -1896: GMT+8 michael@0: * 1897: GMT+7 michael@0: * 1898-1911: GMT+8 michael@0: * 1912- : GMT+9 michael@0: */ michael@0: static void U_CALLCONV initDangiCalZoneAstroCalc(void) { michael@0: U_ASSERT(gDangiCalendarZoneAstroCalc == NULL); michael@0: const UDate millis1897[] = { (UDate)((1897 - 1970) * 365 * kOneDay) }; // some days of error is not a problem here michael@0: const UDate millis1898[] = { (UDate)((1898 - 1970) * 365 * kOneDay) }; // some days of error is not a problem here michael@0: const UDate millis1912[] = { (UDate)((1912 - 1970) * 365 * kOneDay) }; // this doesn't create an issue for 1911/12/20 michael@0: InitialTimeZoneRule* initialTimeZone = new InitialTimeZoneRule(UNICODE_STRING_SIMPLE("GMT+8"), 8*kOneHour, 0); michael@0: TimeZoneRule* rule1897 = new TimeArrayTimeZoneRule(UNICODE_STRING_SIMPLE("Korean 1897"), 7*kOneHour, 0, millis1897, 1, DateTimeRule::STANDARD_TIME); michael@0: TimeZoneRule* rule1898to1911 = new TimeArrayTimeZoneRule(UNICODE_STRING_SIMPLE("Korean 1898-1911"), 8*kOneHour, 0, millis1898, 1, DateTimeRule::STANDARD_TIME); michael@0: TimeZoneRule* ruleFrom1912 = new TimeArrayTimeZoneRule(UNICODE_STRING_SIMPLE("Korean 1912-"), 9*kOneHour, 0, millis1912, 1, DateTimeRule::STANDARD_TIME); michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: RuleBasedTimeZone* dangiCalZoneAstroCalc = new RuleBasedTimeZone(UNICODE_STRING_SIMPLE("KOREA_ZONE"), initialTimeZone); // adopts initialTimeZone michael@0: dangiCalZoneAstroCalc->addTransitionRule(rule1897, status); // adopts rule1897 michael@0: dangiCalZoneAstroCalc->addTransitionRule(rule1898to1911, status); michael@0: dangiCalZoneAstroCalc->addTransitionRule(ruleFrom1912, status); michael@0: dangiCalZoneAstroCalc->complete(status); michael@0: if (U_SUCCESS(status)) { michael@0: gDangiCalendarZoneAstroCalc = dangiCalZoneAstroCalc; michael@0: } else { michael@0: delete dangiCalZoneAstroCalc; michael@0: gDangiCalendarZoneAstroCalc = NULL; michael@0: } michael@0: ucln_i18n_registerCleanup(UCLN_I18N_DANGI_CALENDAR, calendar_dangi_cleanup); michael@0: } michael@0: michael@0: const TimeZone* DangiCalendar::getDangiCalZoneAstroCalc(void) const { michael@0: umtx_initOnce(gDangiCalendarInitOnce, &initDangiCalZoneAstroCalc); michael@0: return gDangiCalendarZoneAstroCalc; michael@0: } michael@0: michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DangiCalendar) michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif michael@0: