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 TAIWNCAL.CPP michael@0: * michael@0: * Modification History: michael@0: * 05/13/2003 srl copied from gregocal.cpp michael@0: * 06/29/2007 srl copied from buddhcal.cpp michael@0: * 05/12/2008 jce modified to use calendar=roc per CLDR 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 "taiwncal.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(TaiwanCalendar) michael@0: michael@0: static const int32_t kTaiwanEraStart = 1911; // 1911 (Gregorian) michael@0: michael@0: static const int32_t kGregorianEpoch = 1970; michael@0: michael@0: TaiwanCalendar::TaiwanCalendar(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: TaiwanCalendar::~TaiwanCalendar() michael@0: { michael@0: } michael@0: michael@0: TaiwanCalendar::TaiwanCalendar(const TaiwanCalendar& source) michael@0: : GregorianCalendar(source) michael@0: { michael@0: } michael@0: michael@0: TaiwanCalendar& TaiwanCalendar::operator= ( const TaiwanCalendar& right) michael@0: { michael@0: GregorianCalendar::operator=(right); michael@0: return *this; michael@0: } michael@0: michael@0: Calendar* TaiwanCalendar::clone(void) const michael@0: { michael@0: return new TaiwanCalendar(*this); michael@0: } michael@0: michael@0: const char *TaiwanCalendar::getType() const michael@0: { michael@0: return "roc"; michael@0: } michael@0: michael@0: int32_t TaiwanCalendar::handleGetExtendedYear() michael@0: { michael@0: // EXTENDED_YEAR in TaiwanCalendar is a Gregorian year michael@0: // The default value of EXTENDED_YEAR is 1970 (Minguo 59) michael@0: int32_t year = kGregorianEpoch; michael@0: michael@0: if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR michael@0: && newerField(UCAL_EXTENDED_YEAR, UCAL_ERA) == UCAL_EXTENDED_YEAR) { michael@0: year = internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch); michael@0: } else { michael@0: int32_t era = internalGet(UCAL_ERA, MINGUO); michael@0: if(era == MINGUO) { michael@0: year = internalGet(UCAL_YEAR, 1) + kTaiwanEraStart; michael@0: } else if(era == BEFORE_MINGUO) { michael@0: year = 1 - internalGet(UCAL_YEAR, 1) + kTaiwanEraStart; michael@0: } michael@0: } michael@0: return year; michael@0: } michael@0: michael@0: void TaiwanCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) michael@0: { michael@0: GregorianCalendar::handleComputeFields(julianDay, status); michael@0: int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kTaiwanEraStart; michael@0: if(y>0) { michael@0: internalSet(UCAL_ERA, MINGUO); michael@0: internalSet(UCAL_YEAR, y); michael@0: } else { michael@0: internalSet(UCAL_ERA, BEFORE_MINGUO); michael@0: internalSet(UCAL_YEAR, 1-y); michael@0: } michael@0: } michael@0: michael@0: int32_t TaiwanCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const michael@0: { michael@0: if(field == UCAL_ERA) { michael@0: if(limitType == UCAL_LIMIT_MINIMUM || limitType == UCAL_LIMIT_GREATEST_MINIMUM) { michael@0: return BEFORE_MINGUO; michael@0: } else { michael@0: return MINGUO; michael@0: } michael@0: } else { michael@0: return GregorianCalendar::handleGetLimit(field,limitType); michael@0: } michael@0: } michael@0: michael@0: #if 0 michael@0: void TaiwanCalendar::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 = TaiwanCalendar::MINGUO; michael@0: } else if(era == GregorianCalendar::AD) { michael@0: era = TaiwanCalendar::MINGUO; michael@0: } else { michael@0: status = U_INTERNAL_PROGRAM_ERROR; michael@0: } michael@0: michael@0: year = year - kTaiwanEraStart; 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 and Year. They are michael@0: * initialized the first time they are used. Once the system default century date michael@0: * and year 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 gSystemDefaultCenturyInit = U_INITONCE_INITIALIZER; michael@0: michael@0: UBool TaiwanCalendar::haveDefaultCentury() const michael@0: { michael@0: return TRUE; michael@0: } michael@0: michael@0: static void U_CALLCONV 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: TaiwanCalendar calendar(Locale("@calendar=roc"),status); michael@0: if (U_SUCCESS(status)) michael@0: { michael@0: calendar.setTime(Calendar::getNow(), status); michael@0: calendar.add(UCAL_YEAR, -80, status); michael@0: michael@0: gSystemDefaultCenturyStart = calendar.getTime(status); michael@0: gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status); 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 TaiwanCalendar::defaultCenturyStart() const { michael@0: // lazy-evaluate systemDefaultCenturyStart michael@0: umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); michael@0: return gSystemDefaultCenturyStart; michael@0: } michael@0: michael@0: int32_t TaiwanCalendar::defaultCenturyStartYear() const { michael@0: // lazy-evaluate systemDefaultCenturyStartYear michael@0: umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); michael@0: return gSystemDefaultCenturyStartYear; michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif