mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-21 12:40:02 +00:00
ICU-5555 handleGetMonthLength needs to check for out of range months.
X-SVN-Rev: 21092
This commit is contained in:
parent
2bd5d6d8a8
commit
3dbd215bdb
4 changed files with 64 additions and 7 deletions
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (C) 1997-2006, International Business Machines Corporation and *
|
||||
* Copyright (C) 1997-2007, International Business Machines Corporation and *
|
||||
* others. All Rights Reserved. *
|
||||
*******************************************************************************
|
||||
*
|
||||
|
@ -574,6 +574,12 @@ int32_t GregorianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month,
|
|||
|
||||
int32_t GregorianCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const
|
||||
{
|
||||
// If the month is out of range, adjust it into range, and
|
||||
// modify the extended year value accordingly.
|
||||
if (month < 0 || month > 11) {
|
||||
extendedYear += Math::floorDivide(month, 12, month);
|
||||
}
|
||||
|
||||
return isLeapYear(extendedYear) ? kLeapMonthLength[month] : kMonthLength[month];
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
******************************************************************************
|
||||
* Copyright (C) 2003-2006, International Business Machines Corporation
|
||||
* Copyright (C) 2003-2007, International Business Machines Corporation
|
||||
* and others. All Rights Reserved.
|
||||
******************************************************************************
|
||||
*
|
||||
|
@ -493,13 +493,27 @@ int32_t HebrewCalendar::handleGetLimit(UCalendarDateFields field, ELimitType lim
|
|||
* @internal
|
||||
*/
|
||||
int32_t HebrewCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const {
|
||||
// Resolve out-of-range months. This is necessary in order to
|
||||
// obtain the correct year. We correct to
|
||||
// a 12- or 13-month year (add/subtract 12 or 13, depending
|
||||
// on the year) but since we _always_ number from 0..12, and
|
||||
// the leap year determines whether or not month 5 (Adar 1)
|
||||
// is present, we allow 0..12 in any given year.
|
||||
while (month < 0) {
|
||||
month += monthsInYear(--extendedYear);
|
||||
}
|
||||
// Careful: allow 0..12 in all years
|
||||
while (month > 12) {
|
||||
month -= monthsInYear(extendedYear++);
|
||||
}
|
||||
|
||||
switch (month) {
|
||||
case HESHVAN:
|
||||
case KISLEV:
|
||||
case HESHVAN:
|
||||
case KISLEV:
|
||||
// These two month lengths can vary
|
||||
return MONTH_LENGTH[month][yearType(extendedYear)];
|
||||
|
||||
default:
|
||||
default:
|
||||
// The rest are a fixed length
|
||||
return MONTH_LENGTH[month][0];
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/********************************************************************
|
||||
* COPYRIGHT:
|
||||
* Copyright (c) 1997-2006, International Business Machines Corporation
|
||||
* Copyright (c) 1997-2007, International Business Machines Corporation
|
||||
* and others. All Rights Reserved.
|
||||
********************************************************************/
|
||||
|
||||
|
@ -81,6 +81,7 @@ CalendarRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* &
|
|||
CASE(42,TestWeekShift);
|
||||
CASE(43,TestTimeZoneTransitionAdd);
|
||||
CASE(44,TestDeprecates);
|
||||
CASE(45,TestT5555);
|
||||
default: name = ""; break;
|
||||
}
|
||||
}
|
||||
|
@ -2270,6 +2271,41 @@ void CalendarRegressionTest::TestJ438(void) {
|
|||
delete pcal;
|
||||
}
|
||||
|
||||
void CalendarRegressionTest::TestT5555()
|
||||
{
|
||||
UErrorCode ec = U_ZERO_ERROR;
|
||||
Calendar *cal = Calendar::createInstance(ec);
|
||||
|
||||
if (cal == NULL || U_FAILURE(ec)) {
|
||||
errln("FAIL: Calendar::createInstance()");
|
||||
delete cal;
|
||||
return;
|
||||
}
|
||||
|
||||
// Set to Wednesday, February 21, 2007
|
||||
cal->set(2007, UCAL_FEBRUARY, 21);
|
||||
|
||||
// Advance three years
|
||||
cal->add(UCAL_MONTH, 36, ec);
|
||||
|
||||
// Set to last Wednesday of the month
|
||||
cal->set(UCAL_DAY_OF_WEEK_IN_MONTH, -1);
|
||||
|
||||
cal->getTime(ec);
|
||||
|
||||
int32_t yy, mm, dd, ee;
|
||||
|
||||
yy = cal->get(UCAL_YEAR, ec);
|
||||
mm = cal->get(UCAL_MONTH, ec);
|
||||
dd = cal->get(UCAL_DATE, ec);
|
||||
ee = cal->get(UCAL_DAY_OF_WEEK, ec);
|
||||
|
||||
// Should be set to Wednesday, February 24, 2010
|
||||
if (U_FAILURE(ec) || yy != 2010 || mm != UCAL_FEBRUARY || dd != 24 || ee != UCAL_WEDNESDAY) {
|
||||
errln("FAIL: got date %4d/%02d/%02d, expected 210/02/24: ", yy, mm + 1, dd);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test behavior of fieldDifference around leap years. Also test a large
|
||||
* field difference to check binary search.
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/********************************************************************
|
||||
* COPYRIGHT:
|
||||
* Copyright (c) 1997-2003, International Business Machines Corporation and
|
||||
* Copyright (c) 1997-2007, International Business Machines Corporation and
|
||||
* others. All Rights Reserved.
|
||||
********************************************************************/
|
||||
|
||||
|
@ -64,6 +64,7 @@ public:
|
|||
void Test4197699(void);
|
||||
void TestJ81(void);
|
||||
void TestJ438(void);
|
||||
void TestT5555(void);
|
||||
void TestLeapFieldDifference(void);
|
||||
void TestMalaysianInstance(void);
|
||||
void TestWeekShift(void);
|
||||
|
|
Loading…
Add table
Reference in a new issue