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 "astro.h" // CalendarAstronomer
20 #include "uhash.h"
21 
22 // Debugging
23 #ifdef U_DEBUG_INDIANCAL
24 #include <stdio.h>
25 #include <stdarg.h>
26 
27 #endif
28 
29 U_NAMESPACE_BEGIN
30 
31 // Implementation of the IndianCalendar class
32 
33 //-------------------------------------------------------------------------
34 // Constructors...
35 //-------------------------------------------------------------------------
36 
37 
clone() const38 IndianCalendar* IndianCalendar::clone() const {
39   return new IndianCalendar(*this);
40 }
41 
IndianCalendar(const Locale & aLocale,UErrorCode & success)42 IndianCalendar::IndianCalendar(const Locale& aLocale, UErrorCode& success)
43   :   Calendar(TimeZone::forLocaleOrDefault(aLocale), aLocale, success)
44 {
45   setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
46 }
47 
IndianCalendar(const IndianCalendar & other)48 IndianCalendar::IndianCalendar(const IndianCalendar& other) : Calendar(other) {
49 }
50 
~IndianCalendar()51 IndianCalendar::~IndianCalendar()
52 {
53 }
getType() const54 const char *IndianCalendar::getType() const {
55    return "indian";
56 }
57 
58 static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = {
59     // Minimum  Greatest     Least   Maximum
60     //           Minimum   Maximum
61     {        0,        0,        0,        0}, // ERA
62     { -5000000, -5000000,  5000000,  5000000}, // YEAR
63     {        0,        0,       11,       11}, // MONTH
64     {        1,        1,       52,       53}, // WEEK_OF_YEAR
65     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH
66     {        1,        1,       30,       31}, // DAY_OF_MONTH
67     {        1,        1,      365,      366}, // DAY_OF_YEAR
68     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK
69     {       -1,       -1,        5,        5}, // DAY_OF_WEEK_IN_MONTH
70     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM
71     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR
72     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY
73     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE
74     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND
75     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND
76     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET
77     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET
78     { -5000000, -5000000,  5000000,  5000000}, // YEAR_WOY
79     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL
80     { -5000000, -5000000,  5000000,  5000000}, // EXTENDED_YEAR
81     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY
82     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY
83     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_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 eyar 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_DAY_OF_MONTH, IndianDayOfMonth);
298    internalSet(UCAL_DAY_OF_YEAR, yday + 1); // yday is 0-based
299 }
300 
301 UBool
inDaylightTime(UErrorCode & status) const302 IndianCalendar::inDaylightTime(UErrorCode& status) const
303 {
304     // copied from GregorianCalendar
305     if (U_FAILURE(status) || !getTimeZone().useDaylightTime()) {
306         return FALSE;
307     }
308 
309     // Force an update of the state of the Calendar.
310     ((IndianCalendar*)this)->complete(status); // cast away const
311 
312     return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE);
313 }
314 
315 
316 /**
317  * The system maintains a static default century start date and Year.  They are
318  * initialized the first time they are used.  Once the system default century date
319  * and year are set, they do not change.
320  */
321 static UDate           gSystemDefaultCenturyStart       = DBL_MIN;
322 static int32_t         gSystemDefaultCenturyStartYear   = -1;
323 static icu::UInitOnce  gSystemDefaultCenturyInit        = U_INITONCE_INITIALIZER;
324 
325 
haveDefaultCentury() const326 UBool IndianCalendar::haveDefaultCentury() const
327 {
328     return TRUE;
329 }
330 
331 static void U_CALLCONV
initializeSystemDefaultCentury()332 initializeSystemDefaultCentury()
333 {
334     // initialize systemDefaultCentury and systemDefaultCenturyYear based
335     // on the current time.  They'll be set to 80 years before
336     // the current time.
337     UErrorCode status = U_ZERO_ERROR;
338 
339     IndianCalendar calendar ( Locale ( "@calendar=Indian" ), status);
340     if ( U_SUCCESS ( status ) ) {
341         calendar.setTime ( Calendar::getNow(), status );
342         calendar.add ( UCAL_YEAR, -80, status );
343 
344         UDate    newStart = calendar.getTime ( status );
345         int32_t  newYear  = calendar.get ( UCAL_YEAR, status );
346 
347         gSystemDefaultCenturyStart = newStart;
348         gSystemDefaultCenturyStartYear = newYear;
349     }
350     // We have no recourse upon failure.
351 }
352 
353 
354 UDate
defaultCenturyStart() const355 IndianCalendar::defaultCenturyStart() const
356 {
357     // lazy-evaluate systemDefaultCenturyStart
358     umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
359     return gSystemDefaultCenturyStart;
360 }
361 
362 int32_t
defaultCenturyStartYear() const363 IndianCalendar::defaultCenturyStartYear() const
364 {
365     // lazy-evaluate systemDefaultCenturyStartYear
366     umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
367     return    gSystemDefaultCenturyStartYear;
368 }
369 
370 
371 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IndianCalendar)
372 
373 U_NAMESPACE_END
374 
375 #endif
376 
377