]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/buddhcal.cpp
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / buddhcal.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/*
4*******************************************************************************
57a6839d 5* Copyright (C) 2003-2013, International Business Machines Corporation and *
b75a7d8f
A
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"
46f4442e 22#include "umutex.h"
b75a7d8f
A
23#include <float.h>
24
25U_NAMESPACE_BEGIN
26
374ca955 27UOBJECT_DEFINE_RTTI_IMPLEMENTATION(BuddhistCalendar)
b75a7d8f 28
51004dcb 29//static const int32_t kMaxEra = 0; // only 1 era
b75a7d8f
A
30
31static const int32_t kBuddhistEraStart = -543; // 544 BC (Gregorian)
32
46f4442e 33static const int32_t kGregorianEpoch = 1970; // used as the default value of EXTENDED_YEAR
b75a7d8f
A
34
35BuddhistCalendar::BuddhistCalendar(const Locale& aLocale, UErrorCode& success)
46f4442e 36: GregorianCalendar(aLocale, success)
b75a7d8f
A
37{
38 setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
39}
40
41BuddhistCalendar::~BuddhistCalendar()
42{
43}
44
45BuddhistCalendar::BuddhistCalendar(const BuddhistCalendar& source)
46f4442e 46: GregorianCalendar(source)
b75a7d8f
A
47{
48}
49
50BuddhistCalendar& BuddhistCalendar::operator= ( const BuddhistCalendar& right)
51{
46f4442e
A
52 GregorianCalendar::operator=(right);
53 return *this;
b75a7d8f
A
54}
55
56Calendar* BuddhistCalendar::clone(void) const
57{
46f4442e 58 return new BuddhistCalendar(*this);
b75a7d8f
A
59}
60
61const char *BuddhistCalendar::getType() const
62{
46f4442e 63 return "buddhist";
b75a7d8f
A
64}
65
374ca955
A
66int32_t BuddhistCalendar::handleGetExtendedYear()
67{
46f4442e
A
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;
374ca955
A
79}
80
81int32_t BuddhistCalendar::handleComputeMonthStart(int32_t eyear, int32_t month,
82
46f4442e 83 UBool useMonth) const
374ca955 84{
46f4442e 85 return GregorianCalendar::handleComputeMonthStart(eyear, month, useMonth);
374ca955
A
86}
87
88void BuddhistCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status)
89{
46f4442e
A
90 GregorianCalendar::handleComputeFields(julianDay, status);
91 int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kBuddhistEraStart;
92 internalSet(UCAL_ERA, 0);
93 internalSet(UCAL_YEAR, y);
374ca955
A
94}
95
96int32_t BuddhistCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
97{
46f4442e
A
98 if(field == UCAL_ERA) {
99 return BE;
100 } else {
101 return GregorianCalendar::handleGetLimit(field,limitType);
102 }
374ca955
A
103}
104
105#if 0
b75a7d8f
A
106void BuddhistCalendar::timeToFields(UDate theTime, UBool quick, UErrorCode& status)
107{
46f4442e 108 //Calendar::timeToFields(theTime, quick, status);
b75a7d8f 109
46f4442e
A
110 int32_t era = internalGet(UCAL_ERA);
111 int32_t year = internalGet(UCAL_YEAR);
112
113 if(era == GregorianCalendar::BC) {
114 year = 1-year;
115 era = BuddhistCalendar::BE;
116 } else if(era == GregorianCalendar::AD) {
117 era = BuddhistCalendar::BE;
b75a7d8f 118 } else {
46f4442e 119 status = U_INTERNAL_PROGRAM_ERROR;
b75a7d8f 120 }
b75a7d8f 121
46f4442e 122 year = year - kBuddhistEraStart;
b75a7d8f 123
46f4442e
A
124 internalSet(UCAL_ERA, era);
125 internalSet(UCAL_YEAR, year);
126}
127#endif
b75a7d8f 128
57a6839d
A
129/**
130 * The system maintains a static default century start date. This is initialized
131 * the first time it is used. Once the system default century date and year
132 * are set, they do not change.
133 */
134static UDate gSystemDefaultCenturyStart = DBL_MIN;
135static int32_t gSystemDefaultCenturyStartYear = -1;
3d1f044b 136static icu::UInitOnce gBCInitOnce = U_INITONCE_INITIALIZER;
b75a7d8f
A
137
138
139UBool BuddhistCalendar::haveDefaultCentury() const
140{
46f4442e 141 return TRUE;
b75a7d8f
A
142}
143
57a6839d
A
144static void U_CALLCONV
145initializeSystemDefaultCentury()
b75a7d8f 146{
46f4442e
A
147 // initialize systemDefaultCentury and systemDefaultCenturyYear based
148 // on the current time. They'll be set to 80 years before
149 // the current time.
729e4ab9
A
150 UErrorCode status = U_ZERO_ERROR;
151 BuddhistCalendar calendar(Locale("@calendar=buddhist"),status);
57a6839d 152 if (U_SUCCESS(status)) {
729e4ab9
A
153 calendar.setTime(Calendar::getNow(), status);
154 calendar.add(UCAL_YEAR, -80, status);
155 UDate newStart = calendar.getTime(status);
156 int32_t newYear = calendar.get(UCAL_YEAR, status);
57a6839d
A
157 gSystemDefaultCenturyStartYear = newYear;
158 gSystemDefaultCenturyStart = newStart;
b75a7d8f 159 }
729e4ab9
A
160 // We have no recourse upon failure unless we want to propagate the failure
161 // out.
b75a7d8f
A
162}
163
57a6839d
A
164UDate BuddhistCalendar::defaultCenturyStart() const
165{
166 // lazy-evaluate systemDefaultCenturyStart and systemDefaultCenturyStartYear
167 umtx_initOnce(gBCInitOnce, &initializeSystemDefaultCentury);
168 return gSystemDefaultCenturyStart;
169}
170
171int32_t BuddhistCalendar::defaultCenturyStartYear() const
172{
173 // lazy-evaluate systemDefaultCenturyStartYear and systemDefaultCenturyStart
174 umtx_initOnce(gBCInitOnce, &initializeSystemDefaultCentury);
175 return gSystemDefaultCenturyStartYear;
176}
177
b75a7d8f
A
178
179U_NAMESPACE_END
180
181#endif