michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2003-2013, International Business Machines Corporation and * michael@0: * others. All Rights Reserved. * michael@0: ******************************************************************************* michael@0: * michael@0: * File BUDDHCAL.CPP michael@0: * michael@0: * Modification History: michael@0: * 05/13/2003 srl copied from gregocal.cpp michael@0: * michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "buddhcal.h" michael@0: #include "unicode/gregocal.h" michael@0: #include "umutex.h" michael@0: #include michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(BuddhistCalendar) michael@0: michael@0: //static const int32_t kMaxEra = 0; // only 1 era michael@0: michael@0: static const int32_t kBuddhistEraStart = -543; // 544 BC (Gregorian) michael@0: michael@0: static const int32_t kGregorianEpoch = 1970; // used as the default value of EXTENDED_YEAR michael@0: michael@0: BuddhistCalendar::BuddhistCalendar(const Locale& aLocale, UErrorCode& success) michael@0: : GregorianCalendar(aLocale, success) michael@0: { michael@0: setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly. michael@0: } michael@0: michael@0: BuddhistCalendar::~BuddhistCalendar() michael@0: { michael@0: } michael@0: michael@0: BuddhistCalendar::BuddhistCalendar(const BuddhistCalendar& source) michael@0: : GregorianCalendar(source) michael@0: { michael@0: } michael@0: michael@0: BuddhistCalendar& BuddhistCalendar::operator= ( const BuddhistCalendar& right) michael@0: { michael@0: GregorianCalendar::operator=(right); michael@0: return *this; michael@0: } michael@0: michael@0: Calendar* BuddhistCalendar::clone(void) const michael@0: { michael@0: return new BuddhistCalendar(*this); michael@0: } michael@0: michael@0: const char *BuddhistCalendar::getType() const michael@0: { michael@0: return "buddhist"; michael@0: } michael@0: michael@0: int32_t BuddhistCalendar::handleGetExtendedYear() michael@0: { michael@0: // EXTENDED_YEAR in BuddhistCalendar is a Gregorian year. michael@0: // The default value of EXTENDED_YEAR is 1970 (Buddhist 2513) michael@0: int32_t year; michael@0: if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { michael@0: year = internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch); michael@0: } else { michael@0: // extended year is a gregorian year, where 1 = 1AD, 0 = 1BC, -1 = 2BC, etc michael@0: year = internalGet(UCAL_YEAR, kGregorianEpoch - kBuddhistEraStart) michael@0: + kBuddhistEraStart; michael@0: } michael@0: return year; michael@0: } michael@0: michael@0: int32_t BuddhistCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, michael@0: michael@0: UBool useMonth) const michael@0: { michael@0: return GregorianCalendar::handleComputeMonthStart(eyear, month, useMonth); michael@0: } michael@0: michael@0: void BuddhistCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) michael@0: { michael@0: GregorianCalendar::handleComputeFields(julianDay, status); michael@0: int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kBuddhistEraStart; michael@0: internalSet(UCAL_ERA, 0); michael@0: internalSet(UCAL_YEAR, y); michael@0: } michael@0: michael@0: int32_t BuddhistCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const michael@0: { michael@0: if(field == UCAL_ERA) { michael@0: return BE; michael@0: } else { michael@0: return GregorianCalendar::handleGetLimit(field,limitType); michael@0: } michael@0: } michael@0: michael@0: #if 0 michael@0: void BuddhistCalendar::timeToFields(UDate theTime, UBool quick, UErrorCode& status) michael@0: { michael@0: //Calendar::timeToFields(theTime, quick, status); michael@0: michael@0: int32_t era = internalGet(UCAL_ERA); michael@0: int32_t year = internalGet(UCAL_YEAR); michael@0: michael@0: if(era == GregorianCalendar::BC) { michael@0: year = 1-year; michael@0: era = BuddhistCalendar::BE; michael@0: } else if(era == GregorianCalendar::AD) { michael@0: era = BuddhistCalendar::BE; michael@0: } else { michael@0: status = U_INTERNAL_PROGRAM_ERROR; michael@0: } michael@0: michael@0: year = year - kBuddhistEraStart; michael@0: michael@0: internalSet(UCAL_ERA, era); michael@0: internalSet(UCAL_YEAR, year); michael@0: } michael@0: #endif michael@0: michael@0: /** michael@0: * The system maintains a static default century start date. This is initialized michael@0: * the first time it is used. Once the system default century date and year michael@0: * are set, they do not change. michael@0: */ michael@0: static UDate gSystemDefaultCenturyStart = DBL_MIN; michael@0: static int32_t gSystemDefaultCenturyStartYear = -1; michael@0: static icu::UInitOnce gBCInitOnce; michael@0: michael@0: michael@0: UBool BuddhistCalendar::haveDefaultCentury() const michael@0: { michael@0: return TRUE; michael@0: } michael@0: michael@0: static void U_CALLCONV michael@0: initializeSystemDefaultCentury() michael@0: { michael@0: // initialize systemDefaultCentury and systemDefaultCenturyYear based michael@0: // on the current time. They'll be set to 80 years before michael@0: // the current time. michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: BuddhistCalendar calendar(Locale("@calendar=buddhist"),status); michael@0: if (U_SUCCESS(status)) { michael@0: calendar.setTime(Calendar::getNow(), status); michael@0: calendar.add(UCAL_YEAR, -80, status); michael@0: UDate newStart = calendar.getTime(status); michael@0: int32_t newYear = calendar.get(UCAL_YEAR, status); michael@0: gSystemDefaultCenturyStartYear = newYear; michael@0: gSystemDefaultCenturyStart = newStart; michael@0: } michael@0: // We have no recourse upon failure unless we want to propagate the failure michael@0: // out. michael@0: } michael@0: michael@0: UDate BuddhistCalendar::defaultCenturyStart() const michael@0: { michael@0: // lazy-evaluate systemDefaultCenturyStart and systemDefaultCenturyStartYear michael@0: umtx_initOnce(gBCInitOnce, &initializeSystemDefaultCentury); michael@0: return gSystemDefaultCenturyStart; michael@0: } michael@0: michael@0: int32_t BuddhistCalendar::defaultCenturyStartYear() const michael@0: { michael@0: // lazy-evaluate systemDefaultCenturyStartYear and systemDefaultCenturyStart michael@0: umtx_initOnce(gBCInitOnce, &initializeSystemDefaultCentury); michael@0: return gSystemDefaultCenturyStartYear; michael@0: } michael@0: michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif