/src/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 | IndianCalendar* IndianCalendar::clone() const { |
39 | 0 | return new IndianCalendar(*this); |
40 | 0 | } |
41 | | |
42 | | IndianCalendar::IndianCalendar(const Locale& aLocale, UErrorCode& success) |
43 | 0 | : Calendar(TimeZone::forLocaleOrDefault(aLocale), 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 int32_t INDIAN_ERA_START = 78; |
87 | | static const int32_t INDIAN_YEAR_START = 80; |
88 | | |
89 | 0 | int32_t IndianCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const { |
90 | 0 | return LIMITS[field][limitType]; |
91 | 0 | } |
92 | | |
93 | | /* |
94 | | * Determine whether the given gregorian year is a Leap year |
95 | | */ |
96 | | static UBool isGregorianLeap(int32_t year) |
97 | 0 | { |
98 | 0 | return Grego::isLeapYear(year); |
99 | 0 | } |
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 | | */ |
111 | 0 | int32_t IndianCalendar::handleGetMonthLength(int32_t eyear, int32_t month) const { |
112 | 0 | if (month < 0 || month > 11) { |
113 | 0 | eyear += ClockMath::floorDivide(month, 12, month); |
114 | 0 | } |
115 | |
|
116 | 0 | if (isGregorianLeap(eyear + INDIAN_ERA_START) && month == 0) { |
117 | 0 | return 31; |
118 | 0 | } |
119 | | |
120 | 0 | if (month >= 1 && month <= 5) { |
121 | 0 | return 31; |
122 | 0 | } |
123 | | |
124 | 0 | return 30; |
125 | 0 | } |
126 | | |
127 | | /* |
128 | | * Return the number of days in the given Indian year |
129 | | * |
130 | | * @param eyear The year in Saka Era. |
131 | | */ |
132 | 0 | int32_t IndianCalendar::handleGetYearLength(int32_t eyear) const { |
133 | 0 | return isGregorianLeap(eyear + INDIAN_ERA_START) ? 366 : 365; |
134 | 0 | } |
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 | | */ |
142 | 0 | static double gregorianToJD(int32_t year, int32_t month, int32_t date) { |
143 | 0 | return Grego::fieldsToDay(year, month, date) + kEpochStartAsJulianDay - 0.5; |
144 | 0 | } |
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 | | */ |
151 | 0 | static int32_t* jdToGregorian(double jd, int32_t gregorianDate[3]) { |
152 | 0 | int32_t gdow; |
153 | 0 | Grego::dayToFields(jd - kEpochStartAsJulianDay, |
154 | 0 | gregorianDate[0], gregorianDate[1], gregorianDate[2], gdow); |
155 | 0 | return gregorianDate; |
156 | 0 | } |
157 | | |
158 | | |
159 | | //------------------------------------------------------------------------- |
160 | | // Functions for converting from field values to milliseconds.... |
161 | | //------------------------------------------------------------------------- |
162 | 0 | static double IndianToJD(int32_t year, int32_t month, int32_t date) { |
163 | 0 | int32_t leapMonth, gyear, m; |
164 | 0 | double start, jd; |
165 | |
|
166 | 0 | gyear = year + INDIAN_ERA_START; |
167 | | |
168 | |
|
169 | 0 | if(isGregorianLeap(gyear)) { |
170 | 0 | leapMonth = 31; |
171 | 0 | start = gregorianToJD(gyear, 2 /* The third month in 0 based month */, 21); |
172 | 0 | } |
173 | 0 | else { |
174 | 0 | leapMonth = 30; |
175 | 0 | start = gregorianToJD(gyear, 2 /* The third month in 0 based month */, 22); |
176 | 0 | } |
177 | |
|
178 | 0 | if (month == 1) { |
179 | 0 | jd = start + (date - 1); |
180 | 0 | } else { |
181 | 0 | jd = start + leapMonth; |
182 | 0 | m = month - 2; |
183 | | |
184 | | //m = Math.min(m, 5); |
185 | 0 | if (m > 5) { |
186 | 0 | m = 5; |
187 | 0 | } |
188 | |
|
189 | 0 | jd += m * 31; |
190 | |
|
191 | 0 | if (month >= 8) { |
192 | 0 | m = month - 7; |
193 | 0 | jd += m * 30; |
194 | 0 | } |
195 | 0 | jd += date - 1; |
196 | 0 | } |
197 | |
|
198 | 0 | return jd; |
199 | 0 | } |
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 | | */ |
206 | 0 | 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 | 0 | int32_t imonth; |
210 | | |
211 | | // If the month is out of range, adjust it into range, and adjust the extended year accordingly |
212 | 0 | if (month < 0 || month > 11) { |
213 | 0 | eyear += (int32_t)ClockMath::floorDivide(month, 12, month); |
214 | 0 | } |
215 | |
|
216 | 0 | if(month == 12){ |
217 | 0 | imonth = 1; |
218 | 0 | } else { |
219 | 0 | imonth = month + 1; |
220 | 0 | } |
221 | | |
222 | 0 | double jd = IndianToJD(eyear ,imonth, 1); |
223 | |
|
224 | 0 | return (int32_t)jd; |
225 | 0 | } |
226 | | |
227 | | //------------------------------------------------------------------------- |
228 | | // Functions for converting from milliseconds to field values |
229 | | //------------------------------------------------------------------------- |
230 | | |
231 | 0 | int32_t IndianCalendar::handleGetExtendedYear() { |
232 | 0 | int32_t year; |
233 | |
|
234 | 0 | if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { |
235 | 0 | year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1 |
236 | 0 | } else { |
237 | 0 | year = internalGet(UCAL_YEAR, 1); // Default to year 1 |
238 | 0 | } |
239 | |
|
240 | 0 | return year; |
241 | 0 | } |
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 | | */ |
257 | 0 | void IndianCalendar::handleComputeFields(int32_t julianDay, UErrorCode& /* status */) { |
258 | 0 | double jdAtStartOfGregYear; |
259 | 0 | int32_t leapMonth, IndianYear, yday, IndianMonth, IndianDayOfMonth, mday; |
260 | 0 | int32_t gregorianYear; // Stores gregorian date corresponding to Julian day; |
261 | 0 | int32_t gd[3]; |
262 | |
|
263 | 0 | gregorianYear = jdToGregorian(julianDay, gd)[0]; // Gregorian date for Julian day |
264 | 0 | IndianYear = gregorianYear - INDIAN_ERA_START; // Year in Saka era |
265 | 0 | jdAtStartOfGregYear = gregorianToJD(gregorianYear, 0, 1); // JD at start of Gregorian year |
266 | 0 | yday = (int32_t)(julianDay - jdAtStartOfGregYear); // Day number in Gregorian year (starting from 0) |
267 | |
|
268 | 0 | if (yday < INDIAN_YEAR_START) { |
269 | | // Day is at the end of the preceding Saka year |
270 | 0 | IndianYear -= 1; |
271 | 0 | leapMonth = isGregorianLeap(gregorianYear - 1) ? 31 : 30; // Days in leapMonth this year, previous Gregorian year |
272 | 0 | yday += leapMonth + (31 * 5) + (30 * 3) + 10; |
273 | 0 | } else { |
274 | 0 | leapMonth = isGregorianLeap(gregorianYear) ? 31 : 30; // Days in leapMonth this year |
275 | 0 | yday -= INDIAN_YEAR_START; |
276 | 0 | } |
277 | |
|
278 | 0 | if (yday < leapMonth) { |
279 | 0 | IndianMonth = 0; |
280 | 0 | IndianDayOfMonth = yday + 1; |
281 | 0 | } else { |
282 | 0 | mday = yday - leapMonth; |
283 | 0 | if (mday < (31 * 5)) { |
284 | 0 | IndianMonth = (int32_t)uprv_floor(mday / 31) + 1; |
285 | 0 | IndianDayOfMonth = (mday % 31) + 1; |
286 | 0 | } else { |
287 | 0 | mday -= 31 * 5; |
288 | 0 | IndianMonth = (int32_t)uprv_floor(mday / 30) + 6; |
289 | 0 | IndianDayOfMonth = (mday % 30) + 1; |
290 | 0 | } |
291 | 0 | } |
292 | |
|
293 | 0 | internalSet(UCAL_ERA, 0); |
294 | 0 | internalSet(UCAL_EXTENDED_YEAR, IndianYear); |
295 | 0 | internalSet(UCAL_YEAR, IndianYear); |
296 | 0 | internalSet(UCAL_MONTH, IndianMonth); |
297 | 0 | internalSet(UCAL_DAY_OF_MONTH, IndianDayOfMonth); |
298 | 0 | internalSet(UCAL_DAY_OF_YEAR, yday + 1); // yday is 0-based |
299 | 0 | } |
300 | | |
301 | | UBool |
302 | | IndianCalendar::inDaylightTime(UErrorCode& status) const |
303 | 0 | { |
304 | | // copied from GregorianCalendar |
305 | 0 | if (U_FAILURE(status) || !getTimeZone().useDaylightTime()) { |
306 | 0 | return FALSE; |
307 | 0 | } |
308 | | |
309 | | // Force an update of the state of the Calendar. |
310 | 0 | ((IndianCalendar*)this)->complete(status); // cast away const |
311 | |
|
312 | 0 | return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE); |
313 | 0 | } |
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 | | |
326 | | UBool IndianCalendar::haveDefaultCentury() const |
327 | 0 | { |
328 | 0 | return TRUE; |
329 | 0 | } |
330 | | |
331 | | static void U_CALLCONV |
332 | | initializeSystemDefaultCentury() |
333 | 0 | { |
334 | | // initialize systemDefaultCentury and systemDefaultCenturyYear based |
335 | | // on the current time. They'll be set to 80 years before |
336 | | // the current time. |
337 | 0 | UErrorCode status = U_ZERO_ERROR; |
338 | |
|
339 | 0 | IndianCalendar calendar ( Locale ( "@calendar=Indian" ), status); |
340 | 0 | if ( U_SUCCESS ( status ) ) { |
341 | 0 | calendar.setTime ( Calendar::getNow(), status ); |
342 | 0 | calendar.add ( UCAL_YEAR, -80, status ); |
343 | |
|
344 | 0 | UDate newStart = calendar.getTime ( status ); |
345 | 0 | int32_t newYear = calendar.get ( UCAL_YEAR, status ); |
346 | |
|
347 | 0 | gSystemDefaultCenturyStart = newStart; |
348 | 0 | gSystemDefaultCenturyStartYear = newYear; |
349 | 0 | } |
350 | | // We have no recourse upon failure. |
351 | 0 | } |
352 | | |
353 | | |
354 | | UDate |
355 | | IndianCalendar::defaultCenturyStart() const |
356 | 0 | { |
357 | | // lazy-evaluate systemDefaultCenturyStart |
358 | 0 | umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); |
359 | 0 | return gSystemDefaultCenturyStart; |
360 | 0 | } |
361 | | |
362 | | int32_t |
363 | | IndianCalendar::defaultCenturyStartYear() const |
364 | 0 | { |
365 | | // lazy-evaluate systemDefaultCenturyStartYear |
366 | 0 | umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); |
367 | 0 | return gSystemDefaultCenturyStartYear; |
368 | 0 | } |
369 | | |
370 | | |
371 | | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IndianCalendar) |
372 | | |
373 | | U_NAMESPACE_END |
374 | | |
375 | | #endif |
376 | | |