Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions icu4c/source/i18n/dtitvfmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,21 +1239,8 @@ DateIntervalFormat::setSeparateDateTimePtn(
int8_t differenceInfo = 0;
const UnicodeString* bestSkeleton = fInfo->getBestSkeleton(*skeleton,
differenceInfo);
/* best skeleton could be nullptr.
For example: in "ca" resource file,
interval format is defined as following
intervalFormats{
fallback{"{0} - {1}"}
}
there is no skeletons/interval patterns defined,
and the best skeleton match could be nullptr
*/
if ( bestSkeleton == nullptr ) {
return false;
}

// Set patterns for fallback use, need to do this
// before returning if differenceInfo == -1
// before returning if bestSkeleton == nullptr or differenceInfo == -1
UErrorCode status;
if ( dateSkeleton.length() != 0) {
status = U_ZERO_ERROR;
Expand All @@ -1268,15 +1255,18 @@ DateIntervalFormat::setSeparateDateTimePtn(
// no way to report OOM. :(
}

/* best skeleton could be nullptr when no intervalFormatItem skeletons are defined
(e.g. for locales without their own intervalFormatItem data).
*/
// difference:
// 0 means the best matched skeleton is the same as input skeleton
// 1 means the fields are the same, but field width are different
// 2 means the only difference between fields are v/z,
// -1 means there are other fields difference
// (this will happen, for instance, if the supplied skeleton has seconds,
// but no skeletons in the intervalFormats data do)
if ( differenceInfo == -1 ) {
// skeleton has different fields, not only v/z difference
if ( bestSkeleton == nullptr || differenceInfo == -1 ) {
// no interval skeleton available, or skeleton has different fields
return false;
}

Expand Down Expand Up @@ -1629,7 +1619,8 @@ DateIntervalFormat::fallbackFormat(Calendar& fromCalendar,
return appendTo;
}

UBool formatDatePlusTimeRange = (fromToOnSameDay && fDatePattern && fTimePattern);
UBool formatDatePlusTimeRange =
(fromToOnSameDay && fDatePattern && fTimePattern && fDateTimeFormat);
if (formatDatePlusTimeRange) {
SimpleFormatter sf(*fDateTimeFormat, 2, 2, status);
if (U_FAILURE(status)) {
Expand Down
24 changes: 20 additions & 4 deletions icu4c/source/i18n/dtitvinf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,16 @@ struct DateIntervalInfo::DateIntervalSink : public ResourceSink {
// Next calendar type
UnicodeString nextCalendarType;

DateIntervalSink(DateIntervalInfo &diInfo, const char *currentCalendarType)
: dateIntervalInfo(diInfo), nextCalendarType(currentCalendarType, -1, US_INV) { }
// Whether the requested locale resolved to root/und
UBool isRootLocale;

DateIntervalSink(DateIntervalInfo &diInfo, const char *currentCalendarType, UBool isRoot)
: dateIntervalInfo(diInfo),
nextCalendarType(currentCalendarType, -1, US_INV),
isRootLocale(isRoot) { }
virtual ~DateIntervalSink();

virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/, UErrorCode &errorCode) override {
virtual void put(const char *key, ResourceValue &value, UBool noFallback, UErrorCode &errorCode) override {
if (U_FAILURE(errorCode)) { return; }

// Iterate over all the calendar entries and only pick the 'intervalFormats' table.
Expand All @@ -266,6 +271,11 @@ struct DateIntervalInfo::DateIntervalSink : public ResourceSink {
break;

} else if (value.getType() == URES_TABLE) {
// Per UTS #35 Part 4, intervalFormatItem patterns do not inherit from root
// for non-root locales. Root's calendar aliases (above) are still followed.
if (noFallback && !isRootLocale) {
break;
}
// Iterate over all the skeletons in the 'intervalFormat' table.
ResourceTable skeletonData = value.getTable(errorCode);
if (U_FAILURE(errorCode)) { return; }
Expand Down Expand Up @@ -444,7 +454,13 @@ DateIntervalInfo::initializeData(const Locale& locale, UErrorCode& status)


// Instantiate the sink
DateIntervalSink sink(*this, calendarTypeToUse);
UErrorCode locStatus = U_ZERO_ERROR;
const char *actualLocale = ures_getLocaleByType(rb, ULOC_ACTUAL_LOCALE, &locStatus);
UBool isRootLocale = U_SUCCESS(locStatus)
&& actualLocale != nullptr
&& (actualLocale[0] == '\0'
|| uprv_strcmp(actualLocale, "root") == 0);
DateIntervalSink sink(*this, calendarTypeToUse, isRootLocale);
const UnicodeString &calendarTypeToUseUString = sink.getNextCalendarType();

// Already loaded calendar types
Expand Down
218 changes: 218 additions & 0 deletions icu4c/source/test/intltest/dtifmtts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void DateIntervalFormatTest::runIndexedTest( int32_t index, UBool exec, const ch
TESTCASE_AUTO(testTicket21939);
TESTCASE_AUTO(testTicket20710_FieldIdentity);
TESTCASE_AUTO(testTicket20710_IntervalIdentity);
TESTCASE_AUTO(testTicket23656);
TESTCASE_AUTO_END;
}

Expand Down Expand Up @@ -2477,4 +2478,221 @@ void DateIntervalFormatTest::testTicket20710_IntervalIdentity() {

}

void DateIntervalFormatTest::testTicket23656() {
IcuTestErrorCode status(*this, "testTicket23656");

// Non-root locales must not inherit intervalFormatItem patterns from root.
{
Locale frChineseLoc = Locale::forLanguageTag("fr-u-ca-chinese", status);
DateIntervalInfo frChinese(frChineseLoc, status);
if (status.errDataIfFailureAndReset()) { return; }
UnicodeString pattern;
frChinese.getIntervalPattern(u"y", UCAL_YEAR, pattern, status);
assertEquals("fr-u-ca-chinese must not inherit y/y from root", u"", pattern);
pattern.remove();
frChinese.getIntervalPattern(u"yMd", UCAL_DATE, pattern, status);
assertEquals("fr-u-ca-chinese must not inherit yMd/d from root", u"", pattern);
status.errIfFailureAndReset();

// Fallback pattern still inherits from root.
UnicodeString fallback;
frChinese.getFallbackIntervalPattern(fallback);
assertEquals("fr-u-ca-chinese fallback pattern", u"{0}\u2009\u2013\u2009{1}", fallback);

// Formatting uses French's localized Chinese yMd pattern with fallback rather than root's
// ISO-like U-MM-dd pattern.
LocalPointer<DateIntervalFormat> dif(
DateIntervalFormat::createInstance(u"yMd", frChineseLoc, status));
LocalPointer<Calendar> cal1(Calendar::createInstance(*TimeZone::getGMT(), frChineseLoc, status));
if (status.errDataIfFailureAndReset() || dif.isNull() || cal1.isNull()) { return; }
LocalPointer<Calendar> cal2(cal1->clone());
if (cal2.isNull()) { errln("cal1->clone() returned nullptr"); return; }
// 2024-01-10 UTC and 2024-01-12 UTC
cal1->setTime(1704844800000.0, status);
cal2->setTime(1705017600000.0, status);
UnicodeString formatted;
FieldPosition pos(0);
dif->format(*cal1, *cal2, formatted, pos, status);
assertSuccess("fr-u-ca-chinese yMd format", status);
assertEquals("fr-u-ca-chinese yMd should use fallback with French d/M/y",
u"29/11/40\u2009\u2013\u20092/12/40", formatted);

// Combined date+time formatting when fIntervalPatterns is empty must still produce
// single date + time range on the same day (not repeating the date).
LocalPointer<DateIntervalFormat> difDateTime(
DateIntervalFormat::createInstance(u"yMdHm", frChineseLoc, status));
if (assertSuccess("fr-u-ca-chinese yMdHm createInstance", status) && difDateTime.isValid()) {
// Same day: 2024-01-10 10:00 UTC to 2024-01-10 12:00 UTC
cal1->setTime(1704880800000.0, status);
cal2->setTime(1704888000000.0, status);
formatted.remove();
pos.setBeginIndex(0);
pos.setEndIndex(0);
difDateTime->format(*cal1, *cal2, formatted, pos, status);
assertSuccess("fr-u-ca-chinese yMdHm format", status);
assertEquals("fr-u-ca-chinese yMdHm same-day should format date once + time range",
u"29/11/40, 10:00\u2009\u2013\u200912:00", formatted);
}

// Date+hour skeleton without minutes on same day differing only in minutes (empty map).
LocalPointer<DateIntervalFormat> difDateHour(
DateIntervalFormat::createInstance(u"yMdH", frChineseLoc, status));
if (assertSuccess("fr-u-ca-chinese yMdH createInstance", status) && difDateHour.isValid()) {
// Same day: 2024-01-10 10:00 UTC to 2024-01-10 10:30 UTC
cal1->setTime(1704880800000.0, status);
cal2->setTime(1704882600000.0, status);
formatted.remove();
pos.setBeginIndex(0);
pos.setEndIndex(0);
difDateHour->format(*cal1, *cal2, formatted, pos, status);
assertSuccess("fr-u-ca-chinese yMdH format", status);
assertEquals("fr-u-ca-chinese yMdH same-hour minute diff",
u"29/11/40 10", formatted);
}
status.errIfFailureAndReset();
}

// dangi aliases chinese in root; fr has no chinese interval patterns, so dangi gets none.
{
Locale frDangiLoc = Locale::forLanguageTag("fr-u-ca-dangi", status);
DateIntervalInfo frDangi(frDangiLoc, status);
UnicodeString pattern;
frDangi.getIntervalPattern(u"y", UCAL_YEAR, pattern, status);
assertEquals("fr-u-ca-dangi must not inherit y/y from root", u"", pattern);

LocalPointer<DateIntervalFormat> dif(
DateIntervalFormat::createInstance(u"yMd", frDangiLoc, status));
LocalPointer<Calendar> cal1(Calendar::createInstance(*TimeZone::getGMT(), frDangiLoc, status));
if (assertSuccess("fr-u-ca-dangi createInstance", status) && dif.isValid() && cal1.isValid()) {
LocalPointer<Calendar> cal2(cal1->clone());
if (cal2.isValid()) {
cal1->setTime(1704844800000.0, status);
cal2->setTime(1705017600000.0, status);
UnicodeString formatted;
FieldPosition pos(0);
dif->format(*cal1, *cal2, formatted, pos, status);
assertSuccess("fr-u-ca-dangi yMd format", status);
assertEquals("fr-u-ca-dangi yMd fallback",
u"29/11/40\u2009\u2013\u20092/12/40", formatted);
}
}
status.errIfFailureAndReset();
}

// Non-root locales without intervalFormatItem data:
// - agq: has calendar/gregorian and calendar/generic without intervalFormatItem
// - agq-CM: empty child bundle inheriting from agq
// - agq-u-ca-chinese: calendar absent from agq, so lookup jumps straight to root
// - uz-Cyrl: %%Parent{"root"} locale without intervalFormatItem
for (const char* tag : {"agq", "agq-CM", "agq-u-ca-chinese", "uz-Cyrl"}) {
Locale loc = Locale::forLanguageTag(tag, status);
DateIntervalInfo info(loc, status);
UnicodeString pattern;
info.getIntervalPattern(u"y", UCAL_YEAR, pattern, status);
assertEquals(UnicodeString(tag) + u" must not inherit y/y from root", u"", pattern);
UnicodeString fallback;
info.getFallbackIntervalPattern(fallback);
assertFalse(UnicodeString(tag) + u" should still inherit fallback pattern", fallback.isEmpty());
status.errIfFailureAndReset();
}

// End-to-end formatting on agq (empty fIntervalPatterns) with yMd and yMdH.
{
Locale agqLoc = Locale::forLanguageTag("agq", status);
LocalPointer<DateIntervalFormat> difYmd(
DateIntervalFormat::createInstance(u"yMd", agqLoc, status));
LocalPointer<DateIntervalFormat> difYmdH(
DateIntervalFormat::createInstance(u"yMdH", agqLoc, status));
LocalPointer<Calendar> cal1(Calendar::createInstance(*TimeZone::getGMT(), agqLoc, status));
if (assertSuccess("agq createInstance", status)
&& difYmd.isValid() && difYmdH.isValid() && cal1.isValid()) {
LocalPointer<Calendar> cal2(cal1->clone());
if (cal2.isValid()) {
cal1->setTime(1704844800000.0, status);
cal2->setTime(1705017600000.0, status);
UnicodeString formatted;
FieldPosition pos(0);
difYmd->format(*cal1, *cal2, formatted, pos, status);
assertSuccess("agq yMd format", status);
assertEquals("agq yMd fallback", u"10/1/2024\u2009\u2013\u200912/1/2024", formatted);

cal1->setTime(1704880800000.0, status);
cal2->setTime(1704882600000.0, status);
formatted.remove();
pos.setBeginIndex(0);
pos.setEndIndex(0);
difYmdH->format(*cal1, *cal2, formatted, pos, status);
assertSuccess("agq yMdH format", status);
assertFalse("agq yMdH should produce non-empty output", formatted.isEmpty());
}
}
status.errIfFailureAndReset();
}

// Calendar aliases in root must still resolve to the non-root locale's target calendar.
{
DateIntervalInfo frBuddhist(Locale::forLanguageTag("fr-u-ca-buddhist", status), status);
UnicodeString pattern;
frBuddhist.getIntervalPattern(u"y", UCAL_YEAR, pattern, status);
assertEquals("fr-u-ca-buddhist should inherit y/y from fr generic via root alias",
u"y\u2009\u2013\u2009y G", pattern);

DateIntervalInfo frIslamicCivil(Locale::forLanguageTag("fr-u-ca-islamic-civil", status), status);
pattern.remove();
frIslamicCivil.getIntervalPattern(u"y", UCAL_YEAR, pattern, status);
assertEquals("fr-u-ca-islamic-civil should inherit y/y via 2-hop root alias",
u"y\u2009\u2013\u2009y G", pattern);

DateIntervalInfo zhDangi(Locale::forLanguageTag("zh-u-ca-dangi", status), status);
pattern.remove();
zhDangi.getIntervalPattern(u"y", UCAL_YEAR, pattern, status);
assertFalse("zh-u-ca-dangi should inherit y/y from zh chinese via root alias", pattern.isEmpty());

DateIntervalInfo zhBuddhist(Locale::forLanguageTag("zh-u-ca-buddhist", status), status);
pattern.remove();
zhBuddhist.getIntervalPattern(u"MMM", UCAL_MONTH, pattern, status);
assertEquals("zh-u-ca-buddhist should keep its own MMM/M override", u"MMM \u2013 MMM", pattern);
pattern.remove();
zhBuddhist.getIntervalPattern(u"y", UCAL_YEAR, pattern, status);
assertFalse("zh-u-ca-buddhist should inherit y/y from zh generic via root alias", pattern.isEmpty());
status.errIfFailureAndReset();
}

// Non-root parent fallback must still work.
{
DateIntervalInfo enGb(Locale::forLanguageTag("en-GB", status), status);
UnicodeString pattern;
enGb.getIntervalPattern(u"yMd", UCAL_DATE, pattern, status);
assertFalse("en-GB should inherit yMd/d from non-root parent chain", pattern.isEmpty());
status.errIfFailureAndReset();
}

// Explicit root/und locales must retain root's own intervalFormatItem patterns, including
// through calendar aliases.
for (const Locale& rootLoc : {
Locale::getRoot(),
Locale("root"),
Locale::forLanguageTag("und", status)}) {
DateIntervalInfo rootInfo(rootLoc, status);
UnicodeString pattern;
rootInfo.getIntervalPattern(u"y", UCAL_YEAR, pattern, status);
assertEquals("root/und should keep its own gregorian y/y pattern", u"y\u2013y", pattern);
status.errIfFailureAndReset();
}
{
Locale undChineseLoc = Locale::forLanguageTag("und-u-ca-chinese", status);
DateIntervalInfo undChinese(undChineseLoc, status);
UnicodeString pattern;
undChinese.getIntervalPattern(u"y", UCAL_YEAR, pattern, status);
assertEquals("und-u-ca-chinese should keep root chinese y/y pattern", u"U\u2013U", pattern);

DateIntervalInfo undBuddhist(Locale::forLanguageTag("und-u-ca-buddhist", status), status);
pattern.remove();
undBuddhist.getIntervalPattern(u"y", UCAL_YEAR, pattern, status);
assertEquals("und-u-ca-buddhist should follow root alias to root generic y/y",
u"G y\u2013y", pattern);
status.errIfFailureAndReset();
}
}

#endif /* #if !UCONFIG_NO_FORMATTING */
1 change: 1 addition & 0 deletions icu4c/source/test/intltest/dtifmtts.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class DateIntervalFormatTest: public IntlTestWithFieldPosition {
void testTicket21939();
void testTicket20710_FieldIdentity();
void testTicket20710_IntervalIdentity();
void testTicket23656();

private:
/**
Expand Down
Loading
Loading