2 *******************************************************************************
3 * Copyright (C) 2003-2004, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 * Modification History:
10 * 05/13/2003 srl copied from gregocal.cpp
14 #include "unicode/utypes.h"
16 #if !UCONFIG_NO_FORMATTING
19 #include "unicode/gregocal.h"
25 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(BuddhistCalendar
)
27 static const int32_t kMaxEra
= 0; // only 1 era
29 static const int32_t kBuddhistEraStart
= -543; // 544 BC (Gregorian)
31 static const int32_t kGregorianEpoch
= 1970;
33 BuddhistCalendar::BuddhistCalendar(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 BuddhistCalendar::~BuddhistCalendar()
43 BuddhistCalendar::BuddhistCalendar(const BuddhistCalendar
& source
)
44 : GregorianCalendar(source
)
48 BuddhistCalendar
& BuddhistCalendar::operator= ( const BuddhistCalendar
& right
)
50 GregorianCalendar::operator=(right
);
54 Calendar
* BuddhistCalendar::clone(void) const
56 return new BuddhistCalendar(*this);
59 const char *BuddhistCalendar::getType() const
65 BuddhistCalendar::getMaximum(UCalendarDateFields field
) const
67 if(field
== UCAL_ERA
) {
70 return GregorianCalendar::getMaximum(field
);
75 BuddhistCalendar::getLeastMaximum(UCalendarDateFields field
) const
77 if(field
== UCAL_ERA
) {
80 return GregorianCalendar::getLeastMaximum(field
);
85 BuddhistCalendar::monthLength(int32_t month
, int32_t year
) const
87 return GregorianCalendar::monthLength(month
,year
);
92 BuddhistCalendar::monthLength(int32_t month
) const
94 UErrorCode status
= U_ZERO_ERROR
;
96 return GregorianCalendar::monthLength(month
, getGregorianYear(status
));
99 int32_t BuddhistCalendar::internalGetEra() const
101 return internalGet(UCAL_ERA
, BE
);
105 BuddhistCalendar::getGregorianYear(UErrorCode
&status
) const
107 int32_t year
= (fStamp
[UCAL_YEAR
] != kUnset
) ? internalGet(UCAL_YEAR
) : kGregorianEpoch
+kBuddhistEraStart
;
109 if (fStamp
[UCAL_ERA
] != kUnset
) {
110 era
= internalGet(UCAL_ERA
);
112 status
= U_ILLEGAL_ARGUMENT_ERROR
;
113 return kGregorianEpoch
+ kBuddhistEraStart
;
116 return year
+ kBuddhistEraStart
;
119 int32_t BuddhistCalendar::handleGetExtendedYear()
122 if (newerField(UCAL_EXTENDED_YEAR
, UCAL_YEAR
) == UCAL_EXTENDED_YEAR
) {
123 year
= internalGet(UCAL_EXTENDED_YEAR
, 1);
125 // Ignore the era, as there is only one
126 year
= internalGet(UCAL_YEAR
, 1);
131 int32_t BuddhistCalendar::handleComputeMonthStart(int32_t eyear
, int32_t month
,
133 UBool useMonth
) const
135 return GregorianCalendar::handleComputeMonthStart(eyear
+kBuddhistEraStart
, month
, useMonth
);
138 void BuddhistCalendar::handleComputeFields(int32_t julianDay
, UErrorCode
& status
)
140 GregorianCalendar::handleComputeFields(julianDay
, status
);
141 int32_t y
= internalGet(UCAL_EXTENDED_YEAR
) - kBuddhistEraStart
;
142 internalSet(UCAL_EXTENDED_YEAR
, y
);
143 internalSet(UCAL_ERA
, 0);
144 internalSet(UCAL_YEAR
, y
);
147 int32_t BuddhistCalendar::handleGetLimit(UCalendarDateFields field
, ELimitType limitType
) const
149 if(field
== UCAL_ERA
) {
152 return GregorianCalendar::handleGetLimit(field
,limitType
);
157 void BuddhistCalendar::timeToFields(UDate theTime
, UBool quick
, UErrorCode
& status
)
159 //Calendar::timeToFields(theTime, quick, status);
161 int32_t era
= internalGet(UCAL_ERA
);
162 int32_t year
= internalGet(UCAL_YEAR
);
164 if(era
== GregorianCalendar::BC
) {
166 era
= BuddhistCalendar::BE
;
167 } else if(era
== GregorianCalendar::AD
) {
168 era
= BuddhistCalendar::BE
;
170 status
= U_INTERNAL_PROGRAM_ERROR
;
173 year
= year
- kBuddhistEraStart
;
175 internalSet(UCAL_ERA
, era
);
176 internalSet(UCAL_YEAR
, year
);
180 void BuddhistCalendar::add(UCalendarDateFields field
, int32_t amount
, UErrorCode
& status
)
182 if (U_FAILURE(status
))
186 return; // Do nothing!
188 if(field
== UCAL_YEAR
/* || field == UCAL_YEAR_WOY */) {
189 int32_t year
= get(field
, status
); // not internalGet -- force completion
196 GregorianCalendar::add(field
,amount
,status
);
203 const UDate
BuddhistCalendar::fgSystemDefaultCentury
= DBL_MIN
;
204 const int32_t BuddhistCalendar::fgSystemDefaultCenturyYear
= -1;
206 UDate
BuddhistCalendar::fgSystemDefaultCenturyStart
= DBL_MIN
;
207 int32_t BuddhistCalendar::fgSystemDefaultCenturyStartYear
= -1;
210 UBool
BuddhistCalendar::haveDefaultCentury() const
215 UDate
BuddhistCalendar::defaultCenturyStart() const
217 return internalGetDefaultCenturyStart();
220 int32_t BuddhistCalendar::defaultCenturyStartYear() const
222 return internalGetDefaultCenturyStartYear();
226 BuddhistCalendar::internalGetDefaultCenturyStart() const
228 // lazy-evaluate systemDefaultCenturyStart
232 needsUpdate
= (fgSystemDefaultCenturyStart
== fgSystemDefaultCentury
);
236 initializeSystemDefaultCentury();
239 // use defaultCenturyStart unless it's the flag value;
240 // then use systemDefaultCenturyStart
242 return fgSystemDefaultCenturyStart
;
246 BuddhistCalendar::internalGetDefaultCenturyStartYear() const
248 // lazy-evaluate systemDefaultCenturyStartYear
252 needsUpdate
= (fgSystemDefaultCenturyStart
== fgSystemDefaultCentury
);
256 initializeSystemDefaultCentury();
259 // use defaultCenturyStart unless it's the flag value;
260 // then use systemDefaultCenturyStartYear
262 return fgSystemDefaultCenturyStartYear
;
266 BuddhistCalendar::initializeSystemDefaultCentury()
268 // initialize systemDefaultCentury and systemDefaultCenturyYear based
269 // on the current time. They'll be set to 80 years before
271 // No point in locking as it should be idempotent.
272 if (fgSystemDefaultCenturyStart
== fgSystemDefaultCentury
)
274 UErrorCode status
= U_ZERO_ERROR
;
275 BuddhistCalendar
calendar(Locale("@calendar=buddhist"),status
);
276 if (U_SUCCESS(status
))
278 calendar
.setTime(Calendar::getNow(), status
);
279 calendar
.add(UCAL_YEAR
, -80, status
);
280 UDate newStart
= calendar
.getTime(status
);
281 int32_t newYear
= calendar
.get(UCAL_YEAR
, status
);
284 fgSystemDefaultCenturyStart
= newStart
;
285 fgSystemDefaultCenturyStartYear
= newYear
;
288 // We have no recourse upon failure unless we want to propagate the failure