• 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 
10 #include "unicode/utypes.h"
11 
12 #if !UCONFIG_NO_FORMATTING
13 
14 #include "umutex.h"
15 #include "ethpccal.h"
16 #include "cecal.h"
17 #include <float.h>
18 
19 U_NAMESPACE_BEGIN
20 
21 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EthiopicCalendar)
22 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EthiopicAmeteAlemCalendar)
23 
24 //static const int32_t JD_EPOCH_OFFSET_AMETE_ALEM = -285019;
25 static const int32_t JD_EPOCH_OFFSET_AMETE_MIHRET = 1723856;
26 static const int32_t AMETE_MIHRET_DELTA = 5500; // 5501 - 1 (Amete Alem 5501 = Amete Mihret 1)
27 
28 //-------------------------------------------------------------------------
29 // Constructors...
30 //-------------------------------------------------------------------------
31 
EthiopicCalendar(const Locale & aLocale,UErrorCode & success)32 EthiopicCalendar::EthiopicCalendar(const Locale& aLocale,
33                                    UErrorCode& success)
34 :   CECalendar(aLocale, success)
35 {
36 }
37 
~EthiopicCalendar()38 EthiopicCalendar::~EthiopicCalendar()
39 {
40 }
41 
42 EthiopicCalendar*
clone() const43 EthiopicCalendar::clone() const
44 {
45     return new EthiopicCalendar(*this);
46 }
47 
48 const char *
getType() const49 EthiopicCalendar::getType() const
50 {
51     return "ethiopic";
52 }
53 
54 //-------------------------------------------------------------------------
55 // Calendar framework
56 //-------------------------------------------------------------------------
57 
58 int32_t
handleGetExtendedYear()59 EthiopicCalendar::handleGetExtendedYear()
60 {
61     // Ethiopic calendar uses EXTENDED_YEAR aligned to
62     // Amelete Hihret year always.
63     if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
64         return internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
65     }
66     // The year defaults to the epoch start, the era to AMETE_MIHRET
67     if (internalGet(UCAL_ERA, AMETE_MIHRET) == AMETE_MIHRET) {
68         return internalGet(UCAL_YEAR, 1); // Default to year 1
69     }
70     return internalGet(UCAL_YEAR, 1) - AMETE_MIHRET_DELTA;
71 }
72 
73 void
handleComputeFields(int32_t julianDay,UErrorCode &)74 EthiopicCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/)
75 {
76     int32_t eyear, month, day;
77     jdToCE(julianDay, getJDEpochOffset(), eyear, month, day);
78 
79     internalSet(UCAL_EXTENDED_YEAR, eyear);
80     internalSet(UCAL_ERA, (eyear > 0) ? AMETE_MIHRET : AMETE_ALEM);
81     internalSet(UCAL_YEAR, (eyear > 0) ? eyear : (eyear + AMETE_MIHRET_DELTA));
82     internalSet(UCAL_MONTH, month);
83     internalSet(UCAL_ORDINAL_MONTH, month);
84     internalSet(UCAL_DATE, day);
85     internalSet(UCAL_DAY_OF_YEAR, (30 * month) + day);
86 }
87 
88 constexpr uint32_t kEthiopicRelatedYearDiff = 8;
89 
getRelatedYear(UErrorCode & status) const90 int32_t EthiopicCalendar::getRelatedYear(UErrorCode &status) const
91 {
92     int32_t year = get(UCAL_EXTENDED_YEAR, status);
93     if (U_FAILURE(status)) {
94         return 0;
95     }
96     return year + kEthiopicRelatedYearDiff;
97 }
98 
setRelatedYear(int32_t year)99 void EthiopicCalendar::setRelatedYear(int32_t year)
100 {
101     // set extended year
102     set(UCAL_EXTENDED_YEAR, year - kEthiopicRelatedYearDiff);
103 }
104 
105 /**
106  * The system maintains a static default century start date and Year.  They are
107  * initialized the first time they are used.  Once the system default century date
108  * and year are set, they do not change.
109  */
110 static UDate           gSystemDefaultCenturyStart       = DBL_MIN;
111 static int32_t         gSystemDefaultCenturyStartYear   = -1;
112 static icu::UInitOnce  gSystemDefaultCenturyInit        {};
113 
initializeSystemDefaultCentury()114 static void U_CALLCONV initializeSystemDefaultCentury()
115 {
116     UErrorCode status = U_ZERO_ERROR;
117     EthiopicCalendar calendar(Locale("@calendar=ethiopic"), status);
118     if (U_SUCCESS(status)) {
119         calendar.setTime(Calendar::getNow(), status);
120         calendar.add(UCAL_YEAR, -80, status);
121 
122         gSystemDefaultCenturyStart = calendar.getTime(status);
123         gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);
124     }
125     // We have no recourse upon failure unless we want to propagate the failure
126     // out.
127 }
128 
129 UDate
defaultCenturyStart() const130 EthiopicCalendar::defaultCenturyStart() const
131 {
132     // lazy-evaluate systemDefaultCenturyStart
133     umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
134     return gSystemDefaultCenturyStart;
135 }
136 
137 int32_t
defaultCenturyStartYear() const138 EthiopicCalendar::defaultCenturyStartYear() const
139 {
140     // lazy-evaluate systemDefaultCenturyStartYear
141     umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
142     return gSystemDefaultCenturyStartYear;
143 }
144 
145 
146 int32_t
getJDEpochOffset() const147 EthiopicCalendar::getJDEpochOffset() const
148 {
149     return JD_EPOCH_OFFSET_AMETE_MIHRET;
150 }
151 
152 
153 #if 0
154 // We do not want to introduce this API in ICU4C.
155 // It was accidentally introduced in ICU4J as a public API.
156 
157 //-------------------------------------------------------------------------
158 // Calendar system Conversion methods...
159 //-------------------------------------------------------------------------
160 
161 int32_t
162 EthiopicCalendar::ethiopicToJD(int32_t year, int32_t month, int32_t date)
163 {
164     return ceToJD(year, month, date, JD_EPOCH_OFFSET_AMETE_MIHRET);
165 }
166 #endif
167 
168 //-------------------------------------------------------------------------
169 // Constructors...
170 //-------------------------------------------------------------------------
171 
EthiopicAmeteAlemCalendar(const Locale & aLocale,UErrorCode & success)172 EthiopicAmeteAlemCalendar::EthiopicAmeteAlemCalendar(const Locale& aLocale,
173                                    UErrorCode& success)
174 :   EthiopicCalendar(aLocale, success)
175 {
176 }
177 
~EthiopicAmeteAlemCalendar()178 EthiopicAmeteAlemCalendar::~EthiopicAmeteAlemCalendar()
179 {
180 }
181 
182 EthiopicAmeteAlemCalendar*
clone() const183 EthiopicAmeteAlemCalendar::clone() const
184 {
185     return new EthiopicAmeteAlemCalendar(*this);
186 }
187 
188 //-------------------------------------------------------------------------
189 // Calendar framework
190 //-------------------------------------------------------------------------
191 
192 const char *
getType() const193 EthiopicAmeteAlemCalendar::getType() const
194 {
195     return "ethiopic-amete-alem";
196 }
197 
198 int32_t
handleGetExtendedYear()199 EthiopicAmeteAlemCalendar::handleGetExtendedYear()
200 {
201     // Ethiopic calendar uses EXTENDED_YEAR aligned to
202     // Amelete Hihret year always.
203     if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
204         return internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
205     }
206     return internalGet(UCAL_YEAR, 1 + AMETE_MIHRET_DELTA)
207             - AMETE_MIHRET_DELTA; // Default to year 1 of Amelete Mihret
208 }
209 
210 void
handleComputeFields(int32_t julianDay,UErrorCode &)211 EthiopicAmeteAlemCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/)
212 {
213     int32_t eyear, month, day;
214     jdToCE(julianDay, getJDEpochOffset(), eyear, month, day);
215 
216     internalSet(UCAL_EXTENDED_YEAR, eyear);
217     internalSet(UCAL_ERA, AMETE_ALEM);
218     internalSet(UCAL_YEAR, eyear + AMETE_MIHRET_DELTA);
219     internalSet(UCAL_MONTH, month);
220     internalSet(UCAL_ORDINAL_MONTH, month);
221     internalSet(UCAL_DATE, day);
222     internalSet(UCAL_DAY_OF_YEAR, (30 * month) + day);
223 }
224 
225 int32_t
handleGetLimit(UCalendarDateFields field,ELimitType limitType) const226 EthiopicAmeteAlemCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
227 {
228     if (field == UCAL_ERA) {
229         return 0; // Only one era in this mode, era is always 0
230     }
231     return EthiopicCalendar::handleGetLimit(field, limitType);
232 }
233 
234 constexpr uint32_t kEthiopicAmeteAlemRelatedYearDiff = -5492;
235 
getRelatedYear(UErrorCode & status) const236 int32_t EthiopicAmeteAlemCalendar::getRelatedYear(UErrorCode &status) const
237 {
238     int32_t year = get(UCAL_EXTENDED_YEAR, status);
239     if (U_FAILURE(status)) {
240         return 0;
241     }
242     return year + kEthiopicAmeteAlemRelatedYearDiff;
243 }
244 
setRelatedYear(int32_t year)245 void EthiopicAmeteAlemCalendar::setRelatedYear(int32_t year)
246 {
247     // set extended year
248     set(UCAL_EXTENDED_YEAR, year - kEthiopicAmeteAlemRelatedYearDiff);
249 }
250 
251 int32_t
defaultCenturyStartYear() const252 EthiopicAmeteAlemCalendar::defaultCenturyStartYear() const
253 {
254     return EthiopicCalendar::defaultCenturyStartYear() + AMETE_MIHRET_DELTA;
255 }
256 
257 U_NAMESPACE_END
258 
259 #endif
260