1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 * Copyright (C) 2003-2014, International Business Machines Corporation
5 * and others. All Rights Reserved.
6 ******************************************************************************
7 *
8 * File INDIANCAL.CPP
9 *****************************************************************************
10 */
11
12 #include "indiancal.h"
13 #include <stdlib.h>
14 #if !UCONFIG_NO_FORMATTING
15
16 #include "mutex.h"
17 #include <float.h>
18 #include "gregoimp.h" // Math
19 #include "uhash.h"
20
21 // Debugging
22 #ifdef U_DEBUG_INDIANCAL
23 #include <stdio.h>
24 #include <stdarg.h>
25
26 #endif
27
28 U_NAMESPACE_BEGIN
29
30 // Implementation of the IndianCalendar class
31
32 //-------------------------------------------------------------------------
33 // Constructors...
34 //-------------------------------------------------------------------------
35
36
clone() const37 IndianCalendar* IndianCalendar::clone() const {
38 return new IndianCalendar(*this);
39 }
40
IndianCalendar(const Locale & aLocale,UErrorCode & success)41 IndianCalendar::IndianCalendar(const Locale& aLocale, UErrorCode& success)
42 : Calendar(TimeZone::forLocaleOrDefault(aLocale), aLocale, success)
43 {
44 setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
45 }
46
IndianCalendar(const IndianCalendar & other)47 IndianCalendar::IndianCalendar(const IndianCalendar& other) : Calendar(other) {
48 }
49
~IndianCalendar()50 IndianCalendar::~IndianCalendar()
51 {
52 }
getType() const53 const char *IndianCalendar::getType() const {
54 return "indian";
55 }
56
57 static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = {
58 // Minimum Greatest Least Maximum
59 // Minimum Maximum
60 { 0, 0, 0, 0}, // ERA
61 { -5000000, -5000000, 5000000, 5000000}, // YEAR
62 { 0, 0, 11, 11}, // MONTH
63 { 1, 1, 52, 53}, // WEEK_OF_YEAR
64 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH
65 { 1, 1, 30, 31}, // DAY_OF_MONTH
66 { 1, 1, 365, 366}, // DAY_OF_YEAR
67 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK
68 { -1, -1, 5, 5}, // DAY_OF_WEEK_IN_MONTH
69 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM
70 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR
71 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY
72 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE
73 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND
74 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND
75 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET
76 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET
77 { -5000000, -5000000, 5000000, 5000000}, // YEAR_WOY
78 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL
79 { -5000000, -5000000, 5000000, 5000000}, // EXTENDED_YEAR
80 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY
81 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY
82 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH
83 { 0, 0, 11, 11}, // ORDINAL_MONTH
84 };
85
86 static const int32_t INDIAN_ERA_START = 78;
87 static const int32_t INDIAN_YEAR_START = 80;
88
handleGetLimit(UCalendarDateFields field,ELimitType limitType) const89 int32_t IndianCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const {
90 return LIMITS[field][limitType];
91 }
92
93 /*
94 * Determine whether the given gregorian year is a Leap year
95 */
isGregorianLeap(int32_t year)96 static UBool isGregorianLeap(int32_t year)
97 {
98 return Grego::isLeapYear(year);
99 }
100
101 //----------------------------------------------------------------------
102 // Calendar framework
103 //----------------------------------------------------------------------
104
105 /*
106 * Return the length (in days) of the given month.
107 *
108 * @param eyear The year in Saka Era
109 * @param month The month(0-based) in Indian calendar
110 */
handleGetMonthLength(int32_t eyear,int32_t month) const111 int32_t IndianCalendar::handleGetMonthLength(int32_t eyear, int32_t month) const {
112 if (month < 0 || month > 11) {
113 eyear += ClockMath::floorDivide(month, 12, &month);
114 }
115
116 if (isGregorianLeap(eyear + INDIAN_ERA_START) && month == 0) {
117 return 31;
118 }
119
120 if (month >= 1 && month <= 5) {
121 return 31;
122 }
123
124 return 30;
125 }
126
127 /*
128 * Return the number of days in the given Indian year
129 *
130 * @param eyear The year in Saka Era.
131 */
handleGetYearLength(int32_t eyear) const132 int32_t IndianCalendar::handleGetYearLength(int32_t eyear) const {
133 return isGregorianLeap(eyear + INDIAN_ERA_START) ? 366 : 365;
134 }
135 /*
136 * Returns the Julian Day corresponding to gregorian date
137 *
138 * @param year The Gregorian year
139 * @param month The month in Gregorian Year, 0 based.
140 * @param date The date in Gregorian day in month
141 */
gregorianToJD(int32_t year,int32_t month,int32_t date)142 static double gregorianToJD(int32_t year, int32_t month, int32_t date) {
143 return Grego::fieldsToDay(year, month, date) + kEpochStartAsJulianDay - 0.5;
144 }
145
146 /*
147 * Returns the Gregorian Date corresponding to a given Julian Day
148 * Month is 0 based.
149 * @param jd The Julian Day
150 */
jdToGregorian(double jd,int32_t gregorianDate[3])151 static int32_t* jdToGregorian(double jd, int32_t gregorianDate[3]) {
152 int32_t gdow;
153 Grego::dayToFields(jd - kEpochStartAsJulianDay,
154 gregorianDate[0], gregorianDate[1], gregorianDate[2], gdow);
155 return gregorianDate;
156 }
157
158
159 //-------------------------------------------------------------------------
160 // Functions for converting from field values to milliseconds....
161 //-------------------------------------------------------------------------
IndianToJD(int32_t year,int32_t month,int32_t date)162 static double IndianToJD(int32_t year, int32_t month, int32_t date) {
163 int32_t leapMonth, gyear, m;
164 double start, jd;
165
166 gyear = year + INDIAN_ERA_START;
167
168
169 if(isGregorianLeap(gyear)) {
170 leapMonth = 31;
171 start = gregorianToJD(gyear, 2 /* The third month in 0 based month */, 21);
172 }
173 else {
174 leapMonth = 30;
175 start = gregorianToJD(gyear, 2 /* The third month in 0 based month */, 22);
176 }
177
178 if (month == 1) {
179 jd = start + (date - 1);
180 } else {
181 jd = start + leapMonth;
182 m = month - 2;
183
184 //m = Math.min(m, 5);
185 if (m > 5) {
186 m = 5;
187 }
188
189 jd += m * 31;
190
191 if (month >= 8) {
192 m = month - 7;
193 jd += m * 30;
194 }
195 jd += date - 1;
196 }
197
198 return jd;
199 }
200
201 /*
202 * Return JD of start of given month/year of Indian Calendar
203 * @param eyear The year in Indian Calendar measured from Saka Era (78 AD).
204 * @param month The month in Indian calendar
205 */
handleComputeMonthStart(int32_t eyear,int32_t month,UBool) const206 int32_t IndianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /* useMonth */ ) const {
207
208 //month is 0 based; converting it to 1-based
209 int32_t imonth;
210
211 // If the month is out of range, adjust it into range, and adjust the extended year accordingly
212 if (month < 0 || month > 11) {
213 eyear += (int32_t)ClockMath::floorDivide(month, 12, &month);
214 }
215
216 if(month == 12){
217 imonth = 1;
218 } else {
219 imonth = month + 1;
220 }
221
222 double jd = IndianToJD(eyear ,imonth, 1);
223
224 return (int32_t)jd;
225 }
226
227 //-------------------------------------------------------------------------
228 // Functions for converting from milliseconds to field values
229 //-------------------------------------------------------------------------
230
handleGetExtendedYear()231 int32_t IndianCalendar::handleGetExtendedYear() {
232 int32_t year;
233
234 if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
235 year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
236 } else {
237 year = internalGet(UCAL_YEAR, 1); // Default to year 1
238 }
239
240 return year;
241 }
242
243 /*
244 * Override Calendar to compute several fields specific to the Indian
245 * calendar system. These are:
246 *
247 * <ul><li>ERA
248 * <li>YEAR
249 * <li>MONTH
250 * <li>DAY_OF_MONTH
251 * <li>EXTENDED_YEAR</ul>
252 *
253 * The DAY_OF_WEEK and DOW_LOCAL fields are already set when this
254 * method is called. The getGregorianXxx() methods return Gregorian
255 * calendar equivalents for the given Julian day.
256 */
handleComputeFields(int32_t julianDay,UErrorCode &)257 void IndianCalendar::handleComputeFields(int32_t julianDay, UErrorCode& /* status */) {
258 double jdAtStartOfGregYear;
259 int32_t leapMonth, IndianYear, yday, IndianMonth, IndianDayOfMonth, mday;
260 int32_t gregorianYear; // Stores gregorian date corresponding to Julian day;
261 int32_t gd[3];
262
263 gregorianYear = jdToGregorian(julianDay, gd)[0]; // Gregorian date for Julian day
264 IndianYear = gregorianYear - INDIAN_ERA_START; // Year in Saka era
265 jdAtStartOfGregYear = gregorianToJD(gregorianYear, 0, 1); // JD at start of Gregorian year
266 yday = (int32_t)(julianDay - jdAtStartOfGregYear); // Day number in Gregorian year (starting from 0)
267
268 if (yday < INDIAN_YEAR_START) {
269 // Day is at the end of the preceding Saka year
270 IndianYear -= 1;
271 leapMonth = isGregorianLeap(gregorianYear - 1) ? 31 : 30; // Days in leapMonth this year, previous Gregorian year
272 yday += leapMonth + (31 * 5) + (30 * 3) + 10;
273 } else {
274 leapMonth = isGregorianLeap(gregorianYear) ? 31 : 30; // Days in leapMonth this year
275 yday -= INDIAN_YEAR_START;
276 }
277
278 if (yday < leapMonth) {
279 IndianMonth = 0;
280 IndianDayOfMonth = yday + 1;
281 } else {
282 mday = yday - leapMonth;
283 if (mday < (31 * 5)) {
284 IndianMonth = (int32_t)uprv_floor(mday / 31) + 1;
285 IndianDayOfMonth = (mday % 31) + 1;
286 } else {
287 mday -= 31 * 5;
288 IndianMonth = (int32_t)uprv_floor(mday / 30) + 6;
289 IndianDayOfMonth = (mday % 30) + 1;
290 }
291 }
292
293 internalSet(UCAL_ERA, 0);
294 internalSet(UCAL_EXTENDED_YEAR, IndianYear);
295 internalSet(UCAL_YEAR, IndianYear);
296 internalSet(UCAL_MONTH, IndianMonth);
297 internalSet(UCAL_ORDINAL_MONTH, IndianMonth);
298 internalSet(UCAL_DAY_OF_MONTH, IndianDayOfMonth);
299 internalSet(UCAL_DAY_OF_YEAR, yday + 1); // yday is 0-based
300 }
301
302 constexpr uint32_t kIndianRelatedYearDiff = 79;
303
getRelatedYear(UErrorCode & status) const304 int32_t IndianCalendar::getRelatedYear(UErrorCode &status) const
305 {
306 int32_t year = get(UCAL_EXTENDED_YEAR, status);
307 if (U_FAILURE(status)) {
308 return 0;
309 }
310 return year + kIndianRelatedYearDiff;
311 }
312
setRelatedYear(int32_t year)313 void IndianCalendar::setRelatedYear(int32_t year)
314 {
315 // set extended year
316 set(UCAL_EXTENDED_YEAR, year - kIndianRelatedYearDiff);
317 }
318
319 /**
320 * The system maintains a static default century start date and Year. They are
321 * initialized the first time they are used. Once the system default century date
322 * and year are set, they do not change.
323 */
324 static UDate gSystemDefaultCenturyStart = DBL_MIN;
325 static int32_t gSystemDefaultCenturyStartYear = -1;
326 static icu::UInitOnce gSystemDefaultCenturyInit {};
327
328
haveDefaultCentury() const329 UBool IndianCalendar::haveDefaultCentury() const
330 {
331 return true;
332 }
333
334 static void U_CALLCONV
initializeSystemDefaultCentury()335 initializeSystemDefaultCentury()
336 {
337 // initialize systemDefaultCentury and systemDefaultCenturyYear based
338 // on the current time. They'll be set to 80 years before
339 // the current time.
340 UErrorCode status = U_ZERO_ERROR;
341
342 IndianCalendar calendar ( Locale ( "@calendar=Indian" ), status);
343 if ( U_SUCCESS ( status ) ) {
344 calendar.setTime ( Calendar::getNow(), status );
345 calendar.add ( UCAL_YEAR, -80, status );
346
347 UDate newStart = calendar.getTime ( status );
348 int32_t newYear = calendar.get ( UCAL_YEAR, status );
349
350 gSystemDefaultCenturyStart = newStart;
351 gSystemDefaultCenturyStartYear = newYear;
352 }
353 // We have no recourse upon failure.
354 }
355
356
357 UDate
defaultCenturyStart() const358 IndianCalendar::defaultCenturyStart() const
359 {
360 // lazy-evaluate systemDefaultCenturyStart
361 umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
362 return gSystemDefaultCenturyStart;
363 }
364
365 int32_t
defaultCenturyStartYear() const366 IndianCalendar::defaultCenturyStartYear() const
367 {
368 // lazy-evaluate systemDefaultCenturyStartYear
369 umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
370 return gSystemDefaultCenturyStartYear;
371 }
372
373
374 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IndianCalendar)
375
376 U_NAMESPACE_END
377
378 #endif
379
380