michael@0: /* michael@0: * Copyright (C) 2003-2009, International Business Machines Corporation michael@0: * and others. All Rights Reserved. michael@0: ****************************************************************************** michael@0: * michael@0: * File INDIANCAL.CPP michael@0: ***************************************************************************** michael@0: */ michael@0: michael@0: #include "indiancal.h" michael@0: #include michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "mutex.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: michael@0: // Debugging michael@0: #ifdef U_DEBUG_INDIANCAL michael@0: #include michael@0: #include michael@0: michael@0: #endif michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: // Implementation of the IndianCalendar class michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Constructors... michael@0: //------------------------------------------------------------------------- michael@0: michael@0: michael@0: Calendar* IndianCalendar::clone() const { michael@0: return new IndianCalendar(*this); michael@0: } michael@0: michael@0: IndianCalendar::IndianCalendar(const Locale& aLocale, UErrorCode& success) michael@0: : Calendar(TimeZone::createDefault(), aLocale, success) michael@0: { michael@0: setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly. michael@0: } michael@0: michael@0: IndianCalendar::IndianCalendar(const IndianCalendar& other) : Calendar(other) { michael@0: } michael@0: michael@0: IndianCalendar::~IndianCalendar() michael@0: { michael@0: } michael@0: const char *IndianCalendar::getType() const { michael@0: return "indian"; michael@0: } michael@0: 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: { -5000000, -5000000, 5000000, 5000000}, // YEAR michael@0: { 0, 0, 11, 11}, // MONTH michael@0: { 1, 1, 52, 53}, // WEEK_OF_YEAR michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH michael@0: { 1, 1, 30, 31}, // DAY_OF_MONTH michael@0: { 1, 1, 365, 366}, // 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: { -5000000, -5000000, 5000000, 5000000}, // YEAR_WOY michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL michael@0: { -5000000, -5000000, 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: static const double JULIAN_EPOCH = 1721425.5; michael@0: static const int32_t INDIAN_ERA_START = 78; michael@0: static const int32_t INDIAN_YEAR_START = 80; michael@0: michael@0: int32_t IndianCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const { michael@0: return LIMITS[field][limitType]; michael@0: } michael@0: michael@0: /* michael@0: * Determine whether the given gregorian year is a Leap year michael@0: */ michael@0: static UBool isGregorianLeap(int32_t year) michael@0: { michael@0: return ((year % 4) == 0) && (!(((year % 100) == 0) && ((year % 400) != 0))); 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 eyear The year in Saka Era michael@0: * @param month The month(0-based) in Indian calendar michael@0: */ michael@0: int32_t IndianCalendar::handleGetMonthLength(int32_t eyear, int32_t month) const { michael@0: if (month < 0 || month > 11) { michael@0: eyear += ClockMath::floorDivide(month, 12, month); michael@0: } michael@0: michael@0: if (isGregorianLeap(eyear + INDIAN_ERA_START) && month == 0) { michael@0: return 31; michael@0: } michael@0: michael@0: if (month >= 1 && month <= 5) { michael@0: return 31; michael@0: } michael@0: michael@0: return 30; michael@0: } michael@0: michael@0: /* michael@0: * Return the number of days in the given Indian year michael@0: * michael@0: * @param eyear The year in Saka Era. michael@0: */ michael@0: int32_t IndianCalendar::handleGetYearLength(int32_t eyear) const { michael@0: return isGregorianLeap(eyear + INDIAN_ERA_START) ? 366 : 365; michael@0: } michael@0: /* michael@0: * Returns the Julian Day corresponding to gregorian date michael@0: * michael@0: * @param year The Gregorian year michael@0: * @param month The month in Gregorian Year michael@0: * @param date The date in Gregorian day in month michael@0: */ michael@0: static double gregorianToJD(int32_t year, int32_t month, int32_t date) { michael@0: double julianDay = (JULIAN_EPOCH - 1) + michael@0: (365 * (year - 1)) + michael@0: uprv_floor((year - 1) / 4) + michael@0: (-uprv_floor((year - 1) / 100)) + michael@0: uprv_floor((year - 1) / 400) + michael@0: uprv_floor((((367 * month) - 362) / 12) + michael@0: ((month <= 2) ? 0 : michael@0: (isGregorianLeap(year) ? -1 : -2) michael@0: ) + michael@0: date); michael@0: michael@0: return julianDay; michael@0: } michael@0: michael@0: /* michael@0: * Returns the Gregorian Date corresponding to a given Julian Day michael@0: * @param jd The Julian Day michael@0: */ michael@0: static int32_t* jdToGregorian(double jd, int32_t gregorianDate[3]) { michael@0: double wjd, depoch, quadricent, dqc, cent, dcent, quad, dquad, yindex, yearday, leapadj; michael@0: int32_t year, month, day; michael@0: wjd = uprv_floor(jd - 0.5) + 0.5; michael@0: depoch = wjd - JULIAN_EPOCH; michael@0: quadricent = uprv_floor(depoch / 146097); michael@0: dqc = (int32_t)uprv_floor(depoch) % 146097; michael@0: cent = uprv_floor(dqc / 36524); michael@0: dcent = (int32_t)uprv_floor(dqc) % 36524; michael@0: quad = uprv_floor(dcent / 1461); michael@0: dquad = (int32_t)uprv_floor(dcent) % 1461; michael@0: yindex = uprv_floor(dquad / 365); michael@0: year = (int32_t)((quadricent * 400) + (cent * 100) + (quad * 4) + yindex); michael@0: if (!((cent == 4) || (yindex == 4))) { michael@0: year++; michael@0: } michael@0: yearday = wjd - gregorianToJD(year, 1, 1); michael@0: leapadj = ((wjd < gregorianToJD(year, 3, 1)) ? 0 michael@0: : michael@0: (isGregorianLeap(year) ? 1 : 2) michael@0: ); michael@0: month = (int32_t)uprv_floor((((yearday + leapadj) * 12) + 373) / 367); michael@0: day = (int32_t)(wjd - gregorianToJD(year, month, 1)) + 1; michael@0: michael@0: gregorianDate[0] = year; michael@0: gregorianDate[1] = month; michael@0: gregorianDate[2] = day; michael@0: michael@0: return gregorianDate; michael@0: } michael@0: michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Functions for converting from field values to milliseconds.... michael@0: //------------------------------------------------------------------------- michael@0: static double IndianToJD(int32_t year, int32_t month, int32_t date) { michael@0: int32_t leapMonth, gyear, m; michael@0: double start, jd; michael@0: michael@0: gyear = year + INDIAN_ERA_START; michael@0: michael@0: michael@0: if(isGregorianLeap(gyear)) { michael@0: leapMonth = 31; michael@0: start = gregorianToJD(gyear, 3, 21); michael@0: } michael@0: else { michael@0: leapMonth = 30; michael@0: start = gregorianToJD(gyear, 3, 22); michael@0: } michael@0: michael@0: if (month == 1) { michael@0: jd = start + (date - 1); michael@0: } else { michael@0: jd = start + leapMonth; michael@0: m = month - 2; michael@0: michael@0: //m = Math.min(m, 5); michael@0: if (m > 5) { michael@0: m = 5; michael@0: } michael@0: michael@0: jd += m * 31; michael@0: michael@0: if (month >= 8) { michael@0: m = month - 7; michael@0: jd += m * 30; michael@0: } michael@0: jd += date - 1; michael@0: } michael@0: michael@0: return jd; michael@0: } michael@0: michael@0: /* michael@0: * Return JD of start of given month/year of Indian Calendar michael@0: * @param eyear The year in Indian Calendar measured from Saka Era (78 AD). michael@0: * @param month The month in Indian calendar michael@0: */ michael@0: int32_t IndianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /* useMonth */ ) const { michael@0: michael@0: //month is 0 based; converting it to 1-based michael@0: int32_t imonth; michael@0: michael@0: // If the month is out of range, adjust it into range, and adjust the extended eyar accordingly michael@0: if (month < 0 || month > 11) { michael@0: eyear += (int32_t)ClockMath::floorDivide(month, 12, month); michael@0: } michael@0: michael@0: if(month == 12){ michael@0: imonth = 1; michael@0: } else { michael@0: imonth = month + 1; michael@0: } michael@0: michael@0: double jd = IndianToJD(eyear ,imonth, 1); michael@0: michael@0: return (int32_t)jd; michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Functions for converting from milliseconds to field values michael@0: //------------------------------------------------------------------------- michael@0: michael@0: int32_t IndianCalendar::handleGetExtendedYear() { michael@0: int32_t year; michael@0: 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: michael@0: return year; michael@0: } michael@0: michael@0: /* michael@0: * Override Calendar to compute several fields specific to the Indian michael@0: * calendar system. These are: michael@0: * michael@0: *
  • ERA michael@0: *
  • YEAR michael@0: *
  • MONTH michael@0: *
  • DAY_OF_MONTH 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: */ michael@0: void IndianCalendar::handleComputeFields(int32_t julianDay, UErrorCode& /* status */) { michael@0: double jdAtStartOfGregYear; michael@0: int32_t leapMonth, IndianYear, yday, IndianMonth, IndianDayOfMonth, mday; michael@0: int32_t gregorianYear; // Stores gregorian date corresponding to Julian day; michael@0: int32_t gd[3]; michael@0: michael@0: gregorianYear = jdToGregorian(julianDay, gd)[0]; // Gregorian date for Julian day michael@0: IndianYear = gregorianYear - INDIAN_ERA_START; // Year in Saka era michael@0: jdAtStartOfGregYear = gregorianToJD(gregorianYear, 1, 1); // JD at start of Gregorian year michael@0: yday = (int32_t)(julianDay - jdAtStartOfGregYear); // Day number in Gregorian year (starting from 0) michael@0: michael@0: if (yday < INDIAN_YEAR_START) { michael@0: // Day is at the end of the preceding Saka year michael@0: IndianYear -= 1; michael@0: leapMonth = isGregorianLeap(gregorianYear - 1) ? 31 : 30; // Days in leapMonth this year, previous Gregorian year michael@0: yday += leapMonth + (31 * 5) + (30 * 3) + 10; michael@0: } else { michael@0: leapMonth = isGregorianLeap(gregorianYear) ? 31 : 30; // Days in leapMonth this year michael@0: yday -= INDIAN_YEAR_START; michael@0: } michael@0: michael@0: if (yday < leapMonth) { michael@0: IndianMonth = 0; michael@0: IndianDayOfMonth = yday + 1; michael@0: } else { michael@0: mday = yday - leapMonth; michael@0: if (mday < (31 * 5)) { michael@0: IndianMonth = (int32_t)uprv_floor(mday / 31) + 1; michael@0: IndianDayOfMonth = (mday % 31) + 1; michael@0: } else { michael@0: mday -= 31 * 5; michael@0: IndianMonth = (int32_t)uprv_floor(mday / 30) + 6; michael@0: IndianDayOfMonth = (mday % 30) + 1; michael@0: } michael@0: } michael@0: michael@0: internalSet(UCAL_ERA, 0); michael@0: internalSet(UCAL_EXTENDED_YEAR, IndianYear); michael@0: internalSet(UCAL_YEAR, IndianYear); michael@0: internalSet(UCAL_MONTH, IndianMonth); michael@0: internalSet(UCAL_DAY_OF_MONTH, IndianDayOfMonth); michael@0: internalSet(UCAL_DAY_OF_YEAR, yday + 1); // yday is 0-based michael@0: } michael@0: michael@0: UBool michael@0: IndianCalendar::inDaylightTime(UErrorCode& status) const michael@0: { michael@0: // copied from GregorianCalendar michael@0: if (U_FAILURE(status) || !getTimeZone().useDaylightTime()) { michael@0: return FALSE; michael@0: } michael@0: michael@0: // Force an update of the state of the Calendar. michael@0: ((IndianCalendar*)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: // default century michael@0: const UDate IndianCalendar::fgSystemDefaultCentury = DBL_MIN; michael@0: const int32_t IndianCalendar::fgSystemDefaultCenturyYear = -1; michael@0: michael@0: UDate IndianCalendar::fgSystemDefaultCenturyStart = DBL_MIN; michael@0: int32_t IndianCalendar::fgSystemDefaultCenturyStartYear = -1; michael@0: michael@0: michael@0: UBool IndianCalendar::haveDefaultCentury() const michael@0: { michael@0: return TRUE; michael@0: } michael@0: michael@0: UDate IndianCalendar::defaultCenturyStart() const michael@0: { michael@0: return internalGetDefaultCenturyStart(); michael@0: } michael@0: michael@0: int32_t IndianCalendar::defaultCenturyStartYear() const michael@0: { michael@0: return internalGetDefaultCenturyStartYear(); michael@0: } michael@0: michael@0: UDate michael@0: IndianCalendar::internalGetDefaultCenturyStart() const michael@0: { michael@0: // lazy-evaluate systemDefaultCenturyStart michael@0: UBool needsUpdate; michael@0: { michael@0: Mutex m; michael@0: needsUpdate = (fgSystemDefaultCenturyStart == fgSystemDefaultCentury); michael@0: } michael@0: michael@0: if (needsUpdate) { michael@0: initializeSystemDefaultCentury(); michael@0: } michael@0: michael@0: // use defaultCenturyStart unless it's the flag value; michael@0: // then use systemDefaultCenturyStart michael@0: michael@0: return fgSystemDefaultCenturyStart; michael@0: } michael@0: michael@0: int32_t michael@0: IndianCalendar::internalGetDefaultCenturyStartYear() const michael@0: { michael@0: // lazy-evaluate systemDefaultCenturyStartYear michael@0: UBool needsUpdate; michael@0: { michael@0: Mutex m; michael@0: michael@0: needsUpdate = (fgSystemDefaultCenturyStart == fgSystemDefaultCentury); michael@0: } michael@0: michael@0: if (needsUpdate) { michael@0: initializeSystemDefaultCentury(); michael@0: } michael@0: michael@0: // use defaultCenturyStart unless it's the flag value; michael@0: // then use systemDefaultCenturyStartYear michael@0: michael@0: return fgSystemDefaultCenturyStartYear; michael@0: } michael@0: michael@0: void michael@0: IndianCalendar::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: // No point in locking as it should be idempotent. michael@0: if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury) { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: michael@0: IndianCalendar calendar(Locale("@calendar=Indian"),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: UDate newStart = calendar.getTime(status); michael@0: int32_t newYear = calendar.get(UCAL_YEAR, status); michael@0: michael@0: { michael@0: Mutex m; michael@0: michael@0: fgSystemDefaultCenturyStart = newStart; michael@0: fgSystemDefaultCenturyStartYear = newYear; michael@0: } michael@0: } 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: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IndianCalendar) michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif michael@0: