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