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