2 *******************************************************************************
3 * Copyright (C) 2003-2013, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
9 * Modification History:
10 * 05/13/2003 srl copied from gregocal.cpp
11 * 06/29/2007 srl copied from buddhcal.cpp
12 * 05/12/2008 jce modified to use calendar=roc per CLDR
16 #include "unicode/utypes.h"
18 #if !UCONFIG_NO_FORMATTING
21 #include "unicode/gregocal.h"
27 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TaiwanCalendar
)
29 static const int32_t kTaiwanEraStart
= 1911; // 1911 (Gregorian)
31 static const int32_t kGregorianEpoch
= 1970;
33 TaiwanCalendar::TaiwanCalendar(const Locale
& aLocale
, UErrorCode
& success
)
34 : GregorianCalendar(aLocale
, success
)
36 setTimeInMillis(getNow(), success
); // Call this again now that the vtable is set up properly.
39 TaiwanCalendar::~TaiwanCalendar()
43 TaiwanCalendar::TaiwanCalendar(const TaiwanCalendar
& source
)
44 : GregorianCalendar(source
)
48 TaiwanCalendar
& TaiwanCalendar::operator= ( const TaiwanCalendar
& right
)
50 GregorianCalendar::operator=(right
);
54 Calendar
* TaiwanCalendar::clone(void) const
56 return new TaiwanCalendar(*this);
59 const char *TaiwanCalendar::getType() const
64 int32_t TaiwanCalendar::handleGetExtendedYear()
66 // EXTENDED_YEAR in TaiwanCalendar is a Gregorian year
67 // The default value of EXTENDED_YEAR is 1970 (Minguo 59)
68 int32_t year
= kGregorianEpoch
;
70 if (newerField(UCAL_EXTENDED_YEAR
, UCAL_YEAR
) == UCAL_EXTENDED_YEAR
71 && newerField(UCAL_EXTENDED_YEAR
, UCAL_ERA
) == UCAL_EXTENDED_YEAR
) {
72 year
= internalGet(UCAL_EXTENDED_YEAR
, kGregorianEpoch
);
74 int32_t era
= internalGet(UCAL_ERA
, MINGUO
);
76 year
= internalGet(UCAL_YEAR
, 1) + kTaiwanEraStart
;
77 } else if(era
== BEFORE_MINGUO
) {
78 year
= 1 - internalGet(UCAL_YEAR
, 1) + kTaiwanEraStart
;
84 void TaiwanCalendar::handleComputeFields(int32_t julianDay
, UErrorCode
& status
)
86 GregorianCalendar::handleComputeFields(julianDay
, status
);
87 int32_t y
= internalGet(UCAL_EXTENDED_YEAR
) - kTaiwanEraStart
;
89 internalSet(UCAL_ERA
, MINGUO
);
90 internalSet(UCAL_YEAR
, y
);
92 internalSet(UCAL_ERA
, BEFORE_MINGUO
);
93 internalSet(UCAL_YEAR
, 1-y
);
97 int32_t TaiwanCalendar::handleGetLimit(UCalendarDateFields field
, ELimitType limitType
) const
99 if(field
== UCAL_ERA
) {
100 if(limitType
== UCAL_LIMIT_MINIMUM
|| limitType
== UCAL_LIMIT_GREATEST_MINIMUM
) {
101 return BEFORE_MINGUO
;
106 return GregorianCalendar::handleGetLimit(field
,limitType
);
111 void TaiwanCalendar::timeToFields(UDate theTime
, UBool quick
, UErrorCode
& status
)
113 //Calendar::timeToFields(theTime, quick, status);
115 int32_t era
= internalGet(UCAL_ERA
);
116 int32_t year
= internalGet(UCAL_YEAR
);
118 if(era
== GregorianCalendar::BC
) {
120 era
= TaiwanCalendar::MINGUO
;
121 } else if(era
== GregorianCalendar::AD
) {
122 era
= TaiwanCalendar::MINGUO
;
124 status
= U_INTERNAL_PROGRAM_ERROR
;
127 year
= year
- kTaiwanEraStart
;
129 internalSet(UCAL_ERA
, era
);
130 internalSet(UCAL_YEAR
, year
);
135 * The system maintains a static default century start date and Year. They are
136 * initialized the first time they are used. Once the system default century date
137 * and year are set, they do not change.
139 static UDate gSystemDefaultCenturyStart
= DBL_MIN
;
140 static int32_t gSystemDefaultCenturyStartYear
= -1;
141 static icu::UInitOnce gSystemDefaultCenturyInit
= U_INITONCE_INITIALIZER
;
143 UBool
TaiwanCalendar::haveDefaultCentury() const
148 static void U_CALLCONV
initializeSystemDefaultCentury()
150 // initialize systemDefaultCentury and systemDefaultCenturyYear based
151 // on the current time. They'll be set to 80 years before
153 UErrorCode status
= U_ZERO_ERROR
;
154 TaiwanCalendar
calendar(Locale("@calendar=roc"),status
);
155 if (U_SUCCESS(status
))
157 calendar
.setTime(Calendar::getNow(), status
);
158 calendar
.add(UCAL_YEAR
, -80, status
);
160 gSystemDefaultCenturyStart
= calendar
.getTime(status
);
161 gSystemDefaultCenturyStartYear
= calendar
.get(UCAL_YEAR
, status
);
163 // We have no recourse upon failure unless we want to propagate the failure
167 UDate
TaiwanCalendar::defaultCenturyStart() const {
168 // lazy-evaluate systemDefaultCenturyStart
169 umtx_initOnce(gSystemDefaultCenturyInit
, &initializeSystemDefaultCentury
);
170 return gSystemDefaultCenturyStart
;
173 int32_t TaiwanCalendar::defaultCenturyStartYear() const {
174 // lazy-evaluate systemDefaultCenturyStartYear
175 umtx_initOnce(gSystemDefaultCenturyInit
, &initializeSystemDefaultCentury
);
176 return gSystemDefaultCenturyStartYear
;