2 *******************************************************************************
3 * Copyright (C) 2003-2007, 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; // used as the default value of EXTENDED_YEAR
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
64 int32_t BuddhistCalendar::handleGetExtendedYear()
66 // EXTENDED_YEAR in BuddhistCalendar is a Gregorian year.
67 // The default value of EXTENDED_YEAR is 1970 (Buddhist 2513)
69 if (newerField(UCAL_EXTENDED_YEAR
, UCAL_YEAR
) == UCAL_EXTENDED_YEAR
) {
70 year
= internalGet(UCAL_EXTENDED_YEAR
, kGregorianEpoch
);
72 // extended year is a gregorian year, where 1 = 1AD, 0 = 1BC, -1 = 2BC, etc
73 year
= internalGet(UCAL_YEAR
, kGregorianEpoch
- kBuddhistEraStart
)
79 int32_t BuddhistCalendar::handleComputeMonthStart(int32_t eyear
, int32_t month
,
83 return GregorianCalendar::handleComputeMonthStart(eyear
, month
, useMonth
);
86 void BuddhistCalendar::handleComputeFields(int32_t julianDay
, UErrorCode
& status
)
88 GregorianCalendar::handleComputeFields(julianDay
, status
);
89 int32_t y
= internalGet(UCAL_EXTENDED_YEAR
) - kBuddhistEraStart
;
90 internalSet(UCAL_ERA
, 0);
91 internalSet(UCAL_YEAR
, y
);
94 int32_t BuddhistCalendar::handleGetLimit(UCalendarDateFields field
, ELimitType limitType
) const
96 if(field
== UCAL_ERA
) {
99 return GregorianCalendar::handleGetLimit(field
,limitType
);
104 void BuddhistCalendar::timeToFields(UDate theTime
, UBool quick
, UErrorCode
& status
)
106 //Calendar::timeToFields(theTime, quick, status);
108 int32_t era
= internalGet(UCAL_ERA
);
109 int32_t year
= internalGet(UCAL_YEAR
);
111 if(era
== GregorianCalendar::BC
) {
113 era
= BuddhistCalendar::BE
;
114 } else if(era
== GregorianCalendar::AD
) {
115 era
= BuddhistCalendar::BE
;
117 status
= U_INTERNAL_PROGRAM_ERROR
;
120 year
= year
- kBuddhistEraStart
;
122 internalSet(UCAL_ERA
, era
);
123 internalSet(UCAL_YEAR
, year
);
128 const UDate
BuddhistCalendar::fgSystemDefaultCentury
= DBL_MIN
;
129 const int32_t BuddhistCalendar::fgSystemDefaultCenturyYear
= -1;
131 UDate
BuddhistCalendar::fgSystemDefaultCenturyStart
= DBL_MIN
;
132 int32_t BuddhistCalendar::fgSystemDefaultCenturyStartYear
= -1;
135 UBool
BuddhistCalendar::haveDefaultCentury() const
140 UDate
BuddhistCalendar::defaultCenturyStart() const
142 return internalGetDefaultCenturyStart();
145 int32_t BuddhistCalendar::defaultCenturyStartYear() const
147 return internalGetDefaultCenturyStartYear();
151 BuddhistCalendar::internalGetDefaultCenturyStart() const
153 // lazy-evaluate systemDefaultCenturyStart
155 UMTX_CHECK(NULL
, (fgSystemDefaultCenturyStart
== fgSystemDefaultCentury
), needsUpdate
);
158 initializeSystemDefaultCentury();
161 // use defaultCenturyStart unless it's the flag value;
162 // then use systemDefaultCenturyStart
164 return fgSystemDefaultCenturyStart
;
168 BuddhistCalendar::internalGetDefaultCenturyStartYear() const
170 // lazy-evaluate systemDefaultCenturyStartYear
172 UMTX_CHECK(NULL
, (fgSystemDefaultCenturyStart
== fgSystemDefaultCentury
), needsUpdate
);
175 initializeSystemDefaultCentury();
178 // use defaultCenturyStart unless it's the flag value;
179 // then use systemDefaultCenturyStartYear
181 return fgSystemDefaultCenturyStartYear
;
185 BuddhistCalendar::initializeSystemDefaultCentury()
187 // initialize systemDefaultCentury and systemDefaultCenturyYear based
188 // on the current time. They'll be set to 80 years before
190 // No point in locking as it should be idempotent.
191 if (fgSystemDefaultCenturyStart
== fgSystemDefaultCentury
)
193 UErrorCode status
= U_ZERO_ERROR
;
194 BuddhistCalendar
calendar(Locale("@calendar=buddhist"),status
);
195 if (U_SUCCESS(status
))
197 calendar
.setTime(Calendar::getNow(), status
);
198 calendar
.add(UCAL_YEAR
, -80, status
);
199 UDate newStart
= calendar
.getTime(status
);
200 int32_t newYear
= calendar
.get(UCAL_YEAR
, status
);
203 fgSystemDefaultCenturyStart
= newStart
;
204 fgSystemDefaultCenturyStartYear
= newYear
;
208 // We have no recourse upon failure unless we want to propagate the failure