/src/icu/icu4c/source/i18n/dangical.cpp
Line  | Count  | Source (jump to first uncovered line)  | 
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  | 0  | static UBool calendar_dangi_cleanup() { | 
43  | 0  |     if (gWinterSolsticeCache) { | 
44  | 0  |         delete gWinterSolsticeCache;  | 
45  | 0  |         gWinterSolsticeCache = nullptr;  | 
46  | 0  |     }  | 
47  | 0  |     if (gNewYearCache) { | 
48  | 0  |         delete gNewYearCache;  | 
49  | 0  |         gNewYearCache = nullptr;  | 
50  | 0  |     }  | 
51  |  | 
  | 
52  | 0  |     if (gAstronomerTimeZone) { | 
53  | 0  |         delete gAstronomerTimeZone;  | 
54  | 0  |         gAstronomerTimeZone = nullptr;  | 
55  | 0  |     }  | 
56  | 0  |     gAstronomerTimeZoneInitOnce.reset();  | 
57  | 0  |     return true;  | 
58  | 0  | }  | 
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  | 0  | :   ChineseCalendar(aLocale, success)  | 
73  | 0  | { | 
74  | 0  | }  | 
75  |  |  | 
76  |  | DangiCalendar::DangiCalendar (const DangiCalendar& other)   | 
77  | 0  | : ChineseCalendar(other)  | 
78  | 0  | { | 
79  | 0  | }  | 
80  |  |  | 
81  |  | DangiCalendar::~DangiCalendar()  | 
82  | 0  | { | 
83  | 0  | }  | 
84  |  |  | 
85  |  | DangiCalendar*  | 
86  |  | DangiCalendar::clone() const  | 
87  | 0  | { | 
88  | 0  |     return new DangiCalendar(*this);  | 
89  | 0  | }  | 
90  |  |  | 
91  | 0  | const char *DangiCalendar::getType() const {  | 
92  | 0  |     return "dangi";  | 
93  | 0  | }  | 
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  | 0  | static void U_CALLCONV initAstronomerTimeZone(UErrorCode &status) { | 
124  | 0  |     U_ASSERT(gAstronomerTimeZone == nullptr);  | 
125  | 0  |     const UDate millis1897[] = { static_cast<UDate>((1897 - 1970) * 365 * kOneDay) }; // some days of error is not a problem here | 
126  | 0  |     const UDate millis1898[] = { static_cast<UDate>((1898 - 1970) * 365 * kOneDay) }; // some days of error is not a problem here | 
127  | 0  |     const UDate millis1912[] = { static_cast<UDate>((1912 - 1970) * 365 * kOneDay) }; // this doesn't create an issue for 1911/12/20 | 
128  | 0  |     LocalPointer<InitialTimeZoneRule> initialTimeZone(new InitialTimeZoneRule(  | 
129  | 0  |         UnicodeString(u"GMT+8"), 8*kOneHour, 0), status);  | 
130  |  | 
  | 
131  | 0  |     LocalPointer<TimeZoneRule> rule1897(new TimeArrayTimeZoneRule(  | 
132  | 0  |         UnicodeString(u"Korean 1897"), 7*kOneHour, 0, millis1897, 1, DateTimeRule::STANDARD_TIME), status);  | 
133  |  | 
  | 
134  | 0  |     LocalPointer<TimeZoneRule> rule1898to1911(new TimeArrayTimeZoneRule(  | 
135  | 0  |         UnicodeString(u"Korean 1898-1911"), 8*kOneHour, 0, millis1898, 1, DateTimeRule::STANDARD_TIME), status);  | 
136  |  | 
  | 
137  | 0  |     LocalPointer<TimeZoneRule> ruleFrom1912(new TimeArrayTimeZoneRule(  | 
138  | 0  |         UnicodeString(u"Korean 1912-"), 9*kOneHour, 0, millis1912, 1, DateTimeRule::STANDARD_TIME), status);  | 
139  |  | 
  | 
140  | 0  |     LocalPointer<RuleBasedTimeZone> zone(new RuleBasedTimeZone(  | 
141  | 0  |         UnicodeString(u"KOREA_ZONE"), initialTimeZone.orphan()), status); // adopts initialTimeZone  | 
142  |  | 
  | 
143  | 0  |     if (U_FAILURE(status)) { | 
144  | 0  |         return;  | 
145  | 0  |     }  | 
146  | 0  |     zone->addTransitionRule(rule1897.orphan(), status); // adopts rule1897  | 
147  | 0  |     zone->addTransitionRule(rule1898to1911.orphan(), status);  | 
148  | 0  |     zone->addTransitionRule(ruleFrom1912.orphan(), status);  | 
149  | 0  |     zone->complete(status);  | 
150  | 0  |     if (U_SUCCESS(status)) { | 
151  | 0  |         gAstronomerTimeZone = zone.orphan();  | 
152  | 0  |     }  | 
153  | 0  |     ucln_i18n_registerCleanup(UCLN_I18N_DANGI_CALENDAR, calendar_dangi_cleanup);  | 
154  | 0  | }  | 
155  |  |  | 
156  | 0  | const TimeZone* getAstronomerTimeZone(UErrorCode &status) { | 
157  | 0  |     umtx_initOnce(gAstronomerTimeZoneInitOnce, &initAstronomerTimeZone, status);  | 
158  | 0  |     return gAstronomerTimeZone;  | 
159  | 0  | }  | 
160  |  |  | 
161  | 0  | int32_t DangiCalendar::getRelatedYearDifference() const { | 
162  | 0  |     return DANGI_EPOCH_YEAR - 1;  | 
163  | 0  | }  | 
164  |  |  | 
165  | 0  | ChineseCalendar::Setting DangiCalendar::getSetting(UErrorCode& status) const { | 
166  | 0  |   return { DANGI_EPOCH_YEAR, | 
167  | 0  |     getAstronomerTimeZone(status),  | 
168  | 0  |     &gWinterSolsticeCache, &gNewYearCache  | 
169  | 0  |   };  | 
170  | 0  | }  | 
171  |  |  | 
172  |  | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DangiCalendar)  | 
173  |  |  | 
174  |  | U_NAMESPACE_END  | 
175  |  |  | 
176  |  | #endif  | 
177  |  |  |