From 38ec2c830b849ad44ca7e16bd9c4722e0bcdb61f Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Fri, 6 Aug 2021 12:29:56 +0200 Subject: [PATCH] qlocale_win: Fix non-standalone month names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We have previously been using the standalong (nominative) month names both when asked for that and when asked for the plain (genitive) month name, probably because there was no LCTYPE value for the latter. However, MS's docs for the standalone values do contain a comment telling us how to get the genitive names. Rename the old monthName() to standaloneMonthName() and add a monthName() that calls GetDateFormat() suitably, as described by the MS doc. Pick-to: 6.2 5.15 Fixes: QTBUG-92018 Fixes: QTBUG-86279 Change-Id: I27f63198c3a15b792683f476d2019078b0860f99 Reviewed-by: Qt CI Bot Reviewed-by: Ivan Solovev Reviewed-by: MÃ¥rten Nordheim --- src/corelib/text/qlocale_win.cpp | 33 +++++++++++++++++-- .../auto/corelib/text/qlocale/tst_qlocale.cpp | 13 ++++++-- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/corelib/text/qlocale_win.cpp b/src/corelib/text/qlocale_win.cpp index 1dd7a1532e..320988203f 100644 --- a/src/corelib/text/qlocale_win.cpp +++ b/src/corelib/text/qlocale_win.cpp @@ -94,6 +94,7 @@ struct QSystemLocalePrivate QVariant timeFormat(QLocale::FormatType); QVariant dateTimeFormat(QLocale::FormatType); QVariant dayName(int, QLocale::FormatType); + QVariant standaloneMonthName(int, QLocale::FormatType); QVariant monthName(int, QLocale::FormatType); QVariant toString(QDate, QLocale::FormatType); QVariant toString(QTime, QLocale::FormatType); @@ -366,7 +367,7 @@ QVariant QSystemLocalePrivate::dayName(int day, QLocale::FormatType type) return getLocaleInfo(short_day_map[day]); } -QVariant QSystemLocalePrivate::monthName(int month, QLocale::FormatType type) +QVariant QSystemLocalePrivate::standaloneMonthName(int month, QLocale::FormatType type) { static const LCTYPE short_month_map[] = { LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2, LOCALE_SABBREVMONTHNAME3, @@ -388,6 +389,30 @@ QVariant QSystemLocalePrivate::monthName(int month, QLocale::FormatType type) (type == QLocale::LongFormat ? long_month_map : short_month_map)[month - 1]); } +QVariant QSystemLocalePrivate::monthName(int month, QLocale::FormatType type) +{ + SYSTEMTIME st = {}; + st.wYear = 2001; + st.wMonth = month; + st.wDay = 10; + + const DWORD flags{}; // Must be clear when passing a format string. + // MS's docs for the LOCALE_SMONTHNAME* say to include the day in a format. + // Educated guess: this works for the LOCALE_SABBREVMONTHNAME*, too, in so + // far as the abbreviated plain name might differ from abbreviated + // standalone one. + const wchar_t *const format = type == QLocale::LongFormat ? L"ddMMMM" : L"ddMMM"; + wchar_t buf[255]; + if (getDateFormat(flags, &st, format, buf, 255) > 2) { + // Elide the two digits of day number + QString text = QString::fromWCharArray(buf + 2); + if (substitution() == SAlways) + text = substituteDigits(std::move(text)); + return text; + } + return {}; +} + QVariant QSystemLocalePrivate::toString(QDate date, QLocale::FormatType type) { SYSTEMTIME st = {}; @@ -733,11 +758,13 @@ QVariant QSystemLocale::query(QueryType type, QVariant in) const case DayNameShort: return d->dayName(in.toInt(), QLocale::ShortFormat); case MonthNameLong: - case StandaloneMonthNameLong: return d->monthName(in.toInt(), QLocale::LongFormat); + case StandaloneMonthNameLong: + return d->standaloneMonthName(in.toInt(), QLocale::LongFormat); case MonthNameShort: - case StandaloneMonthNameShort: return d->monthName(in.toInt(), QLocale::ShortFormat); + case StandaloneMonthNameShort: + return d->standaloneMonthName(in.toInt(), QLocale::ShortFormat); case DateToStringShort: return d->toString(in.toDate(), QLocale::ShortFormat); case DateToStringLong: diff --git a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp index 631ca9ff0a..52f0c899a8 100644 --- a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp @@ -2614,6 +2614,10 @@ void tst_QLocale::dateFormat() const QLocale ir("ga_IE"); QCOMPARE(ir.dateFormat(QLocale::ShortFormat), QLatin1String("dd/MM/yyyy")); + + const auto sys = QLocale::system(); // QTBUG-92018, ru_RU on MS + const QDate date(2021, 3, 17); + QCOMPARE(sys.toString(date, sys.dateFormat(QLocale::LongFormat)), sys.toString(date)); } void tst_QLocale::timeFormat() @@ -2672,18 +2676,21 @@ void tst_QLocale::monthName() // 'de' locale doesn't have narrow month name QCOMPARE(de.monthName(12, QLocale::NarrowFormat), QLatin1String("D")); - QLocale ru("ru_RU"); + const QLocale ru("ru_RU"); QCOMPARE(ru.monthName(1, QLocale::LongFormat), QString::fromUtf8("\321\217\320\275\320\262\320\260\321\200\321\217")); QCOMPARE(ru.monthName(1, QLocale::ShortFormat), QString::fromUtf8("\321\217\320\275\320\262\56")); QCOMPARE(ru.monthName(1, QLocale::NarrowFormat), QString::fromUtf8("\320\257")); + const auto sys = QLocale::system(); + if (sys.language() == QLocale::Russian) // QTBUG-92018 + QVERIFY(sys.monthName(3) != sys.standaloneMonthName(3)); - QLocale ir("ga_IE"); + const QLocale ir("ga_IE"); QCOMPARE(ir.monthName(1, QLocale::ShortFormat), QLatin1String("Ean")); QCOMPARE(ir.monthName(12, QLocale::ShortFormat), QLatin1String("Noll")); - QLocale cz("cs_CZ"); + const QLocale cz("cs_CZ"); QCOMPARE(cz.monthName(1, QLocale::ShortFormat), QLatin1String("led")); QCOMPARE(cz.monthName(12, QLocale::ShortFormat), QLatin1String("pro")); }