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
6 * and others. All Rights Reserved.
7 ******************************************************************************
8 *
9 * File PERSNCAL.CPP
10 *
11 * Modification History:
12 *
13 * Date Name Description
14 * 9/23/2003 mehran posted to icu-design
15 * 10/1/2012 roozbeh Fixed algorithm and heavily refactored and rewrote
16 * based on the implementation of Gregorian
17 *****************************************************************************
18 */
19
20 #include "persncal.h"
21
22 #if !UCONFIG_NO_FORMATTING
23
24 #include "umutex.h"
25 #include "gregoimp.h" // Math
26 #include <float.h>
27
28 static const int16_t kPersianNumDays[]
29 = {0,31,62,93,124,155,186,216,246,276,306,336}; // 0-based, for day-in-year
30 static const int8_t kPersianMonthLength[]
31 = {31,31,31,31,31,31,30,30,30,30,30,29}; // 0-based
32 static const int8_t kPersianLeapMonthLength[]
33 = {31,31,31,31,31,31,30,30,30,30,30,30}; // 0-based
34
35 static const int32_t kPersianCalendarLimits[UCAL_FIELD_COUNT][4] = {
36 // Minimum Greatest Least Maximum
37 // Minimum Maximum
38 { 0, 0, 0, 0}, // ERA
39 { -5000000, -5000000, 5000000, 5000000}, // YEAR
40 { 0, 0, 11, 11}, // MONTH
41 { 1, 1, 52, 53}, // WEEK_OF_YEAR
42 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH
43 { 1, 1, 29, 31}, // DAY_OF_MONTH
44 { 1, 1, 365, 366}, // DAY_OF_YEAR
45 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK
46 { 1, 1, 5, 5}, // DAY_OF_WEEK_IN_MONTH
47 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM
48 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR
49 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY
50 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE
51 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND
52 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND
53 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET
54 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET
55 { -5000000, -5000000, 5000000, 5000000}, // YEAR_WOY
56 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL
57 { -5000000, -5000000, 5000000, 5000000}, // EXTENDED_YEAR
58 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY
59 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY
60 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH
61 { 0, 0, 11, 11}, // ORDINAL_MONTH
62 };
63
64 U_NAMESPACE_BEGIN
65
66 static const int32_t PERSIAN_EPOCH = 1948320;
67
68 // Implementation of the PersianCalendar class
69
70 //-------------------------------------------------------------------------
71 // Constructors...
72 //-------------------------------------------------------------------------
73
getType() const74 const char *PersianCalendar::getType() const {
75 return "persian";
76 }
77
clone() const78 PersianCalendar* PersianCalendar::clone() const {
79 return new PersianCalendar(*this);
80 }
81
PersianCalendar(const Locale & aLocale,UErrorCode & success)82 PersianCalendar::PersianCalendar(const Locale& aLocale, UErrorCode& success)
83 : Calendar(TimeZone::forLocaleOrDefault(aLocale), aLocale, success)
84 {
85 setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
86 }
87
PersianCalendar(const PersianCalendar & other)88 PersianCalendar::PersianCalendar(const PersianCalendar& other) : Calendar(other) {
89 }
90
~PersianCalendar()91 PersianCalendar::~PersianCalendar()
92 {
93 }
94
95 //-------------------------------------------------------------------------
96 // Minimum / Maximum access functions
97 //-------------------------------------------------------------------------
98
99
handleGetLimit(UCalendarDateFields field,ELimitType limitType) const100 int32_t PersianCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const {
101 return kPersianCalendarLimits[field][limitType];
102 }
103
104 //-------------------------------------------------------------------------
105 // Assorted calculation utilities
106 //
107
108 /**
109 * Determine whether a year is a leap year in the Persian calendar
110 */
isLeapYear(int32_t year)111 UBool PersianCalendar::isLeapYear(int32_t year)
112 {
113 int32_t remainder;
114 ClockMath::floorDivide(25 * year + 11, 33, &remainder);
115 return (remainder < 8);
116 }
117
118 /**
119 * Return the day # on which the given year starts. Days are counted
120 * from the Persian epoch, origin 0.
121 */
yearStart(int32_t year)122 int32_t PersianCalendar::yearStart(int32_t year) {
123 return handleComputeMonthStart(year,0,false);
124 }
125
126 /**
127 * Return the day # on which the given month starts. Days are counted
128 * from the Persian epoch, origin 0.
129 *
130 * @param year The Persian year
131 * @param year The Persian month, 0-based
132 */
monthStart(int32_t year,int32_t month) const133 int32_t PersianCalendar::monthStart(int32_t year, int32_t month) const {
134 return handleComputeMonthStart(year,month,true);
135 }
136
137 //----------------------------------------------------------------------
138 // Calendar framework
139 //----------------------------------------------------------------------
140
141 /**
142 * Return the length (in days) of the given month.
143 *
144 * @param year The Persian year
145 * @param year The Persian month, 0-based
146 */
handleGetMonthLength(int32_t extendedYear,int32_t month) const147 int32_t PersianCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const {
148 // If the month is out of range, adjust it into range, and
149 // modify the extended year value accordingly.
150 if (month < 0 || month > 11) {
151 extendedYear += ClockMath::floorDivide(month, 12, &month);
152 }
153
154 return isLeapYear(extendedYear) ? kPersianLeapMonthLength[month] : kPersianMonthLength[month];
155 }
156
157 /**
158 * Return the number of days in the given Persian year
159 */
handleGetYearLength(int32_t extendedYear) const160 int32_t PersianCalendar::handleGetYearLength(int32_t extendedYear) const {
161 return isLeapYear(extendedYear) ? 366 : 365;
162 }
163
164 //-------------------------------------------------------------------------
165 // Functions for converting from field values to milliseconds....
166 //-------------------------------------------------------------------------
167
168 // Return JD of start of given month/year
handleComputeMonthStart(int32_t eyear,int32_t month,UBool) const169 int32_t PersianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /*useMonth*/) const {
170 // If the month is out of range, adjust it into range, and
171 // modify the extended year value accordingly.
172 if (month < 0 || month > 11) {
173 eyear += ClockMath::floorDivide(month, 12, &month);
174 }
175
176 int32_t julianDay = PERSIAN_EPOCH - 1 + 365 * (eyear - 1) + ClockMath::floorDivide(8 * eyear + 21, 33);
177
178 if (month != 0) {
179 julianDay += kPersianNumDays[month];
180 }
181
182 return julianDay;
183 }
184
185 //-------------------------------------------------------------------------
186 // Functions for converting from milliseconds to field values
187 //-------------------------------------------------------------------------
188
handleGetExtendedYear()189 int32_t PersianCalendar::handleGetExtendedYear() {
190 int32_t year;
191 if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
192 year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
193 } else {
194 year = internalGet(UCAL_YEAR, 1); // Default to year 1
195 }
196 return year;
197 }
198
199 /**
200 * Override Calendar to compute several fields specific to the Persian
201 * calendar system. These are:
202 *
203 * <ul><li>ERA
204 * <li>YEAR
205 * <li>MONTH
206 * <li>DAY_OF_MONTH
207 * <li>DAY_OF_YEAR
208 * <li>EXTENDED_YEAR</ul>
209 *
210 * The DAY_OF_WEEK and DOW_LOCAL fields are already set when this
211 * method is called.
212 */
handleComputeFields(int32_t julianDay,UErrorCode &)213 void PersianCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/) {
214 int32_t year, month, dayOfMonth, dayOfYear;
215
216 int32_t daysSinceEpoch = julianDay - PERSIAN_EPOCH;
217 year = 1 + (int32_t)ClockMath::floorDivide(33 * (int64_t)daysSinceEpoch + 3, (int64_t)12053);
218
219 int32_t farvardin1 = 365 * (year - 1) + ClockMath::floorDivide(8 * year + 21, 33);
220 dayOfYear = (daysSinceEpoch - farvardin1); // 0-based
221 if (dayOfYear < 216) { // Compute 0-based month
222 month = dayOfYear / 31;
223 } else {
224 month = (dayOfYear - 6) / 30;
225 }
226 dayOfMonth = dayOfYear - kPersianNumDays[month] + 1;
227 ++dayOfYear; // Make it 1-based now
228
229 internalSet(UCAL_ERA, 0);
230 internalSet(UCAL_YEAR, year);
231 internalSet(UCAL_EXTENDED_YEAR, year);
232 internalSet(UCAL_MONTH, month);
233 internalSet(UCAL_ORDINAL_MONTH, month);
234 internalSet(UCAL_DAY_OF_MONTH, dayOfMonth);
235 internalSet(UCAL_DAY_OF_YEAR, dayOfYear);
236 }
237
238 constexpr uint32_t kPersianRelatedYearDiff = 622;
239
getRelatedYear(UErrorCode & status) const240 int32_t PersianCalendar::getRelatedYear(UErrorCode &status) const
241 {
242 int32_t year = get(UCAL_EXTENDED_YEAR, status);
243 if (U_FAILURE(status)) {
244 return 0;
245 }
246 return year + kPersianRelatedYearDiff;
247 }
248
setRelatedYear(int32_t year)249 void PersianCalendar::setRelatedYear(int32_t year)
250 {
251 // set extended year
252 set(UCAL_EXTENDED_YEAR, year - kPersianRelatedYearDiff);
253 }
254
255 // default century
256
257 static UDate gSystemDefaultCenturyStart = DBL_MIN;
258 static int32_t gSystemDefaultCenturyStartYear = -1;
259 static icu::UInitOnce gSystemDefaultCenturyInit {};
260
haveDefaultCentury() const261 UBool PersianCalendar::haveDefaultCentury() const
262 {
263 return true;
264 }
265
initializeSystemDefaultCentury()266 static void U_CALLCONV initializeSystemDefaultCentury() {
267 // initialize systemDefaultCentury and systemDefaultCenturyYear based
268 // on the current time. They'll be set to 80 years before
269 // the current time.
270 UErrorCode status = U_ZERO_ERROR;
271 PersianCalendar calendar(Locale("@calendar=persian"),status);
272 if (U_SUCCESS(status))
273 {
274 calendar.setTime(Calendar::getNow(), status);
275 calendar.add(UCAL_YEAR, -80, status);
276
277 gSystemDefaultCenturyStart = calendar.getTime(status);
278 gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);
279 }
280 // We have no recourse upon failure unless we want to propagate the failure
281 // out.
282 }
283
defaultCenturyStart() const284 UDate PersianCalendar::defaultCenturyStart() const {
285 // lazy-evaluate systemDefaultCenturyStart
286 umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
287 return gSystemDefaultCenturyStart;
288 }
289
defaultCenturyStartYear() const290 int32_t PersianCalendar::defaultCenturyStartYear() const {
291 // lazy-evaluate systemDefaultCenturyStartYear
292 umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
293 return gSystemDefaultCenturyStartYear;
294 }
295
296 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(PersianCalendar)
297
298 U_NAMESPACE_END
299
300 #endif
301
302