• 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
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 "uassert.h"
25 #include "umutex.h"
26 #include "gregoimp.h" // Math
27 #include <float.h>
28 
29 static const int16_t kPersianNumDays[]
30 = {0,31,62,93,124,155,186,216,246,276,306,336}; // 0-based, for day-in-year
31 static const int8_t kPersianMonthLength[]
32 = {31,31,31,31,31,31,30,30,30,30,30,29}; // 0-based
33 static const int8_t kPersianLeapMonthLength[]
34 = {31,31,31,31,31,31,30,30,30,30,30,30}; // 0-based
35 
36 static const int32_t kPersianCalendarLimits[UCAL_FIELD_COUNT][4] = {
37     // Minimum  Greatest     Least   Maximum
38     //           Minimum   Maximum
39     {        0,        0,        0,        0}, // ERA
40     { -5000000, -5000000,  5000000,  5000000}, // YEAR
41     {        0,        0,       11,       11}, // MONTH
42     {        1,        1,       52,       53}, // WEEK_OF_YEAR
43     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH
44     {        1,       1,        29,       31}, // DAY_OF_MONTH
45     {        1,       1,       365,      366}, // DAY_OF_YEAR
46     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK
47     {        1,       1,         5,        5}, // DAY_OF_WEEK_IN_MONTH
48     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM
49     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR
50     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY
51     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE
52     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND
53     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND
54     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET
55     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET
56     { -5000000, -5000000,  5000000,  5000000}, // YEAR_WOY
57     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL
58     { -5000000, -5000000,  5000000,  5000000}, // EXTENDED_YEAR
59     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY
60     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY
61     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH
62     {        0,        0,       11,       11}, // ORDINAL_MONTH
63 };
64 
65 U_NAMESPACE_BEGIN
66 
67 static const int32_t PERSIAN_EPOCH = 1948320;
68 
69 // Implementation of the PersianCalendar class
70 
71 //-------------------------------------------------------------------------
72 // Constructors...
73 //-------------------------------------------------------------------------
74 
getType() const75 const char *PersianCalendar::getType() const {
76     return "persian";
77 }
78 
clone() const79 PersianCalendar* PersianCalendar::clone() const {
80     return new PersianCalendar(*this);
81 }
82 
PersianCalendar(const Locale & aLocale,UErrorCode & success)83 PersianCalendar::PersianCalendar(const Locale& aLocale, UErrorCode& success)
84   :   Calendar(TimeZone::forLocaleOrDefault(aLocale), aLocale, success)
85 {
86     setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
87 }
88 
PersianCalendar(const PersianCalendar & other)89 PersianCalendar::PersianCalendar(const PersianCalendar& other) : Calendar(other) {
90 }
91 
~PersianCalendar()92 PersianCalendar::~PersianCalendar()
93 {
94 }
95 
96 //-------------------------------------------------------------------------
97 // Minimum / Maximum access functions
98 //-------------------------------------------------------------------------
99 
100 
handleGetLimit(UCalendarDateFields field,ELimitType limitType) const101 int32_t PersianCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const {
102     return kPersianCalendarLimits[field][limitType];
103 }
104 
105 //-------------------------------------------------------------------------
106 // Assorted calculation utilities
107 //
108 
109 /**
110  * Determine whether a year is a leap year in the Persian calendar
111  */
isLeapYear(int32_t year)112 UBool PersianCalendar::isLeapYear(int32_t year)
113 {
114     int64_t y = static_cast<int64_t>(year) * 25LL + 11LL;
115     return (y % 33L < 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,UErrorCode & status)122 int32_t PersianCalendar::yearStart(int32_t year, UErrorCode& status) {
123     return handleComputeMonthStart(year,0,false, status);
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,UErrorCode & status) const133 int32_t PersianCalendar::monthStart(int32_t year, int32_t month, UErrorCode& status) const {
134     return handleComputeMonthStart(year,month,true, status);
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,UErrorCode &) const147 int32_t PersianCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month, UErrorCode& /*status*/) 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,UErrorCode & status) const169 int64_t PersianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /*useMonth*/, UErrorCode& status) const {
170     if (U_FAILURE(status)) {
171         return 0;
172     }
173     // If the month is out of range, adjust it into range, and
174     // modify the extended year value accordingly.
175     if (month < 0 || month > 11) {
176         if (uprv_add32_overflow(eyear, ClockMath::floorDivide(month, 12, &month), &eyear)) {
177             status = U_ILLEGAL_ARGUMENT_ERROR;
178             return 0;
179         }
180     }
181 
182     int64_t julianDay = PERSIAN_EPOCH - 1LL + 365LL * (eyear - 1LL) + ClockMath::floorDivide(8LL * eyear + 21, 33);
183 
184     if (month != 0) {
185         julianDay += kPersianNumDays[month];
186     }
187 
188     return julianDay;
189 }
190 
191 //-------------------------------------------------------------------------
192 // Functions for converting from milliseconds to field values
193 //-------------------------------------------------------------------------
194 
handleGetExtendedYear(UErrorCode & status)195 int32_t PersianCalendar::handleGetExtendedYear(UErrorCode& status) {
196     if (U_FAILURE(status)) {
197         return 0;
198     }
199     if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
200         return internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
201     }
202     return internalGet(UCAL_YEAR, 1); // Default to year 1
203 }
204 
205 /**
206  * Override Calendar to compute several fields specific to the Persian
207  * calendar system.  These are:
208  *
209  * <ul><li>ERA
210  * <li>YEAR
211  * <li>MONTH
212  * <li>DAY_OF_MONTH
213  * <li>DAY_OF_YEAR
214  * <li>EXTENDED_YEAR</ul>
215  *
216  * The DAY_OF_WEEK and DOW_LOCAL fields are already set when this
217  * method is called.
218  */
handleComputeFields(int32_t julianDay,UErrorCode & status)219 void PersianCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) {
220     int64_t daysSinceEpoch = julianDay;
221     daysSinceEpoch -= PERSIAN_EPOCH;
222     int64_t year = ClockMath::floorDivideInt64(
223         33LL * daysSinceEpoch + 3LL, 12053LL) + 1LL;
224     if (year > INT32_MAX || year < INT32_MIN) {
225         status = U_ILLEGAL_ARGUMENT_ERROR;
226         return;
227     }
228 
229     int64_t farvardin1 = 365LL * (year - 1) + ClockMath::floorDivide(8LL * year + 21, 33);
230     int32_t dayOfYear = daysSinceEpoch - farvardin1; // 0-based
231     U_ASSERT(dayOfYear >= 0);
232     U_ASSERT(dayOfYear < 366);
233                                                      //
234     int32_t month;
235     if (dayOfYear < 216) { // Compute 0-based month
236         month = dayOfYear / 31;
237     } else {
238         month = (dayOfYear - 6) / 30;
239     }
240     U_ASSERT(month >= 0);
241     U_ASSERT(month < 12);
242 
243     int32_t dayOfMonth = dayOfYear - kPersianNumDays[month] + 1;
244     U_ASSERT(dayOfMonth > 0);
245     U_ASSERT(dayOfMonth <= 31);
246 
247     ++dayOfYear; // Make it 1-based now
248 
249     internalSet(UCAL_ERA, 0);
250     internalSet(UCAL_YEAR, year);
251     internalSet(UCAL_EXTENDED_YEAR, year);
252     internalSet(UCAL_MONTH, month);
253     internalSet(UCAL_ORDINAL_MONTH, month);
254     internalSet(UCAL_DAY_OF_MONTH, dayOfMonth);
255     internalSet(UCAL_DAY_OF_YEAR, dayOfYear);
256 }
257 
258 constexpr uint32_t kPersianRelatedYearDiff = 622;
259 
getRelatedYear(UErrorCode & status) const260 int32_t PersianCalendar::getRelatedYear(UErrorCode &status) const
261 {
262     int32_t year = get(UCAL_EXTENDED_YEAR, status);
263     if (U_FAILURE(status)) {
264         return 0;
265     }
266     if (uprv_add32_overflow(year, kPersianRelatedYearDiff, &year)) {
267         status = U_ILLEGAL_ARGUMENT_ERROR;
268         return 0;
269     }
270     return year;
271 }
272 
setRelatedYear(int32_t year)273 void PersianCalendar::setRelatedYear(int32_t year)
274 {
275     // set extended year
276     set(UCAL_EXTENDED_YEAR, year - kPersianRelatedYearDiff);
277 }
278 
279 IMPL_SYSTEM_DEFAULT_CENTURY(PersianCalendar, "@calendar=persian")
280 
281 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(PersianCalendar)
282 
283 U_NAMESPACE_END
284 
285 #endif
286