1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 * Copyright (C) 2003-2014, International Business Machines Corporation
5 * and others. All Rights Reserved.
6 ******************************************************************************
9 *****************************************************************************
12 #include "indiancal.h"
14 #if !UCONFIG_NO_FORMATTING
18 #include "gregoimp.h" // Math
19 #include "astro.h" // CalendarAstronomer
23 #ifdef U_DEBUG_INDIANCAL
31 // Implementation of the IndianCalendar class
33 //-------------------------------------------------------------------------
35 //-------------------------------------------------------------------------
38 Calendar
* IndianCalendar::clone() const {
39 return new IndianCalendar(*this);
42 IndianCalendar::IndianCalendar(const Locale
& aLocale
, UErrorCode
& success
)
43 : Calendar(TimeZone::createDefault(), aLocale
, success
)
45 setTimeInMillis(getNow(), success
); // Call this again now that the vtable is set up properly.
48 IndianCalendar::IndianCalendar(const IndianCalendar
& other
) : Calendar(other
) {
51 IndianCalendar::~IndianCalendar()
54 const char *IndianCalendar::getType() const {
58 static const int32_t LIMITS
[UCAL_FIELD_COUNT
][4] = {
59 // Minimum Greatest Least Maximum
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
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;
90 int32_t IndianCalendar::handleGetLimit(UCalendarDateFields field
, ELimitType limitType
) const {
91 return LIMITS
[field
][limitType
];
95 * Determine whether the given gregorian year is a Leap year
97 static UBool
isGregorianLeap(int32_t year
)
99 return ((year
% 4) == 0) && (!(((year
% 100) == 0) && ((year
% 400) != 0)));
102 //----------------------------------------------------------------------
103 // Calendar framework
104 //----------------------------------------------------------------------
107 * Return the length (in days) of the given month.
109 * @param eyear The year in Saka Era
110 * @param month The month(0-based) in Indian calendar
112 int32_t IndianCalendar::handleGetMonthLength(int32_t eyear
, int32_t month
) const {
113 if (month
< 0 || month
> 11) {
114 eyear
+= ClockMath::floorDivide(month
, 12, month
);
117 if (isGregorianLeap(eyear
+ INDIAN_ERA_START
) && month
== 0) {
121 if (month
>= 1 && month
<= 5) {
129 * Return the number of days in the given Indian year
131 * @param eyear The year in Saka Era.
133 int32_t IndianCalendar::handleGetYearLength(int32_t eyear
) const {
134 return isGregorianLeap(eyear
+ INDIAN_ERA_START
) ? 366 : 365;
137 * Returns the Julian Day corresponding to gregorian date
139 * @param year The Gregorian year
140 * @param month The month in Gregorian Year
141 * @param date The date in Gregorian day in month
143 static double gregorianToJD(int32_t year
, int32_t month
, int32_t date
) {
144 double julianDay
= (JULIAN_EPOCH
- 1) +
146 uprv_floor((year
- 1) / 4) +
147 (-uprv_floor((year
- 1) / 100)) +
148 uprv_floor((year
- 1) / 400) +
149 uprv_floor((((367 * month
) - 362) / 12) +
151 (isGregorianLeap(year
) ? -1 : -2)
159 * Returns the Gregorian Date corresponding to a given Julian Day
160 * @param jd The Julian Day
162 static int32_t* jdToGregorian(double jd
, int32_t gregorianDate
[3]) {
163 double wjd
, depoch
, quadricent
, dqc
, cent
, dcent
, quad
, dquad
, yindex
, yearday
, leapadj
;
164 int32_t year
, month
, day
;
165 wjd
= uprv_floor(jd
- 0.5) + 0.5;
166 depoch
= wjd
- JULIAN_EPOCH
;
167 quadricent
= uprv_floor(depoch
/ 146097);
168 dqc
= (int32_t)uprv_floor(depoch
) % 146097;
169 cent
= uprv_floor(dqc
/ 36524);
170 dcent
= (int32_t)uprv_floor(dqc
) % 36524;
171 quad
= uprv_floor(dcent
/ 1461);
172 dquad
= (int32_t)uprv_floor(dcent
) % 1461;
173 yindex
= uprv_floor(dquad
/ 365);
174 year
= (int32_t)((quadricent
* 400) + (cent
* 100) + (quad
* 4) + yindex
);
175 if (!((cent
== 4) || (yindex
== 4))) {
178 yearday
= wjd
- gregorianToJD(year
, 1, 1);
179 leapadj
= ((wjd
< gregorianToJD(year
, 3, 1)) ? 0
181 (isGregorianLeap(year
) ? 1 : 2)
183 month
= (int32_t)uprv_floor((((yearday
+ leapadj
) * 12) + 373) / 367);
184 day
= (int32_t)(wjd
- gregorianToJD(year
, month
, 1)) + 1;
186 gregorianDate
[0] = year
;
187 gregorianDate
[1] = month
;
188 gregorianDate
[2] = day
;
190 return gregorianDate
;
194 //-------------------------------------------------------------------------
195 // Functions for converting from field values to milliseconds....
196 //-------------------------------------------------------------------------
197 static double IndianToJD(int32_t year
, int32_t month
, int32_t date
) {
198 int32_t leapMonth
, gyear
, m
;
201 gyear
= year
+ INDIAN_ERA_START
;
204 if(isGregorianLeap(gyear
)) {
206 start
= gregorianToJD(gyear
, 3, 21);
210 start
= gregorianToJD(gyear
, 3, 22);
214 jd
= start
+ (date
- 1);
216 jd
= start
+ leapMonth
;
219 //m = Math.min(m, 5);
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
241 int32_t IndianCalendar::handleComputeMonthStart(int32_t eyear
, int32_t month
, UBool
/* useMonth */ ) const {
243 //month is 0 based; converting it to 1-based
246 // If the month is out of range, adjust it into range, and adjust the extended eyar accordingly
247 if (month
< 0 || month
> 11) {
248 eyear
+= (int32_t)ClockMath::floorDivide(month
, 12, month
);
257 double jd
= IndianToJD(eyear
,imonth
, 1);
262 //-------------------------------------------------------------------------
263 // Functions for converting from milliseconds to field values
264 //-------------------------------------------------------------------------
266 int32_t IndianCalendar::handleGetExtendedYear() {
269 if (newerField(UCAL_EXTENDED_YEAR
, UCAL_YEAR
) == UCAL_EXTENDED_YEAR
) {
270 year
= internalGet(UCAL_EXTENDED_YEAR
, 1); // Default to year 1
272 year
= internalGet(UCAL_YEAR
, 1); // Default to year 1
279 * Override Calendar to compute several fields specific to the Indian
280 * calendar system. These are:
286 * <li>EXTENDED_YEAR</ul>
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.
292 void IndianCalendar::handleComputeFields(int32_t julianDay
, UErrorCode
& /* status */) {
293 double jdAtStartOfGregYear
;
294 int32_t leapMonth
, IndianYear
, yday
, IndianMonth
, IndianDayOfMonth
, mday
;
295 int32_t gregorianYear
; // Stores gregorian date corresponding to Julian day;
298 gregorianYear
= jdToGregorian(julianDay
, gd
)[0]; // Gregorian date for Julian day
299 IndianYear
= gregorianYear
- INDIAN_ERA_START
; // Year in Saka era
300 jdAtStartOfGregYear
= gregorianToJD(gregorianYear
, 1, 1); // JD at start of Gregorian year
301 yday
= (int32_t)(julianDay
- jdAtStartOfGregYear
); // Day number in Gregorian year (starting from 0)
303 if (yday
< INDIAN_YEAR_START
) {
304 // Day is at the end of the preceding Saka year
306 leapMonth
= isGregorianLeap(gregorianYear
- 1) ? 31 : 30; // Days in leapMonth this year, previous Gregorian year
307 yday
+= leapMonth
+ (31 * 5) + (30 * 3) + 10;
309 leapMonth
= isGregorianLeap(gregorianYear
) ? 31 : 30; // Days in leapMonth this year
310 yday
-= INDIAN_YEAR_START
;
313 if (yday
< leapMonth
) {
315 IndianDayOfMonth
= yday
+ 1;
317 mday
= yday
- leapMonth
;
318 if (mday
< (31 * 5)) {
319 IndianMonth
= (int32_t)uprv_floor(mday
/ 31) + 1;
320 IndianDayOfMonth
= (mday
% 31) + 1;
323 IndianMonth
= (int32_t)uprv_floor(mday
/ 30) + 6;
324 IndianDayOfMonth
= (mday
% 30) + 1;
328 internalSet(UCAL_ERA
, 0);
329 internalSet(UCAL_EXTENDED_YEAR
, IndianYear
);
330 internalSet(UCAL_YEAR
, IndianYear
);
331 internalSet(UCAL_MONTH
, IndianMonth
);
332 internalSet(UCAL_DAY_OF_MONTH
, IndianDayOfMonth
);
333 internalSet(UCAL_DAY_OF_YEAR
, yday
+ 1); // yday is 0-based
337 IndianCalendar::inDaylightTime(UErrorCode
& status
) const
339 // copied from GregorianCalendar
340 if (U_FAILURE(status
) || !getTimeZone().useDaylightTime()) {
344 // Force an update of the state of the Calendar.
345 ((IndianCalendar
*)this)->complete(status
); // cast away const
347 return (UBool
)(U_SUCCESS(status
) ? (internalGet(UCAL_DST_OFFSET
) != 0) : FALSE
);
352 * The system maintains a static default century start date and Year. They are
353 * initialized the first time they are used. Once the system default century date
354 * and year are set, they do not change.
356 static UDate gSystemDefaultCenturyStart
= DBL_MIN
;
357 static int32_t gSystemDefaultCenturyStartYear
= -1;
358 static icu::UInitOnce gSystemDefaultCenturyInit
= U_INITONCE_INITIALIZER
;
361 UBool
IndianCalendar::haveDefaultCentury() const
366 static void U_CALLCONV
367 initializeSystemDefaultCentury()
369 // initialize systemDefaultCentury and systemDefaultCenturyYear based
370 // on the current time. They'll be set to 80 years before
372 UErrorCode status
= U_ZERO_ERROR
;
374 IndianCalendar
calendar ( Locale ( "@calendar=Indian" ), status
);
375 if ( U_SUCCESS ( status
) ) {
376 calendar
.setTime ( Calendar::getNow(), status
);
377 calendar
.add ( UCAL_YEAR
, -80, status
);
379 UDate newStart
= calendar
.getTime ( status
);
380 int32_t newYear
= calendar
.get ( UCAL_YEAR
, status
);
382 gSystemDefaultCenturyStart
= newStart
;
383 gSystemDefaultCenturyStartYear
= newYear
;
385 // We have no recourse upon failure.
390 IndianCalendar::defaultCenturyStart() const
392 // lazy-evaluate systemDefaultCenturyStart
393 umtx_initOnce(gSystemDefaultCenturyInit
, &initializeSystemDefaultCentury
);
394 return gSystemDefaultCenturyStart
;
398 IndianCalendar::defaultCenturyStartYear() const
400 // lazy-evaluate systemDefaultCenturyStartYear
401 umtx_initOnce(gSystemDefaultCenturyInit
, &initializeSystemDefaultCentury
);
402 return gSystemDefaultCenturyStartYear
;
406 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IndianCalendar
)