Imported Upstream version 58.1
[platform/upstream/icu.git] / source / i18n / taiwncal.cpp
1 // Copyright (C) 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 TAIWNCAL.CPP
10  *
11  * Modification History:
12  *  05/13/2003    srl     copied from gregocal.cpp
13  *  06/29/2007    srl     copied from buddhcal.cpp
14  *  05/12/2008    jce     modified to use calendar=roc per CLDR
15  *
16  */
17
18 #include "unicode/utypes.h"
19
20 #if !UCONFIG_NO_FORMATTING
21
22 #include "taiwncal.h"
23 #include "unicode/gregocal.h"
24 #include "umutex.h"
25 #include <float.h>
26
27 U_NAMESPACE_BEGIN
28
29 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TaiwanCalendar)
30
31 static const int32_t kTaiwanEraStart = 1911;  // 1911 (Gregorian)
32
33 static const int32_t kGregorianEpoch = 1970; 
34
35 TaiwanCalendar::TaiwanCalendar(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
41 TaiwanCalendar::~TaiwanCalendar()
42 {
43 }
44
45 TaiwanCalendar::TaiwanCalendar(const TaiwanCalendar& source)
46 : GregorianCalendar(source)
47 {
48 }
49
50 TaiwanCalendar& TaiwanCalendar::operator= ( const TaiwanCalendar& right)
51 {
52     GregorianCalendar::operator=(right);
53     return *this;
54 }
55
56 Calendar* TaiwanCalendar::clone(void) const
57 {
58     return new TaiwanCalendar(*this);
59 }
60
61 const char *TaiwanCalendar::getType() const
62 {
63     return "roc";
64 }
65
66 int32_t TaiwanCalendar::handleGetExtendedYear()
67 {
68     // EXTENDED_YEAR in TaiwanCalendar is a Gregorian year
69     // The default value of EXTENDED_YEAR is 1970 (Minguo 59)
70     int32_t year = kGregorianEpoch;
71
72     if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR
73         && newerField(UCAL_EXTENDED_YEAR, UCAL_ERA) == UCAL_EXTENDED_YEAR) {
74         year = internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch);
75     } else {
76         int32_t era = internalGet(UCAL_ERA, MINGUO);
77         if(era == MINGUO) {
78             year =     internalGet(UCAL_YEAR, 1) + kTaiwanEraStart;
79         } else if(era == BEFORE_MINGUO) {
80             year = 1 - internalGet(UCAL_YEAR, 1) + kTaiwanEraStart;
81         }
82     }
83     return year;
84 }
85
86 void TaiwanCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status)
87 {
88     GregorianCalendar::handleComputeFields(julianDay, status);
89     int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kTaiwanEraStart;
90     if(y>0) {
91         internalSet(UCAL_ERA, MINGUO);
92         internalSet(UCAL_YEAR, y);
93     } else {
94         internalSet(UCAL_ERA, BEFORE_MINGUO);
95         internalSet(UCAL_YEAR, 1-y);
96     }
97 }
98
99 int32_t TaiwanCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
100 {
101     if(field == UCAL_ERA) {
102         if(limitType == UCAL_LIMIT_MINIMUM || limitType == UCAL_LIMIT_GREATEST_MINIMUM) {
103             return BEFORE_MINGUO;
104         } else {
105             return MINGUO;
106         }
107     } else {
108         return GregorianCalendar::handleGetLimit(field,limitType);
109     }
110 }
111
112 #if 0
113 void TaiwanCalendar::timeToFields(UDate theTime, UBool quick, UErrorCode& status)
114 {
115     //Calendar::timeToFields(theTime, quick, status);
116
117     int32_t era = internalGet(UCAL_ERA);
118     int32_t year = internalGet(UCAL_YEAR);
119
120     if(era == GregorianCalendar::BC) {
121         year = 1-year;
122         era = TaiwanCalendar::MINGUO;
123     } else if(era == GregorianCalendar::AD) {
124         era = TaiwanCalendar::MINGUO;
125     } else {
126         status = U_INTERNAL_PROGRAM_ERROR;
127     }
128
129     year = year - kTaiwanEraStart;
130
131     internalSet(UCAL_ERA, era);
132     internalSet(UCAL_YEAR, year);
133 }
134 #endif
135
136 /**
137  * The system maintains a static default century start date and Year.  They are
138  * initialized the first time they are used.  Once the system default century date 
139  * and year are set, they do not change.
140  */
141 static UDate           gSystemDefaultCenturyStart       = DBL_MIN;
142 static int32_t         gSystemDefaultCenturyStartYear   = -1;
143 static icu::UInitOnce  gSystemDefaultCenturyInit        = U_INITONCE_INITIALIZER;
144
145 UBool TaiwanCalendar::haveDefaultCentury() const
146 {
147     return TRUE;
148 }
149
150 static void U_CALLCONV initializeSystemDefaultCentury()
151 {
152     // initialize systemDefaultCentury and systemDefaultCenturyYear based
153     // on the current time.  They'll be set to 80 years before
154     // the current time.
155     UErrorCode status = U_ZERO_ERROR;
156     TaiwanCalendar calendar(Locale("@calendar=roc"),status);
157     if (U_SUCCESS(status))
158     {
159         calendar.setTime(Calendar::getNow(), status);
160         calendar.add(UCAL_YEAR, -80, status);
161
162         gSystemDefaultCenturyStart = calendar.getTime(status);
163         gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);
164     }
165     // We have no recourse upon failure unless we want to propagate the failure
166     // out.
167 }
168
169 UDate TaiwanCalendar::defaultCenturyStart() const {
170     // lazy-evaluate systemDefaultCenturyStart
171     umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
172     return gSystemDefaultCenturyStart;
173 }
174
175 int32_t TaiwanCalendar::defaultCenturyStartYear() const {
176     // lazy-evaluate systemDefaultCenturyStartYear
177     umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
178     return gSystemDefaultCenturyStartYear;
179 }
180
181 U_NAMESPACE_END
182
183 #endif