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 PERSNCAL.CPP michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 9/23/2003 mehran posted to icu-design michael@0: * 10/1/2012 roozbeh Fixed algorithm and heavily refactored and rewrote michael@0: * based on the implementation of Gregorian michael@0: ***************************************************************************** michael@0: */ michael@0: michael@0: #include "persncal.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "umutex.h" michael@0: #include "gregoimp.h" // Math michael@0: #include michael@0: michael@0: static const int16_t kPersianNumDays[] michael@0: = {0,31,62,93,124,155,186,216,246,276,306,336}; // 0-based, for day-in-year michael@0: static const int8_t kPersianMonthLength[] michael@0: = {31,31,31,31,31,31,30,30,30,30,30,29}; // 0-based michael@0: static const int8_t kPersianLeapMonthLength[] michael@0: = {31,31,31,31,31,31,30,30,30,30,30,30}; // 0-based michael@0: michael@0: static const int32_t kPersianCalendarLimits[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, 29, 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: U_NAMESPACE_BEGIN michael@0: michael@0: static const int32_t PERSIAN_EPOCH = 1948320; michael@0: michael@0: // Implementation of the PersianCalendar class michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Constructors... michael@0: //------------------------------------------------------------------------- michael@0: michael@0: const char *PersianCalendar::getType() const { michael@0: return "persian"; michael@0: } michael@0: michael@0: Calendar* PersianCalendar::clone() const { michael@0: return new PersianCalendar(*this); michael@0: } michael@0: michael@0: PersianCalendar::PersianCalendar(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: PersianCalendar::PersianCalendar(const PersianCalendar& other) : Calendar(other) { michael@0: } michael@0: michael@0: PersianCalendar::~PersianCalendar() michael@0: { michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Minimum / Maximum access functions michael@0: //------------------------------------------------------------------------- michael@0: michael@0: michael@0: int32_t PersianCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const { michael@0: return kPersianCalendarLimits[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 Persian calendar michael@0: */ michael@0: UBool PersianCalendar::isLeapYear(int32_t year) michael@0: { michael@0: int32_t remainder; michael@0: ClockMath::floorDivide(25 * year + 11, 33, remainder); michael@0: return (remainder < 8); michael@0: } michael@0: michael@0: /** michael@0: * Return the day # on which the given year starts. Days are counted michael@0: * from the Persian epoch, origin 0. michael@0: */ michael@0: int32_t PersianCalendar::yearStart(int32_t year) { michael@0: return handleComputeMonthStart(year,0,FALSE); michael@0: } michael@0: michael@0: /** michael@0: * Return the day # on which the given month starts. Days are counted michael@0: * from the Persian epoch, origin 0. michael@0: * michael@0: * @param year The Persian year michael@0: * @param year The Persian month, 0-based michael@0: */ michael@0: int32_t PersianCalendar::monthStart(int32_t year, int32_t month) const { michael@0: return handleComputeMonthStart(year,month,TRUE); 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 Persian year michael@0: * @param year The Persian month, 0-based michael@0: */ michael@0: int32_t PersianCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const { michael@0: // If the month is out of range, adjust it into range, and michael@0: // modify the extended year value accordingly. michael@0: if (month < 0 || month > 11) { michael@0: extendedYear += ClockMath::floorDivide(month, 12, month); michael@0: } michael@0: michael@0: return isLeapYear(extendedYear) ? kPersianLeapMonthLength[month] : kPersianMonthLength[month]; michael@0: } michael@0: michael@0: /** michael@0: * Return the number of days in the given Persian year michael@0: */ michael@0: int32_t PersianCalendar::handleGetYearLength(int32_t extendedYear) const { michael@0: return isLeapYear(extendedYear) ? 366 : 365; 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: int32_t PersianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /*useMonth*/) const { michael@0: // If the month is out of range, adjust it into range, and michael@0: // modify the extended year value accordingly. michael@0: if (month < 0 || month > 11) { michael@0: eyear += ClockMath::floorDivide(month, 12, month); michael@0: } michael@0: michael@0: int32_t julianDay = PERSIAN_EPOCH - 1 + 365 * (eyear - 1) + ClockMath::floorDivide(8 * eyear + 21, 33); michael@0: michael@0: if (month != 0) { michael@0: julianDay += kPersianNumDays[month]; michael@0: } michael@0: michael@0: return julianDay; michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Functions for converting from milliseconds to field values michael@0: //------------------------------------------------------------------------- michael@0: michael@0: int32_t PersianCalendar::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 Persian michael@0: * calendar system. These are: michael@0: * michael@0: * michael@0: * michael@0: * The DAY_OF_WEEK and DOW_LOCAL fields are already set when this michael@0: * method is called. michael@0: */ michael@0: void PersianCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/) { michael@0: int32_t year, month, dayOfMonth, dayOfYear; michael@0: michael@0: int32_t daysSinceEpoch = julianDay - PERSIAN_EPOCH; michael@0: year = 1 + ClockMath::floorDivide(33 * daysSinceEpoch + 3, 12053); michael@0: michael@0: int32_t farvardin1 = 365 * (year - 1) + ClockMath::floorDivide(8 * year + 21, 33); michael@0: dayOfYear = (daysSinceEpoch - farvardin1); // 0-based michael@0: if (dayOfYear < 216) { // Compute 0-based month michael@0: month = dayOfYear / 31; michael@0: } else { michael@0: month = (dayOfYear - 6) / 30; michael@0: } michael@0: dayOfMonth = dayOfYear - kPersianNumDays[month] + 1; michael@0: ++dayOfYear; // Make it 1-based now 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: PersianCalendar::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: // Force an update of the state of the Calendar. michael@0: ((PersianCalendar*)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: 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: UBool PersianCalendar::haveDefaultCentury() const michael@0: { michael@0: return TRUE; michael@0: } michael@0: michael@0: static void U_CALLCONV initializeSystemDefaultCentury() { 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: PersianCalendar calendar(Locale("@calendar=persian"),status); michael@0: if (U_SUCCESS(status)) michael@0: { 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: UDate PersianCalendar::defaultCenturyStart() const { michael@0: // lazy-evaluate systemDefaultCenturyStart michael@0: umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); michael@0: return gSystemDefaultCenturyStart; michael@0: } michael@0: michael@0: int32_t PersianCalendar::defaultCenturyStartYear() const { michael@0: // lazy-evaluate systemDefaultCenturyStartYear michael@0: umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); michael@0: return gSystemDefaultCenturyStartYear; michael@0: } michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(PersianCalendar) michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif michael@0: