• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 * Copyright (C) 2003-2008, International Business Machines Corporation and    *
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"
20 #include "umutex.h"
21 #include <float.h>
22 
23 U_NAMESPACE_BEGIN
24 
25 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(BuddhistCalendar)
26 
27 static const int32_t kMaxEra = 0; // only 1 era
28 
29 static const int32_t kBuddhistEraStart = -543;  // 544 BC (Gregorian)
30 
31 static const int32_t kGregorianEpoch = 1970;    // used as the default value of EXTENDED_YEAR
32 
BuddhistCalendar(const Locale & aLocale,UErrorCode & success)33 BuddhistCalendar::BuddhistCalendar(const Locale& aLocale, UErrorCode& success)
34 :   GregorianCalendar(aLocale, success)
35 {
36     setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
37 }
38 
~BuddhistCalendar()39 BuddhistCalendar::~BuddhistCalendar()
40 {
41 }
42 
BuddhistCalendar(const BuddhistCalendar & source)43 BuddhistCalendar::BuddhistCalendar(const BuddhistCalendar& source)
44 : GregorianCalendar(source)
45 {
46 }
47 
operator =(const BuddhistCalendar & right)48 BuddhistCalendar& BuddhistCalendar::operator= ( const BuddhistCalendar& right)
49 {
50     GregorianCalendar::operator=(right);
51     return *this;
52 }
53 
clone(void) const54 Calendar* BuddhistCalendar::clone(void) const
55 {
56     return new BuddhistCalendar(*this);
57 }
58 
getType() const59 const char *BuddhistCalendar::getType() const
60 {
61     return "buddhist";
62 }
63 
handleGetExtendedYear()64 int32_t BuddhistCalendar::handleGetExtendedYear()
65 {
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;
77 }
78 
handleComputeMonthStart(int32_t eyear,int32_t month,UBool useMonth) const79 int32_t BuddhistCalendar::handleComputeMonthStart(int32_t eyear, int32_t month,
80 
81                                                   UBool useMonth) const
82 {
83     return GregorianCalendar::handleComputeMonthStart(eyear, month, useMonth);
84 }
85 
handleComputeFields(int32_t julianDay,UErrorCode & status)86 void BuddhistCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status)
87 {
88     GregorianCalendar::handleComputeFields(julianDay, status);
89     int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kBuddhistEraStart;
90     internalSet(UCAL_ERA, 0);
91     internalSet(UCAL_YEAR, y);
92 }
93 
handleGetLimit(UCalendarDateFields field,ELimitType limitType) const94 int32_t BuddhistCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
95 {
96     if(field == UCAL_ERA) {
97         return BE;
98     } else {
99         return GregorianCalendar::handleGetLimit(field,limitType);
100     }
101 }
102 
103 #if 0
104 void BuddhistCalendar::timeToFields(UDate theTime, UBool quick, UErrorCode& status)
105 {
106     //Calendar::timeToFields(theTime, quick, status);
107 
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;
116     } else {
117         status = U_INTERNAL_PROGRAM_ERROR;
118     }
119 
120     year = year - kBuddhistEraStart;
121 
122     internalSet(UCAL_ERA, era);
123     internalSet(UCAL_YEAR, year);
124 }
125 #endif
126 
127 // default century
128 const UDate     BuddhistCalendar::fgSystemDefaultCentury        = DBL_MIN;
129 const int32_t   BuddhistCalendar::fgSystemDefaultCenturyYear    = -1;
130 
131 UDate           BuddhistCalendar::fgSystemDefaultCenturyStart       = DBL_MIN;
132 int32_t         BuddhistCalendar::fgSystemDefaultCenturyStartYear   = -1;
133 
134 
haveDefaultCentury() const135 UBool BuddhistCalendar::haveDefaultCentury() const
136 {
137     return TRUE;
138 }
139 
defaultCenturyStart() const140 UDate BuddhistCalendar::defaultCenturyStart() const
141 {
142     return internalGetDefaultCenturyStart();
143 }
144 
defaultCenturyStartYear() const145 int32_t BuddhistCalendar::defaultCenturyStartYear() const
146 {
147     return internalGetDefaultCenturyStartYear();
148 }
149 
150 UDate
internalGetDefaultCenturyStart() const151 BuddhistCalendar::internalGetDefaultCenturyStart() const
152 {
153     // lazy-evaluate systemDefaultCenturyStart
154     UBool needsUpdate;
155     UMTX_CHECK(NULL, (fgSystemDefaultCenturyStart == fgSystemDefaultCentury), needsUpdate);
156 
157     if (needsUpdate) {
158         initializeSystemDefaultCentury();
159     }
160 
161     // use defaultCenturyStart unless it's the flag value;
162     // then use systemDefaultCenturyStart
163 
164     return fgSystemDefaultCenturyStart;
165 }
166 
167 int32_t
internalGetDefaultCenturyStartYear() const168 BuddhistCalendar::internalGetDefaultCenturyStartYear() const
169 {
170     // lazy-evaluate systemDefaultCenturyStartYear
171     UBool needsUpdate;
172     UMTX_CHECK(NULL, (fgSystemDefaultCenturyStart == fgSystemDefaultCentury), needsUpdate);
173 
174     if (needsUpdate) {
175         initializeSystemDefaultCentury();
176     }
177 
178     // use defaultCenturyStart unless it's the flag value;
179     // then use systemDefaultCenturyStartYear
180 
181     return    fgSystemDefaultCenturyStartYear;
182 }
183 
184 void
initializeSystemDefaultCentury()185 BuddhistCalendar::initializeSystemDefaultCentury()
186 {
187     // initialize systemDefaultCentury and systemDefaultCenturyYear based
188     // on the current time.  They'll be set to 80 years before
189     // the current time.
190     UErrorCode status = U_ZERO_ERROR;
191     BuddhistCalendar calendar(Locale("@calendar=buddhist"),status);
192     if (U_SUCCESS(status))
193     {
194         calendar.setTime(Calendar::getNow(), status);
195         calendar.add(UCAL_YEAR, -80, status);
196         UDate    newStart =  calendar.getTime(status);
197         int32_t  newYear  =  calendar.get(UCAL_YEAR, status);
198         umtx_lock(NULL);
199         if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury) {
200             fgSystemDefaultCenturyStartYear = newYear;
201             fgSystemDefaultCenturyStart = newStart;
202         }
203         umtx_unlock(NULL);
204     }
205     // We have no recourse upon failure unless we want to propagate the failure
206     // out.
207 }
208 
209 
210 U_NAMESPACE_END
211 
212 #endif
213