2 *******************************************************************************
3 * Copyright (C) 2003-2008, 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 const UDate
TaiwanCalendar::fgSystemDefaultCentury
= DBL_MIN
;
136 const int32_t TaiwanCalendar::fgSystemDefaultCenturyYear
= -1;
138 UDate
TaiwanCalendar::fgSystemDefaultCenturyStart
= DBL_MIN
;
139 int32_t TaiwanCalendar::fgSystemDefaultCenturyStartYear
= -1;
142 UBool
TaiwanCalendar::haveDefaultCentury() const
147 UDate
TaiwanCalendar::defaultCenturyStart() const
149 return internalGetDefaultCenturyStart();
152 int32_t TaiwanCalendar::defaultCenturyStartYear() const
154 return internalGetDefaultCenturyStartYear();
158 TaiwanCalendar::internalGetDefaultCenturyStart() const
160 // lazy-evaluate systemDefaultCenturyStart
162 UMTX_CHECK(NULL
, (fgSystemDefaultCenturyStart
== fgSystemDefaultCentury
), needsUpdate
);
165 initializeSystemDefaultCentury();
168 // use defaultCenturyStart unless it's the flag value;
169 // then use systemDefaultCenturyStart
171 return fgSystemDefaultCenturyStart
;
175 TaiwanCalendar::internalGetDefaultCenturyStartYear() const
177 // lazy-evaluate systemDefaultCenturyStartYear
179 UMTX_CHECK(NULL
, (fgSystemDefaultCenturyStart
== fgSystemDefaultCentury
), needsUpdate
);
182 initializeSystemDefaultCentury();
185 // use defaultCenturyStart unless it's the flag value;
186 // then use systemDefaultCenturyStartYear
188 return fgSystemDefaultCenturyStartYear
;
192 TaiwanCalendar::initializeSystemDefaultCentury()
194 // initialize systemDefaultCentury and systemDefaultCenturyYear based
195 // on the current time. They'll be set to 80 years before
197 UErrorCode status
= U_ZERO_ERROR
;
198 TaiwanCalendar
calendar(Locale("@calendar=roc"),status
);
199 if (U_SUCCESS(status
))
201 calendar
.setTime(Calendar::getNow(), status
);
202 calendar
.add(UCAL_YEAR
, -80, status
);
203 UDate newStart
= calendar
.getTime(status
);
204 int32_t newYear
= calendar
.get(UCAL_YEAR
, status
);
206 if (fgSystemDefaultCenturyStart
== fgSystemDefaultCentury
)
208 fgSystemDefaultCenturyStartYear
= newYear
;
209 fgSystemDefaultCenturyStart
= newStart
;
213 // We have no recourse upon failure unless we want to propagate the failure