1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/i18n/buddhcal.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,179 @@ 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 BUDDHCAL.CPP 1.11 +* 1.12 +* Modification History: 1.13 +* 05/13/2003 srl copied from gregocal.cpp 1.14 +* 1.15 +*/ 1.16 + 1.17 +#include "unicode/utypes.h" 1.18 + 1.19 +#if !UCONFIG_NO_FORMATTING 1.20 + 1.21 +#include "buddhcal.h" 1.22 +#include "unicode/gregocal.h" 1.23 +#include "umutex.h" 1.24 +#include <float.h> 1.25 + 1.26 +U_NAMESPACE_BEGIN 1.27 + 1.28 +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(BuddhistCalendar) 1.29 + 1.30 +//static const int32_t kMaxEra = 0; // only 1 era 1.31 + 1.32 +static const int32_t kBuddhistEraStart = -543; // 544 BC (Gregorian) 1.33 + 1.34 +static const int32_t kGregorianEpoch = 1970; // used as the default value of EXTENDED_YEAR 1.35 + 1.36 +BuddhistCalendar::BuddhistCalendar(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 +BuddhistCalendar::~BuddhistCalendar() 1.43 +{ 1.44 +} 1.45 + 1.46 +BuddhistCalendar::BuddhistCalendar(const BuddhistCalendar& source) 1.47 +: GregorianCalendar(source) 1.48 +{ 1.49 +} 1.50 + 1.51 +BuddhistCalendar& BuddhistCalendar::operator= ( const BuddhistCalendar& right) 1.52 +{ 1.53 + GregorianCalendar::operator=(right); 1.54 + return *this; 1.55 +} 1.56 + 1.57 +Calendar* BuddhistCalendar::clone(void) const 1.58 +{ 1.59 + return new BuddhistCalendar(*this); 1.60 +} 1.61 + 1.62 +const char *BuddhistCalendar::getType() const 1.63 +{ 1.64 + return "buddhist"; 1.65 +} 1.66 + 1.67 +int32_t BuddhistCalendar::handleGetExtendedYear() 1.68 +{ 1.69 + // EXTENDED_YEAR in BuddhistCalendar is a Gregorian year. 1.70 + // The default value of EXTENDED_YEAR is 1970 (Buddhist 2513) 1.71 + int32_t year; 1.72 + if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { 1.73 + year = internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch); 1.74 + } else { 1.75 + // extended year is a gregorian year, where 1 = 1AD, 0 = 1BC, -1 = 2BC, etc 1.76 + year = internalGet(UCAL_YEAR, kGregorianEpoch - kBuddhistEraStart) 1.77 + + kBuddhistEraStart; 1.78 + } 1.79 + return year; 1.80 +} 1.81 + 1.82 +int32_t BuddhistCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, 1.83 + 1.84 + UBool useMonth) const 1.85 +{ 1.86 + return GregorianCalendar::handleComputeMonthStart(eyear, month, useMonth); 1.87 +} 1.88 + 1.89 +void BuddhistCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) 1.90 +{ 1.91 + GregorianCalendar::handleComputeFields(julianDay, status); 1.92 + int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kBuddhistEraStart; 1.93 + internalSet(UCAL_ERA, 0); 1.94 + internalSet(UCAL_YEAR, y); 1.95 +} 1.96 + 1.97 +int32_t BuddhistCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const 1.98 +{ 1.99 + if(field == UCAL_ERA) { 1.100 + return BE; 1.101 + } else { 1.102 + return GregorianCalendar::handleGetLimit(field,limitType); 1.103 + } 1.104 +} 1.105 + 1.106 +#if 0 1.107 +void BuddhistCalendar::timeToFields(UDate theTime, UBool quick, UErrorCode& status) 1.108 +{ 1.109 + //Calendar::timeToFields(theTime, quick, status); 1.110 + 1.111 + int32_t era = internalGet(UCAL_ERA); 1.112 + int32_t year = internalGet(UCAL_YEAR); 1.113 + 1.114 + if(era == GregorianCalendar::BC) { 1.115 + year = 1-year; 1.116 + era = BuddhistCalendar::BE; 1.117 + } else if(era == GregorianCalendar::AD) { 1.118 + era = BuddhistCalendar::BE; 1.119 + } else { 1.120 + status = U_INTERNAL_PROGRAM_ERROR; 1.121 + } 1.122 + 1.123 + year = year - kBuddhistEraStart; 1.124 + 1.125 + internalSet(UCAL_ERA, era); 1.126 + internalSet(UCAL_YEAR, year); 1.127 +} 1.128 +#endif 1.129 + 1.130 +/** 1.131 + * The system maintains a static default century start date. This is initialized 1.132 + * the first time it is used. Once the system default century date and year 1.133 + * are set, they do not change. 1.134 + */ 1.135 +static UDate gSystemDefaultCenturyStart = DBL_MIN; 1.136 +static int32_t gSystemDefaultCenturyStartYear = -1; 1.137 +static icu::UInitOnce gBCInitOnce; 1.138 + 1.139 + 1.140 +UBool BuddhistCalendar::haveDefaultCentury() const 1.141 +{ 1.142 + return TRUE; 1.143 +} 1.144 + 1.145 +static void U_CALLCONV 1.146 +initializeSystemDefaultCentury() 1.147 +{ 1.148 + // initialize systemDefaultCentury and systemDefaultCenturyYear based 1.149 + // on the current time. They'll be set to 80 years before 1.150 + // the current time. 1.151 + UErrorCode status = U_ZERO_ERROR; 1.152 + BuddhistCalendar calendar(Locale("@calendar=buddhist"),status); 1.153 + if (U_SUCCESS(status)) { 1.154 + calendar.setTime(Calendar::getNow(), status); 1.155 + calendar.add(UCAL_YEAR, -80, status); 1.156 + UDate newStart = calendar.getTime(status); 1.157 + int32_t newYear = calendar.get(UCAL_YEAR, status); 1.158 + gSystemDefaultCenturyStartYear = newYear; 1.159 + gSystemDefaultCenturyStart = newStart; 1.160 + } 1.161 + // We have no recourse upon failure unless we want to propagate the failure 1.162 + // out. 1.163 +} 1.164 + 1.165 +UDate BuddhistCalendar::defaultCenturyStart() const 1.166 +{ 1.167 + // lazy-evaluate systemDefaultCenturyStart and systemDefaultCenturyStartYear 1.168 + umtx_initOnce(gBCInitOnce, &initializeSystemDefaultCentury); 1.169 + return gSystemDefaultCenturyStart; 1.170 +} 1.171 + 1.172 +int32_t BuddhistCalendar::defaultCenturyStartYear() const 1.173 +{ 1.174 + // lazy-evaluate systemDefaultCenturyStartYear and systemDefaultCenturyStart 1.175 + umtx_initOnce(gBCInitOnce, &initializeSystemDefaultCentury); 1.176 + return gSystemDefaultCenturyStartYear; 1.177 +} 1.178 + 1.179 + 1.180 +U_NAMESPACE_END 1.181 + 1.182 +#endif