Improve fidelity of approximation to CLDR zone representations

I neglected to update the CLDR dateconverter code when I expanded the
range of forms we support for display of a timezone. Even that
expanded range doesn't cover all the cases CLDR does, but we can at
least approximate each of CLDR's options by the closest we do support.
Make matching changes to how the Darwin backend for the system locale
maps its ICU-derived formats to ours.

This in practice changes all locales previously using t (abbreviation)
as zone format to use tttt (IANA ID) instead. Test data updated to
match.

[ChangeLog][QtCore][QLocale] Date-time formats now more faithfully
follow the CLDR data in handling timezones. In most cases this means
the IANA ID is used in place of the abbreviation.

Change-Id: I0276843085839ba9a7855a78922cffe285174643
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2024-04-02 20:31:11 +02:00
parent 9e57e523d3
commit dd56558ecd
4 changed files with 750 additions and 722 deletions

File diff suppressed because it is too large Load Diff

View File

@ -403,13 +403,19 @@ static QVariant macToQtFormat(QStringView sys_fmt)
result += "zzz"_L1; result += "zzz"_L1;
break; break;
case 'O': // Time Zone (1, 4) case 'O': // Time Zone (1, 4)
result += u't';
break;
case 'v': // Time Zone (1, 4) case 'v': // Time Zone (1, 4)
case 'V': // Time Zone (1..4) case 'V': // Time Zone (1..4)
result += "tttt"_L1;
break;
case 'x': // Time Zone (1..5) case 'x': // Time Zone (1..5)
case 'X': // Time Zone (1..5) case 'X': // Time Zone (1..5)
result += (repeat > 1 && (repeat & 1)) ? "ttt"_L1 : "tt"_L1;
break;
case 'z': // Time Zone (1..4) case 'z': // Time Zone (1..4)
case 'Z': // Time Zone (1..5) case 'Z': // Time Zone (1..5)
result += u't'; result += repeat < 4 ? "tt"_L1 : repeat > 4 ? "ttt"_L1 : "t"_L1;
break; break;
default: default:
// a..z and A..Z are reserved for format codes, so any occurrence of these not // a..z and A..Z are reserved for format codes, so any occurrence of these not

View File

@ -3327,24 +3327,24 @@ void tst_QLocale::timeFormat()
const QLocale no("no_NO"); const QLocale no("no_NO");
QCOMPARE(no.timeFormat(QLocale::NarrowFormat), QLatin1String("HH:mm")); QCOMPARE(no.timeFormat(QLocale::NarrowFormat), QLatin1String("HH:mm"));
QCOMPARE(no.timeFormat(QLocale::ShortFormat), QLatin1String("HH:mm")); QCOMPARE(no.timeFormat(QLocale::ShortFormat), QLatin1String("HH:mm"));
QCOMPARE(no.timeFormat(QLocale::LongFormat), QLatin1String("HH:mm:ss t")); QCOMPARE(no.timeFormat(QLocale::LongFormat), "HH:mm:ss tttt"_L1);
const QLocale id("id_ID"); const QLocale id("id_ID");
QCOMPARE(id.timeFormat(QLocale::ShortFormat), QLatin1String("HH.mm")); QCOMPARE(id.timeFormat(QLocale::ShortFormat), QLatin1String("HH.mm"));
QCOMPARE(id.timeFormat(QLocale::LongFormat), QLatin1String("HH.mm.ss t")); QCOMPARE(id.timeFormat(QLocale::LongFormat), "HH.mm.ss tttt"_L1);
const QLocale cat("ca_ES"); const QLocale cat("ca_ES");
QCOMPARE(cat.timeFormat(QLocale::ShortFormat), QLatin1String("H:mm")); QCOMPARE(cat.timeFormat(QLocale::ShortFormat), QLatin1String("H:mm"));
QCOMPARE(cat.timeFormat(QLocale::LongFormat), QLatin1String("H:mm:ss (t)")); QCOMPARE(cat.timeFormat(QLocale::LongFormat), "H:mm:ss (tttt)"_L1);
const QLocale bra("pt_BR"); const QLocale bra("pt_BR");
QCOMPARE(bra.timeFormat(QLocale::ShortFormat), QLatin1String("HH:mm")); QCOMPARE(bra.timeFormat(QLocale::ShortFormat), QLatin1String("HH:mm"));
QCOMPARE(bra.timeFormat(QLocale::LongFormat), QLatin1String("HH:mm:ss t")); QCOMPARE(bra.timeFormat(QLocale::LongFormat), "HH:mm:ss tttt"_L1);
// QTBUG-123872 - we kludge CLDR's B to Ap: // QTBUG-123872 - we kludge CLDR's B to Ap:
const QLocale tw("zh_TW"); const QLocale tw("zh_TW");
QCOMPARE(tw.timeFormat(QLocale::ShortFormat), "Aph:mm"_L1); QCOMPARE(tw.timeFormat(QLocale::ShortFormat), "Aph:mm"_L1);
QCOMPARE(tw.timeFormat(QLocale::LongFormat), "Aph:mm:ss [t]"_L1); QCOMPARE(tw.timeFormat(QLocale::LongFormat), "Aph:mm:ss [tttt]"_L1);
} }
void tst_QLocale::dateTimeFormat() void tst_QLocale::dateTimeFormat()
@ -3356,7 +3356,7 @@ void tst_QLocale::dateTimeFormat()
const QLocale no("no_NO"); const QLocale no("no_NO");
QCOMPARE(no.dateTimeFormat(QLocale::NarrowFormat), QLatin1String("dd.MM.yyyy HH:mm")); QCOMPARE(no.dateTimeFormat(QLocale::NarrowFormat), QLatin1String("dd.MM.yyyy HH:mm"));
QCOMPARE(no.dateTimeFormat(QLocale::ShortFormat), QLatin1String("dd.MM.yyyy HH:mm")); QCOMPARE(no.dateTimeFormat(QLocale::ShortFormat), QLatin1String("dd.MM.yyyy HH:mm"));
QCOMPARE(no.dateTimeFormat(QLocale::LongFormat), QLatin1String("dddd d. MMMM yyyy HH:mm:ss t")); QCOMPARE(no.dateTimeFormat(QLocale::LongFormat), "dddd d. MMMM yyyy HH:mm:ss tttt"_L1);
} }
void tst_QLocale::monthName() void tst_QLocale::monthName()

View File

@ -87,6 +87,10 @@ class Converter (object):
m = __verbatim # Minute within the hour. m = __verbatim # Minute within the hour.
M = L # Plain month names, possibly abbreviated, and numbers. M = L # Plain month names, possibly abbreviated, and numbers.
@classmethod
def O(cls, text): # Localized GMT±offset formats. Map to Z-or-UTC±HH:mm
return 't', cls.__count_first(text)
# q: Quarter. Not supported. # q: Quarter. Not supported.
# Q: Quarter. Not supported. # Q: Quarter. Not supported.
@ -109,13 +113,20 @@ class Converter (object):
# U: Cyclic Year Name. Not supported # U: Cyclic Year Name. Not supported
@classmethod @classmethod
def v(cls, text): # Generic non-location format. Map to abbreviation. def v(cls, text): # Generic non-location format. Map to name.
return 't', cls.__count_first(text) return 'tttt', cls.__count_first(text)
V = v # Zone ID in various forms; VV is IANA ID. Map to abbreviation. V = v # Zone ID in various forms; VV is IANA ID. Map to name.
# w: Week of year. Not supported. # w: Week of year. Not supported.
# W: Week of month. Not supported. # W: Week of month. Not supported.
@classmethod
def x(cls, text): # Variations on offset format.
n = cls.__count_first(text)
# Ignore: n == 1 may omit minutes, n > 3 may include seconds.
return ('ttt' if n > 1 and n & 1 else 'tt'), n
X = x # Should use Z for zero offset.
@classmethod @classmethod
def y(cls, text): # Year number. def y(cls, text): # Year number.
n = cls.__count_first(text) n = cls.__count_first(text)
@ -123,7 +134,10 @@ class Converter (object):
# Y: Year for Week-of-year calendars # Y: Year for Week-of-year calendars
z = v # Specific (i.e. distinguish standard from DST) non-location format. z = v # Specific (i.e. distinguish standard from DST) non-location format.
Z = v # Offset format, optionaly with GMT (Qt uses UTC) prefix. @classmethod
def Z(cls, text): # Offset format, optionaly with GMT (Qt uses UTC) prefix.
n = cls.__count_first(text)
return ('tt' if n < 4 else 'ttt' if n > 4 else 't'), n
@staticmethod @staticmethod
def scanQuote(text): # Can't have ' as a method name, so handle specially def scanQuote(text): # Can't have ' as a method name, so handle specially