From: Mihai Nita Date: Thu, 26 Jan 2023 18:56:50 +0000 (+0000) Subject: ICU-22226 Fix Calendar.getFirstDayOfWeek to honor -u-fw X-Git-Tag: cldr/2023-02-02~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=76df897b77fd938abc29c9121dde794300a171e6;p=icu ICU-22226 Fix Calendar.getFirstDayOfWeek to honor -u-fw --- diff --git a/icu4c/source/i18n/calendar.cpp b/icu4c/source/i18n/calendar.cpp index 8fd7781b792..c67525140ab 100644 --- a/icu4c/source/i18n/calendar.cpp +++ b/icu4c/source/i18n/calendar.cpp @@ -3948,6 +3948,29 @@ Calendar::setWeekData(const Locale& desiredLocale, const char *type, UErrorCode& } else { status = U_INVALID_FORMAT_ERROR; } + + // Check if the locale has a "fw" u extension and we honor it if present. + // And we don't change the overal status, as the presence / lack of "fw" is not an error. + UErrorCode fwStatus = U_ZERO_ERROR; + char fwExt[ULOC_FULLNAME_CAPACITY] = ""; + desiredLocale.getKeywordValue("fw", fwExt, ULOC_FULLNAME_CAPACITY, fwStatus); + if (U_SUCCESS(fwStatus)) { + if (uprv_strcmp(fwExt, "sun") == 0) { + fFirstDayOfWeek = UCAL_SUNDAY; + } else if (uprv_strcmp(fwExt, "mon") == 0) { + fFirstDayOfWeek = UCAL_MONDAY; + } else if (uprv_strcmp(fwExt, "tue") == 0) { + fFirstDayOfWeek = UCAL_TUESDAY; + } else if (uprv_strcmp(fwExt, "wed") == 0) { + fFirstDayOfWeek = UCAL_WEDNESDAY; + } else if (uprv_strcmp(fwExt, "thu") == 0) { + fFirstDayOfWeek = UCAL_THURSDAY; + } else if (uprv_strcmp(fwExt, "fri") == 0) { + fFirstDayOfWeek = UCAL_FRIDAY; + } else if (uprv_strcmp(fwExt, "sat") == 0) { + fFirstDayOfWeek = UCAL_SATURDAY; + } + } } ures_close(weekData); ures_close(rb); diff --git a/icu4c/source/test/intltest/calregts.cpp b/icu4c/source/test/intltest/calregts.cpp index 7fff12b7271..6acf7a4b95f 100644 --- a/icu4c/source/test/intltest/calregts.cpp +++ b/icu4c/source/test/intltest/calregts.cpp @@ -99,6 +99,7 @@ CalendarRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* & CASE(55,Test13745); CASE(56,TestUTCWrongAMPM22023); CASE(57,TestAsiaManilaAfterSetGregorianChange22043); + CASE(58,TestRespectUExtensionFw); default: name = ""; break; } } @@ -3240,4 +3241,40 @@ void CalendarRegressionTest::TestWeekOfYear13548(void) { } } +void CalendarRegressionTest::TestRespectUExtensionFw(void) { // ICU-22226 + static const char* LOCALE_IDS[] = { + "en-US", + "en-US-u-fw-xyz", + "en-US-u-fw-sun", + "en-US-u-fw-mon", + "en-US-u-fw-thu", + "en-US-u-fw-sat" + }; + static const UCalendarDaysOfWeek EXPECTED_VAL[] = { + UCAL_SUNDAY, + UCAL_SUNDAY, + UCAL_SUNDAY, + UCAL_MONDAY, + UCAL_THURSDAY, + UCAL_SATURDAY + }; + + int32_t EXPECTED_VAL_count = UPRV_LENGTHOF(EXPECTED_VAL); + assertEquals("The number of locales should be equal to the number of expected results.", + EXPECTED_VAL_count, UPRV_LENGTHOF(LOCALE_IDS)); + + for (int32_t i=0; i cal(Calendar::createInstance(locale, status)); + UCalendarDaysOfWeek actual = cal->getFirstDayOfWeek(status); + failure(status, "Calendar::getFirstDayOfWeek(status)"); + + assertEquals((UnicodeString)"Calendar.getFirstDayOfWeek() ignores the 'fw' extension u in '" + + localeId + "' locale", expected, actual); + } +} #endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/icu4c/source/test/intltest/calregts.h b/icu4c/source/test/intltest/calregts.h index da815fb448e..174c10c006c 100644 --- a/icu4c/source/test/intltest/calregts.h +++ b/icu4c/source/test/intltest/calregts.h @@ -85,6 +85,7 @@ public: void TestAsiaManilaAfterSetGregorianChange22043(void); void Test13745(void); + void TestRespectUExtensionFw(void); void printdate(GregorianCalendar *cal, const char *string); void dowTest(UBool lenient) ; diff --git a/icu4j/main/classes/core/src/com/ibm/icu/util/Calendar.java b/icu4j/main/classes/core/src/com/ibm/icu/util/Calendar.java index 3d9275adfd8..1aaef8c54f6 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/util/Calendar.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/util/Calendar.java @@ -1595,6 +1595,25 @@ public abstract class Calendar implements Serializable, Cloneable, Comparable