ICU-22424 Fix Calendar::clear(UCAL_MONTH)

Make the calling of clear(UCAL_MONTH or UCAL_ORDINAL_MONTH) clear both fields.
This commit is contained in:
Frank Tang 2023-06-29 14:55:05 -07:00 committed by Frank Yung-Fong Tang
parent adbcfb1e07
commit 1b15a4e9db
4 changed files with 42 additions and 2 deletions

View file

@ -1305,7 +1305,16 @@ Calendar::clear(UCalendarDateFields field)
}
fFields[field] = 0;
fStamp[field] = kUnset;
fIsSet[field] = false; // Remove later
if (field == UCAL_MONTH) {
fFields[UCAL_ORDINAL_MONTH] = 0;
fStamp[UCAL_ORDINAL_MONTH] = kUnset;
fIsSet[UCAL_ORDINAL_MONTH] = false; // Remove later
}
if (field == UCAL_ORDINAL_MONTH) {
fFields[UCAL_MONTH] = 0;
fStamp[UCAL_MONTH] = kUnset;
fIsSet[UCAL_MONTH] = false; // Remove later
}
fIsTimeSet = fAreFieldsSet = fAreAllFieldsSet = fAreFieldsVirtuallySet = false;
}

View file

@ -1237,7 +1237,8 @@ public:
/**
* Clears the value in the given time field, both making it unset and assigning it a
* value of zero. This field value will be determined during the next resolving of
* time into time fields.
* time into time fields. Clearing UCAL_ORDINAL_MONTH or UCAL_MONTH will
* clear both fields.
*
* @param field The time field to be cleared.
* @stable ICU 2.6.

View file

@ -184,6 +184,7 @@ void CalendarTest::runIndexedTest( int32_t index, UBool exec, const char* &name,
TESTCASE_AUTO(TestLimitsOrdinalMonth);
TESTCASE_AUTO(TestActualLimitsOrdinalMonth);
TESTCASE_AUTO(TestChineseCalendarMonthInSpecialYear);
TESTCASE_AUTO(TestClearMonth);
TESTCASE_AUTO_END;
}
@ -3887,6 +3888,33 @@ void CalendarTest::TestChineseCalendarMapping() {
}
}
void CalendarTest::TestClearMonth() {
UErrorCode status = U_ZERO_ERROR;
LocalPointer<Calendar> cal(Calendar::createInstance(Locale::getRoot(), status));
if (failure(status, "construct Calendar")) return;
cal->set(2023, UCAL_JUNE, 29);
assertEquals("Calendar::get(UCAL_MONTH)", UCAL_JUNE, cal->get(UCAL_MONTH, status));
if (failure(status, "Calendar::get(UCAL_MONTH)")) return;
cal->clear(UCAL_MONTH);
assertEquals("Calendar::isSet(UCAL_MONTH) after clear(UCAL_MONTH)", false, !!cal->isSet(UCAL_MONTH));
assertEquals("Calendar::get(UCAL_MONTH after clear(UCAL_MONTH))", UCAL_JANUARY, !!cal->get(UCAL_MONTH, status));
if (failure(status, "Calendar::get(UCAL_MONTH)")) return;
cal->set(UCAL_ORDINAL_MONTH, 7);
assertEquals("Calendar::get(UCAL_MONTH) after set(UCAL_ORDINAL_MONTH, 7)", UCAL_AUGUST, cal->get(UCAL_MONTH, status));
if (failure(status, "Calendar::get(UCAL_MONTH) after set(UCAL_ORDINAL_MONTH, 7)")) return;
assertEquals("Calendar::get(UCAL_ORDINAL_MONTH) after set(UCAL_ORDINAL_MONTH, 7)", 7, cal->get(UCAL_ORDINAL_MONTH, status));
if (failure(status, "Calendar::get(UCAL_ORDINAL_MONTH) after set(UCAL_ORDINAL_MONTH, 7)")) return;
cal->clear(UCAL_ORDINAL_MONTH);
assertEquals("Calendar::isSet(UCAL_ORDINAL_MONTH) after clear(UCAL_ORDINAL_MONTH)", false, !!cal->isSet(UCAL_ORDINAL_MONTH));
assertEquals("Calendar::get(UCAL_MONTH) after clear(UCAL_ORDINAL_MONTH)", UCAL_JANUARY, cal->get(UCAL_MONTH, status));
if (failure(status, "Calendar::get(UCAL_MONTH) after clear(UCAL_ORDINAL_MONTH)")) return;
assertEquals("Calendar::get(UCAL_ORDINAL_MONTH) after clear(UCAL_ORDINAL_MONTH)", 0, cal->get(UCAL_ORDINAL_MONTH, status));
if (failure(status, "Calendar::get(UCAL_ORDINAL_MONTH) after clear(UCAL_ORDINAL_MONTH)")) return;
}
void CalendarTest::TestGregorianCalendarInTemporalLeapYear() {
// test from year 1800 to 2500
UErrorCode status = U_ZERO_ERROR;

View file

@ -56,6 +56,8 @@ public:
virtual void TestDebug();
virtual void TestClearMonth();
public: // package
/**
* test subroutine used by TestDisambiguation765