michael@0: /* michael@0: ****************************************************************************** michael@0: * Copyright (C) 2003-2013, International Business Machines Corporation michael@0: * and others. All Rights Reserved. michael@0: ****************************************************************************** michael@0: * michael@0: * File ISLAMCAL.H michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 10/14/2003 srl ported from java IslamicCalendar michael@0: ***************************************************************************** michael@0: */ michael@0: michael@0: #include "islamcal.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "umutex.h" michael@0: #include michael@0: #include "gregoimp.h" // Math michael@0: #include "astro.h" // CalendarAstronomer michael@0: #include "uhash.h" michael@0: #include "ucln_in.h" michael@0: #include "uassert.h" michael@0: michael@0: static const UDate HIJRA_MILLIS = -42521587200000.0; // 7/16/622 AD 00:00 michael@0: michael@0: // Debugging michael@0: #ifdef U_DEBUG_ISLAMCAL michael@0: # include michael@0: # include michael@0: static void debug_islamcal_loc(const char *f, int32_t l) michael@0: { michael@0: fprintf(stderr, "%s:%d: ", f, l); michael@0: } michael@0: michael@0: static void debug_islamcal_msg(const char *pat, ...) michael@0: { michael@0: va_list ap; michael@0: va_start(ap, pat); michael@0: vfprintf(stderr, pat, ap); michael@0: fflush(stderr); michael@0: } michael@0: // must use double parens, i.e.: U_DEBUG_ISLAMCAL_MSG(("four is: %d",4)); michael@0: #define U_DEBUG_ISLAMCAL_MSG(x) {debug_islamcal_loc(__FILE__,__LINE__);debug_islamcal_msg x;} michael@0: #else michael@0: #define U_DEBUG_ISLAMCAL_MSG(x) michael@0: #endif michael@0: michael@0: michael@0: // --- The cache -- michael@0: // cache of months michael@0: static UMutex astroLock = U_MUTEX_INITIALIZER; // pod bay door lock michael@0: static icu::CalendarCache *gMonthCache = NULL; michael@0: static icu::CalendarAstronomer *gIslamicCalendarAstro = NULL; michael@0: michael@0: U_CDECL_BEGIN michael@0: static UBool calendar_islamic_cleanup(void) { michael@0: if (gMonthCache) { michael@0: delete gMonthCache; michael@0: gMonthCache = NULL; michael@0: } michael@0: if (gIslamicCalendarAstro) { michael@0: delete gIslamicCalendarAstro; michael@0: gIslamicCalendarAstro = NULL; michael@0: } michael@0: return TRUE; michael@0: } michael@0: U_CDECL_END michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: // Implementation of the IslamicCalendar class michael@0: michael@0: /** michael@0: * Friday EPOC michael@0: */ michael@0: static const int32_t CIVIL_EPOC = 1948440; michael@0: michael@0: /** michael@0: * Thursday EPOC michael@0: */ michael@0: static const int32_t ASTRONOMICAL_EPOC = 1948439; michael@0: michael@0: michael@0: static const int32_t UMALQURA_YEAR_START = 1318; michael@0: static const int32_t UMALQURA_YEAR_END = 1480; michael@0: michael@0: static const int UMALQURA_MONTHLENGTH[] = { michael@0: //* 1318 -1322 */ "0101 0111 0100", "1001 0111 0110", "0100 1011 0111", "0010 0101 0111", "0101 0010 1011", michael@0: 0x0574, 0x0975, 0x06A7, 0x0257, 0x052B, michael@0: //* 1323 -1327 */ "0110 1001 0101", "0110 1100 1010", "1010 1101 0101", "0101 0101 1011", "0010 0101 1101", michael@0: 0x0695, 0x06CA, 0x0AD5, 0x055B, 0x025B, michael@0: //* 1328 -1332 */ "1001 0010 1101", "1100 1001 0101", "1101 0100 1010", "1110 1010 0101", "0110 1101 0010", michael@0: 0x092D, 0x0C95, 0x0D4A, 0x0E5B, 0x025B, michael@0: //* 1333 -1337 */ "1010 1101 0101", "0101 0101 1010", "1010 1010 1011", "0100 0100 1011", "0110 1010 0101", michael@0: 0x0AD5, 0x055A, 0x0AAB, 0x044B, 0x06A5, michael@0: //* 1338 -1342 */ "0111 0101 0010", "1011 1010 1001", "0011 0111 0100", "1010 1011 0110", "0101 0101 0110", michael@0: 0x0752, 0x0BA9, 0x0374, 0x0AB6, 0x0556, michael@0: //* 1343 -1347 */ "1010 1010 1010", "1101 0101 0010", "1101 1010 1001", "0101 1101 0100", "1010 1110 1010", michael@0: 0x0AAA, 0x0D52, 0x0DA9, 0x05D4, 0x0AEA, michael@0: //* 1348 -1352 */ "0100 1101 1101", "0010 0110 1110", "1001 0010 1110", "1010 1010 0110", "1101 0101 0100", michael@0: 0x04DD, 0x026E, 0x092E, 0x0AA6, 0x0D54, michael@0: //* 1353 -1357 */ "0101 1010 1010", "0101 1011 0101", "0010 1011 0100", "1001 0011 0111", "0100 1001 1011", michael@0: 0x05AA, 0x05B5, 0x02B4, 0x0937, 0x049B, michael@0: //* 1358 -1362 */ "1010 0100 1011", "1011 0010 0101", "1011 0101 0100", "1011 0110 1010", "0101 0110 1101", michael@0: 0x0A4B, 0x0B25, 0x0B54, 0x0B6A, 0x056D, michael@0: //* 1363 -1367 */ "0100 1010 1101", "1010 0101 0101", "1101 0010 0101", "1110 1001 0010", "1110 1100 1001", michael@0: 0x04AD, 0x0A55, 0x0D25, 0x0E92, 0x0EC9, michael@0: //* 1368 -1372 */ "0110 1101 0100", "1010 1110 1010", "0101 0110 1011", "0100 1010 1011", "0110 1000 0101", michael@0: 0x06D4, 0x0ADA, 0x056B, 0x04AB, 0x0685, michael@0: //* 1373 -1377 */ "1011 0100 1001", "1011 1010 0100", "1011 1011 0010", "0101 1011 0101", "0010 1011 1010", michael@0: 0x0B49, 0x0BA4, 0x0BB2, 0x05B5, 0x02BA, michael@0: //* 1378 -1382 */ "1001 0101 1011", "0100 1010 1011", "0101 0101 0101", "0110 1011 0010", "0110 1101 1001", michael@0: 0x095B, 0x04AB, 0x0555, 0x06B2, 0x06D9, michael@0: //* 1383 -1387 */ "0010 1110 1100", "1001 0110 1110", "0100 1010 1110", "1010 0101 0110", "1101 0010 1010", michael@0: 0x02EC, 0x096E, 0x04AE, 0x0A56, 0x0D2A, michael@0: //* 1388 -1392 */ "1101 0101 0101", "0101 1010 1010", "1010 1011 0101", "0100 1011 1011", "0000 0101 1011", michael@0: 0x0D55, 0x05AA, 0x0AB5, 0x04BB, 0x005B, michael@0: //* 1393 -1397 */ "1001 0010 1011", "1010 1001 0101", "0011 0100 1010", "1011 1010 0101", "0101 1010 1010", michael@0: 0x092B, 0x0A95, 0x034A, 0x0BA5, 0x05AA, michael@0: //* 1398 -1402 */ "1010 1011 0101", "0101 0101 0110", "1010 1001 0110", "1101 0100 1010", "1110 1010 0101", michael@0: 0x0AB5, 0x0556, 0x0A96, 0x0B4A, 0x0EA5, michael@0: //* 1403 -1407 */ "0111 0101 0010", "0110 1110 1001", "0011 0110 1010", "1010 1010 1101", "0101 0101 0101", michael@0: 0x0752, 0x06E9, 0x036A, 0x0AAD, 0x0555, michael@0: //* 1408 -1412 */ "1010 1010 0101", "1011 0101 0010", "1011 1010 1001", "0101 1011 0100", "1001 1011 1010", michael@0: 0x0AA5, 0x0B52, 0x0BA9, 0x05B4, 0x09BA, michael@0: //* 1413 -1417 */ "0100 1101 1011", "0010 0101 1101", "0101 0010 1101", "1010 1010 0101", "1010 1101 0100", michael@0: 0x04DB, 0x025D, 0x052D, 0x0AA5, 0x0AD4, michael@0: //* 1418 -1422 */ "1010 1110 1010", "0101 0110 1101", "0100 1011 1101", "0010 0011 1101", "1001 0001 1101", michael@0: 0x0AEA, 0x056D, 0x04BD, 0x023D, 0x091D, michael@0: //* 1423 -1427 */ "1010 1001 0101", "1011 0100 1010", "1011 0101 1010", "0101 0110 1101", "0010 1011 0110", michael@0: 0x0A95, 0x0B4A, 0x0B5A, 0x056D, 0x02B6, michael@0: //* 1428 -1432 */ "1001 0011 1011", "0100 1001 1011", "0110 0101 0101", "0110 1010 1001", "0111 0101 0100", michael@0: 0x093B, 0x049B, 0x0655, 0x06A9, 0x0754, michael@0: //* 1433 -1437 */ "1011 0110 1010", "0101 0110 1100", "1010 1010 1101", "0101 0101 0101", "1011 0010 1001", michael@0: 0x0B6A, 0x056C, 0x0AAD, 0x0555, 0x0B29, michael@0: //* 1438 -1442 */ "1011 1001 0010", "1011 1010 1001", "0101 1101 0100", "1010 1101 1010", "0101 0101 1010", michael@0: 0x0B92, 0x0BA9, 0x05D4, 0x0ADA, 0x055A, michael@0: //* 1443 -1447 */ "1010 1010 1011", "0101 1001 0101", "0111 0100 1001", "0111 0110 0100", "1011 1010 1010", michael@0: 0x0AAB, 0x0595, 0x0749, 0x0764, 0x0BAA, michael@0: //* 1448 -1452 */ "0101 1011 0101", "0010 1011 0110", "1010 0101 0110", "1110 0100 1101", "1011 0010 0101", michael@0: 0x05B5, 0x02B6, 0x0A56, 0x0E4D, 0x0B25, michael@0: //* 1453 -1457 */ "1011 0101 0010", "1011 0110 1010", "0101 1010 1101", "0010 1010 1110", "1001 0010 1111", michael@0: 0x0B52, 0x0B6A, 0x05AD, 0x02AE, 0x092F, michael@0: //* 1458 -1462 */ "0100 1001 0111", "0110 0100 1011", "0110 1010 0101", "0110 1010 1100", "1010 1101 0110", michael@0: 0x0497, 0x064B, 0x06A5, 0x06AC, 0x0AD6, michael@0: //* 1463 -1467 */ "0101 0101 1101", "0100 1001 1101", "1010 0100 1101", "1101 0001 0110", "1101 1001 0101", michael@0: 0x055D, 0x049D, 0x0A4D, 0x0D16, 0x0D95, michael@0: //* 1468 -1472 */ "0101 1010 1010", "0101 1011 0101", "0010 1001 1010", "1001 0101 1011", "0100 1010 1100", michael@0: 0x05AA, 0x05B5, 0x029A, 0x095B, 0x04AC, michael@0: //* 1473 -1477 */ "0101 1001 0101", "0110 1100 1010", "0110 1110 0100", "1010 1110 1010", "0100 1111 0101", michael@0: 0x0595, 0x06CA, 0x06E4, 0x0AEA, 0x04F5, michael@0: //* 1478 -1480 */ "0010 1011 0110", "1001 0101 0110", "1010 1010 1010" michael@0: 0x02B6, 0x0956, 0x0AAA michael@0: }; michael@0: michael@0: int32_t getUmalqura_MonthLength(int32_t y, int32_t m) { michael@0: int32_t mask = (int32_t) (0x01 << (11 - m)); // set mask for bit corresponding to month michael@0: if((UMALQURA_MONTHLENGTH[y] & mask) == 0 ) michael@0: return 29; michael@0: else michael@0: return 30; michael@0: michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Constructors... michael@0: //------------------------------------------------------------------------- michael@0: michael@0: const char *IslamicCalendar::getType() const { michael@0: const char *sType = NULL; michael@0: michael@0: switch (cType) { michael@0: case CIVIL: michael@0: sType = "islamic-civil"; michael@0: break; michael@0: case ASTRONOMICAL: michael@0: sType = "islamic"; michael@0: break; michael@0: case TBLA: michael@0: sType = "islamic-tbla"; michael@0: break; michael@0: case UMALQURA: michael@0: sType = "islamic-umalqura"; michael@0: break; michael@0: default: michael@0: U_ASSERT(false); // out of range michael@0: sType = "islamic"; // "islamic" is used as the generic type michael@0: break; michael@0: } michael@0: return sType; michael@0: } michael@0: michael@0: Calendar* IslamicCalendar::clone() const { michael@0: return new IslamicCalendar(*this); michael@0: } michael@0: michael@0: IslamicCalendar::IslamicCalendar(const Locale& aLocale, UErrorCode& success, ECalculationType type) michael@0: : Calendar(TimeZone::createDefault(), aLocale, success), michael@0: cType(type) michael@0: { michael@0: setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly. michael@0: } michael@0: michael@0: IslamicCalendar::IslamicCalendar(const IslamicCalendar& other) : Calendar(other), cType(other.cType) { michael@0: } michael@0: michael@0: IslamicCalendar::~IslamicCalendar() michael@0: { michael@0: } michael@0: michael@0: void IslamicCalendar::setCalculationType(ECalculationType type, UErrorCode &status) michael@0: { michael@0: if (cType != type) { michael@0: // The fields of the calendar will become invalid, because the calendar michael@0: // rules are different michael@0: UDate m = getTimeInMillis(status); michael@0: cType = type; michael@0: clear(); michael@0: setTimeInMillis(m, status); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Returns true if this object is using the fixed-cycle civil michael@0: * calendar, or false if using the religious, astronomical michael@0: * calendar. michael@0: * @draft ICU 2.4 michael@0: */ michael@0: UBool IslamicCalendar::isCivil() { michael@0: return (cType == CIVIL); michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Minimum / Maximum access functions michael@0: //------------------------------------------------------------------------- michael@0: michael@0: // Note: Current IslamicCalendar implementation does not work michael@0: // well with negative years. michael@0: michael@0: // TODO: In some cases the current ICU Islamic calendar implementation shows michael@0: // a month as having 31 days. Since date parsing now uses range checks based michael@0: // on the table below, we need to change the range for last day of month to michael@0: // include 31 as a workaround until the implementation is fixed. michael@0: static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = { michael@0: // Minimum Greatest Least Maximum michael@0: // Minimum Maximum michael@0: { 0, 0, 0, 0}, // ERA michael@0: { 1, 1, 5000000, 5000000}, // YEAR michael@0: { 0, 0, 11, 11}, // MONTH michael@0: { 1, 1, 50, 51}, // WEEK_OF_YEAR michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH michael@0: { 1, 1, 29, 31}, // DAY_OF_MONTH - 31 to workaround for cal implementation bug, should be 30 michael@0: { 1, 1, 354, 355}, // DAY_OF_YEAR michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK michael@0: { -1, -1, 5, 5}, // DAY_OF_WEEK_IN_MONTH michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET michael@0: { 1, 1, 5000000, 5000000}, // YEAR_WOY michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL michael@0: { 1, 1, 5000000, 5000000}, // EXTENDED_YEAR michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH michael@0: }; michael@0: michael@0: /** michael@0: * @draft ICU 2.4 michael@0: */ michael@0: int32_t IslamicCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const { michael@0: return LIMITS[field][limitType]; michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Assorted calculation utilities michael@0: // michael@0: michael@0: /** michael@0: * Determine whether a year is a leap year in the Islamic civil calendar michael@0: */ michael@0: UBool IslamicCalendar::civilLeapYear(int32_t year) michael@0: { michael@0: return (14 + 11 * year) % 30 < 11; michael@0: } michael@0: michael@0: /** michael@0: * Return the day # on which the given year starts. Days are counted michael@0: * from the Hijri epoch, origin 0. michael@0: */ michael@0: int32_t IslamicCalendar::yearStart(int32_t year) const{ michael@0: if (cType == CIVIL || cType == TBLA || michael@0: (cType == UMALQURA && year < UMALQURA_YEAR_START)) michael@0: { michael@0: return (year-1)*354 + ClockMath::floorDivide((3+11*year),30); michael@0: } else if(cType==ASTRONOMICAL){ michael@0: return trueMonthStart(12*(year-1)); michael@0: } else { michael@0: int32_t ys = yearStart(UMALQURA_YEAR_START-1); michael@0: ys+= handleGetYearLength(UMALQURA_YEAR_START-1); michael@0: for(int i=UMALQURA_YEAR_START; i< year; i++){ michael@0: ys+= handleGetYearLength(i); michael@0: } michael@0: return ys; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Return the day # on which the given month starts. Days are counted michael@0: * from the Hijri epoch, origin 0. michael@0: * michael@0: * @param year The hijri year michael@0: * @param year The hijri month, 0-based michael@0: */ michael@0: int32_t IslamicCalendar::monthStart(int32_t year, int32_t month) const { michael@0: if (cType == CIVIL || cType == TBLA) { michael@0: return (int32_t)uprv_ceil(29.5*month) michael@0: + (year-1)*354 + (int32_t)ClockMath::floorDivide((3+11*year),30); michael@0: } else if(cType==ASTRONOMICAL){ michael@0: return trueMonthStart(12*(year-1) + month); michael@0: } else { michael@0: int32_t ms = yearStart(year); michael@0: for(int i=0; i< month; i++){ michael@0: ms+= handleGetMonthLength(year, i); michael@0: } michael@0: return ms; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Find the day number on which a particular month of the true/lunar michael@0: * Islamic calendar starts. michael@0: * michael@0: * @param month The month in question, origin 0 from the Hijri epoch michael@0: * michael@0: * @return The day number on which the given month starts. michael@0: */ michael@0: int32_t IslamicCalendar::trueMonthStart(int32_t month) const michael@0: { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: int32_t start = CalendarCache::get(&gMonthCache, month, status); michael@0: michael@0: if (start==0) { michael@0: // Make a guess at when the month started, using the average length michael@0: UDate origin = HIJRA_MILLIS michael@0: + uprv_floor(month * CalendarAstronomer::SYNODIC_MONTH) * kOneDay; michael@0: michael@0: // moonAge will fail due to memory allocation error michael@0: double age = moonAge(origin, status); michael@0: if (U_FAILURE(status)) { michael@0: goto trueMonthStartEnd; michael@0: } michael@0: michael@0: if (age >= 0) { michael@0: // The month has already started michael@0: do { michael@0: origin -= kOneDay; michael@0: age = moonAge(origin, status); michael@0: if (U_FAILURE(status)) { michael@0: goto trueMonthStartEnd; michael@0: } michael@0: } while (age >= 0); michael@0: } michael@0: else { michael@0: // Preceding month has not ended yet. michael@0: do { michael@0: origin += kOneDay; michael@0: age = moonAge(origin, status); michael@0: if (U_FAILURE(status)) { michael@0: goto trueMonthStartEnd; michael@0: } michael@0: } while (age < 0); michael@0: } michael@0: start = (int32_t)ClockMath::floorDivide((origin - HIJRA_MILLIS), (double)kOneDay) + 1; michael@0: CalendarCache::put(&gMonthCache, month, start, status); michael@0: } michael@0: trueMonthStartEnd : michael@0: if(U_FAILURE(status)) { michael@0: start = 0; michael@0: } michael@0: return start; michael@0: } michael@0: michael@0: /** michael@0: * Return the "age" of the moon at the given time; this is the difference michael@0: * in ecliptic latitude between the moon and the sun. This method simply michael@0: * calls CalendarAstronomer.moonAge, converts to degrees, michael@0: * and adjusts the result to be in the range [-180, 180]. michael@0: * michael@0: * @param time The time at which the moon's age is desired, michael@0: * in millis since 1/1/1970. michael@0: */ michael@0: double IslamicCalendar::moonAge(UDate time, UErrorCode &status) michael@0: { michael@0: double age = 0; michael@0: michael@0: umtx_lock(&astroLock); michael@0: if(gIslamicCalendarAstro == NULL) { michael@0: gIslamicCalendarAstro = new CalendarAstronomer(); michael@0: if (gIslamicCalendarAstro == NULL) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: return age; michael@0: } michael@0: ucln_i18n_registerCleanup(UCLN_I18N_ISLAMIC_CALENDAR, calendar_islamic_cleanup); michael@0: } michael@0: gIslamicCalendarAstro->setTime(time); michael@0: age = gIslamicCalendarAstro->getMoonAge(); michael@0: umtx_unlock(&astroLock); michael@0: michael@0: // Convert to degrees and normalize... michael@0: age = age * 180 / CalendarAstronomer::PI; michael@0: if (age > 180) { michael@0: age = age - 360; michael@0: } michael@0: michael@0: return age; michael@0: } michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Calendar framework michael@0: //---------------------------------------------------------------------- michael@0: michael@0: /** michael@0: * Return the length (in days) of the given month. michael@0: * michael@0: * @param year The hijri year michael@0: * @param year The hijri month, 0-based michael@0: * @draft ICU 2.4 michael@0: */ michael@0: int32_t IslamicCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const { michael@0: michael@0: int32_t length = 0; michael@0: michael@0: if (cType == CIVIL || cType == TBLA || michael@0: (cType == UMALQURA && (extendedYearUMALQURA_YEAR_END)) ) { michael@0: length = 29 + (month+1) % 2; michael@0: if (month == DHU_AL_HIJJAH && civilLeapYear(extendedYear)) { michael@0: length++; michael@0: } michael@0: } else if(cType == ASTRONOMICAL){ michael@0: month = 12*(extendedYear-1) + month; michael@0: length = trueMonthStart(month+1) - trueMonthStart(month) ; michael@0: } else { michael@0: length = getUmalqura_MonthLength(extendedYear - UMALQURA_YEAR_START, month); michael@0: } michael@0: return length; michael@0: } michael@0: michael@0: /** michael@0: * Return the number of days in the given Islamic year michael@0: * @draft ICU 2.4 michael@0: */ michael@0: int32_t IslamicCalendar::handleGetYearLength(int32_t extendedYear) const { michael@0: if (cType == CIVIL || cType == TBLA || michael@0: (cType == UMALQURA && (extendedYearUMALQURA_YEAR_END)) ) { michael@0: return 354 + (civilLeapYear(extendedYear) ? 1 : 0); michael@0: } else if(cType == ASTRONOMICAL){ michael@0: int32_t month = 12*(extendedYear-1); michael@0: return (trueMonthStart(month + 12) - trueMonthStart(month)); michael@0: } else { michael@0: int len = 0; michael@0: for(int i=0; i<12; i++) michael@0: len += handleGetMonthLength(extendedYear, i); michael@0: return len; michael@0: } michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Functions for converting from field values to milliseconds.... michael@0: //------------------------------------------------------------------------- michael@0: michael@0: // Return JD of start of given month/year michael@0: /** michael@0: * @draft ICU 2.4 michael@0: */ michael@0: int32_t IslamicCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /* useMonth */) const { michael@0: return monthStart(eyear, month) + 1948439; michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Functions for converting from milliseconds to field values michael@0: //------------------------------------------------------------------------- michael@0: michael@0: /** michael@0: * @draft ICU 2.4 michael@0: */ michael@0: int32_t IslamicCalendar::handleGetExtendedYear() { michael@0: int32_t year; michael@0: if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) { michael@0: year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1 michael@0: } else { michael@0: year = internalGet(UCAL_YEAR, 1); // Default to year 1 michael@0: } michael@0: return year; michael@0: } michael@0: michael@0: /** michael@0: * Override Calendar to compute several fields specific to the Islamic michael@0: * calendar system. These are: michael@0: * michael@0: *
  • ERA michael@0: *
  • YEAR michael@0: *
  • MONTH michael@0: *
  • DAY_OF_MONTH michael@0: *
  • DAY_OF_YEAR michael@0: *
  • EXTENDED_YEAR
michael@0: * michael@0: * The DAY_OF_WEEK and DOW_LOCAL fields are already set when this michael@0: * method is called. The getGregorianXxx() methods return Gregorian michael@0: * calendar equivalents for the given Julian day. michael@0: * @draft ICU 2.4 michael@0: */ michael@0: void IslamicCalendar::handleComputeFields(int32_t julianDay, UErrorCode &status) { michael@0: int32_t year, month, dayOfMonth, dayOfYear; michael@0: UDate startDate; michael@0: int32_t days = julianDay - CIVIL_EPOC; michael@0: michael@0: if (cType == CIVIL || cType == TBLA) { michael@0: if(cType == TBLA) michael@0: days = julianDay - ASTRONOMICAL_EPOC; michael@0: // Use the civil calendar approximation, which is just arithmetic michael@0: year = (int)ClockMath::floorDivide( (double)(30 * days + 10646) , 10631.0 ); michael@0: month = (int32_t)uprv_ceil((days - 29 - yearStart(year)) / 29.5 ); michael@0: month = month<11?month:11; michael@0: startDate = monthStart(year, month); michael@0: } else if(cType == ASTRONOMICAL){ michael@0: // Guess at the number of elapsed full months since the epoch michael@0: int32_t months = (int32_t)uprv_floor((double)days / CalendarAstronomer::SYNODIC_MONTH); michael@0: michael@0: startDate = uprv_floor(months * CalendarAstronomer::SYNODIC_MONTH); michael@0: michael@0: double age = moonAge(internalGetTime(), status); michael@0: if (U_FAILURE(status)) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: if ( days - startDate >= 25 && age > 0) { michael@0: // If we're near the end of the month, assume next month and search backwards michael@0: months++; michael@0: } michael@0: michael@0: // Find out the last time that the new moon was actually visible at this longitude michael@0: // This returns midnight the night that the moon was visible at sunset. michael@0: while ((startDate = trueMonthStart(months)) > days) { michael@0: // If it was after the date in question, back up a month and try again michael@0: months--; michael@0: } michael@0: michael@0: year = months / 12 + 1; michael@0: month = months % 12; michael@0: } else if(cType == UMALQURA) { michael@0: int32_t umalquraStartdays = yearStart(UMALQURA_YEAR_START) ; michael@0: if( days < umalquraStartdays){ michael@0: //Use Civil calculation michael@0: year = (int)ClockMath::floorDivide( (double)(30 * days + 10646) , 10631.0 ); michael@0: month = (int32_t)uprv_ceil((days - 29 - yearStart(year)) / 29.5 ); michael@0: month = month<11?month:11; michael@0: startDate = monthStart(year, month); michael@0: }else{ michael@0: int y =UMALQURA_YEAR_START-1, m =0; michael@0: long d = 1; michael@0: while(d > 0){ michael@0: y++; michael@0: d = days - yearStart(y) +1; michael@0: if(d == handleGetYearLength(y)){ michael@0: m=11; michael@0: break; michael@0: }else if(d < handleGetYearLength(y) ){ michael@0: int monthLen = handleGetMonthLength(y, m); michael@0: m=0; michael@0: while(d > monthLen){ michael@0: d -= monthLen; michael@0: m++; michael@0: monthLen = handleGetMonthLength(y, m); michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: year = y; michael@0: month = m; michael@0: } michael@0: } else { // invalid 'civil' michael@0: U_ASSERT(false); // should not get here, out of range michael@0: year=month=0; michael@0: } michael@0: michael@0: dayOfMonth = (days - monthStart(year, month)) + 1; michael@0: michael@0: // Now figure out the day of the year. michael@0: dayOfYear = (days - monthStart(year, 0) + 1); michael@0: michael@0: michael@0: internalSet(UCAL_ERA, 0); michael@0: internalSet(UCAL_YEAR, year); michael@0: internalSet(UCAL_EXTENDED_YEAR, year); michael@0: internalSet(UCAL_MONTH, month); michael@0: internalSet(UCAL_DAY_OF_MONTH, dayOfMonth); michael@0: internalSet(UCAL_DAY_OF_YEAR, dayOfYear); michael@0: } michael@0: michael@0: UBool michael@0: IslamicCalendar::inDaylightTime(UErrorCode& status) const michael@0: { michael@0: // copied from GregorianCalendar michael@0: if (U_FAILURE(status) || (&(getTimeZone()) == NULL && !getTimeZone().useDaylightTime())) michael@0: return FALSE; michael@0: michael@0: // Force an update of the state of the Calendar. michael@0: ((IslamicCalendar*)this)->complete(status); // cast away const michael@0: michael@0: return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE); michael@0: } michael@0: michael@0: /** michael@0: * The system maintains a static default century start date and Year. They are michael@0: * initialized the first time they are used. Once the system default century date michael@0: * and year are set, they do not change. michael@0: */ michael@0: static UDate gSystemDefaultCenturyStart = DBL_MIN; michael@0: static int32_t gSystemDefaultCenturyStartYear = -1; michael@0: static icu::UInitOnce gSystemDefaultCenturyInit = U_INITONCE_INITIALIZER; michael@0: michael@0: michael@0: UBool IslamicCalendar::haveDefaultCentury() const michael@0: { michael@0: return TRUE; michael@0: } michael@0: michael@0: UDate IslamicCalendar::defaultCenturyStart() const michael@0: { michael@0: // lazy-evaluate systemDefaultCenturyStart michael@0: umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); michael@0: return gSystemDefaultCenturyStart; michael@0: } michael@0: michael@0: int32_t IslamicCalendar::defaultCenturyStartYear() const michael@0: { michael@0: // lazy-evaluate systemDefaultCenturyStartYear michael@0: umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); michael@0: return gSystemDefaultCenturyStartYear; michael@0: } michael@0: michael@0: michael@0: void U_CALLCONV michael@0: IslamicCalendar::initializeSystemDefaultCentury() michael@0: { michael@0: // initialize systemDefaultCentury and systemDefaultCenturyYear based michael@0: // on the current time. They'll be set to 80 years before michael@0: // the current time. michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: IslamicCalendar calendar(Locale("@calendar=islamic-civil"),status); michael@0: if (U_SUCCESS(status)) { michael@0: calendar.setTime(Calendar::getNow(), status); michael@0: calendar.add(UCAL_YEAR, -80, status); michael@0: michael@0: gSystemDefaultCenturyStart = calendar.getTime(status); michael@0: gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status); michael@0: } michael@0: // We have no recourse upon failure unless we want to propagate the failure michael@0: // out. michael@0: } michael@0: michael@0: michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IslamicCalendar) michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif michael@0: