• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 "gregoimp.h"
24 #include "unicode/gregocal.h"
25 #include "umutex.h"
26 #include <float.h>
27 
28 U_NAMESPACE_BEGIN
29 
30 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TaiwanCalendar)
31 
32 static const int32_t kTaiwanEraStart = 1911;  // 1911 (Gregorian)
33 
34 static const int32_t kGregorianEpoch = 1970;
35 
TaiwanCalendar(const Locale & aLocale,UErrorCode & success)36 TaiwanCalendar::TaiwanCalendar(const Locale& aLocale, UErrorCode& success)
37 :   GregorianCalendar(aLocale, success)
38 {
39     setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
40 }
41 
~TaiwanCalendar()42 TaiwanCalendar::~TaiwanCalendar()
43 {
44 }
45 
TaiwanCalendar(const TaiwanCalendar & source)46 TaiwanCalendar::TaiwanCalendar(const TaiwanCalendar& source)
47 : GregorianCalendar(source)
48 {
49 }
50 
operator =(const TaiwanCalendar & right)51 TaiwanCalendar& TaiwanCalendar::operator= ( const TaiwanCalendar& right)
52 {
53     GregorianCalendar::operator=(right);
54     return *this;
55 }
56 
clone() const57 TaiwanCalendar* TaiwanCalendar::clone() const
58 {
59     return new TaiwanCalendar(*this);
60 }
61 
getType() const62 const char *TaiwanCalendar::getType() const
63 {
64     return "roc";
65 }
66 
handleGetExtendedYear(UErrorCode & status)67 int32_t TaiwanCalendar::handleGetExtendedYear(UErrorCode& status)
68 {
69     if (U_FAILURE(status)) {
70         return 0;
71     }
72 
73     // EXTENDED_YEAR in TaiwanCalendar is a Gregorian year
74     // The default value of EXTENDED_YEAR is 1970 (Minguo 59)
75     if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR
76         && newerField(UCAL_EXTENDED_YEAR, UCAL_ERA) == UCAL_EXTENDED_YEAR) {
77         return internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch);
78     }
79     int32_t era = internalGet(UCAL_ERA, MINGUO);
80     int32_t year = internalGet(UCAL_YEAR, 1);
81     switch (era) {
82         case MINGUO:
83             if (uprv_add32_overflow(year, kTaiwanEraStart, &year)) {
84                 status = U_ILLEGAL_ARGUMENT_ERROR;
85                 return 0;
86             }
87             return year;
88         case BEFORE_MINGUO:
89             if (uprv_add32_overflow(1 + kTaiwanEraStart, -year, &year)) {
90                 status = U_ILLEGAL_ARGUMENT_ERROR;
91                 return 0;
92             }
93             return year;
94         default:
95             status = U_ILLEGAL_ARGUMENT_ERROR;
96             return 0;
97     }
98 }
99 
handleComputeFields(int32_t julianDay,UErrorCode & status)100 void TaiwanCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status)
101 {
102     GregorianCalendar::handleComputeFields(julianDay, status);
103     int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kTaiwanEraStart;
104     if(y>0) {
105         internalSet(UCAL_ERA, MINGUO);
106         internalSet(UCAL_YEAR, y);
107     } else {
108         internalSet(UCAL_ERA, BEFORE_MINGUO);
109         internalSet(UCAL_YEAR, 1-y);
110     }
111 }
112 
handleGetLimit(UCalendarDateFields field,ELimitType limitType) const113 int32_t TaiwanCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
114 {
115     if(field != UCAL_ERA) {
116         return GregorianCalendar::handleGetLimit(field,limitType);
117     }
118     if (limitType == UCAL_LIMIT_MINIMUM || limitType == UCAL_LIMIT_GREATEST_MINIMUM) {
119         return BEFORE_MINGUO;
120     }
121     return MINGUO;
122 }
123 
124 IMPL_SYSTEM_DEFAULT_CENTURY(TaiwanCalendar, "@calendar=roc")
125 
126 U_NAMESPACE_END
127 
128 #endif
129