// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2/*3*******************************************************************************4* Copyright (C) 2003 - 2009, International Business Machines Corporation and *5* others. All Rights Reserved. *6*******************************************************************************7*/89#include "unicode/utypes.h"1011#if !UCONFIG_NO_FORMATTING1213#include "cecal.h"14#include "gregoimp.h" //Math1516U_NAMESPACE_BEGIN1718static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = {19// Minimum Greatest Least Maximum20// Minimum Maximum21{ 0, 0, 1, 1}, // ERA22{ 1, 1, 5000000, 5000000}, // YEAR23{ 0, 0, 12, 12}, // MONTH24{ 1, 1, 52, 53}, // WEEK_OF_YEAR25{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH26{ 1, 1, 5, 30}, // DAY_OF_MONTH27{ 1, 1, 365, 366}, // DAY_OF_YEAR28{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK29{ -1, -1, 1, 5}, // DAY_OF_WEEK_IN_MONTH30{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM31{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR32{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY33{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE34{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND35{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND36{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET37{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET38{ -5000000, -5000000, 5000000, 5000000}, // YEAR_WOY39{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL40{ -5000000, -5000000, 5000000, 5000000}, // EXTENDED_YEAR41{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY42{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY43{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH44};4546//-------------------------------------------------------------------------47// Constructors...48//-------------------------------------------------------------------------4950CECalendar::CECalendar(const Locale& aLocale, UErrorCode& success)51: Calendar(TimeZone::forLocaleOrDefault(aLocale), aLocale, success)52{53setTimeInMillis(getNow(), success);54}5556CECalendar::CECalendar (const CECalendar& other)57: Calendar(other)58{59}6061CECalendar::~CECalendar()62{63}6465CECalendar&66CECalendar::operator=(const CECalendar& right)67{68Calendar::operator=(right);69return *this;70}7172//-------------------------------------------------------------------------73// Calendar framework74//-------------------------------------------------------------------------7576int32_t77CECalendar::handleComputeMonthStart(int32_t eyear,int32_t emonth, UBool /*useMonth*/) const78{79return ceToJD(eyear, emonth, 0, getJDEpochOffset());80}8182int32_t83CECalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const84{85return LIMITS[field][limitType];86}8788UBool89CECalendar::inDaylightTime(UErrorCode& status) const90{91if (U_FAILURE(status) || !getTimeZone().useDaylightTime()) {92return false;93}9495// Force an update of the state of the Calendar.96((CECalendar*)this)->complete(status); // cast away const9798return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : false);99}100101UBool102CECalendar::haveDefaultCentury() const103{104return true;105}106107//-------------------------------------------------------------------------108// Calendar system Conversion methods...109//-------------------------------------------------------------------------110int32_t111CECalendar::ceToJD(int32_t year, int32_t month, int32_t date, int32_t jdEpochOffset)112{113// handle month > 12, < 0 (e.g. from add/set)114if ( month >= 0 ) {115year += month/13;116month %= 13;117} else {118++month;119year += month/13 - 1;120month = month%13 + 12;121}122return (int32_t) (123jdEpochOffset // difference from Julian epoch to 1,1,1124+ 365 * year // number of days from years125+ ClockMath::floorDivide(year, 4) // extra day of leap year126+ 30 * month // number of days from months (months are 0-based)127+ date - 1 // number of days for present month (1 based)128);129}130131void132CECalendar::jdToCE(int32_t julianDay, int32_t jdEpochOffset, int32_t& year, int32_t& month, int32_t& day)133{134int32_t c4; // number of 4 year cycle (1461 days)135int32_t r4; // remainder of 4 year cycle, always positive136137c4 = ClockMath::floorDivide(julianDay - jdEpochOffset, 1461, &r4);138139year = 4 * c4 + (r4/365 - r4/1460); // 4 * <number of 4year cycle> + <years within the last cycle>140141int32_t doy = (r4 == 1460) ? 365 : (r4 % 365); // days in present year142143month = doy / 30; // 30 -> Coptic/Ethiopic month length up to 12th month144day = (doy % 30) + 1; // 1-based days in a month145}146147U_NAMESPACE_END148149#endif /* #if !UCONFIG_NO_FORMATTING */150//eof151152153