intl/icu/source/i18n/buddhcal.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 *******************************************************************************
michael@0 3 * Copyright (C) 2003-2013, International Business Machines Corporation and *
michael@0 4 * others. All Rights Reserved. *
michael@0 5 *******************************************************************************
michael@0 6 *
michael@0 7 * File BUDDHCAL.CPP
michael@0 8 *
michael@0 9 * Modification History:
michael@0 10 * 05/13/2003 srl copied from gregocal.cpp
michael@0 11 *
michael@0 12 */
michael@0 13
michael@0 14 #include "unicode/utypes.h"
michael@0 15
michael@0 16 #if !UCONFIG_NO_FORMATTING
michael@0 17
michael@0 18 #include "buddhcal.h"
michael@0 19 #include "unicode/gregocal.h"
michael@0 20 #include "umutex.h"
michael@0 21 #include <float.h>
michael@0 22
michael@0 23 U_NAMESPACE_BEGIN
michael@0 24
michael@0 25 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(BuddhistCalendar)
michael@0 26
michael@0 27 //static const int32_t kMaxEra = 0; // only 1 era
michael@0 28
michael@0 29 static const int32_t kBuddhistEraStart = -543; // 544 BC (Gregorian)
michael@0 30
michael@0 31 static const int32_t kGregorianEpoch = 1970; // used as the default value of EXTENDED_YEAR
michael@0 32
michael@0 33 BuddhistCalendar::BuddhistCalendar(const Locale& aLocale, UErrorCode& success)
michael@0 34 : GregorianCalendar(aLocale, success)
michael@0 35 {
michael@0 36 setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
michael@0 37 }
michael@0 38
michael@0 39 BuddhistCalendar::~BuddhistCalendar()
michael@0 40 {
michael@0 41 }
michael@0 42
michael@0 43 BuddhistCalendar::BuddhistCalendar(const BuddhistCalendar& source)
michael@0 44 : GregorianCalendar(source)
michael@0 45 {
michael@0 46 }
michael@0 47
michael@0 48 BuddhistCalendar& BuddhistCalendar::operator= ( const BuddhistCalendar& right)
michael@0 49 {
michael@0 50 GregorianCalendar::operator=(right);
michael@0 51 return *this;
michael@0 52 }
michael@0 53
michael@0 54 Calendar* BuddhistCalendar::clone(void) const
michael@0 55 {
michael@0 56 return new BuddhistCalendar(*this);
michael@0 57 }
michael@0 58
michael@0 59 const char *BuddhistCalendar::getType() const
michael@0 60 {
michael@0 61 return "buddhist";
michael@0 62 }
michael@0 63
michael@0 64 int32_t BuddhistCalendar::handleGetExtendedYear()
michael@0 65 {
michael@0 66 // EXTENDED_YEAR in BuddhistCalendar is a Gregorian year.
michael@0 67 // The default value of EXTENDED_YEAR is 1970 (Buddhist 2513)
michael@0 68 int32_t year;
michael@0 69 if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
michael@0 70 year = internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch);
michael@0 71 } else {
michael@0 72 // extended year is a gregorian year, where 1 = 1AD, 0 = 1BC, -1 = 2BC, etc
michael@0 73 year = internalGet(UCAL_YEAR, kGregorianEpoch - kBuddhistEraStart)
michael@0 74 + kBuddhistEraStart;
michael@0 75 }
michael@0 76 return year;
michael@0 77 }
michael@0 78
michael@0 79 int32_t BuddhistCalendar::handleComputeMonthStart(int32_t eyear, int32_t month,
michael@0 80
michael@0 81 UBool useMonth) const
michael@0 82 {
michael@0 83 return GregorianCalendar::handleComputeMonthStart(eyear, month, useMonth);
michael@0 84 }
michael@0 85
michael@0 86 void BuddhistCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status)
michael@0 87 {
michael@0 88 GregorianCalendar::handleComputeFields(julianDay, status);
michael@0 89 int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kBuddhistEraStart;
michael@0 90 internalSet(UCAL_ERA, 0);
michael@0 91 internalSet(UCAL_YEAR, y);
michael@0 92 }
michael@0 93
michael@0 94 int32_t BuddhistCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
michael@0 95 {
michael@0 96 if(field == UCAL_ERA) {
michael@0 97 return BE;
michael@0 98 } else {
michael@0 99 return GregorianCalendar::handleGetLimit(field,limitType);
michael@0 100 }
michael@0 101 }
michael@0 102
michael@0 103 #if 0
michael@0 104 void BuddhistCalendar::timeToFields(UDate theTime, UBool quick, UErrorCode& status)
michael@0 105 {
michael@0 106 //Calendar::timeToFields(theTime, quick, status);
michael@0 107
michael@0 108 int32_t era = internalGet(UCAL_ERA);
michael@0 109 int32_t year = internalGet(UCAL_YEAR);
michael@0 110
michael@0 111 if(era == GregorianCalendar::BC) {
michael@0 112 year = 1-year;
michael@0 113 era = BuddhistCalendar::BE;
michael@0 114 } else if(era == GregorianCalendar::AD) {
michael@0 115 era = BuddhistCalendar::BE;
michael@0 116 } else {
michael@0 117 status = U_INTERNAL_PROGRAM_ERROR;
michael@0 118 }
michael@0 119
michael@0 120 year = year - kBuddhistEraStart;
michael@0 121
michael@0 122 internalSet(UCAL_ERA, era);
michael@0 123 internalSet(UCAL_YEAR, year);
michael@0 124 }
michael@0 125 #endif
michael@0 126
michael@0 127 /**
michael@0 128 * The system maintains a static default century start date. This is initialized
michael@0 129 * the first time it is used. Once the system default century date and year
michael@0 130 * are set, they do not change.
michael@0 131 */
michael@0 132 static UDate gSystemDefaultCenturyStart = DBL_MIN;
michael@0 133 static int32_t gSystemDefaultCenturyStartYear = -1;
michael@0 134 static icu::UInitOnce gBCInitOnce;
michael@0 135
michael@0 136
michael@0 137 UBool BuddhistCalendar::haveDefaultCentury() const
michael@0 138 {
michael@0 139 return TRUE;
michael@0 140 }
michael@0 141
michael@0 142 static void U_CALLCONV
michael@0 143 initializeSystemDefaultCentury()
michael@0 144 {
michael@0 145 // initialize systemDefaultCentury and systemDefaultCenturyYear based
michael@0 146 // on the current time. They'll be set to 80 years before
michael@0 147 // the current time.
michael@0 148 UErrorCode status = U_ZERO_ERROR;
michael@0 149 BuddhistCalendar calendar(Locale("@calendar=buddhist"),status);
michael@0 150 if (U_SUCCESS(status)) {
michael@0 151 calendar.setTime(Calendar::getNow(), status);
michael@0 152 calendar.add(UCAL_YEAR, -80, status);
michael@0 153 UDate newStart = calendar.getTime(status);
michael@0 154 int32_t newYear = calendar.get(UCAL_YEAR, status);
michael@0 155 gSystemDefaultCenturyStartYear = newYear;
michael@0 156 gSystemDefaultCenturyStart = newStart;
michael@0 157 }
michael@0 158 // We have no recourse upon failure unless we want to propagate the failure
michael@0 159 // out.
michael@0 160 }
michael@0 161
michael@0 162 UDate BuddhistCalendar::defaultCenturyStart() const
michael@0 163 {
michael@0 164 // lazy-evaluate systemDefaultCenturyStart and systemDefaultCenturyStartYear
michael@0 165 umtx_initOnce(gBCInitOnce, &initializeSystemDefaultCentury);
michael@0 166 return gSystemDefaultCenturyStart;
michael@0 167 }
michael@0 168
michael@0 169 int32_t BuddhistCalendar::defaultCenturyStartYear() const
michael@0 170 {
michael@0 171 // lazy-evaluate systemDefaultCenturyStartYear and systemDefaultCenturyStart
michael@0 172 umtx_initOnce(gBCInitOnce, &initializeSystemDefaultCentury);
michael@0 173 return gSystemDefaultCenturyStartYear;
michael@0 174 }
michael@0 175
michael@0 176
michael@0 177 U_NAMESPACE_END
michael@0 178
michael@0 179 #endif

mercurial