]>
Commit | Line | Data |
---|---|---|
374ca955 | 1 | /* |
46f4442e | 2 | ****************************************************************************** |
57a6839d | 3 | * Copyright (C) 2003-2013, International Business Machines Corporation |
46f4442e A |
4 | * and others. All Rights Reserved. |
5 | ****************************************************************************** | |
6 | * | |
7 | * File ISLAMCAL.H | |
8 | * | |
9 | * Modification History: | |
10 | * | |
11 | * Date Name Description | |
12 | * 10/14/2003 srl ported from java IslamicCalendar | |
13 | ***************************************************************************** | |
14 | */ | |
374ca955 A |
15 | |
16 | #include "islamcal.h" | |
17 | ||
18 | #if !UCONFIG_NO_FORMATTING | |
19 | ||
46f4442e | 20 | #include "umutex.h" |
374ca955 A |
21 | #include <float.h> |
22 | #include "gregoimp.h" // Math | |
23 | #include "astro.h" // CalendarAstronomer | |
24 | #include "uhash.h" | |
25 | #include "ucln_in.h" | |
57a6839d | 26 | #include "uassert.h" |
374ca955 A |
27 | |
28 | static const UDate HIJRA_MILLIS = -42521587200000.0; // 7/16/622 AD 00:00 | |
29 | ||
30 | // Debugging | |
31 | #ifdef U_DEBUG_ISLAMCAL | |
32 | # include <stdio.h> | |
33 | # include <stdarg.h> | |
34 | static void debug_islamcal_loc(const char *f, int32_t l) | |
35 | { | |
46f4442e | 36 | fprintf(stderr, "%s:%d: ", f, l); |
374ca955 A |
37 | } |
38 | ||
39 | static void debug_islamcal_msg(const char *pat, ...) | |
40 | { | |
46f4442e A |
41 | va_list ap; |
42 | va_start(ap, pat); | |
43 | vfprintf(stderr, pat, ap); | |
44 | fflush(stderr); | |
374ca955 A |
45 | } |
46 | // must use double parens, i.e.: U_DEBUG_ISLAMCAL_MSG(("four is: %d",4)); | |
47 | #define U_DEBUG_ISLAMCAL_MSG(x) {debug_islamcal_loc(__FILE__,__LINE__);debug_islamcal_msg x;} | |
48 | #else | |
49 | #define U_DEBUG_ISLAMCAL_MSG(x) | |
50 | #endif | |
51 | ||
52 | ||
53 | // --- The cache -- | |
54 | // cache of months | |
51004dcb | 55 | static UMutex astroLock = U_MUTEX_INITIALIZER; // pod bay door lock |
4388f060 A |
56 | static icu::CalendarCache *gMonthCache = NULL; |
57 | static icu::CalendarAstronomer *gIslamicCalendarAstro = NULL; | |
374ca955 A |
58 | |
59 | U_CDECL_BEGIN | |
60 | static UBool calendar_islamic_cleanup(void) { | |
61 | if (gMonthCache) { | |
62 | delete gMonthCache; | |
63 | gMonthCache = NULL; | |
64 | } | |
65 | if (gIslamicCalendarAstro) { | |
66 | delete gIslamicCalendarAstro; | |
67 | gIslamicCalendarAstro = NULL; | |
68 | } | |
374ca955 A |
69 | return TRUE; |
70 | } | |
71 | U_CDECL_END | |
72 | ||
73 | U_NAMESPACE_BEGIN | |
74 | ||
75 | // Implementation of the IslamicCalendar class | |
76 | ||
57a6839d A |
77 | /** |
78 | * Friday EPOC | |
79 | */ | |
80 | static const int32_t CIVIL_EPOC = 1948440; // CE 622 July 16 Friday (Julian calendar) / CE 622 July 19 (Gregorian calendar) | |
81 | ||
82 | /** | |
83 | * Thursday EPOC | |
84 | */ | |
85 | static const int32_t ASTRONOMICAL_EPOC = 1948439; // CE 622 July 15 Thursday (Julian calendar) | |
86 | ||
87 | ||
88 | static const int32_t UMALQURA_YEAR_START = 1318; | |
89 | static const int32_t UMALQURA_YEAR_END = 1480; | |
90 | ||
91 | static const int UMALQURA_MONTHLENGTH[] = { | |
92 | //* 1318 -1322 */ "1011 0110 1010", "1001 0111 0101", "0100 1010 1110", "1010 0100 1111", "0101 0010 1011", | |
93 | 0x0B6A, 0x0975, 0x04AE, 0x0A4F, 0x052B, | |
94 | //* 1323 -1327 */ "0110 1001 0101", "0110 1100 1010", "1010 1101 0101", "0010 1101 0110", "1001 0101 1011", | |
95 | 0x0695, 0x06CA, 0x0AD5, 0x02D6, 0x095B, | |
96 | //* 1328 -1332 */ "1001 0010 1101", "1100 1001 0101", "1101 0100 1010", "1101 1001 0101", "0110 1101 0010", | |
97 | 0x092D, 0x0C95, 0x0D4A, 0x0D95, 0x025B, | |
98 | //* 1333 -1337 */ "1010 1101 0101", "0101 0101 1010", "1010 1010 1011", "0101 0010 1011", "0110 1010 0101", | |
99 | 0x0AD5, 0x055A, 0x0AAB, 0x052B, 0x06A5, | |
100 | //* 1338 -1342 */ "0111 0101 0010", "1011 1010 1001", "0011 0111 0100", "1010 1011 0110", "0101 0101 0110", | |
101 | 0x0752, 0x0BA9, 0x0374, 0x0AB6, 0x0556, | |
102 | //* 1343 -1347 */ "1010 1010 1010", "1101 0101 0010", "1011 1010 0100", "1011 1101 0010", "1010 1110 1010", | |
103 | 0x0AAA, 0x0D52, 0x0BA4, 0x0BD2, 0x0AEA, | |
104 | //* 1348 -1352 */ "0010 1101 1100", "1001 0110 1101", "1001 0010 1110", "1010 1010 0110", "1101 0101 0100", | |
105 | 0x02DC, 0x096D, 0x092E, 0x0AA6, 0x0D54, | |
106 | //* 1353 -1357 */ "1011 1010 0101", "0101 1011 0100", "1001 1011 0110", "1001 0011 0111", "0100 1001 1011", | |
107 | 0x0BA5, 0x05B4, 0x09B6, 0x0937, 0x049B, | |
108 | //* 1358 -1362 */ "1010 0100 1011", "1011 0010 0101", "1011 0101 0100", "1011 0110 1010", "0101 0110 1010", | |
109 | 0x0A4B, 0x0B25, 0x0B54, 0x0B6A, 0x056A, | |
110 | //* 1363 -1367 */ "1010 1010 1011", "1010 0101 0101", "1101 0010 0101", "1110 1001 0010", "1110 1100 1001", | |
111 | 0x0AAB, 0x0A55, 0x0D25, 0x0E92, 0x0EC9, | |
112 | //* 1368 -1372 */ "0110 1101 0100", "1010 1110 1010", "0101 0110 1011", "0100 1010 1011", "1001 0100 1011", | |
113 | 0x06D4, 0x0ADA, 0x056B, 0x04AB, 0x094B, | |
114 | //* 1373 -1377 */ "1011 0100 1001", "1011 1010 0100", "1011 1011 0010", "0101 1011 0101", "0010 1011 1010", | |
115 | 0x0B49, 0x0BA4, 0x0BB2, 0x05B5, 0x02BA, | |
116 | //* 1378 -1382 */ "1001 0101 1011", "0100 1010 1011", "0101 0101 0101", "0110 1011 0010", "0101 1011 0100", | |
117 | 0x095B, 0x04AB, 0x0555, 0x06B2, 0x05B4, | |
118 | //* 1383 -1387 */ "1001 1101 1010", "1001 0110 1110", "0100 1010 1110", "1010 0101 0110", "1101 0010 1010", | |
119 | 0x09DA, 0x096E, 0x04AE, 0x0A56, 0x0D2A, | |
120 | //* 1388 -1392 */ "1101 0101 0100", "1101 1011 0010", "1010 1011 0101", "0010 1101 1010", "1001 0101 1011", | |
121 | 0x0D54, 0x0DB2, 0x0AB5, 0x02DA, 0x095B, | |
122 | //* 1393 -1397 */ "1001 0010 1011", "1010 1001 0101", "1011 0100 1001", "1011 0110 0100", "1011 0111 0001", | |
123 | 0x092B, 0x0A95, 0x0B49, 0x0B64, 0x0B71, | |
124 | //* 1398 -1402 */ "0101 1011 0100", "1010 1011 0101", "1010 1001 0110", "1101 0100 1010", "1110 1001 0010", | |
125 | 0x05B4, 0x0AB5, 0x0A96, 0x0B4A, 0x0E92, | |
126 | //* 1403 -1407 */ "1110 1100 1001", "0110 1101 0100", "1010 1110 1001", "1010 1010 1101", "0101 0101 0101", | |
127 | 0x0EC9, 0x06D4, 0x0AE9, 0x0AAD, 0x0555, | |
128 | //* 1408 -1412 */ "1010 1010 0101", "1011 0101 0010", "1101 1010 0100", "1101 1011 0010", "1001 1011 1010", | |
129 | 0x0AA5, 0x0B52, 0x0DA4, 0x0DB2, 0x09BA, | |
130 | //* 1413 -1417 */ "0100 1011 1010", "1010 0101 1011", "0101 0010 1101", "1010 1010 0101", "1010 1101 0100", | |
131 | 0x04BA, 0x0A5B, 0x052D, 0x0AA5, 0x0AD4, | |
132 | //* 1418 -1422 */ "1010 1110 1010", "0101 0101 1100", "0100 1011 1101", "0010 0011 1101", "1001 0001 1101", | |
133 | 0x0AEA, 0x055C, 0x04BD, 0x023D, 0x091D, | |
134 | //* 1423 -1427 */ "1010 1001 0101", "1011 0100 1010", "1011 0101 1010", "0101 0110 1101", "0010 1011 0110", | |
135 | 0x0A95, 0x0B4A, 0x0B5A, 0x056D, 0x02B6, | |
136 | //* 1428 -1432 */ "1001 0011 1011", "0100 1001 1011", "0110 0101 0101", "0110 1010 1001", "0111 0101 0100", | |
137 | 0x093B, 0x049B, 0x0655, 0x06A9, 0x0754, | |
138 | //* 1433 -1437 */ "1011 0110 1010", "0101 0110 1100", "1010 1010 1101", "0101 0101 0101", "1011 0010 1001", | |
139 | 0x0B6A, 0x056C, 0x0AAD, 0x0555, 0x0B29, | |
140 | //* 1438 -1442 */ "1011 1001 0010", "1011 1010 1001", "0101 1101 0100", "1010 1101 1010", "0101 0101 1010", | |
141 | 0x0B92, 0x0BA9, 0x05D4, 0x0ADA, 0x055A, | |
142 | //* 1443 -1447 */ "1010 1010 1011", "0101 1001 0101", "0111 0100 1001", "0111 0110 0100", "1011 1010 1010", | |
143 | 0x0AAB, 0x0595, 0x0749, 0x0764, 0x0BAA, | |
144 | //* 1448 -1452 */ "0101 1011 0101", "0010 1011 0110", "1010 0101 0110", "1110 0100 1101", "1011 0010 0101", | |
145 | 0x05B5, 0x02B6, 0x0A56, 0x0E4D, 0x0B25, | |
146 | //* 1453 -1457 */ "1011 0101 0010", "1011 0110 1010", "0101 1010 1101", "0010 1010 1110", "1001 0010 1111", | |
147 | 0x0B52, 0x0B6A, 0x05AD, 0x02AE, 0x092F, | |
148 | //* 1458 -1462 */ "0100 1001 0111", "0110 0100 1011", "0110 1010 0101", "0110 1010 1100", "1010 1101 0110", | |
149 | 0x0497, 0x064B, 0x06A5, 0x06AC, 0x0AD6, | |
150 | //* 1463 -1467 */ "0101 0101 1101", "0100 1001 1101", "1010 0100 1101", "1101 0001 0110", "1101 1001 0101", | |
151 | 0x055D, 0x049D, 0x0A4D, 0x0D16, 0x0D95, | |
152 | //* 1468 -1472 */ "0101 1010 1010", "0101 1011 0101", "0010 1101 1010", "1001 0101 1011", "0100 1010 1101", | |
153 | 0x05AA, 0x05B5, 0x02DA, 0x095B, 0x04AD, | |
154 | //* 1473 -1477 */ "0101 1001 0101", "0110 1100 1010", "0110 1110 0100", "1010 1110 1010", "0100 1111 0101", | |
155 | 0x0595, 0x06CA, 0x06E4, 0x0AEA, 0x04F5, | |
156 | //* 1478 -1480 */ "0010 1011 0110", "1001 0101 0110", "1010 1010 1010" | |
157 | 0x02B6, 0x0956, 0x0AAA | |
158 | }; | |
159 | ||
160 | int32_t getUmalqura_MonthLength(int32_t y, int32_t m) { | |
161 | int32_t mask = (int32_t) (0x01 << (11 - m)); // set mask for bit corresponding to month | |
162 | if((UMALQURA_MONTHLENGTH[y] & mask) == 0 ) | |
163 | return 29; | |
164 | else | |
165 | return 30; | |
166 | ||
167 | } | |
168 | ||
374ca955 A |
169 | //------------------------------------------------------------------------- |
170 | // Constructors... | |
171 | //------------------------------------------------------------------------- | |
172 | ||
57a6839d A |
173 | const char *IslamicCalendar::getType() const { |
174 | const char *sType = NULL; | |
175 | ||
176 | switch (cType) { | |
177 | case CIVIL: | |
178 | sType = "islamic-civil"; | |
179 | break; | |
180 | case ASTRONOMICAL: | |
181 | sType = "islamic"; | |
182 | break; | |
183 | case TBLA: | |
184 | sType = "islamic-tbla"; | |
185 | break; | |
186 | case UMALQURA: | |
187 | sType = "islamic-umalqura"; | |
188 | break; | |
189 | default: | |
190 | U_ASSERT(false); // out of range | |
191 | sType = "islamic"; // "islamic" is used as the generic type | |
192 | break; | |
46f4442e | 193 | } |
57a6839d | 194 | return sType; |
374ca955 A |
195 | } |
196 | ||
197 | Calendar* IslamicCalendar::clone() const { | |
46f4442e | 198 | return new IslamicCalendar(*this); |
374ca955 A |
199 | } |
200 | ||
57a6839d | 201 | IslamicCalendar::IslamicCalendar(const Locale& aLocale, UErrorCode& success, ECalculationType type) |
46f4442e | 202 | : Calendar(TimeZone::createDefault(), aLocale, success), |
57a6839d | 203 | cType(type) |
374ca955 | 204 | { |
46f4442e | 205 | setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly. |
374ca955 A |
206 | } |
207 | ||
57a6839d | 208 | IslamicCalendar::IslamicCalendar(const IslamicCalendar& other) : Calendar(other), cType(other.cType) { |
374ca955 A |
209 | } |
210 | ||
211 | IslamicCalendar::~IslamicCalendar() | |
212 | { | |
213 | } | |
214 | ||
57a6839d | 215 | void IslamicCalendar::setCalculationType(ECalculationType type, UErrorCode &status) |
374ca955 | 216 | { |
57a6839d | 217 | if (cType != type) { |
46f4442e A |
218 | // The fields of the calendar will become invalid, because the calendar |
219 | // rules are different | |
220 | UDate m = getTimeInMillis(status); | |
57a6839d | 221 | cType = type; |
46f4442e A |
222 | clear(); |
223 | setTimeInMillis(m, status); | |
224 | } | |
374ca955 | 225 | } |
46f4442e | 226 | |
374ca955 | 227 | /** |
46f4442e A |
228 | * Returns <code>true</code> if this object is using the fixed-cycle civil |
229 | * calendar, or <code>false</code> if using the religious, astronomical | |
230 | * calendar. | |
231 | * @draft ICU 2.4 | |
232 | */ | |
374ca955 | 233 | UBool IslamicCalendar::isCivil() { |
57a6839d | 234 | return (cType == CIVIL); |
374ca955 | 235 | } |
46f4442e | 236 | |
374ca955 A |
237 | //------------------------------------------------------------------------- |
238 | // Minimum / Maximum access functions | |
239 | //------------------------------------------------------------------------- | |
240 | ||
46f4442e A |
241 | // Note: Current IslamicCalendar implementation does not work |
242 | // well with negative years. | |
243 | ||
4388f060 A |
244 | // TODO: In some cases the current ICU Islamic calendar implementation shows |
245 | // a month as having 31 days. Since date parsing now uses range checks based | |
246 | // on the table below, we need to change the range for last day of month to | |
247 | // include 31 as a workaround until the implementation is fixed. | |
374ca955 | 248 | static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = { |
46f4442e A |
249 | // Minimum Greatest Least Maximum |
250 | // Minimum Maximum | |
251 | { 0, 0, 0, 0}, // ERA | |
252 | { 1, 1, 5000000, 5000000}, // YEAR | |
253 | { 0, 0, 11, 11}, // MONTH | |
254 | { 1, 1, 50, 51}, // WEEK_OF_YEAR | |
255 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH | |
4388f060 | 256 | { 1, 1, 29, 31}, // DAY_OF_MONTH - 31 to workaround for cal implementation bug, should be 30 |
46f4442e A |
257 | { 1, 1, 354, 355}, // DAY_OF_YEAR |
258 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK | |
259 | { -1, -1, 5, 5}, // DAY_OF_WEEK_IN_MONTH | |
260 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM | |
261 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR | |
262 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY | |
263 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE | |
264 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND | |
265 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND | |
266 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET | |
267 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET | |
268 | { 1, 1, 5000000, 5000000}, // YEAR_WOY | |
269 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL | |
270 | { 1, 1, 5000000, 5000000}, // EXTENDED_YEAR | |
271 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY | |
272 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY | |
273 | {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH | |
374ca955 A |
274 | }; |
275 | ||
276 | /** | |
46f4442e A |
277 | * @draft ICU 2.4 |
278 | */ | |
374ca955 | 279 | int32_t IslamicCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const { |
46f4442e | 280 | return LIMITS[field][limitType]; |
374ca955 A |
281 | } |
282 | ||
283 | //------------------------------------------------------------------------- | |
284 | // Assorted calculation utilities | |
285 | // | |
286 | ||
57a6839d A |
287 | // table mapping extended year to julian day |
288 | // for year start in the Umm-Al-Qura calendar. | |
289 | // 660 bytes | |
290 | static const int32_t jdForUmAlQuraYrStart[] = { | |
291 | // jdIsUm isYr | |
292 | 466700, // 1318 | |
293 | 467055, // 1319 | |
294 | 467410, // 1320 | |
295 | 467764, // 1321 | |
296 | 468119, // 1322 | |
297 | 468473, // 1323 | |
298 | 468827, // 1324 | |
299 | 469181, // 1325 | |
300 | 469536, // 1326 | |
301 | 469890, // 1327 | |
302 | 470245, // 1328 | |
303 | 470599, // 1329 | |
304 | 470953, // 1330 | |
305 | 471307, // 1331 | |
306 | 471662, // 1332 | |
307 | 472016, // 1333 | |
308 | 472371, // 1334 | |
309 | 472725, // 1335 | |
310 | 473080, // 1336 | |
311 | 473434, // 1337 | |
312 | 473788, // 1338 | |
313 | 474142, // 1339 | |
314 | 474497, // 1340 | |
315 | 474851, // 1341 | |
316 | 475206, // 1342 | |
317 | 475560, // 1343 | |
318 | 475914, // 1344 | |
319 | 476268, // 1345 | |
320 | 476622, // 1346 | |
321 | 476977, // 1347 | |
322 | 477332, // 1348 | |
323 | 477686, // 1349 | |
324 | 478041, // 1350 | |
325 | 478395, // 1351 | |
326 | 478749, // 1352 | |
327 | 479103, // 1353 | |
328 | 479458, // 1354 | |
329 | 479812, // 1355 | |
330 | 480167, // 1356 | |
331 | 480522, // 1357 | |
332 | 480876, // 1358 | |
333 | 481230, // 1359 | |
334 | 481584, // 1360 | |
335 | 481938, // 1361 | |
336 | 482293, // 1362 | |
337 | 482647, // 1363 | |
338 | 483002, // 1364 | |
339 | 483356, // 1365 | |
340 | 483710, // 1366 | |
341 | 484064, // 1367 | |
342 | 484419, // 1368 | |
343 | 484773, // 1369 | |
344 | 485128, // 1370 | |
345 | 485483, // 1371 | |
346 | 485837, // 1372 | |
347 | 486191, // 1373 | |
348 | 486545, // 1374 | |
349 | 486899, // 1375 | |
350 | 487254, // 1376 | |
351 | 487609, // 1377 | |
352 | 487963, // 1378 | |
353 | 488318, // 1379 | |
354 | 488672, // 1380 | |
355 | 489026, // 1381 | |
356 | 489380, // 1382 | |
357 | 489734, // 1383 | |
358 | 490089, // 1384 | |
359 | 490444, // 1385 | |
360 | 490798, // 1386 | |
361 | 491152, // 1387 | |
362 | 491506, // 1388 | |
363 | 491860, // 1389 | |
364 | 492215, // 1390 | |
365 | 492570, // 1391 | |
366 | 492924, // 1392 | |
367 | 493279, // 1393 | |
368 | 493633, // 1394 | |
369 | 493987, // 1395 | |
370 | 494341, // 1396 | |
371 | 494695, // 1397 | |
372 | 495050, // 1398 | |
373 | 495404, // 1399 | |
374 | 495759, // 1400 | |
375 | 496113, // 1401 | |
376 | 496467, // 1402 | |
377 | 496821, // 1403 | |
378 | 497176, // 1404 | |
379 | 497530, // 1405 | |
380 | 497885, // 1406 | |
381 | 498240, // 1407 | |
382 | 498594, // 1408 | |
383 | 498948, // 1409 | |
384 | 499302, // 1410 | |
385 | 499656, // 1411 | |
386 | 500011, // 1412 | |
387 | 500366, // 1413 | |
388 | 500720, // 1414 | |
389 | 501075, // 1415 | |
390 | 501429, // 1416 | |
391 | 501783, // 1417 | |
392 | 502137, // 1418 | |
393 | 502492, // 1419 | |
394 | 502846, // 1420 | |
395 | 503201, // 1421 | |
396 | 503555, // 1422 | |
397 | 503909, // 1423 | |
398 | 504263, // 1424 | |
399 | 504617, // 1425 | |
400 | 504972, // 1426 | |
401 | 505327, // 1427 | |
402 | 505681, // 1428 | |
403 | 506036, // 1429 | |
404 | 506390, // 1430 | |
405 | 506744, // 1431 | |
406 | 507098, // 1432 | |
407 | 507452, // 1433 | |
408 | 507807, // 1434 | |
409 | 508161, // 1435 | |
410 | 508516, // 1436 | |
411 | 508870, // 1437 | |
412 | 509224, // 1438 | |
413 | 509578, // 1439 | |
414 | 509933, // 1440 | |
415 | 510287, // 1441 | |
416 | 510642, // 1442 | |
417 | 510996, // 1443 | |
418 | 511351, // 1444 | |
419 | 511705, // 1445 | |
420 | 512059, // 1446 | |
421 | 512413, // 1447 | |
422 | 512768, // 1448 | |
423 | 513123, // 1449 | |
424 | 513477, // 1450 | |
425 | 513831, // 1451 | |
426 | 514186, // 1452 | |
427 | 514540, // 1453 | |
428 | 514894, // 1454 | |
429 | 515249, // 1455 | |
430 | 515604, // 1456 | |
431 | 515958, // 1457 | |
432 | 516313, // 1458 | |
433 | 516667, // 1459 | |
434 | 517021, // 1460 | |
435 | 517375, // 1461 | |
436 | 517729, // 1462 | |
437 | 518084, // 1463 | |
438 | 518439, // 1464 | |
439 | 518793, // 1465 | |
440 | 519147, // 1466 | |
441 | 519501, // 1467 | |
442 | 519856, // 1468 | |
443 | 520210, // 1469 | |
444 | 520565, // 1470 | |
445 | 520919, // 1471 | |
446 | 521274, // 1472 | |
447 | 521628, // 1473 | |
448 | 521982, // 1474 | |
449 | 522336, // 1475 | |
450 | 522690, // 1476 | |
451 | 523045, // 1477 | |
452 | 523400, // 1478 | |
453 | 523754, // 1479 | |
454 | 524108, // 1480 | |
455 | }; | |
456 | ||
374ca955 | 457 | /** |
46f4442e A |
458 | * Determine whether a year is a leap year in the Islamic civil calendar |
459 | */ | |
374ca955 A |
460 | UBool IslamicCalendar::civilLeapYear(int32_t year) |
461 | { | |
46f4442e | 462 | return (14 + 11 * year) % 30 < 11; |
374ca955 | 463 | } |
46f4442e | 464 | |
374ca955 | 465 | /** |
46f4442e A |
466 | * Return the day # on which the given year starts. Days are counted |
467 | * from the Hijri epoch, origin 0. | |
468 | */ | |
57a6839d A |
469 | int32_t IslamicCalendar::yearStart(int32_t year) const{ |
470 | if (cType == CIVIL || cType == TBLA || | |
471 | (cType == UMALQURA && (year < UMALQURA_YEAR_START || year > UMALQURA_YEAR_END))) | |
472 | { | |
473 | // The following is similar to part of | |
474 | // Dershowitz & Reingold, 3rd ed., formula 6.3 | |
475 | // adjusted to give day offset within current epoch. | |
729e4ab9 | 476 | return (year-1)*354 + ClockMath::floorDivide((3+11*year),30); |
57a6839d | 477 | } else if(cType==ASTRONOMICAL){ |
46f4442e | 478 | return trueMonthStart(12*(year-1)); |
57a6839d A |
479 | } else { |
480 | return jdForUmAlQuraYrStart[year - UMALQURA_YEAR_START]; | |
46f4442e | 481 | } |
374ca955 | 482 | } |
46f4442e | 483 | |
374ca955 | 484 | /** |
46f4442e A |
485 | * Return the day # on which the given month starts. Days are counted |
486 | * from the Hijri epoch, origin 0. | |
487 | * | |
488 | * @param year The hijri year | |
57a6839d | 489 | * @param year The hijri month, 0-based (assumed to be in range 0..11) |
46f4442e | 490 | */ |
374ca955 | 491 | int32_t IslamicCalendar::monthStart(int32_t year, int32_t month) const { |
57a6839d A |
492 | if (cType == CIVIL || cType == TBLA) { |
493 | // The following is similar to part of | |
494 | // Dershowitz & Reingold, 3rd ed., formula 6.3 | |
495 | // adjusted for 0-based months and to give day offset within current epoch. | |
496 | // This does not handle months out of the range 0..11 | |
497 | return (year-1)*354 + (int32_t)ClockMath::floorDivide((3+11*year),30) + | |
498 | (int32_t)uprv_ceil(29.5*month); | |
499 | } else if(cType==ASTRONOMICAL){ | |
46f4442e | 500 | return trueMonthStart(12*(year-1) + month); |
57a6839d A |
501 | } else { |
502 | int32_t ms = yearStart(year); | |
503 | for(int i=0; i< month; i++){ | |
504 | ms+= handleGetMonthLength(year, i); | |
505 | } | |
506 | return ms; | |
46f4442e | 507 | } |
374ca955 | 508 | } |
46f4442e | 509 | |
374ca955 | 510 | /** |
46f4442e A |
511 | * Find the day number on which a particular month of the true/lunar |
512 | * Islamic calendar starts. | |
513 | * | |
514 | * @param month The month in question, origin 0 from the Hijri epoch | |
515 | * | |
516 | * @return The day number on which the given month starts. | |
517 | */ | |
374ca955 A |
518 | int32_t IslamicCalendar::trueMonthStart(int32_t month) const |
519 | { | |
46f4442e A |
520 | UErrorCode status = U_ZERO_ERROR; |
521 | int32_t start = CalendarCache::get(&gMonthCache, month, status); | |
522 | ||
523 | if (start==0) { | |
524 | // Make a guess at when the month started, using the average length | |
525 | UDate origin = HIJRA_MILLIS | |
729e4ab9 | 526 | + uprv_floor(month * CalendarAstronomer::SYNODIC_MONTH) * kOneDay; |
46f4442e A |
527 | |
528 | // moonAge will fail due to memory allocation error | |
529 | double age = moonAge(origin, status); | |
530 | if (U_FAILURE(status)) { | |
531 | goto trueMonthStartEnd; | |
532 | } | |
533 | ||
534 | if (age >= 0) { | |
535 | // The month has already started | |
536 | do { | |
537 | origin -= kOneDay; | |
538 | age = moonAge(origin, status); | |
539 | if (U_FAILURE(status)) { | |
540 | goto trueMonthStartEnd; | |
541 | } | |
542 | } while (age >= 0); | |
543 | } | |
544 | else { | |
545 | // Preceding month has not ended yet. | |
546 | do { | |
547 | origin += kOneDay; | |
548 | age = moonAge(origin, status); | |
549 | if (U_FAILURE(status)) { | |
550 | goto trueMonthStartEnd; | |
551 | } | |
552 | } while (age < 0); | |
553 | } | |
729e4ab9 | 554 | start = (int32_t)ClockMath::floorDivide((origin - HIJRA_MILLIS), (double)kOneDay) + 1; |
46f4442e | 555 | CalendarCache::put(&gMonthCache, month, start, status); |
374ca955 | 556 | } |
46f4442e A |
557 | trueMonthStartEnd : |
558 | if(U_FAILURE(status)) { | |
559 | start = 0; | |
374ca955 | 560 | } |
46f4442e | 561 | return start; |
374ca955 A |
562 | } |
563 | ||
564 | /** | |
46f4442e A |
565 | * Return the "age" of the moon at the given time; this is the difference |
566 | * in ecliptic latitude between the moon and the sun. This method simply | |
567 | * calls CalendarAstronomer.moonAge, converts to degrees, | |
568 | * and adjusts the result to be in the range [-180, 180]. | |
569 | * | |
570 | * @param time The time at which the moon's age is desired, | |
571 | * in millis since 1/1/1970. | |
572 | */ | |
573 | double IslamicCalendar::moonAge(UDate time, UErrorCode &status) | |
374ca955 | 574 | { |
46f4442e A |
575 | double age = 0; |
576 | ||
577 | umtx_lock(&astroLock); | |
578 | if(gIslamicCalendarAstro == NULL) { | |
579 | gIslamicCalendarAstro = new CalendarAstronomer(); | |
580 | if (gIslamicCalendarAstro == NULL) { | |
581 | status = U_MEMORY_ALLOCATION_ERROR; | |
582 | return age; | |
583 | } | |
584 | ucln_i18n_registerCleanup(UCLN_I18N_ISLAMIC_CALENDAR, calendar_islamic_cleanup); | |
585 | } | |
586 | gIslamicCalendarAstro->setTime(time); | |
587 | age = gIslamicCalendarAstro->getMoonAge(); | |
588 | umtx_unlock(&astroLock); | |
589 | ||
590 | // Convert to degrees and normalize... | |
591 | age = age * 180 / CalendarAstronomer::PI; | |
592 | if (age > 180) { | |
593 | age = age - 360; | |
594 | } | |
595 | ||
596 | return age; | |
374ca955 A |
597 | } |
598 | ||
599 | //---------------------------------------------------------------------- | |
600 | // Calendar framework | |
601 | //---------------------------------------------------------------------- | |
602 | ||
603 | /** | |
46f4442e A |
604 | * Return the length (in days) of the given month. |
605 | * | |
606 | * @param year The hijri year | |
607 | * @param year The hijri month, 0-based | |
608 | * @draft ICU 2.4 | |
609 | */ | |
374ca955 A |
610 | int32_t IslamicCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const { |
611 | ||
46f4442e A |
612 | int32_t length = 0; |
613 | ||
57a6839d A |
614 | if (cType == CIVIL || cType == TBLA || |
615 | (cType == UMALQURA && (extendedYear<UMALQURA_YEAR_START || extendedYear>UMALQURA_YEAR_END)) ) { | |
46f4442e A |
616 | length = 29 + (month+1) % 2; |
617 | if (month == DHU_AL_HIJJAH && civilLeapYear(extendedYear)) { | |
618 | length++; | |
619 | } | |
57a6839d | 620 | } else if(cType == ASTRONOMICAL){ |
46f4442e A |
621 | month = 12*(extendedYear-1) + month; |
622 | length = trueMonthStart(month+1) - trueMonthStart(month) ; | |
57a6839d A |
623 | } else { |
624 | length = getUmalqura_MonthLength(extendedYear - UMALQURA_YEAR_START, month); | |
374ca955 | 625 | } |
46f4442e | 626 | return length; |
374ca955 A |
627 | } |
628 | ||
629 | /** | |
46f4442e A |
630 | * Return the number of days in the given Islamic year |
631 | * @draft ICU 2.4 | |
632 | */ | |
374ca955 | 633 | int32_t IslamicCalendar::handleGetYearLength(int32_t extendedYear) const { |
57a6839d A |
634 | if (cType == CIVIL || cType == TBLA || |
635 | (cType == UMALQURA && (extendedYear<UMALQURA_YEAR_START || extendedYear>UMALQURA_YEAR_END)) ) { | |
46f4442e | 636 | return 354 + (civilLeapYear(extendedYear) ? 1 : 0); |
57a6839d | 637 | } else if(cType == ASTRONOMICAL){ |
46f4442e A |
638 | int32_t month = 12*(extendedYear-1); |
639 | return (trueMonthStart(month + 12) - trueMonthStart(month)); | |
57a6839d A |
640 | } else { |
641 | int len = 0; | |
642 | for(int i=0; i<12; i++) { | |
643 | len += handleGetMonthLength(extendedYear, i); | |
644 | } | |
645 | return len; | |
46f4442e | 646 | } |
374ca955 | 647 | } |
46f4442e | 648 | |
374ca955 A |
649 | //------------------------------------------------------------------------- |
650 | // Functions for converting from field values to milliseconds.... | |
651 | //------------------------------------------------------------------------- | |
652 | ||
653 | // Return JD of start of given month/year | |
57a6839d A |
654 | // Calendar says: |
655 | // Get the Julian day of the day BEFORE the start of this year. | |
656 | // If useMonth is true, get the day before the start of the month. | |
657 | // Hence the -1 | |
374ca955 | 658 | /** |
46f4442e A |
659 | * @draft ICU 2.4 |
660 | */ | |
374ca955 | 661 | int32_t IslamicCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /* useMonth */) const { |
57a6839d A |
662 | // This may be called by Calendar::handleComputeJulianDay with months out of the range |
663 | // 0..11. Need to handle that here since monthStart requires months in the range 0.11. | |
664 | if (month > 11) { | |
665 | eyear += (month / 12); | |
666 | month %= 12; | |
667 | } else if (month < 0) { | |
668 | month++; | |
669 | eyear += (month / 12) - 1; | |
670 | month = (month % 12) + 11; | |
671 | } | |
672 | return monthStart(eyear, month) + ((cType == TBLA)? ASTRONOMICAL_EPOC: CIVIL_EPOC) - 1; | |
374ca955 A |
673 | } |
674 | ||
675 | //------------------------------------------------------------------------- | |
676 | // Functions for converting from milliseconds to field values | |
677 | //------------------------------------------------------------------------- | |
678 | ||
46f4442e A |
679 | /** |
680 | * @draft ICU 2.4 | |
681 | */ | |
374ca955 A |
682 | int32_t IslamicCalendar::handleGetExtendedYear() { |
683 | int32_t year; | |
684 | if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { | |
685 | year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1 | |
686 | } else { | |
687 | year = internalGet(UCAL_YEAR, 1); // Default to year 1 | |
688 | } | |
689 | return year; | |
690 | } | |
691 | ||
692 | /** | |
46f4442e A |
693 | * Override Calendar to compute several fields specific to the Islamic |
694 | * calendar system. These are: | |
695 | * | |
696 | * <ul><li>ERA | |
697 | * <li>YEAR | |
698 | * <li>MONTH | |
699 | * <li>DAY_OF_MONTH | |
700 | * <li>DAY_OF_YEAR | |
701 | * <li>EXTENDED_YEAR</ul> | |
702 | * | |
703 | * The DAY_OF_WEEK and DOW_LOCAL fields are already set when this | |
704 | * method is called. The getGregorianXxx() methods return Gregorian | |
705 | * calendar equivalents for the given Julian day. | |
706 | * @draft ICU 2.4 | |
707 | */ | |
708 | void IslamicCalendar::handleComputeFields(int32_t julianDay, UErrorCode &status) { | |
374ca955 | 709 | int32_t year, month, dayOfMonth, dayOfYear; |
57a6839d A |
710 | int32_t startDate; |
711 | int32_t days = julianDay - CIVIL_EPOC; | |
46f4442e | 712 | |
57a6839d A |
713 | if (cType == CIVIL || cType == TBLA) { |
714 | if(cType == TBLA) { | |
715 | days = julianDay - ASTRONOMICAL_EPOC; | |
716 | } | |
717 | // Use the civil calendar approximation, which is just arithmetic. | |
718 | // The following is similar to part of | |
719 | // Dershowitz & Reingold, 3rd ed., formula 6.4 | |
729e4ab9 | 720 | year = (int)ClockMath::floorDivide( (double)(30 * days + 10646) , 10631.0 ); |
374ca955 A |
721 | month = (int32_t)uprv_ceil((days - 29 - yearStart(year)) / 29.5 ); |
722 | month = month<11?month:11; | |
723 | startDate = monthStart(year, month); | |
57a6839d | 724 | } else if(cType == ASTRONOMICAL){ |
374ca955 A |
725 | // Guess at the number of elapsed full months since the epoch |
726 | int32_t months = (int32_t)uprv_floor((double)days / CalendarAstronomer::SYNODIC_MONTH); | |
727 | ||
57a6839d | 728 | startDate = (int32_t)uprv_floor(months * CalendarAstronomer::SYNODIC_MONTH); |
374ca955 | 729 | |
46f4442e A |
730 | double age = moonAge(internalGetTime(), status); |
731 | if (U_FAILURE(status)) { | |
57a6839d A |
732 | status = U_MEMORY_ALLOCATION_ERROR; |
733 | return; | |
46f4442e | 734 | } |
729e4ab9 | 735 | if ( days - startDate >= 25 && age > 0) { |
374ca955 A |
736 | // If we're near the end of the month, assume next month and search backwards |
737 | months++; | |
738 | } | |
739 | ||
740 | // Find out the last time that the new moon was actually visible at this longitude | |
741 | // This returns midnight the night that the moon was visible at sunset. | |
742 | while ((startDate = trueMonthStart(months)) > days) { | |
743 | // If it was after the date in question, back up a month and try again | |
744 | months--; | |
745 | } | |
46f4442e | 746 | |
374ca955 A |
747 | year = months / 12 + 1; |
748 | month = months % 12; | |
57a6839d A |
749 | } else if(cType == UMALQURA) { |
750 | int32_t umalquraStartdays = yearStart(UMALQURA_YEAR_START) ; | |
751 | if( days < umalquraStartdays){ | |
752 | //Use Civil calculation | |
753 | year = (int)ClockMath::floorDivide( (double)(30 * days + 10646) , 10631.0 ); | |
754 | month = (int32_t)uprv_ceil((days - 29 - yearStart(year)) / 29.5 ); | |
755 | month = month<11?month:11; | |
756 | startDate = monthStart(year, month); | |
757 | }else{ | |
758 | int y =UMALQURA_YEAR_START-1, m =0; | |
759 | long d = 1; | |
760 | while(d > 0){ | |
761 | y++; | |
762 | d = days - yearStart(y) +1; | |
763 | if(d == handleGetYearLength(y)){ | |
764 | m=11; | |
765 | break; | |
766 | }else if(d < handleGetYearLength(y) ){ | |
767 | int monthLen = handleGetMonthLength(y, m); | |
768 | m=0; | |
769 | while(d > monthLen){ | |
770 | d -= monthLen; | |
771 | m++; | |
772 | monthLen = handleGetMonthLength(y, m); | |
773 | } | |
774 | break; | |
775 | } | |
776 | } | |
777 | year = y; | |
778 | month = m; | |
779 | } | |
780 | } else { // invalid 'civil' | |
781 | U_ASSERT(false); // should not get here, out of range | |
782 | year=month=0; | |
374ca955 | 783 | } |
46f4442e | 784 | |
374ca955 A |
785 | dayOfMonth = (days - monthStart(year, month)) + 1; |
786 | ||
787 | // Now figure out the day of the year. | |
57a6839d A |
788 | dayOfYear = (days - monthStart(year, 0)) + 1; |
789 | ||
374ca955 A |
790 | |
791 | internalSet(UCAL_ERA, 0); | |
792 | internalSet(UCAL_YEAR, year); | |
793 | internalSet(UCAL_EXTENDED_YEAR, year); | |
794 | internalSet(UCAL_MONTH, month); | |
795 | internalSet(UCAL_DAY_OF_MONTH, dayOfMonth); | |
796 | internalSet(UCAL_DAY_OF_YEAR, dayOfYear); | |
797 | } | |
798 | ||
799 | UBool | |
800 | IslamicCalendar::inDaylightTime(UErrorCode& status) const | |
801 | { | |
46f4442e A |
802 | // copied from GregorianCalendar |
803 | if (U_FAILURE(status) || (&(getTimeZone()) == NULL && !getTimeZone().useDaylightTime())) | |
804 | return FALSE; | |
374ca955 A |
805 | |
806 | // Force an update of the state of the Calendar. | |
46f4442e | 807 | ((IslamicCalendar*)this)->complete(status); // cast away const |
374ca955 | 808 | |
46f4442e | 809 | return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE); |
374ca955 A |
810 | } |
811 | ||
57a6839d A |
812 | /** |
813 | * The system maintains a static default century start date and Year. They are | |
814 | * initialized the first time they are used. Once the system default century date | |
815 | * and year are set, they do not change. | |
816 | */ | |
817 | static UDate gSystemDefaultCenturyStart = DBL_MIN; | |
818 | static int32_t gSystemDefaultCenturyStartYear = -1; | |
819 | static icu::UInitOnce gSystemDefaultCenturyInit = U_INITONCE_INITIALIZER; | |
374ca955 A |
820 | |
821 | ||
822 | UBool IslamicCalendar::haveDefaultCentury() const | |
823 | { | |
46f4442e | 824 | return TRUE; |
374ca955 A |
825 | } |
826 | ||
827 | UDate IslamicCalendar::defaultCenturyStart() const | |
374ca955 | 828 | { |
46f4442e | 829 | // lazy-evaluate systemDefaultCenturyStart |
57a6839d A |
830 | umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); |
831 | return gSystemDefaultCenturyStart; | |
374ca955 A |
832 | } |
833 | ||
57a6839d | 834 | int32_t IslamicCalendar::defaultCenturyStartYear() const |
374ca955 | 835 | { |
46f4442e | 836 | // lazy-evaluate systemDefaultCenturyStartYear |
57a6839d A |
837 | umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); |
838 | return gSystemDefaultCenturyStartYear; | |
374ca955 A |
839 | } |
840 | ||
57a6839d A |
841 | |
842 | void U_CALLCONV | |
374ca955 A |
843 | IslamicCalendar::initializeSystemDefaultCentury() |
844 | { | |
46f4442e A |
845 | // initialize systemDefaultCentury and systemDefaultCenturyYear based |
846 | // on the current time. They'll be set to 80 years before | |
847 | // the current time. | |
729e4ab9 A |
848 | UErrorCode status = U_ZERO_ERROR; |
849 | IslamicCalendar calendar(Locale("@calendar=islamic-civil"),status); | |
57a6839d | 850 | if (U_SUCCESS(status)) { |
729e4ab9 A |
851 | calendar.setTime(Calendar::getNow(), status); |
852 | calendar.add(UCAL_YEAR, -80, status); | |
57a6839d A |
853 | |
854 | gSystemDefaultCenturyStart = calendar.getTime(status); | |
855 | gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status); | |
374ca955 | 856 | } |
729e4ab9 A |
857 | // We have no recourse upon failure unless we want to propagate the failure |
858 | // out. | |
374ca955 A |
859 | } |
860 | ||
57a6839d A |
861 | |
862 | ||
374ca955 A |
863 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IslamicCalendar) |
864 | ||
865 | U_NAMESPACE_END | |
866 | ||
867 | #endif | |
868 |