blob: 59cdc661dac817f3d40ac755342668991cfd6a17 [file] [log] [blame]
Jungshik Shin87232d82017-05-13 21:10:13 -07001// © 2016 and later: Unicode, Inc. and others.
Jungshik Shin5feb9ad2016-10-21 12:52:48 -07002// License & terms of use: http://www.unicode.org/copyright.html
jshin@chromium.org6f31ac32014-03-26 22:15:14 +00003/*
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 "gregoimp.h" // Math
19#include "uassert.h"
20#include "ucln_in.h"
21#include "umutex.h"
22#include "unicode/rbtz.h"
23#include "unicode/tzrule.h"
24
25// --- The cache --
26static icu::TimeZone *gDangiCalendarZoneAstroCalc = NULL;
Frank Tang1c67b4e2022-05-18 10:13:51 -070027static icu::UInitOnce gDangiCalendarInitOnce {};
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000028
29/**
30 * The start year of the Korean traditional calendar (Dan-gi) is the inaugural
31 * year of Dan-gun (BC 2333).
32 */
33static const int32_t DANGI_EPOCH_YEAR = -2332; // Gregorian year
34
35U_CDECL_BEGIN
36static UBool calendar_dangi_cleanup(void) {
37 if (gDangiCalendarZoneAstroCalc) {
38 delete gDangiCalendarZoneAstroCalc;
39 gDangiCalendarZoneAstroCalc = NULL;
40 }
41 gDangiCalendarInitOnce.reset();
Frank Tang1f164ee2022-11-08 12:31:27 -080042 return true;
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000043}
44U_CDECL_END
45
46U_NAMESPACE_BEGIN
47
48// Implementation of the DangiCalendar class
49
50//-------------------------------------------------------------------------
51// Constructors...
52//-------------------------------------------------------------------------
53
54DangiCalendar::DangiCalendar(const Locale& aLocale, UErrorCode& success)
Frank Tang3e05d9d2021-11-08 14:04:04 -080055: ChineseCalendar(aLocale, DANGI_EPOCH_YEAR, getDangiCalZoneAstroCalc(success), success)
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000056{
57}
58
59DangiCalendar::DangiCalendar (const DangiCalendar& other)
60: ChineseCalendar(other)
61{
62}
63
64DangiCalendar::~DangiCalendar()
65{
66}
67
Frank Tangb8696612019-10-25 14:58:21 -070068DangiCalendar*
jshin@chromium.org6f31ac32014-03-26 22:15:14 +000069DangiCalendar::clone() const
70{
71 return new DangiCalendar(*this);
72}
73
74const char *DangiCalendar::getType() const {
75 return "dangi";
76}
77
78/**
79 * The time zone used for performing astronomical computations for
80 * Dangi calendar. In Korea various timezones have been used historically
81 * (cf. http://www.math.snu.ac.kr/~kye/others/lunar.html):
82 *
83 * - 1908/04/01: GMT+8
84 * 1908/04/01 - 1911/12/31: GMT+8.5
85 * 1912/01/01 - 1954/03/20: GMT+9
86 * 1954/03/21 - 1961/08/09: GMT+8.5
87 * 1961/08/10 - : GMT+9
88 *
89 * Note that, in 1908-1911, the government did not apply the timezone change
90 * but used GMT+8. In addition, 1954-1961's timezone change does not affect
91 * the lunar date calculation. Therefore, the following simpler rule works:
92 *
93 * -1911: GMT+8
94 * 1912-: GMT+9
95 *
96 * Unfortunately, our astronomer's approximation doesn't agree with the
97 * references (http://www.math.snu.ac.kr/~kye/others/lunar.html and
98 * http://astro.kasi.re.kr/Life/ConvertSolarLunarForm.aspx?MenuID=115)
99 * in 1897/7/30. So the following ad hoc fix is used here:
100 *
101 * -1896: GMT+8
102 * 1897: GMT+7
103 * 1898-1911: GMT+8
104 * 1912- : GMT+9
105 */
Frank Tang3e05d9d2021-11-08 14:04:04 -0800106static void U_CALLCONV initDangiCalZoneAstroCalc(UErrorCode &status) {
107 U_ASSERT(gDangiCalendarZoneAstroCalc == nullptr);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000108 const UDate millis1897[] = { (UDate)((1897 - 1970) * 365 * kOneDay) }; // some days of error is not a problem here
109 const UDate millis1898[] = { (UDate)((1898 - 1970) * 365 * kOneDay) }; // some days of error is not a problem here
110 const UDate millis1912[] = { (UDate)((1912 - 1970) * 365 * kOneDay) }; // this doesn't create an issue for 1911/12/20
Frank Tang3e05d9d2021-11-08 14:04:04 -0800111 LocalPointer<InitialTimeZoneRule> initialTimeZone(new InitialTimeZoneRule(
112 UnicodeString(u"GMT+8"), 8*kOneHour, 0), status);
113
114 LocalPointer<TimeZoneRule> rule1897(new TimeArrayTimeZoneRule(
115 UnicodeString(u"Korean 1897"), 7*kOneHour, 0, millis1897, 1, DateTimeRule::STANDARD_TIME), status);
116
117 LocalPointer<TimeZoneRule> rule1898to1911(new TimeArrayTimeZoneRule(
118 UnicodeString(u"Korean 1898-1911"), 8*kOneHour, 0, millis1898, 1, DateTimeRule::STANDARD_TIME), status);
119
120 LocalPointer<TimeZoneRule> ruleFrom1912(new TimeArrayTimeZoneRule(
121 UnicodeString(u"Korean 1912-"), 9*kOneHour, 0, millis1912, 1, DateTimeRule::STANDARD_TIME), status);
122
123 LocalPointer<RuleBasedTimeZone> dangiCalZoneAstroCalc(new RuleBasedTimeZone(
124 UnicodeString(u"KOREA_ZONE"), initialTimeZone.orphan()), status); // adopts initialTimeZone
125
126 if (U_FAILURE(status)) {
127 return;
128 }
129 dangiCalZoneAstroCalc->addTransitionRule(rule1897.orphan(), status); // adopts rule1897
130 dangiCalZoneAstroCalc->addTransitionRule(rule1898to1911.orphan(), status);
131 dangiCalZoneAstroCalc->addTransitionRule(ruleFrom1912.orphan(), status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000132 dangiCalZoneAstroCalc->complete(status);
133 if (U_SUCCESS(status)) {
Frank Tang3e05d9d2021-11-08 14:04:04 -0800134 gDangiCalendarZoneAstroCalc = dangiCalZoneAstroCalc.orphan();
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000135 }
136 ucln_i18n_registerCleanup(UCLN_I18N_DANGI_CALENDAR, calendar_dangi_cleanup);
137}
138
Frank Tang3e05d9d2021-11-08 14:04:04 -0800139const TimeZone* DangiCalendar::getDangiCalZoneAstroCalc(UErrorCode &status) const {
140 umtx_initOnce(gDangiCalendarInitOnce, &initDangiCalZoneAstroCalc, status);
jshin@chromium.org6f31ac32014-03-26 22:15:14 +0000141 return gDangiCalendarZoneAstroCalc;
142}
143
144
145UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DangiCalendar)
146
147U_NAMESPACE_END
148
149#endif
150