michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 1997-2013, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * michael@0: * File GREGOCAL.CPP michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 02/05/97 clhuang Creation. michael@0: * 03/28/97 aliu Made highly questionable fix to computeFields to michael@0: * handle DST correctly. michael@0: * 04/22/97 aliu Cleaned up code drastically. Added monthLength(). michael@0: * Finished unimplemented parts of computeTime() for michael@0: * week-based date determination. Removed quetionable michael@0: * fix and wrote correct fix for computeFields() and michael@0: * daylight time handling. Rewrote inDaylightTime() michael@0: * and computeFields() to handle sensitive Daylight to michael@0: * Standard time transitions correctly. michael@0: * 05/08/97 aliu Added code review changes. Fixed isLeapYear() to michael@0: * not cutover. michael@0: * 08/12/97 aliu Added equivalentTo. Misc other fixes. Updated michael@0: * add() from Java source. michael@0: * 07/28/98 stephen Sync up with JDK 1.2 michael@0: * 09/14/98 stephen Changed type of kOneDay, kOneWeek to double. michael@0: * Fixed bug in roll() michael@0: * 10/15/99 aliu Fixed j31, incorrect WEEK_OF_YEAR computation. michael@0: * 10/15/99 aliu Fixed j32, cannot set date to Feb 29 2000 AD. michael@0: * {JDK bug 4210209 4209272} michael@0: * 11/15/99 weiv Added YEAR_WOY and DOW_LOCAL computation michael@0: * to timeToFields method, updated kMinValues, kMaxValues & kLeastMaxValues michael@0: * 12/09/99 aliu Fixed j81, calculation errors and roll bugs michael@0: * in year of cutover. michael@0: * 01/24/2000 aliu Revised computeJulianDay for YEAR YEAR_WOY WOY. michael@0: ******************************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/gregocal.h" michael@0: #include "gregoimp.h" michael@0: #include "umutex.h" michael@0: #include "uassert.h" michael@0: michael@0: // ***************************************************************************** michael@0: // class GregorianCalendar michael@0: // ***************************************************************************** michael@0: michael@0: /** michael@0: * Note that the Julian date used here is not a true Julian date, since michael@0: * it is measured from midnight, not noon. This value is the Julian michael@0: * day number of January 1, 1970 (Gregorian calendar) at noon UTC. [LIU] michael@0: */ michael@0: michael@0: static const int16_t kNumDays[] michael@0: = {0,31,59,90,120,151,181,212,243,273,304,334}; // 0-based, for day-in-year michael@0: static const int16_t kLeapNumDays[] michael@0: = {0,31,60,91,121,152,182,213,244,274,305,335}; // 0-based, for day-in-year michael@0: static const int8_t kMonthLength[] michael@0: = {31,28,31,30,31,30,31,31,30,31,30,31}; // 0-based michael@0: static const int8_t kLeapMonthLength[] michael@0: = {31,29,31,30,31,30,31,31,30,31,30,31}; // 0-based michael@0: michael@0: // setTimeInMillis() limits the Julian day range to +/-7F000000. michael@0: // This would seem to limit the year range to: michael@0: // ms=+183882168921600000 jd=7f000000 December 20, 5828963 AD michael@0: // ms=-184303902528000000 jd=81000000 September 20, 5838270 BC michael@0: // HOWEVER, CalendarRegressionTest/Test4167060 shows that the actual michael@0: // range limit on the year field is smaller (~ +/-140000). [alan 3.0] michael@0: michael@0: static const int32_t kGregorianCalendarLimits[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, 140742, 144683}, // 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, 28, 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, 4, 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: { -140742, -140742, 140742, 144683}, // YEAR_WOY michael@0: {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL michael@0: { -140742, -140742, 140742, 144683}, // 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: *
michael@0: *                            Greatest       Least 
michael@0: * Field name        Minimum   Minimum     Maximum     Maximum
michael@0: * ----------        -------   -------     -------     -------
michael@0: * ERA                     0         0           1           1
michael@0: * YEAR                    1         1      140742      144683
michael@0: * MONTH                   0         0          11          11
michael@0: * WEEK_OF_YEAR            1         1          52          53
michael@0: * WEEK_OF_MONTH           0         0           4           6
michael@0: * DAY_OF_MONTH            1         1          28          31
michael@0: * DAY_OF_YEAR             1         1         365         366
michael@0: * DAY_OF_WEEK             1         1           7           7
michael@0: * DAY_OF_WEEK_IN_MONTH   -1        -1           4           5
michael@0: * AM_PM                   0         0           1           1
michael@0: * HOUR                    0         0          11          11
michael@0: * HOUR_OF_DAY             0         0          23          23
michael@0: * MINUTE                  0         0          59          59
michael@0: * SECOND                  0         0          59          59
michael@0: * MILLISECOND             0         0         999         999
michael@0: * ZONE_OFFSET           -12*      -12*         12*         12*
michael@0: * DST_OFFSET              0         0           1*          1*
michael@0: * YEAR_WOY                1         1      140742      144683
michael@0: * DOW_LOCAL               1         1           7           7
michael@0: * 
michael@0: * (*) In units of one-hour michael@0: */ michael@0: michael@0: #if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL) michael@0: #include michael@0: #endif michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(GregorianCalendar) michael@0: michael@0: // 00:00:00 UTC, October 15, 1582, expressed in ms from the epoch. michael@0: // Note that only Italy and other Catholic countries actually michael@0: // observed this cutover. Most other countries followed in michael@0: // the next few centuries, some as late as 1928. [LIU] michael@0: // in Java, -12219292800000L michael@0: //const UDate GregorianCalendar::kPapalCutover = -12219292800000L; michael@0: static const uint32_t kCutoverJulianDay = 2299161; michael@0: static const UDate kPapalCutover = (2299161.0 - kEpochStartAsJulianDay) * U_MILLIS_PER_DAY; michael@0: //static const UDate kPapalCutoverJulian = (2299161.0 - kEpochStartAsJulianDay); michael@0: michael@0: // ------------------------------------- michael@0: michael@0: GregorianCalendar::GregorianCalendar(UErrorCode& status) michael@0: : Calendar(status), michael@0: fGregorianCutover(kPapalCutover), michael@0: fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), michael@0: fIsGregorian(TRUE), fInvertGregorian(FALSE) michael@0: { michael@0: setTimeInMillis(getNow(), status); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: GregorianCalendar::GregorianCalendar(TimeZone* zone, UErrorCode& status) michael@0: : Calendar(zone, Locale::getDefault(), status), michael@0: fGregorianCutover(kPapalCutover), michael@0: fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), michael@0: fIsGregorian(TRUE), fInvertGregorian(FALSE) michael@0: { michael@0: setTimeInMillis(getNow(), status); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: GregorianCalendar::GregorianCalendar(const TimeZone& zone, UErrorCode& status) michael@0: : Calendar(zone, Locale::getDefault(), status), michael@0: fGregorianCutover(kPapalCutover), michael@0: fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), michael@0: fIsGregorian(TRUE), fInvertGregorian(FALSE) michael@0: { michael@0: setTimeInMillis(getNow(), status); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: GregorianCalendar::GregorianCalendar(const Locale& aLocale, UErrorCode& status) michael@0: : Calendar(TimeZone::createDefault(), aLocale, status), michael@0: fGregorianCutover(kPapalCutover), michael@0: fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), michael@0: fIsGregorian(TRUE), fInvertGregorian(FALSE) michael@0: { michael@0: setTimeInMillis(getNow(), status); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: GregorianCalendar::GregorianCalendar(TimeZone* zone, const Locale& aLocale, michael@0: UErrorCode& status) michael@0: : Calendar(zone, aLocale, status), michael@0: fGregorianCutover(kPapalCutover), michael@0: fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), michael@0: fIsGregorian(TRUE), fInvertGregorian(FALSE) michael@0: { michael@0: setTimeInMillis(getNow(), status); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: GregorianCalendar::GregorianCalendar(const TimeZone& zone, const Locale& aLocale, michael@0: UErrorCode& status) michael@0: : Calendar(zone, aLocale, status), michael@0: fGregorianCutover(kPapalCutover), michael@0: fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), michael@0: fIsGregorian(TRUE), fInvertGregorian(FALSE) michael@0: { michael@0: setTimeInMillis(getNow(), status); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: GregorianCalendar::GregorianCalendar(int32_t year, int32_t month, int32_t date, michael@0: UErrorCode& status) michael@0: : Calendar(TimeZone::createDefault(), Locale::getDefault(), status), michael@0: fGregorianCutover(kPapalCutover), michael@0: fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), michael@0: fIsGregorian(TRUE), fInvertGregorian(FALSE) michael@0: { michael@0: set(UCAL_ERA, AD); michael@0: set(UCAL_YEAR, year); michael@0: set(UCAL_MONTH, month); michael@0: set(UCAL_DATE, date); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: GregorianCalendar::GregorianCalendar(int32_t year, int32_t month, int32_t date, michael@0: int32_t hour, int32_t minute, UErrorCode& status) michael@0: : Calendar(TimeZone::createDefault(), Locale::getDefault(), status), michael@0: fGregorianCutover(kPapalCutover), michael@0: fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), michael@0: fIsGregorian(TRUE), fInvertGregorian(FALSE) michael@0: { michael@0: set(UCAL_ERA, AD); michael@0: set(UCAL_YEAR, year); michael@0: set(UCAL_MONTH, month); michael@0: set(UCAL_DATE, date); michael@0: set(UCAL_HOUR_OF_DAY, hour); michael@0: set(UCAL_MINUTE, minute); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: GregorianCalendar::GregorianCalendar(int32_t year, int32_t month, int32_t date, michael@0: int32_t hour, int32_t minute, int32_t second, michael@0: UErrorCode& status) michael@0: : Calendar(TimeZone::createDefault(), Locale::getDefault(), status), michael@0: fGregorianCutover(kPapalCutover), michael@0: fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(1582), michael@0: fIsGregorian(TRUE), fInvertGregorian(FALSE) michael@0: { michael@0: set(UCAL_ERA, AD); michael@0: set(UCAL_YEAR, year); michael@0: set(UCAL_MONTH, month); michael@0: set(UCAL_DATE, date); michael@0: set(UCAL_HOUR_OF_DAY, hour); michael@0: set(UCAL_MINUTE, minute); michael@0: set(UCAL_SECOND, second); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: GregorianCalendar::~GregorianCalendar() michael@0: { michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: GregorianCalendar::GregorianCalendar(const GregorianCalendar &source) michael@0: : Calendar(source), michael@0: fGregorianCutover(source.fGregorianCutover), michael@0: fCutoverJulianDay(source.fCutoverJulianDay), fNormalizedGregorianCutover(source.fNormalizedGregorianCutover), fGregorianCutoverYear(source.fGregorianCutoverYear), michael@0: fIsGregorian(source.fIsGregorian), fInvertGregorian(source.fInvertGregorian) michael@0: { michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: Calendar* GregorianCalendar::clone() const michael@0: { michael@0: return new GregorianCalendar(*this); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: GregorianCalendar & michael@0: GregorianCalendar::operator=(const GregorianCalendar &right) michael@0: { michael@0: if (this != &right) michael@0: { michael@0: Calendar::operator=(right); michael@0: fGregorianCutover = right.fGregorianCutover; michael@0: fNormalizedGregorianCutover = right.fNormalizedGregorianCutover; michael@0: fGregorianCutoverYear = right.fGregorianCutoverYear; michael@0: fCutoverJulianDay = right.fCutoverJulianDay; michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: UBool GregorianCalendar::isEquivalentTo(const Calendar& other) const michael@0: { michael@0: // Calendar override. michael@0: return Calendar::isEquivalentTo(other) && michael@0: fGregorianCutover == ((GregorianCalendar*)&other)->fGregorianCutover; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: void michael@0: GregorianCalendar::setGregorianChange(UDate date, UErrorCode& status) michael@0: { michael@0: if (U_FAILURE(status)) michael@0: return; michael@0: michael@0: fGregorianCutover = date; michael@0: michael@0: // Precompute two internal variables which we use to do the actual michael@0: // cutover computations. These are the normalized cutover, which is the michael@0: // midnight at or before the cutover, and the cutover year. The michael@0: // normalized cutover is in pure date milliseconds; it contains no time michael@0: // of day or timezone component, and it used to compare against other michael@0: // pure date values. michael@0: int32_t cutoverDay = (int32_t)ClockMath::floorDivide(fGregorianCutover, (double)kOneDay); michael@0: fNormalizedGregorianCutover = cutoverDay * kOneDay; michael@0: michael@0: // Handle the rare case of numeric overflow. If the user specifies a michael@0: // change of UDate(Long.MIN_VALUE), in order to get a pure Gregorian michael@0: // calendar, then the epoch day is -106751991168, which when multiplied michael@0: // by ONE_DAY gives 9223372036794351616 -- the negative value is too michael@0: // large for 64 bits, and overflows into a positive value. We correct michael@0: // this by using the next day, which for all intents is semantically michael@0: // equivalent. michael@0: if (cutoverDay < 0 && fNormalizedGregorianCutover > 0) { michael@0: fNormalizedGregorianCutover = (cutoverDay + 1) * kOneDay; michael@0: } michael@0: michael@0: // Normalize the year so BC values are represented as 0 and negative michael@0: // values. michael@0: GregorianCalendar *cal = new GregorianCalendar(getTimeZone(), status); michael@0: /* test for NULL */ michael@0: if (cal == 0) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: if(U_FAILURE(status)) michael@0: return; michael@0: cal->setTime(date, status); michael@0: fGregorianCutoverYear = cal->get(UCAL_YEAR, status); michael@0: if (cal->get(UCAL_ERA, status) == BC) michael@0: fGregorianCutoverYear = 1 - fGregorianCutoverYear; michael@0: fCutoverJulianDay = cutoverDay; michael@0: delete cal; michael@0: } michael@0: michael@0: michael@0: void GregorianCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) { michael@0: int32_t eyear, month, dayOfMonth, dayOfYear, unusedRemainder; michael@0: michael@0: michael@0: if(U_FAILURE(status)) { michael@0: return; michael@0: } michael@0: michael@0: #if defined (U_DEBUG_CAL) michael@0: fprintf(stderr, "%s:%d: jd%d- (greg's %d)- [cut=%d]\n", michael@0: __FILE__, __LINE__, julianDay, getGregorianDayOfYear(), fCutoverJulianDay); michael@0: #endif michael@0: michael@0: michael@0: if (julianDay >= fCutoverJulianDay) { michael@0: month = getGregorianMonth(); michael@0: dayOfMonth = getGregorianDayOfMonth(); michael@0: dayOfYear = getGregorianDayOfYear(); michael@0: eyear = getGregorianYear(); michael@0: } else { michael@0: // The Julian epoch day (not the same as Julian Day) michael@0: // is zero on Saturday December 30, 0 (Gregorian). michael@0: int32_t julianEpochDay = julianDay - (kJan1_1JulianDay - 2); michael@0: eyear = (int32_t) ClockMath::floorDivide((4.0*julianEpochDay) + 1464.0, (int32_t) 1461, unusedRemainder); michael@0: michael@0: // Compute the Julian calendar day number for January 1, eyear michael@0: int32_t january1 = 365*(eyear-1) + ClockMath::floorDivide(eyear-1, (int32_t)4); michael@0: dayOfYear = (julianEpochDay - january1); // 0-based michael@0: michael@0: // Julian leap years occurred historically every 4 years starting michael@0: // with 8 AD. Before 8 AD the spacing is irregular; every 3 years michael@0: // from 45 BC to 9 BC, and then none until 8 AD. However, we don't michael@0: // implement this historical detail; instead, we implement the michael@0: // computatinally cleaner proleptic calendar, which assumes michael@0: // consistent 4-year cycles throughout time. michael@0: UBool isLeap = ((eyear&0x3) == 0); // equiv. to (eyear%4 == 0) michael@0: michael@0: // Common Julian/Gregorian calculation michael@0: int32_t correction = 0; michael@0: int32_t march1 = isLeap ? 60 : 59; // zero-based DOY for March 1 michael@0: if (dayOfYear >= march1) { michael@0: correction = isLeap ? 1 : 2; michael@0: } michael@0: month = (12 * (dayOfYear + correction) + 6) / 367; // zero-based month michael@0: dayOfMonth = dayOfYear - (isLeap?kLeapNumDays[month]:kNumDays[month]) + 1; // one-based DOM michael@0: ++dayOfYear; michael@0: #if defined (U_DEBUG_CAL) michael@0: // fprintf(stderr, "%d - %d[%d] + 1\n", dayOfYear, isLeap?kLeapNumDays[month]:kNumDays[month], month ); michael@0: // fprintf(stderr, "%s:%d: greg's HCF %d -> %d/%d/%d not %d/%d/%d\n", michael@0: // __FILE__, __LINE__,julianDay, michael@0: // eyear,month,dayOfMonth, michael@0: // getGregorianYear(), getGregorianMonth(), getGregorianDayOfMonth() ); michael@0: fprintf(stderr, "%s:%d: doy %d (greg's %d)- [cut=%d]\n", michael@0: __FILE__, __LINE__, dayOfYear, getGregorianDayOfYear(), fCutoverJulianDay); michael@0: #endif michael@0: michael@0: } michael@0: michael@0: // [j81] if we are after the cutover in its year, shift the day of the year michael@0: if((eyear == fGregorianCutoverYear) && (julianDay >= fCutoverJulianDay)) { michael@0: //from handleComputeMonthStart michael@0: int32_t gregShift = Grego::gregorianShift(eyear); michael@0: #if defined (U_DEBUG_CAL) michael@0: fprintf(stderr, "%s:%d: gregorian shift %d ::: doy%d => %d [cut=%d]\n", michael@0: __FILE__, __LINE__,gregShift, dayOfYear, dayOfYear+gregShift, fCutoverJulianDay); michael@0: #endif michael@0: dayOfYear += gregShift; michael@0: } michael@0: michael@0: internalSet(UCAL_MONTH, month); michael@0: internalSet(UCAL_DAY_OF_MONTH, dayOfMonth); michael@0: internalSet(UCAL_DAY_OF_YEAR, dayOfYear); michael@0: internalSet(UCAL_EXTENDED_YEAR, eyear); michael@0: int32_t era = AD; michael@0: if (eyear < 1) { michael@0: era = BC; michael@0: eyear = 1 - eyear; michael@0: } michael@0: internalSet(UCAL_ERA, era); michael@0: internalSet(UCAL_YEAR, eyear); michael@0: } michael@0: michael@0: michael@0: // ------------------------------------- michael@0: michael@0: UDate michael@0: GregorianCalendar::getGregorianChange() const michael@0: { michael@0: return fGregorianCutover; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: UBool michael@0: GregorianCalendar::isLeapYear(int32_t year) const michael@0: { michael@0: // MSVC complains bitterly if we try to use Grego::isLeapYear here michael@0: // NOTE: year&0x3 == year%4 michael@0: return (year >= fGregorianCutoverYear ? michael@0: (((year&0x3) == 0) && ((year%100 != 0) || (year%400 == 0))) : // Gregorian michael@0: ((year&0x3) == 0)); // Julian michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: int32_t GregorianCalendar::handleComputeJulianDay(UCalendarDateFields bestField) michael@0: { michael@0: fInvertGregorian = FALSE; michael@0: michael@0: int32_t jd = Calendar::handleComputeJulianDay(bestField); michael@0: michael@0: if((bestField == UCAL_WEEK_OF_YEAR) && // if we are doing WOY calculations, we are counting relative to Jan 1 *julian* michael@0: (internalGet(UCAL_EXTENDED_YEAR)==fGregorianCutoverYear) && michael@0: jd >= fCutoverJulianDay) { michael@0: fInvertGregorian = TRUE; // So that the Julian Jan 1 will be used in handleComputeMonthStart michael@0: return Calendar::handleComputeJulianDay(bestField); michael@0: } michael@0: michael@0: michael@0: // The following check handles portions of the cutover year BEFORE the michael@0: // cutover itself happens. michael@0: //if ((fIsGregorian==TRUE) != (jd >= fCutoverJulianDay)) { /* cutoverJulianDay)) { */ michael@0: if ((fIsGregorian==TRUE) != (jd >= fCutoverJulianDay)) { /* cutoverJulianDay)) { */ michael@0: #if defined (U_DEBUG_CAL) michael@0: fprintf(stderr, "%s:%d: jd [invert] %d\n", michael@0: __FILE__, __LINE__, jd); michael@0: #endif michael@0: fInvertGregorian = TRUE; michael@0: jd = Calendar::handleComputeJulianDay(bestField); michael@0: #if defined (U_DEBUG_CAL) michael@0: fprintf(stderr, "%s:%d: fIsGregorian %s, fInvertGregorian %s - ", michael@0: __FILE__, __LINE__,fIsGregorian?"T":"F", fInvertGregorian?"T":"F"); michael@0: fprintf(stderr, " jd NOW %d\n", michael@0: jd); michael@0: #endif michael@0: } else { michael@0: #if defined (U_DEBUG_CAL) michael@0: fprintf(stderr, "%s:%d: jd [==] %d - %sfIsGregorian %sfInvertGregorian, %d\n", michael@0: __FILE__, __LINE__, jd, fIsGregorian?"T":"F", fInvertGregorian?"T":"F", bestField); michael@0: #endif michael@0: } michael@0: michael@0: if(fIsGregorian && (internalGet(UCAL_EXTENDED_YEAR) == fGregorianCutoverYear)) { michael@0: int32_t gregShift = Grego::gregorianShift(internalGet(UCAL_EXTENDED_YEAR)); michael@0: if (bestField == UCAL_DAY_OF_YEAR) { michael@0: #if defined (U_DEBUG_CAL) michael@0: fprintf(stderr, "%s:%d: [DOY%d] gregorian shift of JD %d += %d\n", michael@0: __FILE__, __LINE__, fFields[bestField],jd, gregShift); michael@0: #endif michael@0: jd -= gregShift; michael@0: } else if ( bestField == UCAL_WEEK_OF_MONTH ) { michael@0: int32_t weekShift = 14; michael@0: #if defined (U_DEBUG_CAL) michael@0: fprintf(stderr, "%s:%d: [WOY/WOM] gregorian week shift of %d += %d\n", michael@0: __FILE__, __LINE__, jd, weekShift); michael@0: #endif michael@0: jd += weekShift; // shift by weeks for week based fields. michael@0: } michael@0: } michael@0: michael@0: return jd; michael@0: } michael@0: michael@0: int32_t GregorianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, michael@0: michael@0: UBool /* useMonth */) const michael@0: { michael@0: GregorianCalendar *nonConstThis = (GregorianCalendar*)this; // cast away const michael@0: 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: UBool isLeap = eyear%4 == 0; michael@0: int32_t y = eyear-1; michael@0: int32_t julianDay = 365*y + ClockMath::floorDivide(y, 4) + (kJan1_1JulianDay - 3); michael@0: michael@0: nonConstThis->fIsGregorian = (eyear >= fGregorianCutoverYear); michael@0: #if defined (U_DEBUG_CAL) michael@0: fprintf(stderr, "%s:%d: (hcms%d/%d) fIsGregorian %s, fInvertGregorian %s\n", michael@0: __FILE__, __LINE__, eyear,month, fIsGregorian?"T":"F", fInvertGregorian?"T":"F"); michael@0: #endif michael@0: if (fInvertGregorian) { michael@0: nonConstThis->fIsGregorian = !fIsGregorian; michael@0: } michael@0: if (fIsGregorian) { michael@0: isLeap = isLeap && ((eyear%100 != 0) || (eyear%400 == 0)); michael@0: // Add 2 because Gregorian calendar starts 2 days after michael@0: // Julian calendar michael@0: int32_t gregShift = Grego::gregorianShift(eyear); michael@0: #if defined (U_DEBUG_CAL) michael@0: fprintf(stderr, "%s:%d: (hcms%d/%d) gregorian shift of %d += %d\n", michael@0: __FILE__, __LINE__, eyear, month, julianDay, gregShift); michael@0: #endif michael@0: julianDay += gregShift; michael@0: } michael@0: michael@0: // At this point julianDay indicates the day BEFORE the first michael@0: // day of January 1, of either the Julian or Gregorian michael@0: // calendar. michael@0: michael@0: if (month != 0) { michael@0: julianDay += isLeap?kLeapNumDays[month]:kNumDays[month]; michael@0: } michael@0: michael@0: return julianDay; michael@0: } michael@0: michael@0: int32_t GregorianCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const michael@0: { 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) ? kLeapMonthLength[month] : kMonthLength[month]; michael@0: } michael@0: michael@0: int32_t GregorianCalendar::handleGetYearLength(int32_t eyear) const { michael@0: return isLeapYear(eyear) ? 366 : 365; michael@0: } michael@0: michael@0: michael@0: int32_t michael@0: GregorianCalendar::monthLength(int32_t month) const michael@0: { michael@0: int32_t year = internalGet(UCAL_EXTENDED_YEAR); michael@0: return handleGetMonthLength(year, month); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: int32_t michael@0: GregorianCalendar::monthLength(int32_t month, int32_t year) const michael@0: { michael@0: return isLeapYear(year) ? kLeapMonthLength[month] : kMonthLength[month]; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: int32_t michael@0: GregorianCalendar::yearLength(int32_t year) const michael@0: { michael@0: return isLeapYear(year) ? 366 : 365; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: int32_t michael@0: GregorianCalendar::yearLength() const michael@0: { michael@0: return isLeapYear(internalGet(UCAL_YEAR)) ? 366 : 365; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: /** michael@0: * After adjustments such as add(MONTH), add(YEAR), we don't want the michael@0: * month to jump around. E.g., we don't want Jan 31 + 1 month to go to Mar michael@0: * 3, we want it to go to Feb 28. Adjustments which might run into this michael@0: * problem call this method to retain the proper month. michael@0: */ michael@0: void michael@0: GregorianCalendar::pinDayOfMonth() michael@0: { michael@0: int32_t monthLen = monthLength(internalGet(UCAL_MONTH)); michael@0: int32_t dom = internalGet(UCAL_DATE); michael@0: if(dom > monthLen) michael@0: set(UCAL_DATE, monthLen); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: michael@0: UBool michael@0: GregorianCalendar::validateFields() const michael@0: { michael@0: for (int32_t field = 0; field < UCAL_FIELD_COUNT; field++) { michael@0: // Ignore DATE and DAY_OF_YEAR which are handled below michael@0: if (field != UCAL_DATE && michael@0: field != UCAL_DAY_OF_YEAR && michael@0: isSet((UCalendarDateFields)field) && michael@0: ! boundsCheck(internalGet((UCalendarDateFields)field), (UCalendarDateFields)field)) michael@0: return FALSE; michael@0: } michael@0: michael@0: // Values differ in Least-Maximum and Maximum should be handled michael@0: // specially. michael@0: if (isSet(UCAL_DATE)) { michael@0: int32_t date = internalGet(UCAL_DATE); michael@0: if (date < getMinimum(UCAL_DATE) || michael@0: date > monthLength(internalGet(UCAL_MONTH))) { michael@0: return FALSE; michael@0: } michael@0: } michael@0: michael@0: if (isSet(UCAL_DAY_OF_YEAR)) { michael@0: int32_t days = internalGet(UCAL_DAY_OF_YEAR); michael@0: if (days < 1 || days > yearLength()) { michael@0: return FALSE; michael@0: } michael@0: } michael@0: michael@0: // Handle DAY_OF_WEEK_IN_MONTH, which must not have the value zero. michael@0: // We've checked against minimum and maximum above already. michael@0: if (isSet(UCAL_DAY_OF_WEEK_IN_MONTH) && michael@0: 0 == internalGet(UCAL_DAY_OF_WEEK_IN_MONTH)) { michael@0: return FALSE; michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: UBool michael@0: GregorianCalendar::boundsCheck(int32_t value, UCalendarDateFields field) const michael@0: { michael@0: return value >= getMinimum(field) && value <= getMaximum(field); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: UDate michael@0: GregorianCalendar::getEpochDay(UErrorCode& status) michael@0: { michael@0: complete(status); michael@0: // Divide by 1000 (convert to seconds) in order to prevent overflow when michael@0: // dealing with UDate(Long.MIN_VALUE) and UDate(Long.MAX_VALUE). michael@0: double wallSec = internalGetTime()/1000 + (internalGet(UCAL_ZONE_OFFSET) + internalGet(UCAL_DST_OFFSET))/1000; michael@0: michael@0: return ClockMath::floorDivide(wallSec, kOneDay/1000.0); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: michael@0: // ------------------------------------- michael@0: michael@0: /** michael@0: * Compute the julian day number of the day BEFORE the first day of michael@0: * January 1, year 1 of the given calendar. If julianDay == 0, it michael@0: * specifies (Jan. 1, 1) - 1, in whatever calendar we are using (Julian michael@0: * or Gregorian). michael@0: */ michael@0: double GregorianCalendar::computeJulianDayOfYear(UBool isGregorian, michael@0: int32_t year, UBool& isLeap) michael@0: { michael@0: isLeap = year%4 == 0; michael@0: int32_t y = year - 1; michael@0: double julianDay = 365.0*y + ClockMath::floorDivide(y, 4) + (kJan1_1JulianDay - 3); michael@0: michael@0: if (isGregorian) { michael@0: isLeap = isLeap && ((year%100 != 0) || (year%400 == 0)); michael@0: // Add 2 because Gregorian calendar starts 2 days after Julian calendar michael@0: julianDay += Grego::gregorianShift(year); michael@0: } michael@0: michael@0: return julianDay; michael@0: } michael@0: michael@0: // /** michael@0: // * Compute the day of week, relative to the first day of week, from michael@0: // * 0..6, of the current DOW_LOCAL or DAY_OF_WEEK fields. This is michael@0: // * equivalent to get(DOW_LOCAL) - 1. michael@0: // */ michael@0: // int32_t GregorianCalendar::computeRelativeDOW() const { michael@0: // int32_t relDow = 0; michael@0: // if (fStamp[UCAL_DOW_LOCAL] > fStamp[UCAL_DAY_OF_WEEK]) { michael@0: // relDow = internalGet(UCAL_DOW_LOCAL) - 1; // 1-based michael@0: // } else if (fStamp[UCAL_DAY_OF_WEEK] != kUnset) { michael@0: // relDow = internalGet(UCAL_DAY_OF_WEEK) - getFirstDayOfWeek(); michael@0: // if (relDow < 0) relDow += 7; michael@0: // } michael@0: // return relDow; michael@0: // } michael@0: michael@0: // /** michael@0: // * Compute the day of week, relative to the first day of week, michael@0: // * from 0..6 of the given julian day. michael@0: // */ michael@0: // int32_t GregorianCalendar::computeRelativeDOW(double julianDay) const { michael@0: // int32_t relDow = julianDayToDayOfWeek(julianDay) - getFirstDayOfWeek(); michael@0: // if (relDow < 0) { michael@0: // relDow += 7; michael@0: // } michael@0: // return relDow; michael@0: // } michael@0: michael@0: // /** michael@0: // * Compute the DOY using the WEEK_OF_YEAR field and the julian day michael@0: // * of the day BEFORE January 1 of a year (a return value from michael@0: // * computeJulianDayOfYear). michael@0: // */ michael@0: // int32_t GregorianCalendar::computeDOYfromWOY(double julianDayOfYear) const { michael@0: // // Compute DOY from day of week plus week of year michael@0: michael@0: // // Find the day of the week for the first of this year. This michael@0: // // is zero-based, with 0 being the locale-specific first day of michael@0: // // the week. Add 1 to get first day of year. michael@0: // int32_t fdy = computeRelativeDOW(julianDayOfYear + 1); michael@0: michael@0: // return michael@0: // // Compute doy of first (relative) DOW of WOY 1 michael@0: // (((7 - fdy) < getMinimalDaysInFirstWeek()) michael@0: // ? (8 - fdy) : (1 - fdy)) michael@0: michael@0: // // Adjust for the week number. michael@0: // + (7 * (internalGet(UCAL_WEEK_OF_YEAR) - 1)) michael@0: michael@0: // // Adjust for the DOW michael@0: // + computeRelativeDOW(); michael@0: // } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: double michael@0: GregorianCalendar::millisToJulianDay(UDate millis) michael@0: { michael@0: return (double)kEpochStartAsJulianDay + ClockMath::floorDivide(millis, (double)kOneDay); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: UDate michael@0: GregorianCalendar::julianDayToMillis(double julian) michael@0: { michael@0: return (UDate) ((julian - kEpochStartAsJulianDay) * (double) kOneDay); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: int32_t michael@0: GregorianCalendar::aggregateStamp(int32_t stamp_a, int32_t stamp_b) michael@0: { michael@0: return (((stamp_a != kUnset && stamp_b != kUnset) michael@0: ? uprv_max(stamp_a, stamp_b) michael@0: : (int32_t)kUnset)); michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: /** michael@0: * Roll a field by a signed amount. michael@0: * Note: This will be made public later. [LIU] michael@0: */ michael@0: michael@0: void michael@0: GregorianCalendar::roll(EDateFields field, int32_t amount, UErrorCode& status) { michael@0: roll((UCalendarDateFields) field, amount, status); michael@0: } michael@0: michael@0: void michael@0: GregorianCalendar::roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) michael@0: { michael@0: if((amount == 0) || U_FAILURE(status)) { michael@0: return; michael@0: } michael@0: michael@0: // J81 processing. (gregorian cutover) michael@0: UBool inCutoverMonth = FALSE; michael@0: int32_t cMonthLen=0; // 'c' for cutover; in days michael@0: int32_t cDayOfMonth=0; // no discontinuity: [0, cMonthLen) michael@0: double cMonthStart=0.0; // in ms michael@0: michael@0: // Common code - see if we're in the cutover month of the cutover year michael@0: if(get(UCAL_EXTENDED_YEAR, status) == fGregorianCutoverYear) { michael@0: switch (field) { michael@0: case UCAL_DAY_OF_MONTH: michael@0: case UCAL_WEEK_OF_MONTH: michael@0: { michael@0: int32_t max = monthLength(internalGet(UCAL_MONTH)); michael@0: UDate t = internalGetTime(); michael@0: // We subtract 1 from the DAY_OF_MONTH to make it zero-based, and an michael@0: // additional 10 if we are after the cutover. Thus the monthStart michael@0: // value will be correct iff we actually are in the cutover month. michael@0: cDayOfMonth = internalGet(UCAL_DAY_OF_MONTH) - ((t >= fGregorianCutover) ? 10 : 0); michael@0: cMonthStart = t - ((cDayOfMonth - 1) * kOneDay); michael@0: // A month containing the cutover is 10 days shorter. michael@0: if ((cMonthStart < fGregorianCutover) && michael@0: (cMonthStart + (cMonthLen=(max-10))*kOneDay >= fGregorianCutover)) { michael@0: inCutoverMonth = TRUE; michael@0: } michael@0: } michael@0: default: michael@0: ; michael@0: } michael@0: } michael@0: michael@0: switch (field) { michael@0: case UCAL_WEEK_OF_YEAR: { michael@0: // Unlike WEEK_OF_MONTH, WEEK_OF_YEAR never shifts the day of the michael@0: // week. Also, rolling the week of the year can have seemingly michael@0: // strange effects simply because the year of the week of year michael@0: // may be different from the calendar year. For example, the michael@0: // date Dec 28, 1997 is the first day of week 1 of 1998 (if michael@0: // weeks start on Sunday and the minimal days in first week is michael@0: // <= 3). michael@0: int32_t woy = get(UCAL_WEEK_OF_YEAR, status); michael@0: // Get the ISO year, which matches the week of year. This michael@0: // may be one year before or after the calendar year. michael@0: int32_t isoYear = get(UCAL_YEAR_WOY, status); michael@0: int32_t isoDoy = internalGet(UCAL_DAY_OF_YEAR); michael@0: if (internalGet(UCAL_MONTH) == UCAL_JANUARY) { michael@0: if (woy >= 52) { michael@0: isoDoy += handleGetYearLength(isoYear); michael@0: } michael@0: } else { michael@0: if (woy == 1) { michael@0: isoDoy -= handleGetYearLength(isoYear - 1); michael@0: } michael@0: } michael@0: woy += amount; michael@0: // Do fast checks to avoid unnecessary computation: michael@0: if (woy < 1 || woy > 52) { michael@0: // Determine the last week of the ISO year. michael@0: // We do this using the standard formula we use michael@0: // everywhere in this file. If we can see that the michael@0: // days at the end of the year are going to fall into michael@0: // week 1 of the next year, we drop the last week by michael@0: // subtracting 7 from the last day of the year. michael@0: int32_t lastDoy = handleGetYearLength(isoYear); michael@0: int32_t lastRelDow = (lastDoy - isoDoy + internalGet(UCAL_DAY_OF_WEEK) - michael@0: getFirstDayOfWeek()) % 7; michael@0: if (lastRelDow < 0) lastRelDow += 7; michael@0: if ((6 - lastRelDow) >= getMinimalDaysInFirstWeek()) lastDoy -= 7; michael@0: int32_t lastWoy = weekNumber(lastDoy, lastRelDow + 1); michael@0: woy = ((woy + lastWoy - 1) % lastWoy) + 1; michael@0: } michael@0: set(UCAL_WEEK_OF_YEAR, woy); michael@0: set(UCAL_YEAR_WOY,isoYear); michael@0: return; michael@0: } michael@0: michael@0: case UCAL_DAY_OF_MONTH: michael@0: if( !inCutoverMonth ) { michael@0: Calendar::roll(field, amount, status); michael@0: return; michael@0: } else { michael@0: // [j81] 1582 special case for DOM michael@0: // The default computation works except when the current month michael@0: // contains the Gregorian cutover. We handle this special case michael@0: // here. [j81 - aliu] michael@0: double monthLen = cMonthLen * kOneDay; michael@0: double msIntoMonth = uprv_fmod(internalGetTime() - cMonthStart + michael@0: amount * kOneDay, monthLen); michael@0: if (msIntoMonth < 0) { michael@0: msIntoMonth += monthLen; michael@0: } michael@0: #if defined (U_DEBUG_CAL) michael@0: fprintf(stderr, "%s:%d: roll DOM %d -> %.0lf ms \n", michael@0: __FILE__, __LINE__,amount, cMonthLen, cMonthStart+msIntoMonth); michael@0: #endif michael@0: setTimeInMillis(cMonthStart + msIntoMonth, status); michael@0: return; michael@0: } michael@0: michael@0: case UCAL_WEEK_OF_MONTH: michael@0: if( !inCutoverMonth ) { michael@0: Calendar::roll(field, amount, status); michael@0: return; michael@0: } else { michael@0: #if defined (U_DEBUG_CAL) michael@0: fprintf(stderr, "%s:%d: roll WOM %d ??????????????????? \n", michael@0: __FILE__, __LINE__,amount); michael@0: #endif michael@0: // NOTE: following copied from the old michael@0: // GregorianCalendar::roll( WEEK_OF_MONTH ) code michael@0: michael@0: // This is tricky, because during the roll we may have to shift michael@0: // to a different day of the week. For example: michael@0: michael@0: // s m t w r f s michael@0: // 1 2 3 4 5 michael@0: // 6 7 8 9 10 11 12 michael@0: michael@0: // When rolling from the 6th or 7th back one week, we go to the michael@0: // 1st (assuming that the first partial week counts). The same michael@0: // thing happens at the end of the month. michael@0: michael@0: // The other tricky thing is that we have to figure out whether michael@0: // the first partial week actually counts or not, based on the michael@0: // minimal first days in the week. And we have to use the michael@0: // correct first day of the week to delineate the week michael@0: // boundaries. michael@0: michael@0: // Here's our algorithm. First, we find the real boundaries of michael@0: // the month. Then we discard the first partial week if it michael@0: // doesn't count in this locale. Then we fill in the ends with michael@0: // phantom days, so that the first partial week and the last michael@0: // partial week are full weeks. We then have a nice square michael@0: // block of weeks. We do the usual rolling within this block, michael@0: // as is done elsewhere in this method. If we wind up on one of michael@0: // the phantom days that we added, we recognize this and pin to michael@0: // the first or the last day of the month. Easy, eh? michael@0: michael@0: // Another wrinkle: To fix jitterbug 81, we have to make all this michael@0: // work in the oddball month containing the Gregorian cutover. michael@0: // This month is 10 days shorter than usual, and also contains michael@0: // a discontinuity in the days; e.g., the default cutover month michael@0: // is Oct 1582, and goes from day of month 4 to day of month 15. michael@0: michael@0: // Normalize the DAY_OF_WEEK so that 0 is the first day of the week michael@0: // in this locale. We have dow in 0..6. michael@0: int32_t dow = internalGet(UCAL_DAY_OF_WEEK) - getFirstDayOfWeek(); michael@0: if (dow < 0) michael@0: dow += 7; michael@0: michael@0: // Find the day of month, compensating for cutover discontinuity. michael@0: int32_t dom = cDayOfMonth; michael@0: michael@0: // Find the day of the week (normalized for locale) for the first michael@0: // of the month. michael@0: int32_t fdm = (dow - dom + 1) % 7; michael@0: if (fdm < 0) michael@0: fdm += 7; michael@0: michael@0: // Get the first day of the first full week of the month, michael@0: // including phantom days, if any. Figure out if the first week michael@0: // counts or not; if it counts, then fill in phantom days. If michael@0: // not, advance to the first real full week (skip the partial week). michael@0: int32_t start; michael@0: if ((7 - fdm) < getMinimalDaysInFirstWeek()) michael@0: start = 8 - fdm; // Skip the first partial week michael@0: else michael@0: start = 1 - fdm; // This may be zero or negative michael@0: michael@0: // Get the day of the week (normalized for locale) for the last michael@0: // day of the month. michael@0: int32_t monthLen = cMonthLen; michael@0: int32_t ldm = (monthLen - dom + dow) % 7; michael@0: // We know monthLen >= DAY_OF_MONTH so we skip the += 7 step here. michael@0: michael@0: // Get the limit day for the blocked-off rectangular month; that michael@0: // is, the day which is one past the last day of the month, michael@0: // after the month has already been filled in with phantom days michael@0: // to fill out the last week. This day has a normalized DOW of 0. michael@0: int32_t limit = monthLen + 7 - ldm; michael@0: michael@0: // Now roll between start and (limit - 1). michael@0: int32_t gap = limit - start; michael@0: int32_t newDom = (dom + amount*7 - start) % gap; michael@0: if (newDom < 0) michael@0: newDom += gap; michael@0: newDom += start; michael@0: michael@0: // Finally, pin to the real start and end of the month. michael@0: if (newDom < 1) michael@0: newDom = 1; michael@0: if (newDom > monthLen) michael@0: newDom = monthLen; michael@0: michael@0: // Set the DAY_OF_MONTH. We rely on the fact that this field michael@0: // takes precedence over everything else (since all other fields michael@0: // are also set at this point). If this fact changes (if the michael@0: // disambiguation algorithm changes) then we will have to unset michael@0: // the appropriate fields here so that DAY_OF_MONTH is attended michael@0: // to. michael@0: michael@0: // If we are in the cutover month, manipulate ms directly. Don't do michael@0: // this in general because it doesn't work across DST boundaries michael@0: // (details, details). This takes care of the discontinuity. michael@0: setTimeInMillis(cMonthStart + (newDom-1)*kOneDay, status); michael@0: return; michael@0: } michael@0: michael@0: default: michael@0: Calendar::roll(field, amount, status); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: // ------------------------------------- michael@0: michael@0: michael@0: /** michael@0: * Return the minimum value that this field could have, given the current date. michael@0: * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum(). michael@0: * @param field the time field. michael@0: * @return the minimum value that this field could have, given the current date. michael@0: * @deprecated ICU 2.6. Use getActualMinimum(UCalendarDateFields field) instead. michael@0: */ michael@0: int32_t GregorianCalendar::getActualMinimum(EDateFields field) const michael@0: { michael@0: return getMinimum((UCalendarDateFields)field); michael@0: } michael@0: michael@0: int32_t GregorianCalendar::getActualMinimum(EDateFields field, UErrorCode& /* status */) const michael@0: { michael@0: return getMinimum((UCalendarDateFields)field); michael@0: } michael@0: michael@0: /** michael@0: * Return the minimum value that this field could have, given the current date. michael@0: * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum(). michael@0: * @param field the time field. michael@0: * @return the minimum value that this field could have, given the current date. michael@0: * @draft ICU 2.6. michael@0: */ michael@0: int32_t GregorianCalendar::getActualMinimum(UCalendarDateFields field, UErrorCode& /* status */) const michael@0: { michael@0: return getMinimum(field); michael@0: } michael@0: michael@0: michael@0: // ------------------------------------ michael@0: michael@0: /** michael@0: * Old year limits were least max 292269054, max 292278994. michael@0: */ michael@0: michael@0: /** michael@0: * @stable ICU 2.0 michael@0: */ michael@0: int32_t GregorianCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const { michael@0: return kGregorianCalendarLimits[field][limitType]; michael@0: } michael@0: michael@0: /** michael@0: * Return the maximum value that this field could have, given the current date. michael@0: * For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual michael@0: * maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for a Hebrew calendar, michael@0: * for some years the actual maximum for MONTH is 12, and for others 13. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: int32_t GregorianCalendar::getActualMaximum(UCalendarDateFields field, UErrorCode& status) const michael@0: { michael@0: /* It is a known limitation that the code here (and in getActualMinimum) michael@0: * won't behave properly at the extreme limits of GregorianCalendar's michael@0: * representable range (except for the code that handles the YEAR michael@0: * field). That's because the ends of the representable range are at michael@0: * odd spots in the year. For calendars with the default Gregorian michael@0: * cutover, these limits are Sun Dec 02 16:47:04 GMT 292269055 BC to Sun michael@0: * Aug 17 07:12:55 GMT 292278994 AD, somewhat different for non-GMT michael@0: * zones. As a result, if the calendar is set to Aug 1 292278994 AD, michael@0: * the actual maximum of DAY_OF_MONTH is 17, not 30. If the date is Mar michael@0: * 31 in that year, the actual maximum month might be Jul, whereas is michael@0: * the date is Mar 15, the actual maximum might be Aug -- depending on michael@0: * the precise semantics that are desired. Similar considerations michael@0: * affect all fields. Nonetheless, this effect is sufficiently arcane michael@0: * that we permit it, rather than complicating the code to handle such michael@0: * intricacies. - liu 8/20/98 michael@0: michael@0: * UPDATE: No longer true, since we have pulled in the limit values on michael@0: * the year. - Liu 11/6/00 */ michael@0: michael@0: switch (field) { michael@0: michael@0: case UCAL_YEAR: michael@0: /* The year computation is no different, in principle, from the michael@0: * others, however, the range of possible maxima is large. In michael@0: * addition, the way we know we've exceeded the range is different. michael@0: * For these reasons, we use the special case code below to handle michael@0: * this field. michael@0: * michael@0: * The actual maxima for YEAR depend on the type of calendar: michael@0: * michael@0: * Gregorian = May 17, 292275056 BC - Aug 17, 292278994 AD michael@0: * Julian = Dec 2, 292269055 BC - Jan 3, 292272993 AD michael@0: * Hybrid = Dec 2, 292269055 BC - Aug 17, 292278994 AD michael@0: * michael@0: * We know we've exceeded the maximum when either the month, date, michael@0: * time, or era changes in response to setting the year. We don't michael@0: * check for month, date, and time here because the year and era are michael@0: * sufficient to detect an invalid year setting. NOTE: If code is michael@0: * added to check the month and date in the future for some reason, michael@0: * Feb 29 must be allowed to shift to Mar 1 when setting the year. michael@0: */ michael@0: { michael@0: if(U_FAILURE(status)) return 0; michael@0: Calendar *cal = clone(); michael@0: if(!cal) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: cal->setLenient(TRUE); michael@0: michael@0: int32_t era = cal->get(UCAL_ERA, status); michael@0: UDate d = cal->getTime(status); michael@0: michael@0: /* Perform a binary search, with the invariant that lowGood is a michael@0: * valid year, and highBad is an out of range year. michael@0: */ michael@0: int32_t lowGood = kGregorianCalendarLimits[UCAL_YEAR][1]; michael@0: int32_t highBad = kGregorianCalendarLimits[UCAL_YEAR][2]+1; michael@0: while ((lowGood + 1) < highBad) { michael@0: int32_t y = (lowGood + highBad) / 2; michael@0: cal->set(UCAL_YEAR, y); michael@0: if (cal->get(UCAL_YEAR, status) == y && cal->get(UCAL_ERA, status) == era) { michael@0: lowGood = y; michael@0: } else { michael@0: highBad = y; michael@0: cal->setTime(d, status); // Restore original fields michael@0: } michael@0: } michael@0: michael@0: delete cal; michael@0: return lowGood; michael@0: } michael@0: michael@0: default: michael@0: return Calendar::getActualMaximum(field,status); michael@0: } michael@0: } michael@0: michael@0: michael@0: int32_t GregorianCalendar::handleGetExtendedYear() { michael@0: // the year to return michael@0: int32_t year = kEpochYear; michael@0: michael@0: // year field to use michael@0: int32_t yearField = UCAL_EXTENDED_YEAR; michael@0: michael@0: // There are three separate fields which could be used to michael@0: // derive the proper year. Use the one most recently set. michael@0: if (fStamp[yearField] < fStamp[UCAL_YEAR]) michael@0: yearField = UCAL_YEAR; michael@0: if (fStamp[yearField] < fStamp[UCAL_YEAR_WOY]) michael@0: yearField = UCAL_YEAR_WOY; michael@0: michael@0: // based on the "best" year field, get the year michael@0: switch(yearField) { michael@0: case UCAL_EXTENDED_YEAR: michael@0: year = internalGet(UCAL_EXTENDED_YEAR, kEpochYear); michael@0: break; michael@0: michael@0: case UCAL_YEAR: michael@0: { michael@0: // The year defaults to the epoch start, the era to AD michael@0: int32_t era = internalGet(UCAL_ERA, AD); michael@0: if (era == BC) { michael@0: year = 1 - internalGet(UCAL_YEAR, 1); // Convert to extended year michael@0: } else { michael@0: year = internalGet(UCAL_YEAR, kEpochYear); michael@0: } michael@0: } michael@0: break; michael@0: michael@0: case UCAL_YEAR_WOY: michael@0: year = handleGetExtendedYearFromWeekFields(internalGet(UCAL_YEAR_WOY), internalGet(UCAL_WEEK_OF_YEAR)); michael@0: #if defined (U_DEBUG_CAL) michael@0: // if(internalGet(UCAL_YEAR_WOY) != year) { michael@0: fprintf(stderr, "%s:%d: hGEYFWF[%d,%d] -> %d\n", michael@0: __FILE__, __LINE__,internalGet(UCAL_YEAR_WOY),internalGet(UCAL_WEEK_OF_YEAR),year); michael@0: //} michael@0: #endif michael@0: break; michael@0: michael@0: default: michael@0: year = kEpochYear; michael@0: } michael@0: return year; michael@0: } michael@0: michael@0: int32_t GregorianCalendar::handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy) michael@0: { michael@0: // convert year to extended form michael@0: int32_t era = internalGet(UCAL_ERA, AD); michael@0: if(era == BC) { michael@0: yearWoy = 1 - yearWoy; michael@0: } michael@0: return Calendar::handleGetExtendedYearFromWeekFields(yearWoy, woy); michael@0: } michael@0: michael@0: michael@0: // ------------------------------------- michael@0: michael@0: UBool michael@0: GregorianCalendar::inDaylightTime(UErrorCode& status) const michael@0: { 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: ((GregorianCalendar*)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: michael@0: /** michael@0: * Return the ERA. We need a special method for this because the michael@0: * default ERA is AD, but a zero (unset) ERA is BC. michael@0: */ michael@0: int32_t michael@0: GregorianCalendar::internalGetEra() const { michael@0: return isSet(UCAL_ERA) ? internalGet(UCAL_ERA) : (int32_t)AD; michael@0: } michael@0: michael@0: const char * michael@0: GregorianCalendar::getType() const { michael@0: //static const char kGregorianType = "gregorian"; michael@0: michael@0: return "gregorian"; 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 GregorianCalendar::haveDefaultCentury() const michael@0: { michael@0: return TRUE; michael@0: } michael@0: michael@0: static void U_CALLCONV michael@0: 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: GregorianCalendar calendar(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: UDate GregorianCalendar::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 GregorianCalendar::defaultCenturyStartYear() const { michael@0: // lazy-evaluate systemDefaultCenturyStartYear michael@0: umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury); michael@0: return gSystemDefaultCenturyStartYear; michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: //eof