michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2003 - 2009, International Business Machines Corporation and * michael@0: * others. All Rights Reserved. * michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "cecal.h" michael@0: #include "gregoimp.h" //Math michael@0: michael@0: U_NAMESPACE_BEGIN 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, 1, 1}, // ERA michael@0: { 1, 1, 5000000, 5000000}, // YEAR michael@0: { 0, 0, 12, 12}, // 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, 5, 30}, // 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, 1, 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: //------------------------------------------------------------------------- michael@0: // Constructors... michael@0: //------------------------------------------------------------------------- michael@0: michael@0: CECalendar::CECalendar(const Locale& aLocale, UErrorCode& success) michael@0: : Calendar(TimeZone::createDefault(), aLocale, success) michael@0: { michael@0: setTimeInMillis(getNow(), success); michael@0: } michael@0: michael@0: CECalendar::CECalendar (const CECalendar& other) michael@0: : Calendar(other) michael@0: { michael@0: } michael@0: michael@0: CECalendar::~CECalendar() michael@0: { michael@0: } michael@0: michael@0: CECalendar& michael@0: CECalendar::operator=(const CECalendar& right) michael@0: { michael@0: Calendar::operator=(right); michael@0: return *this; michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Calendar framework michael@0: //------------------------------------------------------------------------- michael@0: michael@0: int32_t michael@0: CECalendar::handleComputeMonthStart(int32_t eyear,int32_t emonth, UBool /*useMonth*/) const michael@0: { michael@0: return ceToJD(eyear, emonth, 0, getJDEpochOffset()); michael@0: } michael@0: michael@0: int32_t michael@0: CECalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const michael@0: { michael@0: return LIMITS[field][limitType]; michael@0: } michael@0: michael@0: UBool michael@0: CECalendar::inDaylightTime(UErrorCode& status) const michael@0: { 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: ((CECalendar*)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: UBool michael@0: CECalendar::haveDefaultCentury() const michael@0: { michael@0: return TRUE; michael@0: } michael@0: michael@0: //------------------------------------------------------------------------- michael@0: // Calendar system Conversion methods... michael@0: //------------------------------------------------------------------------- michael@0: int32_t michael@0: CECalendar::ceToJD(int32_t year, int32_t month, int32_t date, int32_t jdEpochOffset) michael@0: { michael@0: // handle month > 12, < 0 (e.g. from add/set) michael@0: if ( month >= 0 ) { michael@0: year += month/13; michael@0: month %= 13; michael@0: } else { michael@0: ++month; michael@0: year += month/13 - 1; michael@0: month = month%13 + 12; michael@0: } michael@0: return (int32_t) ( michael@0: jdEpochOffset // difference from Julian epoch to 1,1,1 michael@0: + 365 * year // number of days from years michael@0: + ClockMath::floorDivide(year, 4) // extra day of leap year michael@0: + 30 * month // number of days from months (months are 0-based) michael@0: + date - 1 // number of days for present month (1 based) michael@0: ); michael@0: } michael@0: michael@0: void michael@0: CECalendar::jdToCE(int32_t julianDay, int32_t jdEpochOffset, int32_t& year, int32_t& month, int32_t& day) michael@0: { michael@0: int32_t c4; // number of 4 year cycle (1461 days) michael@0: int32_t r4; // remainder of 4 year cycle, always positive michael@0: michael@0: c4 = ClockMath::floorDivide(julianDay - jdEpochOffset, 1461, r4); michael@0: michael@0: year = 4 * c4 + (r4/365 - r4/1460); // 4 * + michael@0: michael@0: int32_t doy = (r4 == 1460) ? 365 : (r4 % 365); // days in present year michael@0: michael@0: month = doy / 30; // 30 -> Coptic/Ethiopic month length up to 12th month michael@0: day = (doy % 30) + 1; // 1-based days in a month michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: //eof