• 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  * 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,UErrorCode &) const111 int32_t IndianCalendar::handleGetMonthLength(int32_t eyear, int32_t month, UErrorCode& /* status */) 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,UErrorCode & status) const206 int64_t IndianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /* useMonth */, UErrorCode& status) const {
207    if (U_FAILURE(status)) {
208        return 0;
209    }
210 
211    //month is 0 based; converting it to 1-based
212    int32_t imonth;
213 
214     // If the month is out of range, adjust it into range, and adjust the extended year accordingly
215    if (month < 0 || month > 11) {
216       if (uprv_add32_overflow(eyear, ClockMath::floorDivide(month, 12, &month), &eyear)) {
217           status = U_ILLEGAL_ARGUMENT_ERROR;
218           return 0;
219       }
220    }
221 
222    if(month == 12){
223        imonth = 1;
224    } else {
225        imonth = month + 1;
226    }
227 
228    int64_t jd = IndianToJD(eyear ,imonth, 1);
229 
230    return jd;
231 }
232 
233 //-------------------------------------------------------------------------
234 // Functions for converting from milliseconds to field values
235 //-------------------------------------------------------------------------
236 
handleGetExtendedYear(UErrorCode & status)237 int32_t IndianCalendar::handleGetExtendedYear(UErrorCode& status) {
238     if (U_FAILURE(status)) {
239         return 0;
240     }
241     int32_t year;
242 
243     if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
244         year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
245     } else {
246         year = internalGet(UCAL_YEAR, 1); // Default to year 1
247     }
248 
249     return year;
250 }
251 
252 /*
253  * Override Calendar to compute several fields specific to the Indian
254  * calendar system.  These are:
255  *
256  * <ul><li>ERA
257  * <li>YEAR
258  * <li>MONTH
259  * <li>DAY_OF_MONTH
260  * <li>EXTENDED_YEAR</ul>
261  *
262  * The DAY_OF_WEEK and DOW_LOCAL fields are already set when this
263  * method is called. The getGregorianXxx() methods return Gregorian
264  * calendar equivalents for the given Julian day.
265  */
handleComputeFields(int32_t julianDay,UErrorCode &)266 void IndianCalendar::handleComputeFields(int32_t julianDay, UErrorCode&  /* status */) {
267     double jdAtStartOfGregYear;
268     int32_t leapMonth, IndianYear, yday, IndianMonth, IndianDayOfMonth, mday;
269     int32_t gregorianYear;      // Stores gregorian date corresponding to Julian day;
270     int32_t gd[3];
271 
272     gregorianYear = jdToGregorian(julianDay, gd)[0];          // Gregorian date for Julian day
273     IndianYear = gregorianYear - INDIAN_ERA_START;            // Year in Saka era
274     jdAtStartOfGregYear = gregorianToJD(gregorianYear, 0, 1); // JD at start of Gregorian year
275     yday = (int32_t)(julianDay - jdAtStartOfGregYear);        // Day number in Gregorian year (starting from 0)
276 
277     if (yday < INDIAN_YEAR_START) {
278         // Day is at the end of the preceding Saka year
279         IndianYear -= 1;
280         leapMonth = isGregorianLeap(gregorianYear - 1) ? 31 : 30; // Days in leapMonth this year, previous Gregorian year
281         yday += leapMonth + (31 * 5) + (30 * 3) + 10;
282     } else {
283         leapMonth = isGregorianLeap(gregorianYear) ? 31 : 30; // Days in leapMonth this year
284         yday -= INDIAN_YEAR_START;
285     }
286 
287     if (yday < leapMonth) {
288         IndianMonth = 0;
289         IndianDayOfMonth = yday + 1;
290     } else {
291         mday = yday - leapMonth;
292         if (mday < (31 * 5)) {
293             IndianMonth = (int32_t)uprv_floor(mday / 31) + 1;
294             IndianDayOfMonth = (mday % 31) + 1;
295         } else {
296             mday -= 31 * 5;
297             IndianMonth = (int32_t)uprv_floor(mday / 30) + 6;
298             IndianDayOfMonth = (mday % 30) + 1;
299         }
300    }
301 
302    internalSet(UCAL_ERA, 0);
303    internalSet(UCAL_EXTENDED_YEAR, IndianYear);
304    internalSet(UCAL_YEAR, IndianYear);
305    internalSet(UCAL_MONTH, IndianMonth);
306    internalSet(UCAL_ORDINAL_MONTH, IndianMonth);
307    internalSet(UCAL_DAY_OF_MONTH, IndianDayOfMonth);
308    internalSet(UCAL_DAY_OF_YEAR, yday + 1); // yday is 0-based
309 }
310 
311 constexpr uint32_t kIndianRelatedYearDiff = 79;
312 
getRelatedYear(UErrorCode & status) const313 int32_t IndianCalendar::getRelatedYear(UErrorCode &status) const
314 {
315     int32_t year = get(UCAL_EXTENDED_YEAR, status);
316     if (U_FAILURE(status)) {
317         return 0;
318     }
319     if (uprv_add32_overflow(year, kIndianRelatedYearDiff, &year)) {
320         status = U_ILLEGAL_ARGUMENT_ERROR;
321         return 0;
322     }
323     return year;
324 }
325 
setRelatedYear(int32_t year)326 void IndianCalendar::setRelatedYear(int32_t year)
327 {
328     // set extended year
329     set(UCAL_EXTENDED_YEAR, year - kIndianRelatedYearDiff);
330 }
331 
332 IMPL_SYSTEM_DEFAULT_CENTURY(IndianCalendar, "@calendar=indian")
333 
334 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IndianCalendar)
335 
336 U_NAMESPACE_END
337 
338 #endif
339 
340