1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/i18n/taiwncal.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,181 @@ 1.4 +/* 1.5 + ******************************************************************************* 1.6 + * Copyright (C) 2003-2013, International Business Machines Corporation and 1.7 + * others. All Rights Reserved. 1.8 + ******************************************************************************* 1.9 + * 1.10 + * File TAIWNCAL.CPP 1.11 + * 1.12 + * Modification History: 1.13 + * 05/13/2003 srl copied from gregocal.cpp 1.14 + * 06/29/2007 srl copied from buddhcal.cpp 1.15 + * 05/12/2008 jce modified to use calendar=roc per CLDR 1.16 + * 1.17 + */ 1.18 + 1.19 +#include "unicode/utypes.h" 1.20 + 1.21 +#if !UCONFIG_NO_FORMATTING 1.22 + 1.23 +#include "taiwncal.h" 1.24 +#include "unicode/gregocal.h" 1.25 +#include "umutex.h" 1.26 +#include <float.h> 1.27 + 1.28 +U_NAMESPACE_BEGIN 1.29 + 1.30 +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TaiwanCalendar) 1.31 + 1.32 +static const int32_t kTaiwanEraStart = 1911; // 1911 (Gregorian) 1.33 + 1.34 +static const int32_t kGregorianEpoch = 1970; 1.35 + 1.36 +TaiwanCalendar::TaiwanCalendar(const Locale& aLocale, UErrorCode& success) 1.37 +: GregorianCalendar(aLocale, success) 1.38 +{ 1.39 + setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly. 1.40 +} 1.41 + 1.42 +TaiwanCalendar::~TaiwanCalendar() 1.43 +{ 1.44 +} 1.45 + 1.46 +TaiwanCalendar::TaiwanCalendar(const TaiwanCalendar& source) 1.47 +: GregorianCalendar(source) 1.48 +{ 1.49 +} 1.50 + 1.51 +TaiwanCalendar& TaiwanCalendar::operator= ( const TaiwanCalendar& right) 1.52 +{ 1.53 + GregorianCalendar::operator=(right); 1.54 + return *this; 1.55 +} 1.56 + 1.57 +Calendar* TaiwanCalendar::clone(void) const 1.58 +{ 1.59 + return new TaiwanCalendar(*this); 1.60 +} 1.61 + 1.62 +const char *TaiwanCalendar::getType() const 1.63 +{ 1.64 + return "roc"; 1.65 +} 1.66 + 1.67 +int32_t TaiwanCalendar::handleGetExtendedYear() 1.68 +{ 1.69 + // EXTENDED_YEAR in TaiwanCalendar is a Gregorian year 1.70 + // The default value of EXTENDED_YEAR is 1970 (Minguo 59) 1.71 + int32_t year = kGregorianEpoch; 1.72 + 1.73 + if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR 1.74 + && newerField(UCAL_EXTENDED_YEAR, UCAL_ERA) == UCAL_EXTENDED_YEAR) { 1.75 + year = internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch); 1.76 + } else { 1.77 + int32_t era = internalGet(UCAL_ERA, MINGUO); 1.78 + if(era == MINGUO) { 1.79 + year = internalGet(UCAL_YEAR, 1) + kTaiwanEraStart; 1.80 + } else if(era == BEFORE_MINGUO) { 1.81 + year = 1 - internalGet(UCAL_YEAR, 1) + kTaiwanEraStart; 1.82 + } 1.83 + } 1.84 + return year; 1.85 +} 1.86 + 1.87 +void TaiwanCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) 1.88 +{ 1.89 + GregorianCalendar::handleComputeFields(julianDay, status); 1.90 + int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kTaiwanEraStart; 1.91 + if(y>0) { 1.92 + internalSet(UCAL_ERA, MINGUO); 1.93 + internalSet(UCAL_YEAR, y); 1.94 + } else { 1.95 + internalSet(UCAL_ERA, BEFORE_MINGUO); 1.96 + internalSet(UCAL_YEAR, 1-y); 1.97 + } 1.98 +} 1.99 + 1.100 +int32_t TaiwanCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const 1.101 +{ 1.102 + if(field == UCAL_ERA) { 1.103 + if(limitType == UCAL_LIMIT_MINIMUM || limitType == UCAL_LIMIT_GREATEST_MINIMUM) { 1.104 + return BEFORE_MINGUO; 1.105 + } else { 1.106 + return MINGUO; 1.107 + } 1.108 + } else { 1.109 + return GregorianCalendar::handleGetLimit(field,limitType); 1.110 + } 1.111 +} 1.112 + 1.113 +#if 0 1.114 +void TaiwanCalendar::timeToFields(UDate theTime, UBool quick, UErrorCode& status) 1.115 +{ 1.116 + //Calendar::timeToFields(theTime, quick, status); 1.117 + 1.118 + int32_t era = internalGet(UCAL_ERA); 1.119 + int32_t year = internalGet(UCAL_YEAR); 1.120 + 1.121 + if(era == GregorianCalendar::BC) { 1.122 + year = 1-year; 1.123 + era = TaiwanCalendar::MINGUO; 1.124 + } else if(era == GregorianCalendar::AD) { 1.125 + era = TaiwanCalendar::MINGUO; 1.126 + } else { 1.127 + status = U_INTERNAL_PROGRAM_ERROR; 1.128 + } 1.129 + 1.130 + year = year - kTaiwanEraStart; 1.131 + 1.132 + internalSet(UCAL_ERA, era); 1.133 + internalSet(UCAL_YEAR, year); 1.134 +} 1.135 +#endif 1.136 + 1.137 +/** 1.138 + * The system maintains a static default century start date and Year. They are 1.139 + * initialized the first time they are used. Once the system default century date 1.140 + * and year are set, they do not change. 1.141 + */ 1.142 +static UDate gSystemDefaultCenturyStart = DBL_MIN; 1.143 +static int32_t gSystemDefaultCenturyStartYear = -1; 1.144 +static icu::UInitOnce gSystemDefaultCenturyInit = U_INITONCE_INITIALIZER; 1.145 + 1.146 +UBool TaiwanCalendar::haveDefaultCentury() const 1.147 +{ 1.148 + return TRUE; 1.149 +} 1.150 + 1.151 +static void U_CALLCONV initializeSystemDefaultCentury() 1.152 +{ 1.153 + // initialize systemDefaultCentury and systemDefaultCenturyYear based 1.154 + // on the current time. They'll be set to 80 years before 1.155 + // the current time. 1.156 + UErrorCode status = U_ZERO_ERROR; 1.157 + TaiwanCalendar calendar(Locale("@calendar=roc"),status); 1.158 + if (U_SUCCESS(status)) 1.159 + { 1.160 + calendar.setTime(Calendar::getNow(), status); 1.161 + calendar.add(UCAL_YEAR, -80, status); 1.162 + 1.163 + gSystemDefaultCenturyStart = calendar.getTime(status); 1.164 + gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status); 1.165 + } 1.166 + // We have no recourse upon failure unless we want to propagate the failure 1.167 + // out. 1.168 +} 1.169 + 1.170 +UDate TaiwanCalendar::defaultCenturyStart() const { 1.171 + // lazy-evaluate systemDefaultCenturyStart 1.172 + umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); 1.173 + return gSystemDefaultCenturyStart; 1.174 +} 1.175 + 1.176 +int32_t TaiwanCalendar::defaultCenturyStartYear() const { 1.177 + // lazy-evaluate systemDefaultCenturyStartYear 1.178 + umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); 1.179 + return gSystemDefaultCenturyStartYear; 1.180 +} 1.181 + 1.182 +U_NAMESPACE_END 1.183 + 1.184 +#endif