1/*
2******************************************************************************
3* Copyright (C) 2003-2012, International Business Machines Corporation
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*/
15
16#include "islamcal.h"
17
18#if !UCONFIG_NO_FORMATTING
19
20#include "umutex.h"
21#include <float.h>
22#include "gregoimp.h" // Math
23#include "astro.h" // CalendarAstronomer
24#include "uhash.h"
25#include "ucln_in.h"
26
27static const UDate HIJRA_MILLIS = -42521587200000.0;    // 7/16/622 AD 00:00
28
29// Debugging
30#ifdef U_DEBUG_ISLAMCAL
31# include <stdio.h>
32# include <stdarg.h>
33static void debug_islamcal_loc(const char *f, int32_t l)
34{
35    fprintf(stderr, "%s:%d: ", f, l);
36}
37
38static void debug_islamcal_msg(const char *pat, ...)
39{
40    va_list ap;
41    va_start(ap, pat);
42    vfprintf(stderr, pat, ap);
43    fflush(stderr);
44}
45// must use double parens, i.e.:  U_DEBUG_ISLAMCAL_MSG(("four is: %d",4));
46#define U_DEBUG_ISLAMCAL_MSG(x) {debug_islamcal_loc(__FILE__,__LINE__);debug_islamcal_msg x;}
47#else
48#define U_DEBUG_ISLAMCAL_MSG(x)
49#endif
50
51
52// --- The cache --
53// cache of months
54static UMutex astroLock = U_MUTEX_INITIALIZER;  // pod bay door lock
55static icu::CalendarCache *gMonthCache = NULL;
56static icu::CalendarAstronomer *gIslamicCalendarAstro = NULL;
57
58U_CDECL_BEGIN
59static UBool calendar_islamic_cleanup(void) {
60    if (gMonthCache) {
61        delete gMonthCache;
62        gMonthCache = NULL;
63    }
64    if (gIslamicCalendarAstro) {
65        delete gIslamicCalendarAstro;
66        gIslamicCalendarAstro = NULL;
67    }
68    return TRUE;
69}
70U_CDECL_END
71
72U_NAMESPACE_BEGIN
73
74// Implementation of the IslamicCalendar class
75
76//-------------------------------------------------------------------------
77// Constructors...
78//-------------------------------------------------------------------------
79
80const char *IslamicCalendar::getType() const {
81    if(civil==CIVIL) {
82        return "islamic-civil";
83    } else {
84        return "islamic";
85    }
86}
87
88Calendar* IslamicCalendar::clone() const {
89    return new IslamicCalendar(*this);
90}
91
92IslamicCalendar::IslamicCalendar(const Locale& aLocale, UErrorCode& success, ECivil beCivil)
93:   Calendar(TimeZone::createDefault(), aLocale, success),
94civil(beCivil)
95{
96    setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
97}
98
99IslamicCalendar::IslamicCalendar(const IslamicCalendar& other) : Calendar(other), civil(other.civil) {
100}
101
102IslamicCalendar::~IslamicCalendar()
103{
104}
105
106/**
107* Determines whether this object uses the fixed-cycle Islamic civil calendar
108* or an approximation of the religious, astronomical calendar.
109*
110* @param beCivil   <code>true</code> to use the civil calendar,
111*                  <code>false</code> to use the astronomical calendar.
112* @draft ICU 2.4
113*/
114void IslamicCalendar::setCivil(ECivil beCivil, UErrorCode &status)
115{
116    if (civil != beCivil) {
117        // The fields of the calendar will become invalid, because the calendar
118        // rules are different
119        UDate m = getTimeInMillis(status);
120        civil = beCivil;
121        clear();
122        setTimeInMillis(m, status);
123    }
124}
125
126/**
127* Returns <code>true</code> if this object is using the fixed-cycle civil
128* calendar, or <code>false</code> if using the religious, astronomical
129* calendar.
130* @draft ICU 2.4
131*/
132UBool IslamicCalendar::isCivil() {
133    return (civil == CIVIL);
134}
135
136//-------------------------------------------------------------------------
137// Minimum / Maximum access functions
138//-------------------------------------------------------------------------
139
140// Note: Current IslamicCalendar implementation does not work
141// well with negative years.
142
143// TODO: In some cases the current ICU Islamic calendar implementation shows
144// a month as having 31 days. Since date parsing now uses range checks based
145// on the table below, we need to change the range for last day of month to
146// include 31 as a workaround until the implementation is fixed.
147static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = {
148    // Minimum  Greatest    Least  Maximum
149    //           Minimum  Maximum
150    {        0,        0,        0,        0}, // ERA
151    {        1,        1,  5000000,  5000000}, // YEAR
152    {        0,        0,       11,       11}, // MONTH
153    {        1,        1,       50,       51}, // WEEK_OF_YEAR
154    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH
155    {        1,        1,       29,       31}, // DAY_OF_MONTH - 31 to workaround for cal implementation bug, should be 30
156    {        1,        1,      354,      355}, // DAY_OF_YEAR
157    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK
158    {       -1,       -1,        5,        5}, // DAY_OF_WEEK_IN_MONTH
159    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM
160    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR
161    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY
162    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE
163    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND
164    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND
165    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET
166    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET
167    {        1,        1,  5000000,  5000000}, // YEAR_WOY
168    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL
169    {        1,        1,  5000000,  5000000}, // EXTENDED_YEAR
170    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY
171    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY
172    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH
173};
174
175/**
176* @draft ICU 2.4
177*/
178int32_t IslamicCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const {
179    return LIMITS[field][limitType];
180}
181
182//-------------------------------------------------------------------------
183// Assorted calculation utilities
184//
185
186/**
187* Determine whether a year is a leap year in the Islamic civil calendar
188*/
189UBool IslamicCalendar::civilLeapYear(int32_t year)
190{
191    return (14 + 11 * year) % 30 < 11;
192}
193
194/**
195* Return the day # on which the given year starts.  Days are counted
196* from the Hijri epoch, origin 0.
197*/
198int32_t IslamicCalendar::yearStart(int32_t year) {
199    if (civil == CIVIL) {
200        return (year-1)*354 + ClockMath::floorDivide((3+11*year),30);
201    } else {
202        return trueMonthStart(12*(year-1));
203    }
204}
205
206/**
207* Return the day # on which the given month starts.  Days are counted
208* from the Hijri epoch, origin 0.
209*
210* @param year  The hijri year
211* @param year  The hijri month, 0-based
212*/
213int32_t IslamicCalendar::monthStart(int32_t year, int32_t month) const {
214    if (civil == CIVIL) {
215        return (int32_t)uprv_ceil(29.5*month)
216            + (year-1)*354 + (int32_t)ClockMath::floorDivide((3+11*year),30);
217    } else {
218        return trueMonthStart(12*(year-1) + month);
219    }
220}
221
222/**
223* Find the day number on which a particular month of the true/lunar
224* Islamic calendar starts.
225*
226* @param month The month in question, origin 0 from the Hijri epoch
227*
228* @return The day number on which the given month starts.
229*/
230int32_t IslamicCalendar::trueMonthStart(int32_t month) const
231{
232    UErrorCode status = U_ZERO_ERROR;
233    int32_t start = CalendarCache::get(&gMonthCache, month, status);
234
235    if (start==0) {
236        // Make a guess at when the month started, using the average length
237        UDate origin = HIJRA_MILLIS
238            + uprv_floor(month * CalendarAstronomer::SYNODIC_MONTH) * kOneDay;
239
240        // moonAge will fail due to memory allocation error
241        double age = moonAge(origin, status);
242        if (U_FAILURE(status)) {
243            goto trueMonthStartEnd;
244        }
245
246        if (age >= 0) {
247            // The month has already started
248            do {
249                origin -= kOneDay;
250                age = moonAge(origin, status);
251                if (U_FAILURE(status)) {
252                    goto trueMonthStartEnd;
253                }
254            } while (age >= 0);
255        }
256        else {
257            // Preceding month has not ended yet.
258            do {
259                origin += kOneDay;
260                age = moonAge(origin, status);
261                if (U_FAILURE(status)) {
262                    goto trueMonthStartEnd;
263                }
264            } while (age < 0);
265        }
266        start = (int32_t)ClockMath::floorDivide((origin - HIJRA_MILLIS), (double)kOneDay) + 1;
267        CalendarCache::put(&gMonthCache, month, start, status);
268    }
269trueMonthStartEnd :
270    if(U_FAILURE(status)) {
271        start = 0;
272    }
273    return start;
274}
275
276/**
277* Return the "age" of the moon at the given time; this is the difference
278* in ecliptic latitude between the moon and the sun.  This method simply
279* calls CalendarAstronomer.moonAge, converts to degrees,
280* and adjusts the result to be in the range [-180, 180].
281*
282* @param time  The time at which the moon's age is desired,
283*              in millis since 1/1/1970.
284*/
285double IslamicCalendar::moonAge(UDate time, UErrorCode &status)
286{
287    double age = 0;
288
289    umtx_lock(&astroLock);
290    if(gIslamicCalendarAstro == NULL) {
291        gIslamicCalendarAstro = new CalendarAstronomer();
292        if (gIslamicCalendarAstro == NULL) {
293            status = U_MEMORY_ALLOCATION_ERROR;
294            return age;
295        }
296        ucln_i18n_registerCleanup(UCLN_I18N_ISLAMIC_CALENDAR, calendar_islamic_cleanup);
297    }
298    gIslamicCalendarAstro->setTime(time);
299    age = gIslamicCalendarAstro->getMoonAge();
300    umtx_unlock(&astroLock);
301
302    // Convert to degrees and normalize...
303    age = age * 180 / CalendarAstronomer::PI;
304    if (age > 180) {
305        age = age - 360;
306    }
307
308    return age;
309}
310
311//----------------------------------------------------------------------
312// Calendar framework
313//----------------------------------------------------------------------
314
315/**
316* Return the length (in days) of the given month.
317*
318* @param year  The hijri year
319* @param year  The hijri month, 0-based
320* @draft ICU 2.4
321*/
322int32_t IslamicCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const {
323
324    int32_t length = 0;
325
326    if (civil == CIVIL) {
327        length = 29 + (month+1) % 2;
328        if (month == DHU_AL_HIJJAH && civilLeapYear(extendedYear)) {
329            length++;
330        }
331    } else {
332        month = 12*(extendedYear-1) + month;
333        length =  trueMonthStart(month+1) - trueMonthStart(month) ;
334    }
335    return length;
336}
337
338/**
339* Return the number of days in the given Islamic year
340* @draft ICU 2.4
341*/
342int32_t IslamicCalendar::handleGetYearLength(int32_t extendedYear) const {
343    if (civil == CIVIL) {
344        return 354 + (civilLeapYear(extendedYear) ? 1 : 0);
345    } else {
346        int32_t month = 12*(extendedYear-1);
347        return (trueMonthStart(month + 12) - trueMonthStart(month));
348    }
349}
350
351//-------------------------------------------------------------------------
352// Functions for converting from field values to milliseconds....
353//-------------------------------------------------------------------------
354
355// Return JD of start of given month/year
356/**
357* @draft ICU 2.4
358*/
359int32_t IslamicCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /* useMonth */) const {
360    return monthStart(eyear, month) + 1948439;
361}
362
363//-------------------------------------------------------------------------
364// Functions for converting from milliseconds to field values
365//-------------------------------------------------------------------------
366
367/**
368* @draft ICU 2.4
369*/
370int32_t IslamicCalendar::handleGetExtendedYear() {
371    int32_t year;
372    if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
373        year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
374    } else {
375        year = internalGet(UCAL_YEAR, 1); // Default to year 1
376    }
377    return year;
378}
379
380/**
381* Override Calendar to compute several fields specific to the Islamic
382* calendar system.  These are:
383*
384* <ul><li>ERA
385* <li>YEAR
386* <li>MONTH
387* <li>DAY_OF_MONTH
388* <li>DAY_OF_YEAR
389* <li>EXTENDED_YEAR</ul>
390*
391* The DAY_OF_WEEK and DOW_LOCAL fields are already set when this
392* method is called. The getGregorianXxx() methods return Gregorian
393* calendar equivalents for the given Julian day.
394* @draft ICU 2.4
395*/
396void IslamicCalendar::handleComputeFields(int32_t julianDay, UErrorCode &status) {
397    int32_t year, month, dayOfMonth, dayOfYear;
398    UDate startDate;
399    int32_t days = julianDay - 1948440;
400
401    if (civil == CIVIL) {
402        // Use the civil calendar approximation, which is just arithmetic
403        year  = (int)ClockMath::floorDivide( (double)(30 * days + 10646) , 10631.0 );
404        month = (int32_t)uprv_ceil((days - 29 - yearStart(year)) / 29.5 );
405        month = month<11?month:11;
406        startDate = monthStart(year, month);
407    } else {
408        // Guess at the number of elapsed full months since the epoch
409        int32_t months = (int32_t)uprv_floor((double)days / CalendarAstronomer::SYNODIC_MONTH);
410
411        startDate = uprv_floor(months * CalendarAstronomer::SYNODIC_MONTH);
412
413        double age = moonAge(internalGetTime(), status);
414        if (U_FAILURE(status)) {
415        	status = U_MEMORY_ALLOCATION_ERROR;
416        	return;
417        }
418        if ( days - startDate >= 25 && age > 0) {
419            // If we're near the end of the month, assume next month and search backwards
420            months++;
421        }
422
423        // Find out the last time that the new moon was actually visible at this longitude
424        // This returns midnight the night that the moon was visible at sunset.
425        while ((startDate = trueMonthStart(months)) > days) {
426            // If it was after the date in question, back up a month and try again
427            months--;
428        }
429
430        year = months / 12 + 1;
431        month = months % 12;
432    }
433
434    dayOfMonth = (days - monthStart(year, month)) + 1;
435
436    // Now figure out the day of the year.
437    dayOfYear = (days - monthStart(year, 0) + 1);
438
439    internalSet(UCAL_ERA, 0);
440    internalSet(UCAL_YEAR, year);
441    internalSet(UCAL_EXTENDED_YEAR, year);
442    internalSet(UCAL_MONTH, month);
443    internalSet(UCAL_DAY_OF_MONTH, dayOfMonth);
444    internalSet(UCAL_DAY_OF_YEAR, dayOfYear);
445}
446
447UBool
448IslamicCalendar::inDaylightTime(UErrorCode& status) const
449{
450    // copied from GregorianCalendar
451    if (U_FAILURE(status) || (&(getTimeZone()) == NULL && !getTimeZone().useDaylightTime()))
452        return FALSE;
453
454    // Force an update of the state of the Calendar.
455    ((IslamicCalendar*)this)->complete(status); // cast away const
456
457    return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE);
458}
459
460// default century
461const UDate     IslamicCalendar::fgSystemDefaultCentury        = DBL_MIN;
462const int32_t   IslamicCalendar::fgSystemDefaultCenturyYear    = -1;
463
464UDate           IslamicCalendar::fgSystemDefaultCenturyStart       = DBL_MIN;
465int32_t         IslamicCalendar::fgSystemDefaultCenturyStartYear   = -1;
466
467
468UBool IslamicCalendar::haveDefaultCentury() const
469{
470    return TRUE;
471}
472
473UDate IslamicCalendar::defaultCenturyStart() const
474{
475    return internalGetDefaultCenturyStart();
476}
477
478int32_t IslamicCalendar::defaultCenturyStartYear() const
479{
480    return internalGetDefaultCenturyStartYear();
481}
482
483UDate
484IslamicCalendar::internalGetDefaultCenturyStart() const
485{
486    // lazy-evaluate systemDefaultCenturyStart
487    UBool needsUpdate;
488    UMTX_CHECK(NULL, (fgSystemDefaultCenturyStart == fgSystemDefaultCentury), needsUpdate);
489
490    if (needsUpdate) {
491        initializeSystemDefaultCentury();
492    }
493
494    // use defaultCenturyStart unless it's the flag value;
495    // then use systemDefaultCenturyStart
496
497    return fgSystemDefaultCenturyStart;
498}
499
500int32_t
501IslamicCalendar::internalGetDefaultCenturyStartYear() const
502{
503    // lazy-evaluate systemDefaultCenturyStartYear
504    UBool needsUpdate;
505    UMTX_CHECK(NULL, (fgSystemDefaultCenturyStart == fgSystemDefaultCentury), needsUpdate);
506
507    if (needsUpdate) {
508        initializeSystemDefaultCentury();
509    }
510
511    // use defaultCenturyStart unless it's the flag value;
512    // then use systemDefaultCenturyStartYear
513
514    return    fgSystemDefaultCenturyStartYear;
515}
516
517void
518IslamicCalendar::initializeSystemDefaultCentury()
519{
520    // initialize systemDefaultCentury and systemDefaultCenturyYear based
521    // on the current time.  They'll be set to 80 years before
522    // the current time.
523    UErrorCode status = U_ZERO_ERROR;
524    IslamicCalendar calendar(Locale("@calendar=islamic-civil"),status);
525    if (U_SUCCESS(status))
526    {
527        calendar.setTime(Calendar::getNow(), status);
528        calendar.add(UCAL_YEAR, -80, status);
529        UDate    newStart =  calendar.getTime(status);
530        int32_t  newYear  =  calendar.get(UCAL_YEAR, status);
531        umtx_lock(NULL);
532        if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
533        {
534            fgSystemDefaultCenturyStartYear = newYear;
535            fgSystemDefaultCenturyStart = newStart;
536        }
537        umtx_unlock(NULL);
538    }
539    // We have no recourse upon failure unless we want to propagate the failure
540    // out.
541}
542
543UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IslamicCalendar)
544
545U_NAMESPACE_END
546
547#endif
548
549