Coverage Report

Created: 2018-09-25 14:53

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