summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/time
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2023-09-27 16:35:48 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2023-10-27 10:52:48 +0200
commit58fd829cdf6899a5f01f759f2018b40682813f15 (patch)
tree86c48ba405cd5b282b82583e93f4124e252f4dff /tests/auto/corelib/time
parent4756062828c30e4b7ed63f8dae4239799333b7d3 (diff)
Use localized time-zone abbreviations or offset
The actual formatting of date-time strings is handled by the calendar backend, but the code's in qlocale.cpp as it uses some of its tools. When feature timezone is unavailable, we're stuck (as before) with using QDateTime::timeZoneAbbreviation(), but when it's available we can use QTimeZone::displayName() to get the localized form of the abbreviation and offset string. Make matching changes in QDTP so that it recognizes these localized abbreviations. We now have another candidate for what local time might be called, to add to those that must be checked. This naturally implied some changes to tests. It turns out ICU believes en_US uses GMT+1/GMT+2 for CET/CEST. Replace some MS QEXPECT_FAIL()s by including the non-abbreviations we do in fact use on MS in the lists of "abbreviations" to accept. [ChangeLog][QtCore][QLocale] When a datetime format includes the timezone (or offset), the appropriately localised form is (to the extent the timezone backend in use supports this) used where, previously, a haphazard choice of system and C locale was used. This applies to both serialization and parsing. Task-number: QTBUG-115158 Change-Id: I04f9c1055c3b9008320bb8b758490287fd8be5cd Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/time')
-rw-r--r--tests/auto/corelib/time/qdatetimeparser/tst_qdatetimeparser.cpp44
1 files changed, 35 insertions, 9 deletions
diff --git a/tests/auto/corelib/time/qdatetimeparser/tst_qdatetimeparser.cpp b/tests/auto/corelib/time/qdatetimeparser/tst_qdatetimeparser.cpp
index 7f97e551a6..1da4affefb 100644
--- a/tests/auto/corelib/time/qdatetimeparser/tst_qdatetimeparser.cpp
+++ b/tests/auto/corelib/time/qdatetimeparser/tst_qdatetimeparser.cpp
@@ -4,6 +4,8 @@
#include <QTest>
#include <private/qdatetimeparser_p.h>
+using namespace Qt::StringLiterals;
+
QT_BEGIN_NAMESPACE
// access to needed members in QDateTimeParser
@@ -58,11 +60,11 @@ void tst_QDateTimeParser::reparse()
{
const QDateTime when = QDate(2023, 6, 15).startOfDay();
// QTBUG-114575: 6.2 through 6.5 got back a bogus Qt::TimeZone (with zero offset):
- const Qt::TimeSpec spec = ([](QStringView name) {
+ const auto expect = ([](QStringView name) {
// When local time is UTC or a fixed offset from it, the parser prefers
// to interpret a UTC or offset suffix as such, rather than as local
// time (thereby avoiding DST-ness checks). We have to match that here.
- if (name == QLatin1StringView("UTC"))
+ if (name == "UTC"_L1)
return Qt::UTC;
if (name.startsWith(u'+') || name.startsWith(u'-')) {
if (std::all_of(name.begin() + 1, name.end(), [](QChar ch) { return ch == u'0'; }))
@@ -72,17 +74,41 @@ void tst_QDateTimeParser::reparse()
// Potential hh:mm offset ? Not yet seen as local tzname[] entry.
}
return Qt::LocalTime;
- })(when.timeZoneAbbreviation());
+ });
const QStringView format = u"dd/MM/yyyy HH:mm t";
QDateTimeParser who(QMetaType::QDateTime, QDateTimeParser::DateTimeEdit);
QVERIFY(who.parseFormat(format));
- const auto state = who.parse(when.toString(format), -1, when, false);
- QCOMPARE(state.state, QDateTimeParser::Acceptable);
- QVERIFY(!state.conflicts);
- QCOMPARE(state.padded, 0);
- QCOMPARE(state.value.timeSpec(), spec);
- QCOMPARE(state.value, when);
+ {
+ // QDTP defaults to the system locale.
+ const auto state = who.parse(QLocale::system().toString(when, format), -1, when, false);
+ QCOMPARE(state.state, QDateTimeParser::Acceptable);
+ QVERIFY(!state.conflicts);
+ QCOMPARE(state.padded, 0);
+ QCOMPARE(state.value.timeSpec(), expect(when.timeZoneAbbreviation()));
+ QCOMPARE(state.value, when);
+ }
+ {
+ // QDT::toString() uses the C locale:
+ who.setDefaultLocale(QLocale::c());
+ const QString zoneName = ([when]() {
+#if QT_CONFIG(timezone)
+ if (QLocale::c() != QLocale::system()) {
+ const QString local = when.timeRepresentation().displayName(
+ when, QTimeZone::ShortName, QLocale::c());
+ if (!local.isEmpty())
+ return local;
+ }
+#endif
+ return when.timeZoneAbbreviation();
+ })();
+ const auto state = who.parse(when.toString(format), -1, when, false);
+ QCOMPARE(state.state, QDateTimeParser::Acceptable);
+ QVERIFY(!state.conflicts);
+ QCOMPARE(state.padded, 0);
+ QCOMPARE(state.value.timeSpec(), expect(zoneName));
+ QCOMPARE(state.value, when);
+ }
}
void tst_QDateTimeParser::parseSection_data()