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 "gregoimp.h"
15 #include "umutex.h"
16 #include "ethpccal.h"
17 #include "cecal.h"
18 #include <float.h>
19
20 U_NAMESPACE_BEGIN
21
22 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EthiopicCalendar)
23 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EthiopicAmeteAlemCalendar)
24
25 //static const int32_t JD_EPOCH_OFFSET_AMETE_ALEM = -285019;
26 static const int32_t JD_EPOCH_OFFSET_AMETE_MIHRET = 1723856;
27 static const int32_t AMETE_MIHRET_DELTA = 5500; // 5501 - 1 (Amete Alem 5501 = Amete Mihret 1)
28
29 //-------------------------------------------------------------------------
30 // Constructors...
31 //-------------------------------------------------------------------------
32
EthiopicCalendar(const Locale & aLocale,UErrorCode & success)33 EthiopicCalendar::EthiopicCalendar(const Locale& aLocale,
34 UErrorCode& success)
35 : CECalendar(aLocale, success)
36 {
37 }
38
~EthiopicCalendar()39 EthiopicCalendar::~EthiopicCalendar()
40 {
41 }
42
43 EthiopicCalendar*
clone() const44 EthiopicCalendar::clone() const
45 {
46 return new EthiopicCalendar(*this);
47 }
48
49 const char *
getType() const50 EthiopicCalendar::getType() const
51 {
52 return "ethiopic";
53 }
54
55 //-------------------------------------------------------------------------
56 // Calendar framework
57 //-------------------------------------------------------------------------
58
59 int32_t
handleGetExtendedYear(UErrorCode & status)60 EthiopicCalendar::handleGetExtendedYear(UErrorCode& status)
61 {
62 if (U_FAILURE(status)) {
63 return 0;
64 }
65 // Ethiopic calendar uses EXTENDED_YEAR aligned to
66 // Amelete Hihret year always.
67 if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
68 return internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
69 }
70 // The year defaults to the epoch start, the era to AMETE_MIHRET
71 if (internalGet(UCAL_ERA, AMETE_MIHRET) == AMETE_MIHRET) {
72 return internalGet(UCAL_YEAR, 1); // Default to year 1
73 }
74 int32_t year = internalGet(UCAL_YEAR, 1);
75 if (uprv_add32_overflow(year, -AMETE_MIHRET_DELTA, &year)) {
76 status = U_ILLEGAL_ARGUMENT_ERROR;
77 return 0;
78 }
79 return year;
80 }
81
82 void
handleComputeFields(int32_t julianDay,UErrorCode &)83 EthiopicCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/)
84 {
85 int32_t eyear, month, day;
86 jdToCE(julianDay, getJDEpochOffset(), eyear, month, day);
87
88 internalSet(UCAL_EXTENDED_YEAR, eyear);
89 internalSet(UCAL_ERA, (eyear > 0) ? AMETE_MIHRET : AMETE_ALEM);
90 internalSet(UCAL_YEAR, (eyear > 0) ? eyear : (eyear + AMETE_MIHRET_DELTA));
91 internalSet(UCAL_MONTH, month);
92 internalSet(UCAL_ORDINAL_MONTH, month);
93 internalSet(UCAL_DATE, day);
94 internalSet(UCAL_DAY_OF_YEAR, (30 * month) + day);
95 }
96
97 constexpr uint32_t kEthiopicRelatedYearDiff = 8;
98
getRelatedYear(UErrorCode & status) const99 int32_t EthiopicCalendar::getRelatedYear(UErrorCode &status) const
100 {
101 int32_t year = get(UCAL_EXTENDED_YEAR, status);
102 if (U_FAILURE(status)) {
103 return 0;
104 }
105 return year + kEthiopicRelatedYearDiff;
106 }
107
setRelatedYear(int32_t year)108 void EthiopicCalendar::setRelatedYear(int32_t year)
109 {
110 // set extended year
111 set(UCAL_EXTENDED_YEAR, year - kEthiopicRelatedYearDiff);
112 }
113
114 IMPL_SYSTEM_DEFAULT_CENTURY(EthiopicCalendar, "@calendar=ethiopic")
115
116 int32_t
getJDEpochOffset() const117 EthiopicCalendar::getJDEpochOffset() const
118 {
119 return JD_EPOCH_OFFSET_AMETE_MIHRET;
120 }
121
122
123 //-------------------------------------------------------------------------
124 // Constructors...
125 //-------------------------------------------------------------------------
126
EthiopicAmeteAlemCalendar(const Locale & aLocale,UErrorCode & success)127 EthiopicAmeteAlemCalendar::EthiopicAmeteAlemCalendar(const Locale& aLocale,
128 UErrorCode& success)
129 : EthiopicCalendar(aLocale, success)
130 {
131 }
132
~EthiopicAmeteAlemCalendar()133 EthiopicAmeteAlemCalendar::~EthiopicAmeteAlemCalendar()
134 {
135 }
136
137 EthiopicAmeteAlemCalendar*
clone() const138 EthiopicAmeteAlemCalendar::clone() const
139 {
140 return new EthiopicAmeteAlemCalendar(*this);
141 }
142
143 //-------------------------------------------------------------------------
144 // Calendar framework
145 //-------------------------------------------------------------------------
146
147 const char *
getType() const148 EthiopicAmeteAlemCalendar::getType() const
149 {
150 return "ethiopic-amete-alem";
151 }
152
153 int32_t
handleGetExtendedYear(UErrorCode & status)154 EthiopicAmeteAlemCalendar::handleGetExtendedYear(UErrorCode& status)
155 {
156 if (U_FAILURE(status)) {
157 return 0;
158 }
159 // Ethiopic calendar uses EXTENDED_YEAR aligned to
160 // Amelete Hihret year always.
161 if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
162 return internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
163 }
164 // Default to year 1 of Amelete Mihret
165 int32_t year = internalGet(UCAL_YEAR, 1 + AMETE_MIHRET_DELTA);
166 if (uprv_add32_overflow(year, -AMETE_MIHRET_DELTA, &year)) {
167 status = U_ILLEGAL_ARGUMENT_ERROR;
168 return 0;
169 }
170 return year;
171 }
172
173 void
handleComputeFields(int32_t julianDay,UErrorCode &)174 EthiopicAmeteAlemCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/)
175 {
176 int32_t eyear, month, day;
177 jdToCE(julianDay, getJDEpochOffset(), eyear, month, day);
178
179 internalSet(UCAL_EXTENDED_YEAR, eyear);
180 internalSet(UCAL_ERA, AMETE_ALEM);
181 internalSet(UCAL_YEAR, eyear + AMETE_MIHRET_DELTA);
182 internalSet(UCAL_MONTH, month);
183 internalSet(UCAL_ORDINAL_MONTH, month);
184 internalSet(UCAL_DATE, day);
185 internalSet(UCAL_DAY_OF_YEAR, (30 * month) + day);
186 }
187
188 int32_t
handleGetLimit(UCalendarDateFields field,ELimitType limitType) const189 EthiopicAmeteAlemCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
190 {
191 if (field == UCAL_ERA) {
192 return 0; // Only one era in this mode, era is always 0
193 }
194 return EthiopicCalendar::handleGetLimit(field, limitType);
195 }
196
197 constexpr uint32_t kEthiopicAmeteAlemRelatedYearDiff = -5492;
198
getRelatedYear(UErrorCode & status) const199 int32_t EthiopicAmeteAlemCalendar::getRelatedYear(UErrorCode &status) const
200 {
201 int32_t year = get(UCAL_EXTENDED_YEAR, status);
202 if (U_FAILURE(status)) {
203 return 0;
204 }
205 return year + kEthiopicAmeteAlemRelatedYearDiff;
206 }
207
setRelatedYear(int32_t year)208 void EthiopicAmeteAlemCalendar::setRelatedYear(int32_t year)
209 {
210 // set extended year
211 set(UCAL_EXTENDED_YEAR, year - kEthiopicAmeteAlemRelatedYearDiff);
212 }
213
214 int32_t
defaultCenturyStartYear() const215 EthiopicAmeteAlemCalendar::defaultCenturyStartYear() const
216 {
217 return EthiopicCalendar::defaultCenturyStartYear() + AMETE_MIHRET_DELTA;
218 }
219
220 U_NAMESPACE_END
221
222 #endif
223