Coverage Report

Created: 2025-01-28 06:38

/src/icu/source/i18n/taiwncal.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) 2003-2013, International Business Machines Corporation and
6
 * others. All Rights Reserved.
7
 *******************************************************************************
8
 *
9
 * File TAIWNCAL.CPP
10
 *
11
 * Modification History:
12
 *  05/13/2003    srl     copied from gregocal.cpp
13
 *  06/29/2007    srl     copied from buddhcal.cpp
14
 *  05/12/2008    jce     modified to use calendar=roc per CLDR
15
 *
16
 */
17
18
#include "unicode/utypes.h"
19
20
#if !UCONFIG_NO_FORMATTING
21
22
#include "taiwncal.h"
23
#include "unicode/gregocal.h"
24
#include "umutex.h"
25
#include <float.h>
26
27
U_NAMESPACE_BEGIN
28
29
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TaiwanCalendar)
30
31
static const int32_t kTaiwanEraStart = 1911;  // 1911 (Gregorian)
32
33
static const int32_t kGregorianEpoch = 1970; 
34
35
TaiwanCalendar::TaiwanCalendar(const Locale& aLocale, UErrorCode& success)
36
0
:   GregorianCalendar(aLocale, success)
37
0
{
38
0
    setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
39
0
}
40
41
TaiwanCalendar::~TaiwanCalendar()
42
0
{
43
0
}
44
45
TaiwanCalendar::TaiwanCalendar(const TaiwanCalendar& source)
46
0
: GregorianCalendar(source)
47
0
{
48
0
}
49
50
TaiwanCalendar& TaiwanCalendar::operator= ( const TaiwanCalendar& right)
51
0
{
52
0
    GregorianCalendar::operator=(right);
53
0
    return *this;
54
0
}
55
56
TaiwanCalendar* TaiwanCalendar::clone() const
57
0
{
58
0
    return new TaiwanCalendar(*this);
59
0
}
60
61
const char *TaiwanCalendar::getType() const
62
0
{
63
0
    return "roc";
64
0
}
65
66
int32_t TaiwanCalendar::handleGetExtendedYear()
67
0
{
68
    // EXTENDED_YEAR in TaiwanCalendar is a Gregorian year
69
    // The default value of EXTENDED_YEAR is 1970 (Minguo 59)
70
0
    int32_t year = kGregorianEpoch;
71
72
0
    if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR
73
0
        && newerField(UCAL_EXTENDED_YEAR, UCAL_ERA) == UCAL_EXTENDED_YEAR) {
74
0
        year = internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch);
75
0
    } else {
76
0
        int32_t era = internalGet(UCAL_ERA, MINGUO);
77
0
        if(era == MINGUO) {
78
0
            year =     internalGet(UCAL_YEAR, 1) + kTaiwanEraStart;
79
0
        } else if(era == BEFORE_MINGUO) {
80
0
            year = 1 - internalGet(UCAL_YEAR, 1) + kTaiwanEraStart;
81
0
        }
82
0
    }
83
0
    return year;
84
0
}
85
86
void TaiwanCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status)
87
0
{
88
0
    GregorianCalendar::handleComputeFields(julianDay, status);
89
0
    int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kTaiwanEraStart;
90
0
    if(y>0) {
91
0
        internalSet(UCAL_ERA, MINGUO);
92
0
        internalSet(UCAL_YEAR, y);
93
0
    } else {
94
0
        internalSet(UCAL_ERA, BEFORE_MINGUO);
95
0
        internalSet(UCAL_YEAR, 1-y);
96
0
    }
97
0
}
98
99
int32_t TaiwanCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
100
0
{
101
0
    if(field == UCAL_ERA) {
102
0
        if(limitType == UCAL_LIMIT_MINIMUM || limitType == UCAL_LIMIT_GREATEST_MINIMUM) {
103
0
            return BEFORE_MINGUO;
104
0
        } else {
105
0
            return MINGUO;
106
0
        }
107
0
    } else {
108
0
        return GregorianCalendar::handleGetLimit(field,limitType);
109
0
    }
110
0
}
111
112
#if 0
113
void TaiwanCalendar::timeToFields(UDate theTime, UBool quick, UErrorCode& status)
114
{
115
    //Calendar::timeToFields(theTime, quick, status);
116
117
    int32_t era = internalGet(UCAL_ERA);
118
    int32_t year = internalGet(UCAL_YEAR);
119
120
    if(era == GregorianCalendar::BC) {
121
        year = 1-year;
122
        era = TaiwanCalendar::MINGUO;
123
    } else if(era == GregorianCalendar::AD) {
124
        era = TaiwanCalendar::MINGUO;
125
    } else {
126
        status = U_INTERNAL_PROGRAM_ERROR;
127
    }
128
129
    year = year - kTaiwanEraStart;
130
131
    internalSet(UCAL_ERA, era);
132
    internalSet(UCAL_YEAR, year);
133
}
134
#endif
135
136
/**
137
 * The system maintains a static default century start date and Year.  They are
138
 * initialized the first time they are used.  Once the system default century date 
139
 * and year are set, they do not change.
140
 */
141
static UDate           gSystemDefaultCenturyStart       = DBL_MIN;
142
static int32_t         gSystemDefaultCenturyStartYear   = -1;
143
static icu::UInitOnce  gSystemDefaultCenturyInit        = U_INITONCE_INITIALIZER;
144
145
UBool TaiwanCalendar::haveDefaultCentury() const
146
0
{
147
0
    return TRUE;
148
0
}
149
150
static void U_CALLCONV initializeSystemDefaultCentury()
151
0
{
152
    // initialize systemDefaultCentury and systemDefaultCenturyYear based
153
    // on the current time.  They'll be set to 80 years before
154
    // the current time.
155
0
    UErrorCode status = U_ZERO_ERROR;
156
0
    TaiwanCalendar calendar(Locale("@calendar=roc"),status);
157
0
    if (U_SUCCESS(status))
158
0
    {
159
0
        calendar.setTime(Calendar::getNow(), status);
160
0
        calendar.add(UCAL_YEAR, -80, status);
161
162
0
        gSystemDefaultCenturyStart = calendar.getTime(status);
163
0
        gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);
164
0
    }
165
    // We have no recourse upon failure unless we want to propagate the failure
166
    // out.
167
0
}
168
169
0
UDate TaiwanCalendar::defaultCenturyStart() const {
170
    // lazy-evaluate systemDefaultCenturyStart
171
0
    umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
172
0
    return gSystemDefaultCenturyStart;
173
0
}
174
175
0
int32_t TaiwanCalendar::defaultCenturyStartYear() const {
176
    // lazy-evaluate systemDefaultCenturyStartYear
177
0
    umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
178
0
    return gSystemDefaultCenturyStartYear;
179
0
}
180
181
U_NAMESPACE_END
182
183
#endif