/src/icu/source/i18n/buddhcal.cpp
Line  | Count  | Source (jump to first uncovered line)  | 
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  |  |  | 
35  |  | BuddhistCalendar::BuddhistCalendar(const Locale& aLocale, UErrorCode& success)  | 
36  | 0  | :   GregorianCalendar(aLocale, success)  | 
37  | 0  | { | 
38  | 0  |     setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.  | 
39  | 0  | }  | 
40  |  |  | 
41  |  | BuddhistCalendar::~BuddhistCalendar()  | 
42  | 0  | { | 
43  | 0  | }  | 
44  |  |  | 
45  |  | BuddhistCalendar::BuddhistCalendar(const BuddhistCalendar& source)  | 
46  | 0  | : GregorianCalendar(source)  | 
47  | 0  | { | 
48  | 0  | }  | 
49  |  |  | 
50  |  | BuddhistCalendar& BuddhistCalendar::operator= ( const BuddhistCalendar& right)  | 
51  | 0  | { | 
52  | 0  |     GregorianCalendar::operator=(right);  | 
53  | 0  |     return *this;  | 
54  | 0  | }  | 
55  |  |  | 
56  |  | BuddhistCalendar* BuddhistCalendar::clone() const  | 
57  | 0  | { | 
58  | 0  |     return new BuddhistCalendar(*this);  | 
59  | 0  | }  | 
60  |  |  | 
61  |  | const char *BuddhistCalendar::getType() const  | 
62  | 0  | { | 
63  | 0  |     return "buddhist";  | 
64  | 0  | }  | 
65  |  |  | 
66  |  | int32_t BuddhistCalendar::handleGetExtendedYear()  | 
67  | 0  | { | 
68  |  |     // EXTENDED_YEAR in BuddhistCalendar is a Gregorian year.  | 
69  |  |     // The default value of EXTENDED_YEAR is 1970 (Buddhist 2513)  | 
70  | 0  |     int32_t year;  | 
71  | 0  |     if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { | 
72  | 0  |         year = internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch);  | 
73  | 0  |     } else { | 
74  |  |         // extended year is a gregorian year, where 1 = 1AD,  0 = 1BC, -1 = 2BC, etc   | 
75  | 0  |         year = internalGet(UCAL_YEAR, kGregorianEpoch - kBuddhistEraStart)  | 
76  | 0  |                 + kBuddhistEraStart;  | 
77  | 0  |     }  | 
78  | 0  |     return year;  | 
79  | 0  | }  | 
80  |  |  | 
81  |  | int32_t BuddhistCalendar::handleComputeMonthStart(int32_t eyear, int32_t month,  | 
82  |  |  | 
83  |  |                                                   UBool useMonth) const  | 
84  | 0  | { | 
85  | 0  |     return GregorianCalendar::handleComputeMonthStart(eyear, month, useMonth);  | 
86  | 0  | }  | 
87  |  |  | 
88  |  | void BuddhistCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status)  | 
89  | 0  | { | 
90  | 0  |     GregorianCalendar::handleComputeFields(julianDay, status);  | 
91  | 0  |     int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kBuddhistEraStart;  | 
92  | 0  |     internalSet(UCAL_ERA, 0);  | 
93  | 0  |     internalSet(UCAL_YEAR, y);  | 
94  | 0  | }  | 
95  |  |  | 
96  |  | int32_t BuddhistCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const  | 
97  | 0  | { | 
98  | 0  |     if(field == UCAL_ERA) { | 
99  | 0  |         return BE;  | 
100  | 0  |     } else { | 
101  | 0  |         return GregorianCalendar::handleGetLimit(field,limitType);  | 
102  | 0  |     }  | 
103  | 0  | }  | 
104  |  |  | 
105  |  | #if 0  | 
106  |  | void BuddhistCalendar::timeToFields(UDate theTime, UBool quick, UErrorCode& status)  | 
107  |  | { | 
108  |  |     //Calendar::timeToFields(theTime, quick, status);  | 
109  |  |  | 
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;  | 
118  |  |     } else { | 
119  |  |         status = U_INTERNAL_PROGRAM_ERROR;  | 
120  |  |     }  | 
121  |  |  | 
122  |  |     year = year - kBuddhistEraStart;  | 
123  |  |  | 
124  |  |     internalSet(UCAL_ERA, era);  | 
125  |  |     internalSet(UCAL_YEAR, year);  | 
126  |  | }  | 
127  |  | #endif  | 
128  |  |  | 
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  |  |  */  | 
134  |  | static UDate     gSystemDefaultCenturyStart       = DBL_MIN;  | 
135  |  | static int32_t   gSystemDefaultCenturyStartYear   = -1;  | 
136  |  | static icu::UInitOnce gBCInitOnce = U_INITONCE_INITIALIZER;  | 
137  |  |  | 
138  |  |  | 
139  |  | UBool BuddhistCalendar::haveDefaultCentury() const  | 
140  | 0  | { | 
141  | 0  |     return TRUE;  | 
142  | 0  | }  | 
143  |  |  | 
144  |  | static void U_CALLCONV  | 
145  |  | initializeSystemDefaultCentury()  | 
146  | 0  | { | 
147  |  |     // initialize systemDefaultCentury and systemDefaultCenturyYear based  | 
148  |  |     // on the current time.  They'll be set to 80 years before  | 
149  |  |     // the current time.  | 
150  | 0  |     UErrorCode status = U_ZERO_ERROR;  | 
151  | 0  |     BuddhistCalendar calendar(Locale("@calendar=buddhist"),status); | 
152  | 0  |     if (U_SUCCESS(status)) { | 
153  | 0  |         calendar.setTime(Calendar::getNow(), status);  | 
154  | 0  |         calendar.add(UCAL_YEAR, -80, status);  | 
155  | 0  |         UDate    newStart =  calendar.getTime(status);  | 
156  | 0  |         int32_t  newYear  =  calendar.get(UCAL_YEAR, status);  | 
157  | 0  |         gSystemDefaultCenturyStartYear = newYear;  | 
158  | 0  |         gSystemDefaultCenturyStart = newStart;  | 
159  | 0  |     }  | 
160  |  |     // We have no recourse upon failure unless we want to propagate the failure  | 
161  |  |     // out.  | 
162  | 0  | }  | 
163  |  |  | 
164  |  | UDate BuddhistCalendar::defaultCenturyStart() const  | 
165  | 0  | { | 
166  |  |     // lazy-evaluate systemDefaultCenturyStart and systemDefaultCenturyStartYear  | 
167  | 0  |     umtx_initOnce(gBCInitOnce, &initializeSystemDefaultCentury);  | 
168  | 0  |     return gSystemDefaultCenturyStart;  | 
169  | 0  | }  | 
170  |  |  | 
171  |  | int32_t BuddhistCalendar::defaultCenturyStartYear() const  | 
172  | 0  | { | 
173  |  |     // lazy-evaluate systemDefaultCenturyStartYear and systemDefaultCenturyStart   | 
174  | 0  |     umtx_initOnce(gBCInitOnce, &initializeSystemDefaultCentury);  | 
175  | 0  |     return gSystemDefaultCenturyStartYear;  | 
176  | 0  | }  | 
177  |  |  | 
178  |  |  | 
179  |  | U_NAMESPACE_END  | 
180  |  |  | 
181  |  | #endif  |