1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2003-2013, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
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
18 #include "unicode/utypes.h"
20 #if !UCONFIG_NO_FORMATTING
23 #include "unicode/gregocal.h"
29 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TaiwanCalendar
)
31 static const int32_t kTaiwanEraStart
= 1911; // 1911 (Gregorian)
33 static const int32_t kGregorianEpoch
= 1970;
35 TaiwanCalendar::TaiwanCalendar(const Locale
& aLocale
, UErrorCode
& success
)
36 : GregorianCalendar(aLocale
, success
)
38 setTimeInMillis(getNow(), success
); // Call this again now that the vtable is set up properly.
41 TaiwanCalendar::~TaiwanCalendar()
45 TaiwanCalendar::TaiwanCalendar(const TaiwanCalendar
& source
)
46 : GregorianCalendar(source
)
50 TaiwanCalendar
& TaiwanCalendar::operator= ( const TaiwanCalendar
& right
)
52 GregorianCalendar::operator=(right
);
56 TaiwanCalendar
* TaiwanCalendar::clone() const
58 return new TaiwanCalendar(*this);
61 const char *TaiwanCalendar::getType() const
66 int32_t TaiwanCalendar::handleGetExtendedYear()
68 // EXTENDED_YEAR in TaiwanCalendar is a Gregorian year
69 // The default value of EXTENDED_YEAR is 1970 (Minguo 59)
70 int32_t year
= kGregorianEpoch
;
72 if (newerField(UCAL_EXTENDED_YEAR
, UCAL_YEAR
) == UCAL_EXTENDED_YEAR
73 && newerField(UCAL_EXTENDED_YEAR
, UCAL_ERA
) == UCAL_EXTENDED_YEAR
) {
74 year
= internalGet(UCAL_EXTENDED_YEAR
, kGregorianEpoch
);
76 int32_t era
= internalGet(UCAL_ERA
, MINGUO
);
78 year
= internalGet(UCAL_YEAR
, 1) + kTaiwanEraStart
;
79 } else if(era
== BEFORE_MINGUO
) {
80 year
= 1 - internalGet(UCAL_YEAR
, 1) + kTaiwanEraStart
;
86 void TaiwanCalendar::handleComputeFields(int32_t julianDay
, UErrorCode
& status
)
88 GregorianCalendar::handleComputeFields(julianDay
, status
);
89 int32_t y
= internalGet(UCAL_EXTENDED_YEAR
) - kTaiwanEraStart
;
91 internalSet(UCAL_ERA
, MINGUO
);
92 internalSet(UCAL_YEAR
, y
);
94 internalSet(UCAL_ERA
, BEFORE_MINGUO
);
95 internalSet(UCAL_YEAR
, 1-y
);
99 int32_t TaiwanCalendar::handleGetLimit(UCalendarDateFields field
, ELimitType limitType
) const
101 if(field
== UCAL_ERA
) {
102 if(limitType
== UCAL_LIMIT_MINIMUM
|| limitType
== UCAL_LIMIT_GREATEST_MINIMUM
) {
103 return BEFORE_MINGUO
;
108 return GregorianCalendar::handleGetLimit(field
,limitType
);
113 void TaiwanCalendar::timeToFields(UDate theTime
, UBool quick
, UErrorCode
& status
)
115 //Calendar::timeToFields(theTime, quick, status);
117 int32_t era
= internalGet(UCAL_ERA
);
118 int32_t year
= internalGet(UCAL_YEAR
);
120 if(era
== GregorianCalendar::BC
) {
122 era
= TaiwanCalendar::MINGUO
;
123 } else if(era
== GregorianCalendar::AD
) {
124 era
= TaiwanCalendar::MINGUO
;
126 status
= U_INTERNAL_PROGRAM_ERROR
;
129 year
= year
- kTaiwanEraStart
;
131 internalSet(UCAL_ERA
, era
);
132 internalSet(UCAL_YEAR
, year
);
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.
141 static UDate gSystemDefaultCenturyStart
= DBL_MIN
;
142 static int32_t gSystemDefaultCenturyStartYear
= -1;
143 static icu::UInitOnce gSystemDefaultCenturyInit
= U_INITONCE_INITIALIZER
;
145 UBool
TaiwanCalendar::haveDefaultCentury() const
150 static void U_CALLCONV
initializeSystemDefaultCentury()
152 // initialize systemDefaultCentury and systemDefaultCenturyYear based
153 // on the current time. They'll be set to 80 years before
155 UErrorCode status
= U_ZERO_ERROR
;
156 TaiwanCalendar
calendar(Locale("@calendar=roc"),status
);
157 if (U_SUCCESS(status
))
159 calendar
.setTime(Calendar::getNow(), status
);
160 calendar
.add(UCAL_YEAR
, -80, status
);
162 gSystemDefaultCenturyStart
= calendar
.getTime(status
);
163 gSystemDefaultCenturyStartYear
= calendar
.get(UCAL_YEAR
, status
);
165 // We have no recourse upon failure unless we want to propagate the failure
169 UDate
TaiwanCalendar::defaultCenturyStart() const {
170 // lazy-evaluate systemDefaultCenturyStart
171 umtx_initOnce(gSystemDefaultCenturyInit
, &initializeSystemDefaultCentury
);
172 return gSystemDefaultCenturyStart
;
175 int32_t TaiwanCalendar::defaultCenturyStartYear() const {
176 // lazy-evaluate systemDefaultCenturyStartYear
177 umtx_initOnce(gSystemDefaultCenturyInit
, &initializeSystemDefaultCentury
);
178 return gSystemDefaultCenturyStartYear
;