]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/persncal.cpp
ICU-66108.tar.gz
[apple/icu.git] / icuSources / i18n / persncal.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
46f4442e
A
3/*
4 ******************************************************************************
57a6839d 5 * Copyright (C) 2003-2013, International Business Machines Corporation
46f4442e
A
6 * and others. All Rights Reserved.
7 ******************************************************************************
8 *
9 * File PERSNCAL.CPP
10 *
11 * Modification History:
12 *
13 * Date Name Description
51004dcb
A
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
46f4442e
A
17 *****************************************************************************
18 */
19
20#include "persncal.h"
21
22#if !UCONFIG_NO_FORMATTING
23
24#include "umutex.h"
51004dcb 25#include "gregoimp.h" // Math
46f4442e
A
26#include <float.h>
27
51004dcb
A
28static const int16_t kPersianNumDays[]
29= {0,31,62,93,124,155,186,216,246,276,306,336}; // 0-based, for day-in-year
30static const int8_t kPersianMonthLength[]
31= {31,31,31,31,31,31,30,30,30,30,30,29}; // 0-based
32static const int8_t kPersianLeapMonthLength[]
33= {31,31,31,31,31,31,30,30,30,30,30,30}; // 0-based
46f4442e 34
51004dcb
A
35static 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};
46f4442e
A
62
63U_NAMESPACE_BEGIN
64
51004dcb
A
65static const int32_t PERSIAN_EPOCH = 1948320;
66
46f4442e
A
67// Implementation of the PersianCalendar class
68
69//-------------------------------------------------------------------------
70// Constructors...
71//-------------------------------------------------------------------------
72
73const char *PersianCalendar::getType() const {
74 return "persian";
75}
76
340931cb 77PersianCalendar* PersianCalendar::clone() const {
46f4442e
A
78 return new PersianCalendar(*this);
79}
80
81PersianCalendar::PersianCalendar(const Locale& aLocale, UErrorCode& success)
82 : Calendar(TimeZone::createDefault(), aLocale, success)
83{
84 setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
85}
86
87PersianCalendar::PersianCalendar(const PersianCalendar& other) : Calendar(other) {
88}
89
90PersianCalendar::~PersianCalendar()
91{
92}
93
94//-------------------------------------------------------------------------
95// Minimum / Maximum access functions
96//-------------------------------------------------------------------------
97
46f4442e
A
98
99int32_t PersianCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const {
51004dcb 100 return kPersianCalendarLimits[field][limitType];
46f4442e
A
101}
102
103//-------------------------------------------------------------------------
104// Assorted calculation utilities
105//
106
107/**
108 * Determine whether a year is a leap year in the Persian calendar
109 */
110UBool PersianCalendar::isLeapYear(int32_t year)
111{
51004dcb
A
112 int32_t remainder;
113 ClockMath::floorDivide(25 * year + 11, 33, remainder);
114 return (remainder < 8);
46f4442e
A
115}
116
117/**
118 * Return the day # on which the given year starts. Days are counted
51004dcb 119 * from the Persian epoch, origin 0.
46f4442e
A
120 */
121int32_t PersianCalendar::yearStart(int32_t year) {
51004dcb 122 return handleComputeMonthStart(year,0,FALSE);
46f4442e
A
123}
124
125/**
126 * Return the day # on which the given month starts. Days are counted
51004dcb 127 * from the Persian epoch, origin 0.
46f4442e 128 *
51004dcb
A
129 * @param year The Persian year
130 * @param year The Persian month, 0-based
46f4442e
A
131 */
132int32_t PersianCalendar::monthStart(int32_t year, int32_t month) const {
51004dcb 133 return handleComputeMonthStart(year,month,TRUE);
46f4442e
A
134}
135
136//----------------------------------------------------------------------
137// Calendar framework
138//----------------------------------------------------------------------
139
140/**
141 * Return the length (in days) of the given month.
142 *
51004dcb
A
143 * @param year The Persian year
144 * @param year The Persian month, 0-based
46f4442e
A
145 */
146int32_t PersianCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const {
51004dcb
A
147 // If the month is out of range, adjust it into range, and
148 // modify the extended year value accordingly.
149 if (month < 0 || month > 11) {
150 extendedYear += ClockMath::floorDivide(month, 12, month);
151 }
152
153 return isLeapYear(extendedYear) ? kPersianLeapMonthLength[month] : kPersianMonthLength[month];
46f4442e
A
154}
155
156/**
157 * Return the number of days in the given Persian year
158 */
159int32_t PersianCalendar::handleGetYearLength(int32_t extendedYear) const {
51004dcb 160 return isLeapYear(extendedYear) ? 366 : 365;
46f4442e
A
161}
162
163//-------------------------------------------------------------------------
164// Functions for converting from field values to milliseconds....
165//-------------------------------------------------------------------------
166
167// Return JD of start of given month/year
51004dcb 168int32_t PersianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /*useMonth*/) const {
46f4442e
A
169 // If the month is out of range, adjust it into range, and
170 // modify the extended year value accordingly.
171 if (month < 0 || month > 11) {
51004dcb
A
172 eyear += ClockMath::floorDivide(month, 12, month);
173 }
174
175 int32_t julianDay = PERSIAN_EPOCH - 1 + 365 * (eyear - 1) + ClockMath::floorDivide(8 * eyear + 21, 33);
176
177 if (month != 0) {
178 julianDay += kPersianNumDays[month];
46f4442e 179 }
51004dcb
A
180
181 return julianDay;
46f4442e
A
182}
183
184//-------------------------------------------------------------------------
185// Functions for converting from milliseconds to field values
186//-------------------------------------------------------------------------
187
188int32_t PersianCalendar::handleGetExtendedYear() {
189 int32_t year;
190 if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
191 year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
192 } else {
193 year = internalGet(UCAL_YEAR, 1); // Default to year 1
194 }
195 return year;
196}
197
198/**
199 * Override Calendar to compute several fields specific to the Persian
200 * calendar system. These are:
201 *
202 * <ul><li>ERA
203 * <li>YEAR
204 * <li>MONTH
205 * <li>DAY_OF_MONTH
206 * <li>DAY_OF_YEAR
207 * <li>EXTENDED_YEAR</ul>
208 *
209 * The DAY_OF_WEEK and DOW_LOCAL fields are already set when this
51004dcb 210 * method is called.
46f4442e
A
211 */
212void PersianCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/) {
51004dcb
A
213 int32_t year, month, dayOfMonth, dayOfYear;
214
215 int32_t daysSinceEpoch = julianDay - PERSIAN_EPOCH;
3bb97ae2 216 year = 1 + (int32_t)ClockMath::floorDivide(33 * (int64_t)daysSinceEpoch + 3, (int64_t)12053);
51004dcb
A
217
218 int32_t farvardin1 = 365 * (year - 1) + ClockMath::floorDivide(8 * year + 21, 33);
219 dayOfYear = (daysSinceEpoch - farvardin1); // 0-based
220 if (dayOfYear < 216) { // Compute 0-based month
221 month = dayOfYear / 31;
222 } else {
223 month = (dayOfYear - 6) / 30;
224 }
225 dayOfMonth = dayOfYear - kPersianNumDays[month] + 1;
226 ++dayOfYear; // Make it 1-based now
227
46f4442e 228 internalSet(UCAL_ERA, 0);
51004dcb
A
229 internalSet(UCAL_YEAR, year);
230 internalSet(UCAL_EXTENDED_YEAR, year);
231 internalSet(UCAL_MONTH, month);
232 internalSet(UCAL_DAY_OF_MONTH, dayOfMonth);
233 internalSet(UCAL_DAY_OF_YEAR, dayOfYear);
46f4442e
A
234}
235
236UBool
237PersianCalendar::inDaylightTime(UErrorCode& status) const
238{
239 // copied from GregorianCalendar
240 if (U_FAILURE(status) || !getTimeZone().useDaylightTime())
241 return FALSE;
242
243 // Force an update of the state of the Calendar.
244 ((PersianCalendar*)this)->complete(status); // cast away const
245
246 return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE);
247}
248
249// default century
46f4442e 250
57a6839d
A
251static UDate gSystemDefaultCenturyStart = DBL_MIN;
252static int32_t gSystemDefaultCenturyStartYear = -1;
253static icu::UInitOnce gSystemDefaultCenturyInit = U_INITONCE_INITIALIZER;
46f4442e
A
254
255UBool PersianCalendar::haveDefaultCentury() const
256{
257 return TRUE;
258}
259
57a6839d 260static void U_CALLCONV initializeSystemDefaultCentury() {
46f4442e
A
261 // initialize systemDefaultCentury and systemDefaultCenturyYear based
262 // on the current time. They'll be set to 80 years before
263 // the current time.
729e4ab9
A
264 UErrorCode status = U_ZERO_ERROR;
265 PersianCalendar calendar(Locale("@calendar=persian"),status);
266 if (U_SUCCESS(status))
46f4442e 267 {
729e4ab9
A
268 calendar.setTime(Calendar::getNow(), status);
269 calendar.add(UCAL_YEAR, -80, status);
57a6839d
A
270
271 gSystemDefaultCenturyStart = calendar.getTime(status);
272 gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);
46f4442e 273 }
729e4ab9
A
274 // We have no recourse upon failure unless we want to propagate the failure
275 // out.
46f4442e
A
276}
277
57a6839d
A
278UDate PersianCalendar::defaultCenturyStart() const {
279 // lazy-evaluate systemDefaultCenturyStart
280 umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
281 return gSystemDefaultCenturyStart;
282}
283
284int32_t PersianCalendar::defaultCenturyStartYear() const {
285 // lazy-evaluate systemDefaultCenturyStartYear
286 umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
287 return gSystemDefaultCenturyStartYear;
288}
289
46f4442e
A
290UOBJECT_DEFINE_RTTI_IMPLEMENTATION(PersianCalendar)
291
292U_NAMESPACE_END
293
294#endif
295