• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2003-2007, International Business Machines Corporation
3  * and others. All Rights Reserved.
4  ******************************************************************************
5  *
6  * File INDIANCAL.CPP
7  *****************************************************************************
8  */
9 
10 #include "indiancal.h"
11 #include <stdlib.h>
12 #if !UCONFIG_NO_FORMATTING
13 
14 #include "mutex.h"
15 #include <float.h>
16 #include "gregoimp.h" // Math
17 #include "astro.h" // CalendarAstronomer
18 #include "uhash.h"
19 #include "ucln_in.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 Calendar* 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::createDefault(), 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   {        1,        1, 5000000, 5000000 }, // YEAR
62   {        0,        0,      11,      11 }, // MONTH
63   {        1,        1,      52,      53 }, // WEEK_OF_YEAR
64   {        0,        0,       4,       6 }, // 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   {        1,        1,  5000001,  5000001}, // YEAR_WOY
78   {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL
79   {        1,        1,  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 };
83 
84 static const double JULIAN_EPOCH = 1721425.5;
85 static const int32_t INDIAN_ERA_START  = 78;
86 static const int32_t INDIAN_YEAR_START = 80;
87 
handleGetLimit(UCalendarDateFields field,ELimitType limitType) const88 int32_t IndianCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const {
89   return LIMITS[field][limitType];
90 }
91 
92 /*
93  * Determine whether the given gregorian year is a Leap year
94  */
isGregorianLeap(int32_t year)95 static UBool isGregorianLeap(int32_t year)
96 {
97     return ((year % 4) == 0) && (!(((year % 100) == 0) && ((year % 400) != 0)));
98 }
99 
100 //----------------------------------------------------------------------
101 // Calendar framework
102 //----------------------------------------------------------------------
103 
104 /*
105  * Return the length (in days) of the given month.
106  *
107  * @param eyear  The year in Saka Era
108  * @param month  The month(0-based) in Indian calendar
109  */
handleGetMonthLength(int32_t eyear,int32_t month) const110 int32_t IndianCalendar::handleGetMonthLength(int32_t eyear, int32_t month) const {
111    if (month < 0 || month > 11) {
112       eyear += Math::floorDivide(month, 12, month);
113    }
114 
115    if (isGregorianLeap(eyear + INDIAN_ERA_START) && month == 0) {
116        return 31;
117    }
118 
119    if (month >= 1 && month <= 5) {
120        return 31;
121    }
122 
123    return 30;
124 }
125 
126 /*
127  * Return the number of days in the given Indian year
128  *
129  * @param eyear The year in Saka Era.
130  */
handleGetYearLength(int32_t eyear) const131 int32_t IndianCalendar::handleGetYearLength(int32_t eyear) const {
132     return isGregorianLeap(eyear + INDIAN_ERA_START) ? 366 : 365;
133 }
134 /*
135  * Returns the Julian Day corresponding to gregorian date
136  *
137  * @param year The Gregorian year
138  * @param month The month in Gregorian Year
139  * @param date The date in Gregorian day in month
140  */
gregorianToJD(int32_t year,int32_t month,int32_t date)141 static double gregorianToJD(int32_t year, int32_t month, int32_t date) {
142    double julianDay = (JULIAN_EPOCH - 1) +
143       (365 * (year - 1)) +
144       uprv_floor((year - 1) / 4) +
145       (-uprv_floor((year - 1) / 100)) +
146       uprv_floor((year - 1) / 400) +
147       uprv_floor((((367 * month) - 362) / 12) +
148             ((month <= 2) ? 0 :
149              (isGregorianLeap(year) ? -1 : -2)
150             ) +
151             date);
152 
153    return julianDay;
154 }
155 
156 /*
157  * Returns the Gregorian Date corresponding to a given Julian Day
158  * @param jd The Julian Day
159  */
jdToGregorian(double jd,int32_t gregorianDate[3])160 static int32_t* jdToGregorian(double jd, int32_t gregorianDate[3]) {
161    double wjd, depoch, quadricent, dqc, cent, dcent, quad, dquad, yindex, yearday, leapadj;
162    int32_t year, month, day;
163    wjd = uprv_floor(jd - 0.5) + 0.5;
164    depoch = wjd - JULIAN_EPOCH;
165    quadricent = uprv_floor(depoch / 146097);
166    dqc = (int32_t)uprv_floor(depoch) % 146097;
167    cent = uprv_floor(dqc / 36524);
168    dcent = (int32_t)uprv_floor(dqc) % 36524;
169    quad = uprv_floor(dcent / 1461);
170    dquad = (int32_t)uprv_floor(dcent) % 1461;
171    yindex = uprv_floor(dquad / 365);
172    year = (int32_t)((quadricent * 400) + (cent * 100) + (quad * 4) + yindex);
173    if (!((cent == 4) || (yindex == 4))) {
174       year++;
175    }
176    yearday = wjd - gregorianToJD(year, 1, 1);
177    leapadj = ((wjd < gregorianToJD(year, 3, 1)) ? 0
178          :
179          (isGregorianLeap(year) ? 1 : 2)
180          );
181    month = (int32_t)uprv_floor((((yearday + leapadj) * 12) + 373) / 367);
182    day = (int32_t)(wjd - gregorianToJD(year, month, 1)) + 1;
183 
184    gregorianDate[0] = year;
185    gregorianDate[1] = month;
186    gregorianDate[2] = day;
187 
188    return gregorianDate;
189 }
190 
191 
192 //-------------------------------------------------------------------------
193 // Functions for converting from field values to milliseconds....
194 //-------------------------------------------------------------------------
IndianToJD(int32_t year,int32_t month,int32_t date)195 static double IndianToJD(int32_t year, int32_t month, int32_t date) {
196    int32_t leapMonth, gyear, m;
197    double start, jd;
198 
199    gyear = year + INDIAN_ERA_START;
200 
201 
202    if(isGregorianLeap(gyear)) {
203       leapMonth = 31;
204       start = gregorianToJD(gyear, 3, 21);
205    }
206    else {
207       leapMonth = 30;
208       start = gregorianToJD(gyear, 3, 22);
209    }
210 
211    if (month == 1) {
212       jd = start + (date - 1);
213    } else {
214       jd = start + leapMonth;
215       m = month - 2;
216 
217       //m = Math.min(m, 5);
218       if (m > 5) {
219           m = 5;
220       }
221 
222       jd += m * 31;
223 
224       if (month >= 8) {
225          m = month - 7;
226          jd += m * 30;
227       }
228       jd += date - 1;
229    }
230 
231    return jd;
232 }
233 
234 /*
235  * Return JD of start of given month/year of Indian Calendar
236  * @param eyear The year in Indian Calendar measured from Saka Era (78 AD).
237  * @param month The month in Indian calendar
238  */
handleComputeMonthStart(int32_t eyear,int32_t month,UBool useMonth) const239 int32_t IndianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool useMonth) const {
240 
241    //month is 0 based; converting it to 1-based
242    int32_t imonth;
243 
244     // If the month is out of range, adjust it into range, and adjust the extended eyar accordingly
245    if (month < 0 || month > 11) {
246       eyear += (int32_t)Math::floorDivide(month, 12, month);
247    }
248 
249    if(month == 12){
250        imonth = 1;
251    } else {
252        imonth = month + 1;
253    }
254 
255    double jd = IndianToJD(eyear ,imonth, 1);
256 
257    return (int32_t)jd;
258 }
259 
260 //-------------------------------------------------------------------------
261 // Functions for converting from milliseconds to field values
262 //-------------------------------------------------------------------------
263 
handleGetExtendedYear()264 int32_t IndianCalendar::handleGetExtendedYear() {
265     int32_t year;
266 
267     if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
268         year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
269     } else {
270         year = internalGet(UCAL_YEAR, 1); // Default to year 1
271     }
272 
273     return year;
274 }
275 
276 /*
277  * Override Calendar to compute several fields specific to the Indian
278  * calendar system.  These are:
279  *
280  * <ul><li>ERA
281  * <li>YEAR
282  * <li>MONTH
283  * <li>DAY_OF_MONTH
284  * <li>EXTENDED_YEAR</ul>
285  *
286  * The DAY_OF_WEEK and DOW_LOCAL fields are already set when this
287  * method is called. The getGregorianXxx() methods return Gregorian
288  * calendar equivalents for the given Julian day.
289  */
handleComputeFields(int32_t julianDay,UErrorCode & status)290 void IndianCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) {
291    double jdAtStartOfGregYear;
292    int32_t leapMonth, IndianYear, yday, IndianMonth, IndianDayOfMonth, mday;
293    int32_t gregorianYear;      // Stores gregorian date corresponding to Julian day;
294    int32_t gd[3];
295 
296    gregorianYear = jdToGregorian(julianDay, gd)[0];          // Gregorian date for Julian day
297    IndianYear = gregorianYear - INDIAN_ERA_START;            // Year in Saka era
298    jdAtStartOfGregYear = gregorianToJD(gregorianYear, 1, 1); // JD at start of Gregorian year
299    yday = (int32_t)(julianDay - jdAtStartOfGregYear);        // Day number in Gregorian year (starting from 0)
300    leapMonth = isGregorianLeap(gregorianYear) ? 31 : 30;     // Days in leapMonth this year
301 
302    if (yday < INDIAN_YEAR_START) {
303       //  Day is at the end of the preceding Saka year
304       IndianYear -= 1;
305       yday += leapMonth + (31 * 5) + (30 * 3) + 10 + INDIAN_YEAR_START;
306    }
307 
308    yday -= INDIAN_YEAR_START;
309    if (yday < leapMonth) {
310       IndianMonth = 0;
311       IndianDayOfMonth = yday + 1;
312    } else {
313       mday = yday - leapMonth;
314       if (mday < (31 * 5)) {
315          IndianMonth = (int32_t)uprv_floor(mday / 31) + 1;
316          IndianDayOfMonth = (mday % 31) + 1;
317       } else {
318          mday -= 31 * 5;
319          IndianMonth = (int32_t)uprv_floor(mday / 30) + 6;
320          IndianDayOfMonth = (mday % 30) + 1;
321       }
322    }
323 
324    internalSet(UCAL_ERA, 0);
325    internalSet(UCAL_EXTENDED_YEAR, IndianYear);
326    internalSet(UCAL_YEAR, IndianYear);
327    internalSet(UCAL_MONTH, IndianMonth);
328    internalSet(UCAL_DAY_OF_MONTH, IndianDayOfMonth );
329 }
330 
331 UBool
inDaylightTime(UErrorCode & status) const332 IndianCalendar::inDaylightTime(UErrorCode& status) const
333 {
334     // copied from GregorianCalendar
335     if (U_FAILURE(status) || !getTimeZone().useDaylightTime()) {
336         return FALSE;
337     }
338 
339     // Force an update of the state of the Calendar.
340     ((IndianCalendar*)this)->complete(status); // cast away const
341 
342     return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE);
343 }
344 
345 // default century
346 const UDate     IndianCalendar::fgSystemDefaultCentury          = DBL_MIN;
347 const int32_t   IndianCalendar::fgSystemDefaultCenturyYear      = -1;
348 
349 UDate           IndianCalendar::fgSystemDefaultCenturyStart     = DBL_MIN;
350 int32_t         IndianCalendar::fgSystemDefaultCenturyStartYear = -1;
351 
352 
haveDefaultCentury() const353 UBool IndianCalendar::haveDefaultCentury() const
354 {
355     return TRUE;
356 }
357 
defaultCenturyStart() const358 UDate IndianCalendar::defaultCenturyStart() const
359 {
360     return internalGetDefaultCenturyStart();
361 }
362 
defaultCenturyStartYear() const363 int32_t IndianCalendar::defaultCenturyStartYear() const
364 {
365     return internalGetDefaultCenturyStartYear();
366 }
367 
368 UDate
internalGetDefaultCenturyStart() const369 IndianCalendar::internalGetDefaultCenturyStart() const
370 {
371     // lazy-evaluate systemDefaultCenturyStart
372     UBool needsUpdate;
373     {
374         Mutex m;
375         needsUpdate = (fgSystemDefaultCenturyStart == fgSystemDefaultCentury);
376     }
377 
378     if (needsUpdate) {
379         initializeSystemDefaultCentury();
380     }
381 
382     // use defaultCenturyStart unless it's the flag value;
383     // then use systemDefaultCenturyStart
384 
385     return fgSystemDefaultCenturyStart;
386 }
387 
388 int32_t
internalGetDefaultCenturyStartYear() const389 IndianCalendar::internalGetDefaultCenturyStartYear() const
390 {
391     // lazy-evaluate systemDefaultCenturyStartYear
392     UBool needsUpdate;
393     {
394         Mutex m;
395 
396         needsUpdate = (fgSystemDefaultCenturyStart == fgSystemDefaultCentury);
397     }
398 
399     if (needsUpdate) {
400         initializeSystemDefaultCentury();
401     }
402 
403     // use defaultCenturyStart unless it's the flag value;
404     // then use systemDefaultCenturyStartYear
405 
406     return    fgSystemDefaultCenturyStartYear;
407 }
408 
409 void
initializeSystemDefaultCentury()410 IndianCalendar::initializeSystemDefaultCentury()
411 {
412     // initialize systemDefaultCentury and systemDefaultCenturyYear based
413     // on the current time.  They'll be set to 80 years before
414     // the current time.
415     // No point in locking as it should be idempotent.
416     if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury) {
417         UErrorCode status = U_ZERO_ERROR;
418 
419         IndianCalendar calendar(Locale("@calendar=Indian"),status);
420         if (U_SUCCESS(status)) {
421             calendar.setTime(Calendar::getNow(), status);
422             calendar.add(UCAL_YEAR, -80, status);
423 
424             UDate    newStart = calendar.getTime(status);
425             int32_t  newYear  = calendar.get(UCAL_YEAR, status);
426 
427             {
428                 Mutex m;
429 
430                 fgSystemDefaultCenturyStart = newStart;
431                 fgSystemDefaultCenturyStartYear = newYear;
432             }
433         }
434 
435         // We have no recourse upon failure unless we want to propagate the failure
436         // out.
437     }
438 }
439 
440 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IndianCalendar)
441 
442 U_NAMESPACE_END
443 
444 #endif
445 
446