/src/icu/icu4c/source/i18n/taiwncal.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 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 | | |
36 | | TaiwanCalendar::TaiwanCalendar(const Locale& aLocale, UErrorCode& success) |
37 | 148 | : GregorianCalendar(aLocale, success) |
38 | 148 | { |
39 | 148 | } |
40 | | |
41 | | TaiwanCalendar::~TaiwanCalendar() |
42 | 17.6k | { |
43 | 17.6k | } |
44 | | |
45 | | TaiwanCalendar::TaiwanCalendar(const TaiwanCalendar& source) |
46 | 17.4k | : GregorianCalendar(source) |
47 | 17.4k | { |
48 | 17.4k | } |
49 | | |
50 | | TaiwanCalendar* TaiwanCalendar::clone() const |
51 | 17.4k | { |
52 | 17.4k | return new TaiwanCalendar(*this); |
53 | 17.4k | } |
54 | | |
55 | | const char *TaiwanCalendar::getType() const |
56 | 31 | { |
57 | 31 | return "roc"; |
58 | 31 | } |
59 | | |
60 | | int32_t TaiwanCalendar::handleGetExtendedYear(UErrorCode& status) |
61 | 19.3k | { |
62 | 19.3k | if (U_FAILURE(status)) { |
63 | 0 | return 0; |
64 | 0 | } |
65 | | |
66 | | // EXTENDED_YEAR in TaiwanCalendar is a Gregorian year |
67 | | // The default value of EXTENDED_YEAR is 1970 (Minguo 59) |
68 | 19.3k | if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR |
69 | 19.3k | && newerField(UCAL_EXTENDED_YEAR, UCAL_ERA) == UCAL_EXTENDED_YEAR) { |
70 | 17.4k | return internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch); |
71 | 17.4k | } |
72 | 1.84k | int32_t era = internalGet(UCAL_ERA, MINGUO); |
73 | 1.84k | int32_t year = internalGet(UCAL_YEAR, 1); |
74 | 1.84k | switch (era) { |
75 | 715 | case MINGUO: |
76 | 715 | if (uprv_add32_overflow(year, kTaiwanEraStart, &year)) { |
77 | 21 | status = U_ILLEGAL_ARGUMENT_ERROR; |
78 | 21 | return 0; |
79 | 21 | } |
80 | 694 | return year; |
81 | 1.11k | case BEFORE_MINGUO: |
82 | 1.11k | if (uprv_add32_overflow(1 + kTaiwanEraStart, -year, &year)) { |
83 | 18 | status = U_ILLEGAL_ARGUMENT_ERROR; |
84 | 18 | return 0; |
85 | 18 | } |
86 | 1.09k | return year; |
87 | 18 | default: |
88 | 18 | status = U_ILLEGAL_ARGUMENT_ERROR; |
89 | 18 | return 0; |
90 | 1.84k | } |
91 | 1.84k | } |
92 | | |
93 | | void TaiwanCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) |
94 | 37.7k | { |
95 | 37.7k | GregorianCalendar::handleComputeFields(julianDay, status); |
96 | 37.7k | int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kTaiwanEraStart; |
97 | 37.7k | if(y>0) { |
98 | 34.3k | internalSet(UCAL_ERA, MINGUO); |
99 | 34.3k | internalSet(UCAL_YEAR, y); |
100 | 34.3k | } else { |
101 | 3.38k | internalSet(UCAL_ERA, BEFORE_MINGUO); |
102 | 3.38k | internalSet(UCAL_YEAR, 1-y); |
103 | 3.38k | } |
104 | 37.7k | } |
105 | | |
106 | | int32_t TaiwanCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const |
107 | 36.3k | { |
108 | 36.3k | if(field != UCAL_ERA) { |
109 | 34.6k | return GregorianCalendar::handleGetLimit(field,limitType); |
110 | 34.6k | } |
111 | 1.71k | if (limitType == UCAL_LIMIT_MINIMUM || limitType == UCAL_LIMIT_GREATEST_MINIMUM) { |
112 | 571 | return BEFORE_MINGUO; |
113 | 571 | } |
114 | 1.14k | return MINGUO; |
115 | 1.71k | } |
116 | | |
117 | | IMPL_SYSTEM_DEFAULT_CENTURY(TaiwanCalendar, "@calendar=roc") |
118 | | |
119 | | U_NAMESPACE_END |
120 | | |
121 | | #endif |