Coverage Report

Created: 2025-06-24 06:54

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