diff options
Diffstat (limited to 'tests/auto/corelib/time')
-rw-r--r-- | tests/auto/corelib/time/CMakeLists.txt | 4 | ||||
-rw-r--r-- | tests/auto/corelib/time/qcalendar/CMakeLists.txt | 6 | ||||
-rw-r--r-- | tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp | 77 | ||||
-rw-r--r-- | tests/auto/corelib/time/qdate/CMakeLists.txt | 7 | ||||
-rw-r--r-- | tests/auto/corelib/time/qdate/tst_qdate.cpp | 736 | ||||
-rw-r--r-- | tests/auto/corelib/time/qdatetime/CMakeLists.txt | 7 | ||||
-rw-r--r-- | tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp | 972 | ||||
-rw-r--r-- | tests/auto/corelib/time/qdatetime/tst_qdatetime_mac.mm | 2 | ||||
-rw-r--r-- | tests/auto/corelib/time/qdatetimeparser/CMakeLists.txt | 6 | ||||
-rw-r--r-- | tests/auto/corelib/time/qdatetimeparser/tst_qdatetimeparser.cpp | 64 | ||||
-rw-r--r-- | tests/auto/corelib/time/qtime/CMakeLists.txt | 7 | ||||
-rw-r--r-- | tests/auto/corelib/time/qtime/tst_qtime.cpp | 196 | ||||
-rw-r--r-- | tests/auto/corelib/time/qtimezone/CMakeLists.txt | 7 | ||||
-rw-r--r-- | tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp | 311 | ||||
-rw-r--r-- | tests/auto/corelib/time/qtimezone/tst_qtimezone_darwin.mm | 6 |
15 files changed, 1464 insertions, 944 deletions
diff --git a/tests/auto/corelib/time/CMakeLists.txt b/tests/auto/corelib/time/CMakeLists.txt index b593cc54d6..1fb95e9245 100644 --- a/tests/auto/corelib/time/CMakeLists.txt +++ b/tests/auto/corelib/time/CMakeLists.txt @@ -4,7 +4,9 @@ add_subdirectory(qcalendar) add_subdirectory(qdate) add_subdirectory(qdatetime) -add_subdirectory(qdatetimeparser) +if(QT_FEATURE_datetimeparser) + add_subdirectory(qdatetimeparser) +endif() add_subdirectory(qtime) if(QT_FEATURE_timezone) add_subdirectory(qtimezone) diff --git a/tests/auto/corelib/time/qcalendar/CMakeLists.txt b/tests/auto/corelib/time/qcalendar/CMakeLists.txt index 6165850324..5b1b2dfc2a 100644 --- a/tests/auto/corelib/time/qcalendar/CMakeLists.txt +++ b/tests/auto/corelib/time/qcalendar/CMakeLists.txt @@ -5,6 +5,12 @@ ## tst_qcalendar Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qcalendar LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qcalendar SOURCES tst_qcalendar.cpp diff --git a/tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp b/tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp index acc190c92e..61999202d2 100644 --- a/tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp +++ b/tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2020 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> @@ -31,6 +31,49 @@ private slots: void gregory(); }; +static void checkCenturyResolution(const QCalendar &cal, const QCalendar::YearMonthDay &base) +{ + quint8 weekDayMask = 0; + for (int offset = -7; offset < 8; ++offset) { + const auto probe = QDate(base.year, base.month, base.day, cal).addYears(100 * offset, cal); + const int dow = cal.dayOfWeek(probe); + if (probe.isValid() && dow > 0 && dow < 8) + weekDayMask |= 1 << quint8(dow - 1); + } + for (int j = 1; j < 8; ++j) { + const bool seen = weekDayMask & (1 << quint8(j - 1)); + const QDate check = cal.matchCenturyToWeekday(base, j); + if (check.isValid()) { + const auto parts = cal.partsFromDate(check); + const int dow = cal.dayOfWeek(check); + QCOMPARE(dow, j); + QCOMPARE(parts.day, base.day); + QCOMPARE(parts.month, base.month); + int gap = parts.year - base.year; + if (!cal.hasYearZero() && (parts.year > 0) != (base.year > 0)) + gap += parts.year > 0 ? -1 : +1; + auto report = qScopeGuard([parts, base]() { + qDebug("Wrongly matched year: %d replaced %d", parts.year, base.year); + }); + QCOMPARE(gap % 100, 0); + // We searched 7 centuries each side of base. + if (seen) { + QCOMPARE_LT(gap / 100, 8); + QCOMPARE_GT(gap / 100, -8); + } else { + QVERIFY(gap / 100 >= 8 || gap / 100 <= -8); + } + report.dismiss(); + } else { + auto report = qScopeGuard([j, base]() { + qDebug("Missed dow[%d] for %d/%d/%d", j, base.year, base.month, base.day); + }); + QVERIFY(!seen); + report.dismiss(); + } + } +} + // Support for basic(): void tst_QCalendar::checkYear(const QCalendar &cal, int year, bool normal) { @@ -49,7 +92,7 @@ void tst_QCalendar::checkYear(const QCalendar &cal, int year, bool normal) int sum = 0; const int longest = cal.maximumDaysInMonth(); - for (int i = moons; i > 0; i--) { + for (int i = moons; i > 0; --i) { const int last = cal.daysInMonth(i, year); sum += last; // Valid month has some days and no more than max: @@ -62,6 +105,10 @@ void tst_QCalendar::checkYear(const QCalendar &cal, int year, bool normal) QVERIFY(!cal.isDateValid(year, i, last + 1)); if (normal) // Unspecified year gets same daysInMonth(): QCOMPARE(cal.daysInMonth(i), last); + + checkCenturyResolution(cal, {year, i, (last + 1) / 2}); + if (QTest::currentTestFailed()) + return; } // Months add up to the whole year: QCOMPARE(sum, days); @@ -368,13 +415,12 @@ void tst_QCalendar::gregory() // dateToJulianDay() and weekDayOfJulian(): if (!year) // No year zero. continue; - qint64 first, last; - QVERIFY2(QGregorianCalendar::julianFromParts(year, 1, 1, &first), - "Only year zero should lack a first day"); + const auto first = QGregorianCalendar::julianFromParts(year, 1, 1); + QVERIFY2(first, "Only year zero should lack a first day"); QCOMPARE(QGregorianCalendar::yearStartWeekDay(year), - QGregorianCalendar::weekDayOfJulian(first)); - QVERIFY2(QGregorianCalendar::julianFromParts(year, 12, 31, &last), - "Only year zero should lack a last day"); + QGregorianCalendar::weekDayOfJulian(*first)); + const auto last = QGregorianCalendar::julianFromParts(year, 12, 31); + QVERIFY2(last, "Only year zero should lack a last day"); const int lastTwo = (year + (year < 0 ? 1 : 0)) % 100 + (year < -1 ? 100 : 0); const QDate probe(year, lastTwo && lastTwo <= 12 ? lastTwo : 8, @@ -392,13 +438,14 @@ void tst_QCalendar::gregory() if (year > 0 && lastTwo > 31) QCOMPARE(match % 100, lastTwo); // Its first and last days of the year match those of year: - qint64 day; - QVERIFY(QGregorianCalendar::julianFromParts(match, 1, 1, &day)); - QCOMPARE(QGregorianCalendar::weekDayOfJulian(day), - QGregorianCalendar::weekDayOfJulian(first)); - QVERIFY(QGregorianCalendar::julianFromParts(match, 12, 31, &day)); - QCOMPARE(QGregorianCalendar::weekDayOfJulian(day), - QGregorianCalendar::weekDayOfJulian(last)); + auto day = QGregorianCalendar::julianFromParts(match, 1, 1); + QVERIFY(day); + QCOMPARE(QGregorianCalendar::weekDayOfJulian(*day), + QGregorianCalendar::weekDayOfJulian(*first)); + day = QGregorianCalendar::julianFromParts(match, 12, 31); + QVERIFY(day); + QCOMPARE(QGregorianCalendar::weekDayOfJulian(*day), + QGregorianCalendar::weekDayOfJulian(*last)); } } } diff --git a/tests/auto/corelib/time/qdate/CMakeLists.txt b/tests/auto/corelib/time/qdate/CMakeLists.txt index a93d263857..4d0f04a967 100644 --- a/tests/auto/corelib/time/qdate/CMakeLists.txt +++ b/tests/auto/corelib/time/qdate/CMakeLists.txt @@ -5,6 +5,12 @@ ## tst_qdate Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qdate LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qdate SOURCES tst_qdate.cpp @@ -13,4 +19,5 @@ qt_internal_add_test(tst_qdate QT_NO_KEYWORDS LIBRARIES Qt::CorePrivate + Qt::TestPrivate ) diff --git a/tests/auto/corelib/time/qdate/tst_qdate.cpp b/tests/auto/corelib/time/qdate/tst_qdate.cpp index 34ca4bcae5..5b715e8c55 100644 --- a/tests/auto/corelib/time/qdate/tst_qdate.cpp +++ b/tests/auto/corelib/time/qdate/tst_qdate.cpp @@ -1,6 +1,6 @@ // Copyright (C) 2022 The Qt Company Ltd. // Copyright (C) 2016 Intel Corporation. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QDateTime> #include <QTest> @@ -10,7 +10,18 @@ #include <QTimeZone> #include <private/qglobal_p.h> // for the icu feature test +#include <private/qcomparisontesthelper_p.h> #include <private/qdatetime_p.h> +#if !QT_CONFIG(timezone) +# include <private/qtenvironmentvariables_p.h> // for qTzName() +#endif + +using namespace QtPrivate::DateTimeConstants; +using namespace Qt::StringLiterals; + +#if defined(Q_OS_WIN) && !QT_CONFIG(icu) +# define USING_WIN_TZ +#endif class tst_QDate : public QObject { @@ -35,10 +46,8 @@ private Q_SLOTS: void weekNumber_invalid(); void weekNumber_data(); void weekNumber(); -#if QT_CONFIG(timezone) void startOfDay_endOfDay_data(); void startOfDay_endOfDay(); -#endif void startOfDay_endOfDay_fixed_data(); void startOfDay_endOfDay_fixed(); void startOfDay_endOfDay_bounds(); @@ -50,12 +59,12 @@ private Q_SLOTS: void addYears_data(); void addYears(); void daysTo(); + void orderingCompiles(); void operator_eq_eq_data(); void operator_eq_eq(); - void operator_lt(); - void operator_gt(); - void operator_lt_eq(); - void operator_gt_eq(); + void ordering_data(); + void ordering(); + void ordering_chrono_types(); void operator_insert_extract_data(); void operator_insert_extract(); #if QT_CONFIG(datestring) @@ -88,6 +97,8 @@ private: return QDate::fromJulianDay(JULIAN_DAY_FOR_EPOCH); } + static constexpr qint64 minJd = JulianDayMin; + static constexpr qint64 maxJd = JulianDayMax; QDate invalidDate() const { return QDate(); } }; @@ -98,9 +109,6 @@ void tst_QDate::isNull_data() QTest::addColumn<qint64>("jd"); QTest::addColumn<bool>("null"); - qint64 minJd = Q_INT64_C(-784350574879); - qint64 maxJd = Q_INT64_C( 784354017364); - QTest::newRow("qint64 min") << std::numeric_limits<qint64>::min() << true; QTest::newRow("minJd - 1") << minJd - 1 << true; QTest::newRow("minJd") << minJd << false; @@ -488,125 +496,178 @@ void tst_QDate::weekNumber_invalid() QCOMPARE( dt.weekNumber( &yearNumber ), 0 ); } -#if QT_CONFIG(timezone) +/* The MS backend tends to lack data for historical transitions. So some of the + transition-based tests will get wrong results, that we can't do anything + about, when using that backend. Rather than complicating the #if-ery more, + overtly record, in a flags column, which we need to ignore and merely make + the testing of these flags subject to #if-ery. + + Android appears to lack at least one other. +*/ +enum BackendKludge { IgnoreStart = 1, IgnoreEnd = 2, }; +Q_DECLARE_FLAGS(BackendKludges, BackendKludge) +Q_DECLARE_OPERATORS_FOR_FLAGS(BackendKludges) + void tst_QDate::startOfDay_endOfDay_data() { QTest::addColumn<QDate>("date"); // Typically a spring-forward. // A zone in which that date's start and end are worth checking: - QTest::addColumn<QByteArray>("zoneName"); + QTest::addColumn<QTimeZone>("zone"); // The start and end times in that zone: QTest::addColumn<QTime>("start"); QTest::addColumn<QTime>("end"); + // Ignored for backends that don't need it: + QTest::addColumn<BackendKludges>("kludge"); + + const QTime early(0, 0), late(23, 59, 59, 999), invalid(QDateTime().time()); + constexpr BackendKludges Clean = {}; + constexpr BackendKludges IgnoreBoth = IgnoreStart | IgnoreEnd; +#ifdef USING_WIN_TZ + constexpr BackendKludges MsNoStart = IgnoreStart; + constexpr BackendKludges MsNoBoth = IgnoreBoth; +#else + constexpr BackendKludges MsNoStart = Clean; + constexpr BackendKludges MsNoBoth = Clean; + // And use IgnoreBoth directly for the one transition Android lacks. +#endif + const QTimeZone UTC(QTimeZone::UTC); - const QTime initial(0, 0), final(23, 59, 59, 999), invalid(QDateTime().time()); + using Bound = std::numeric_limits<qint64>; + const auto dateAtMillis = [UTC](qint64 millis) { + return QDateTime::fromMSecsSinceEpoch(millis, UTC).date(); + }; - // UTC is always a valid zone. - QTest::newRow("epoch") - << epochDate() << QByteArray("UTC") - << initial << final; - if (QTimeZone("America/Sao_Paulo").isValid()) { - QTest::newRow("Brazil") - << QDate(2008, 10, 19) << QByteArray("America/Sao_Paulo") - << QTime(1, 0) << final; - } -#if QT_CONFIG(icu) || !defined(Q_OS_WIN) // MS's TZ APIs lack data - if (QTimeZone("Europe/Sofia").isValid()) { - QTest::newRow("Sofia") - << QDate(1994, 3, 27) << QByteArray("Europe/Sofia") - << QTime(1, 0) << final; - } -#endif - if (QTimeZone("Pacific/Kiritimati").isValid()) { - QTest::newRow("Kiritimati") - << QDate(1994, 12, 31) << QByteArray("Pacific/Kiritimati") - << invalid << invalid; + // UTC and fixed offset are always available and predictable: + QTest::newRow("epoch") << epochDate() << UTC << early << late << Clean; + + // First and last days in QDateTime's supported range: + QTest::newRow("earliest") + << dateAtMillis(Bound::min()) << UTC << invalid << late << Clean; + QTest::newRow("latest") + << dateAtMillis(Bound::max()) << UTC << early << invalid << Clean; + + const struct { + const char *test; + const char *zone; + const QDate day; + const QTime start; + const QTime end; + const BackendKludges msOpt; + } transitions[] = { + // The western Mexico time-zones skipped the first hour of 1970. + { "BajaMexico", "America/Hermosillo", QDate(1970, 1, 1), QTime(1, 0), late, MsNoStart }, + + // Compare tst_QDateTime::fromStringDateFormat(ISO 24:00 in DST). + { "Brazil", "America/Sao_Paulo", QDate(2008, 10, 19), QTime(1, 0), late, Clean }, + + // Several southern zones within EET (but not the northern ones) spent + // part of the 1990s using midnight as spring transition. + { "Sofia", "Europe/Sofia", QDate(1994, 3, 27), QTime(1, 0), late, MsNoStart }, + + // Two Pacific zones skipped days to get on the west of the + // International Date Line; those days have neither start nor end. + { "Kiritimati", "Pacific/Kiritimati", QDate(1994, 12, 31), invalid, invalid, IgnoreBoth }, + { "Samoa", "Pacific/Apia", QDate(2011, 12, 30), invalid, invalid, MsNoBoth }, + + // TODO: find other zones with transitions at/crossing midnight. + }; + const QTimeZone local = QTimeZone::LocalTime; + +#if QT_CONFIG(timezone) + const QTimeZone sys = QTimeZone::systemTimeZone(); + QVERIFY2(sys.isValid(), "Test depends on properly configured system"); + for (const auto &tran : transitions) { + if (QTimeZone zone(tran.zone); zone.isValid()) { + QTest::newRow(tran.test) + << tran.day << zone << tran.start << tran.end << tran.msOpt; + if (zone == sys) { + QTest::addRow("Local=%s", tran.test) + << tran.day << local << tran.start << tran.end << tran.msOpt; + } + } } - if (QTimeZone("Pacific/Apia").isValid()) { - QTest::newRow("Samoa") - << QDate(2011, 12, 30) << QByteArray("Pacific/Apia") - << invalid << invalid; +#else + const auto isLocalZone = [](const char *zone) { + const QLatin1StringView name(zone); + for (int i = 0; i < 2; ++i) { + if (qTzName(i) == name) + return true; + } + return false; + }; + for (const auto &tran : transitions) { + if (isLocalZone(tran.zone)) { // Might need a different name to match + QTest::addRow("Local=%s", tran.test) + << tran.day << local << tran.start << tran.end << tran.msOpt; + } } - // TODO: find other zones with transitions at/crossing midnight. +#endif // timezone } void tst_QDate::startOfDay_endOfDay() { - QFETCH(QDate, date); - QFETCH(QByteArray, zoneName); - QFETCH(QTime, start); - QFETCH(QTime, end); - const auto zone = QTimeZone(zoneName); + QFETCH(const QDate, date); + QFETCH(const QTimeZone, zone); + QFETCH(const QTime, start); + QFETCH(const QTime, end); +#if defined(USING_WIN_TZ) || defined(Q_OS_ANDROID) // Coping with backend limitations. + QFETCH(const BackendKludges, kludge); +#define UNLESSKLUDGE(flag) if (!kludge.testFlag(flag)) +#else +#define UNLESSKLUDGE(flag) +#endif QVERIFY(zone.isValid()); - const bool isSystem = QTimeZone::systemTimeZone() == zone; + QDateTime front(date.startOfDay(zone)), back(date.endOfDay(zone)); if (end.isValid()) QCOMPARE(date.addDays(1).startOfDay(zone).addMSecs(-1), back); if (start.isValid()) QCOMPARE(date.addDays(-1).endOfDay(zone).addMSecs(1), front); - do { // Avoids duplicating these tests for local-time when it *is* zone: - if (start.isValid()) { - QCOMPARE(front.date(), date); - QCOMPARE(front.time(), start); - } - if (end.isValid()) { - QCOMPARE(back.date(), date); - QCOMPARE(back.time(), end); - } - if (front.timeSpec() == Qt::LocalTime) - break; - front = date.startOfDay(); - back = date.endOfDay(); - } while (isSystem); - if (end.isValid()) - QCOMPARE(date.addDays(1).startOfDay().addMSecs(-1), back); - if (start.isValid()) - QCOMPARE(date.addDays(-1).endOfDay().addMSecs(1), front); - if (!isSystem) { - // These might fail if system zone coincides with zone; but only if it - // did something similarly unusual on the date picked for this test. - if (start.isValid()) { - QCOMPARE(front.date(), date); - QCOMPARE(front.time(), QTime(0, 0)); - } - if (end.isValid()) { - QCOMPARE(back.date(), date); - QCOMPARE(back.time(), QTime(23, 59, 59, 999)); - } + + if (start.isValid()) { + QVERIFY(front.isValid()); + QCOMPARE(front.date(), date); + UNLESSKLUDGE(IgnoreStart) QCOMPARE(front.time(), start); + } else UNLESSKLUDGE(IgnoreStart) { + auto report = qScopeGuard([front]() { qDebug() << "Start of day:" << front; }); + QVERIFY(!front.isValid()); + report.dismiss(); + } + if (end.isValid()) { + QVERIFY(back.isValid()); + QCOMPARE(back.date(), date); + UNLESSKLUDGE(IgnoreEnd) QCOMPARE(back.time(), end); + } else UNLESSKLUDGE(IgnoreEnd) { + auto report = qScopeGuard([back]() { qDebug() << "End of day:" << back; }); + QVERIFY(!back.isValid()); + report.dismiss(); } +#undef UNLESSKLUDGE } -#endif // timezone void tst_QDate::startOfDay_endOfDay_fixed_data() { + QTest::addColumn<QDate>("date"); + const qint64 kilo(1000); using Bounds = std::numeric_limits<qint64>; const auto UTC = QTimeZone::UTC; - const QDateTime - first(QDateTime::fromMSecsSinceEpoch(Bounds::min() + 1, UTC)), - start32sign(QDateTime::fromMSecsSinceEpoch(Q_INT64_C(-0x80000000) * kilo, UTC)), - end32sign(QDateTime::fromMSecsSinceEpoch(Q_INT64_C(0x80000000) * kilo, UTC)), - end32unsign(QDateTime::fromMSecsSinceEpoch(Q_INT64_C(0x100000000) * kilo, UTC)), - last(QDateTime::fromMSecsSinceEpoch(Bounds::max(), UTC)); - - const struct { - const char *name; - QDate date; - } data[] = { - { "epoch", epochDate() }, - { "y2k-leap-day", QDate(2000, 2, 29) }, - { "start-1900", QDate(1900, 1, 1) }, // QTBUG-99747 - // Just outside the start and end of 32-bit time_t: - { "pre-sign32", QDate(start32sign.date().year(), 1, 1) }, - { "post-sign32", QDate(end32sign.date().year(), 12, 31) }, - { "post-uint32", QDate(end32unsign.date().year(), 12, 31) }, - // Just inside the start and end of QDateTime's range: - { "first-full", first.date().addDays(1) }, - { "last-full", last.date().addDays(-1) } - }; - - QTest::addColumn<QDate>("date"); - for (const auto &r : data) - QTest::newRow(r.name) << r.date; + const QDateTime first(QDateTime::fromMSecsSinceEpoch(Bounds::min() + 1, UTC)); + const QDateTime start32sign(QDateTime::fromMSecsSinceEpoch(Q_INT64_C(-0x80000000) * kilo, UTC)); + const QDateTime end32sign(QDateTime::fromMSecsSinceEpoch(Q_INT64_C(0x80000000) * kilo, UTC)); + const QDateTime end32unsign(QDateTime::fromMSecsSinceEpoch(Q_INT64_C(0x100000000) * kilo, UTC)); + const QDateTime last(QDateTime::fromMSecsSinceEpoch(Bounds::max(), UTC)); + + QTest::newRow("epoch") << epochDate(); + QTest::newRow("y2k-leap-day") << QDate(2000, 2, 29); + QTest::newRow("start-1900") << QDate(1900, 1, 1); // QTBUG-99747 + // Just outside the start and end of 32-bit time_t: + QTest::newRow("pre-sign32") << QDate(start32sign.date().year(), 1, 1); + QTest::newRow("post-sign32") << QDate(end32sign.date().year(), 12, 31); + QTest::newRow("post-uint32") << QDate(end32unsign.date().year(), 12, 31); + // Just inside the start and end of QDateTime's range: + QTest::newRow("first-full") << first.date().addDays(1); + QTest::newRow("last-full") << last.date().addDays(-1); } void tst_QDate::startOfDay_endOfDay_fixed() @@ -660,7 +721,6 @@ void tst_QDate::startOfDay_endOfDay_bounds() QVERIFY(last.isValid()); QVERIFY(first < epoch); QVERIFY(last > epoch); - // QDateTime's addMSecs doesn't check against {und,ov}erflow ... QVERIFY(!first.addMSecs(-1).isValid() || first.addMSecs(-1) > first); QVERIFY(!last.addMSecs(1).isValid() || last.addMSecs(1) < last); @@ -675,7 +735,9 @@ void tst_QDate::startOfDay_endOfDay_bounds() QCOMPARE(qdteMin.startOfDay(UTC).date(), qdteMin); QCOMPARE(qdteMin.startOfDay().date(), qdteMin); #if QT_CONFIG(timezone) - QCOMPARE(qdteMin.startOfDay(QTimeZone::systemTimeZone()).date(), qdteMin); + const QTimeZone sys = QTimeZone::systemTimeZone(); + QVERIFY2(sys.isValid(), "Test depends on properly configured system"); + QCOMPARE(qdteMin.startOfDay(sys).date(), qdteMin); QTimeZone berlin("Europe/Berlin"); if (berlin.isValid()) QCOMPARE(qdteMin.startOfDay(berlin).date(), qdteMin); @@ -686,8 +748,6 @@ void tst_QDate::julianDaysLimits() { qint64 min = std::numeric_limits<qint64>::min(); qint64 max = std::numeric_limits<qint64>::max(); - qint64 minJd = Q_INT64_C(-784350574879); - qint64 maxJd = Q_INT64_C( 784354017364); QDate maxDate = QDate::fromJulianDay(maxJd); QDate minDate = QDate::fromJulianDay(minJd); @@ -910,9 +970,6 @@ void tst_QDate::addYears_data() void tst_QDate::daysTo() { - qint64 minJd = Q_INT64_C(-784350574879); - qint64 maxJd = Q_INT64_C( 784354017364); - QDate dt1(2000, 1, 1); QDate dt2(2000, 1, 5); QCOMPARE(dt1.daysTo(dt2), (qint64) 4); @@ -937,6 +994,17 @@ void tst_QDate::daysTo() QCOMPARE(zeroDate.daysTo(minDate), minJd); } +void tst_QDate::orderingCompiles() +{ + QTestPrivate::testAllComparisonOperatorsCompile<QDate>(); +#if __cpp_lib_chrono >= 201907L + QTestPrivate::testAllComparisonOperatorsCompile<QDate, std::chrono::year_month_day>(); + QTestPrivate::testAllComparisonOperatorsCompile<QDate, std::chrono::year_month_day_last>(); + QTestPrivate::testAllComparisonOperatorsCompile<QDate, std::chrono::year_month_weekday>(); + QTestPrivate::testAllComparisonOperatorsCompile<QDate, std::chrono::year_month_weekday_last>(); +#endif +} + void tst_QDate::operator_eq_eq_data() { QTest::addColumn<QDate>("d1"); @@ -973,137 +1041,83 @@ void tst_QDate::operator_eq_eq() QFETCH(QDate, d2); QFETCH(bool, expectEqual); - bool equal = d1 == d2; - QCOMPARE(equal, expectEqual); - bool notEqual = d1 != d2; - QCOMPARE(notEqual, !expectEqual); + QT_TEST_EQUALITY_OPS(d1, d2, expectEqual); - if (equal) + if (expectEqual) QVERIFY(qHash(d1) == qHash(d2)); } -void tst_QDate::operator_lt() +void tst_QDate::ordering_data() { - QDate d1(2000,1,2); - QDate d2(2000,1,2); - QVERIFY( !(d1 < d2) ); - - d1 = QDate(2001,12,4); - d2 = QDate(2001,12,5); - QVERIFY( d1 < d2 ); - - d1 = QDate(2001,11,5); - d2 = QDate(2001,12,5); - QVERIFY( d1 < d2 ); - - d1 = QDate(2000,12,5); - d2 = QDate(2001,12,5); - QVERIFY( d1 < d2 ); - - d1 = QDate(2002,12,5); - d2 = QDate(2001,12,5); - QVERIFY( !(d1 < d2) ); - - d1 = QDate(2001,12,5); - d2 = QDate(2001,11,5); - QVERIFY( !(d1 < d2) ); - - d1 = QDate(2001,12,6); - d2 = QDate(2001,12,5); - QVERIFY( !(d1 < d2) ); + QTest::addColumn<QDate>("left"); + QTest::addColumn<QDate>("right"); + QTest::addColumn<Qt::strong_ordering>("expectedOrdering"); + + QTest::newRow("2000-1-2_vs_2000-1-2") + << QDate(2000, 1, 2) << QDate(2000, 1, 2) << Qt::strong_ordering::equivalent; + QTest::newRow("2001-12-4_vs_2001-12-5") + << QDate(2001, 12, 4) << QDate(2001, 12, 5) << Qt::strong_ordering::less; + QTest::newRow("2001-11-5_vs_2001-12-5") + << QDate(2001, 11, 5) << QDate(2001, 12, 5) << Qt::strong_ordering::less; + QTest::newRow("2000-12-5_vs_2001-12-5") + << QDate(2000, 12, 5) << QDate(2001, 12, 5) << Qt::strong_ordering::less; + QTest::newRow("2002-12-5_vs_2001-12-5") + << QDate(2002, 12, 5) << QDate(2001, 12, 5) << Qt::strong_ordering::greater; + QTest::newRow("2001-12-5_vs_2001-11-5") + << QDate(2001, 12, 5) << QDate(2001, 11, 5) << Qt::strong_ordering::greater; + QTest::newRow("2001-12-6_vs_2001-12-5") + << QDate(2001, 12, 6) << QDate(2001, 12, 5) << Qt::strong_ordering::greater; } -void tst_QDate::operator_gt() +void tst_QDate::ordering() { - QDate d1(2000,1,2); - QDate d2(2000,1,2); - QVERIFY( !(d1 > d2) ); - - d1 = QDate(2001,12,4); - d2 = QDate(2001,12,5); - QVERIFY( !(d1 > d2) ); - - d1 = QDate(2001,11,5); - d2 = QDate(2001,12,5); - QVERIFY( !(d1 > d2) ); - - d1 = QDate(2000,12,5); - d2 = QDate(2001,12,5); - QVERIFY( !(d1 > d2) ); - - d1 = QDate(2002,12,5); - d2 = QDate(2001,12,5); - QVERIFY( d1 > d2 ); - - d1 = QDate(2001,12,5); - d2 = QDate(2001,11,5); - QVERIFY( d1 > d2 ); + QFETCH(QDate, left); + QFETCH(QDate, right); + QFETCH(Qt::strong_ordering, expectedOrdering); - d1 = QDate(2001,12,6); - d2 = QDate(2001,12,5); - QVERIFY( d1 > d2 ); + QT_TEST_ALL_COMPARISON_OPS(left, right, expectedOrdering); } -void tst_QDate::operator_lt_eq() +void tst_QDate::ordering_chrono_types() { - QDate d1(2000,1,2); - QDate d2(2000,1,2); - QVERIFY( d1 <= d2 ); - - d1 = QDate(2001,12,4); - d2 = QDate(2001,12,5); - QVERIFY( d1 <= d2 ); - - d1 = QDate(2001,11,5); - d2 = QDate(2001,12,5); - QVERIFY( d1 <= d2 ); - - d1 = QDate(2000,12,5); - d2 = QDate(2001,12,5); - QVERIFY( d1 <= d2 ); - - d1 = QDate(2002,12,5); - d2 = QDate(2001,12,5); - QVERIFY( !(d1 <= d2) ); - - d1 = QDate(2001,12,5); - d2 = QDate(2001,11,5); - QVERIFY( !(d1 <= d2) ); - - d1 = QDate(2001,12,6); - d2 = QDate(2001,12,5); - QVERIFY( !(d1 <= d2) ); -} - -void tst_QDate::operator_gt_eq() -{ - QDate d1(2000,1,2); - QDate d2(2000,1,2); - QVERIFY( d1 >= d2 ); - - d1 = QDate(2001,12,4); - d2 = QDate(2001,12,5); - QVERIFY( !(d1 >= d2) ); - - d1 = QDate(2001,11,5); - d2 = QDate(2001,12,5); - QVERIFY( !(d1 >= d2) ); - - d1 = QDate(2000,12,5); - d2 = QDate(2001,12,5); - QVERIFY( !(d1 >= d2) ); - - d1 = QDate(2002,12,5); - d2 = QDate(2001,12,5); - QVERIFY( d1 >= d2 ); - - d1 = QDate(2001,12,5); - d2 = QDate(2001,11,5); - QVERIFY( d1 >= d2 ); - - d1 = QDate(2001,12,6); - d2 = QDate(2001,12,5); - QVERIFY( d1 >= d2 ); +#if __cpp_lib_chrono >= 201907L + using namespace std::chrono; + QDate friday(2001, 11, 30); // the 5th Friday of November 2001 + // std::chrono::year_month_day + QT_TEST_ALL_COMPARISON_OPS(friday, year_month_day(2001y, November, 29d), + Qt::strong_ordering::greater); + QT_TEST_ALL_COMPARISON_OPS(friday, year_month_day(2001y, November, 30d), + Qt::strong_ordering::equivalent); + QT_TEST_ALL_COMPARISON_OPS(friday, year_month_day(2001y, December, 1d), + Qt::strong_ordering::less); + + // std::chrono::year_month_day_last + QT_TEST_ALL_COMPARISON_OPS(friday, year_month_day_last(2001y, {October / last}), + Qt::strong_ordering::greater); + QT_TEST_ALL_COMPARISON_OPS(friday, year_month_day_last(2001y, {November / last}), + Qt::strong_ordering::equivalent); + QT_TEST_ALL_COMPARISON_OPS(friday, year_month_day_last(2001y, {December / last}), + Qt::strong_ordering::less); + + // std::chrono::year_month_weekday + QT_TEST_ALL_COMPARISON_OPS(friday, year_month_weekday(2001y, November, Thursday[5]), + Qt::strong_ordering::greater); + QT_TEST_ALL_COMPARISON_OPS(friday, year_month_weekday(2001y, November, Friday[5]), + Qt::strong_ordering::equivalent); + QT_TEST_ALL_COMPARISON_OPS(friday, year_month_weekday(2001y, December, Saturday[1]), + Qt::strong_ordering::less); + + // std::chrono::year_month_weekday_last + QDate thursday(2001, 11, 29); // the last Thursday of November 2001 + QT_TEST_ALL_COMPARISON_OPS(thursday, year_month_weekday_last(2001y, November, Wednesday[last]), + Qt::strong_ordering::greater); + QT_TEST_ALL_COMPARISON_OPS(thursday, year_month_weekday_last(2001y, November, Thursday[last]), + Qt::strong_ordering::equivalent); + QT_TEST_ALL_COMPARISON_OPS(thursday, year_month_weekday_last(2001y, November, Friday[last]), + Qt::strong_ordering::less); +#else + QSKIP("This test requires C++20-level <chrono> support enabled in the standard library."); +#endif // __cpp_lib_chrono >= 201907L } Q_DECLARE_METATYPE(QDataStream::Version) @@ -1165,7 +1179,7 @@ void tst_QDate::operator_insert_extract() QCOMPARE(deserialised, date); } -#if QT_CONFIG(datetimeparser) +#if QT_CONFIG(datestring) void tst_QDate::fromStringDateFormat_data() { QTest::addColumn<QString>("dateStr"); @@ -1289,168 +1303,177 @@ void tst_QDate::fromStringDateFormat() QCOMPARE(QDate::fromString(dateStr, dateFormat), expectedDate); } +# if QT_CONFIG(datetimeparser) void tst_QDate::fromStringFormat_data() { QTest::addColumn<QString>("string"); QTest::addColumn<QString>("format"); + QTest::addColumn<int>("baseYear"); QTest::addColumn<QDate>("expected"); - // Get names: - const QString january = QStringLiteral("January"); - const QString february = QStringLiteral("February"); - const QString march = QStringLiteral("March"); - const QString august = QStringLiteral("August"); - const QString mon = QStringLiteral("Mon"); - const QString monday = QStringLiteral("Monday"); - const QString tuesday = QStringLiteral("Tuesday"); - const QString wednesday = QStringLiteral("Wednesday"); - const QString thursday = QStringLiteral("Thursday"); - const QString friday = QStringLiteral("Friday"); - const QString saturday = QStringLiteral("Saturday"); - const QString sunday = QStringLiteral("Sunday"); - - QTest::newRow("data0") << QString("") << QString("") << defDate(); - QTest::newRow("data1") << QString(" ") << QString("") << invalidDate(); - QTest::newRow("data2") << QString(" ") << QString(" ") << defDate(); - QTest::newRow("data3") << QString("-%$%#") << QString("$*(#@") << invalidDate(); - QTest::newRow("data4") << QString("d") << QString("'d'") << defDate(); - QTest::newRow("data5") << QString("101010") << QString("dMyy") << QDate(1910, 10, 10); - QTest::newRow("data6") << QString("101010b") << QString("dMyy") << invalidDate(); - QTest::newRow("data7") << january << QString("MMMM") << defDate(); - QTest::newRow("data8") << QString("ball") << QString("balle") << invalidDate(); - QTest::newRow("data9") << QString("balleh") << QString("balleh") << defDate(); - QTest::newRow("data10") << QString("10.01.1") << QString("M.dd.d") << QDate(defDate().year(), 10, 1); - QTest::newRow("data11") << QString("-1.01.1") << QString("M.dd.d") << invalidDate(); - QTest::newRow("data12") << QString("11010") << QString("dMMyy") << invalidDate(); - QTest::newRow("data13") << QString("-2") << QString("d") << invalidDate(); - QTest::newRow("data14") << QString("132") << QString("Md") << invalidDate(); - QTest::newRow("data15") << february << QString("MMMM") << QDate(defDate().year(), 2, 1); - - QString date = mon + QLatin1Char(' ') + august + " 8 2005"; - QTest::newRow("data16") << date << QString("ddd MMMM d yyyy") << QDate(2005, 8, 8); - QTest::newRow("data17") << QString("2000:00") << QString("yyyy:yy") << QDate(2000, 1, 1); - QTest::newRow("data18") << QString("1999:99") << QString("yyyy:yy") << QDate(1999, 1, 1); - QTest::newRow("data19") << QString("2099:99") << QString("yyyy:yy") << QDate(2099, 1, 1); - QTest::newRow("data20") << QString("2001:01") << QString("yyyy:yy") << QDate(2001, 1, 1); - QTest::newRow("data21") << QString("99") << QString("yy") << QDate(1999, 1, 1); - QTest::newRow("data22") << QString("01") << QString("yy") << QDate(1901, 1, 1); - - QTest::newRow("data23") << monday << QString("dddd") << QDate(1900, 1, 1); - QTest::newRow("data24") << tuesday << QString("dddd") << QDate(1900, 1, 2); - QTest::newRow("data25") << wednesday << QString("dddd") << QDate(1900, 1, 3); - QTest::newRow("data26") << thursday << QString("dddd") << QDate(1900, 1, 4); - QTest::newRow("data27") << friday << QString("dddd") << QDate(1900, 1, 5); - QTest::newRow("data28") << saturday << QString("dddd") << QDate(1900, 1, 6); - QTest::newRow("data29") << sunday << QString("dddd") << QDate(1900, 1, 7); - - QTest::newRow("data30") << monday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 2); - QTest::newRow("data31") << tuesday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 3); - QTest::newRow("data32") << wednesday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 4); - QTest::newRow("data33") << thursday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 5); - QTest::newRow("data34") << friday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 6); - QTest::newRow("data35") << saturday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 7); - QTest::newRow("data36") << sunday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 1); - - QTest::newRow("data37") << tuesday + " 2007 " + march << QString("dddd yyyy MMMM") << QDate(2007, 3, 6); - - QTest::newRow("data38") << QString("21052006") << QString("ddMMyyyy") << QDate(2006,5,21); - QTest::newRow("data39") << QString("210506") << QString("ddMMyy") << QDate(1906,5,21); - QTest::newRow("data40") << QString("21/5/2006") << QString("d/M/yyyy") << QDate(2006,5,21); - QTest::newRow("data41") << QString("21/5/06") << QString("d/M/yy") << QDate(1906,5,21); - QTest::newRow("data42") << QString("20060521") << QString("yyyyMMdd") << QDate(2006,5,21); - QTest::newRow("data43") << QString("060521") << QString("yyMMdd") << QDate(1906,5,21); - QTest::newRow("lateMarch") << QString("9999-03-06") << QString("yyyy-MM-dd") << QDate(9999, 3, 6); - QTest::newRow("late") << QString("9999-12-31") << QString("yyyy-MM-dd") << QDate(9999, 12, 31); - - QTest::newRow("quoted-dd") << QString("21dd-05-2006") << QString("dd'dd'-MM-yyyy") - << QDate(2006, 5, 21); - QTest::newRow("quoted-MM") << QString("21-MM05-2006") << QString("dd-'MM'MM-yyyy") - << QDate(2006, 5, 21); - QTest::newRow("quotes-empty") << QString("21-'05-2006") << QString("dd-MM-''yy") - << QDate(2006, 5, 21); + QTest::newRow("empty") << u""_s << u""_s << 1900 << defDate(); + QTest::newRow("space-as-empty") << u" "_s << u""_s << 1900 << invalidDate(); + QTest::newRow("space") << u" "_s << u" "_s << 1900 << defDate(); + QTest::newRow("mispunc") << u"-%$%#"_s << u"$*(#@"_s << 1900 << invalidDate(); + QTest::newRow("literal-d") << u"d"_s << u"'d'"_s << 1900 << defDate(); + QTest::newRow("greedy") << u"101010"_s << u"dMyy"_s << 1900 << QDate(1910, 10, 10); + QTest::newRow("greedy-miss") << u"101010b"_s << u"dMyy"_s << 1900 << invalidDate(); + QTest::newRow("January") << u"January"_s << u"MMMM"_s << 1900 << defDate(); + QTest::newRow("mistext") << u"ball"_s << u"balle"_s << 1900 << invalidDate(); + QTest::newRow("text") << u"balleh"_s << u"balleh"_s << 1900 << defDate(); + QTest::newRow("yearless:19") << u"10.01.1"_s << u"M.dd.d"_s << 1900 << QDate(1900, 10, 1); + QTest::newRow("yearless:20") << u"10.01.1"_s << u"M.dd.d"_s << 2000 << QDate(2000, 10, 1); + QTest::newRow("neg-month") << u"-1.01.1"_s << u"M.dd.d"_s << 1900 << invalidDate(); + QTest::newRow("greedy-break") << u"11010"_s << u"dMMyy"_s << 1900 << invalidDate(); + QTest::newRow("neg-day") << u"-2"_s << u"d"_s << 1900 << invalidDate(); + QTest::newRow("Md:132") << u"132"_s << u"Md"_s << 1900 << invalidDate(); + QTest::newRow("February") << u"February"_s << u"MMMM"_s << 1900 << QDate(1900, 2, 1); + + QTest::newRow("mon-aug-8th") + << u"Mon August 8 2005"_s << u"ddd MMMM d yyyy"_s << 1900 << QDate(2005, 8, 8); + QTest::newRow("year-match-20000") << u"2000:00"_s << u"yyyy:yy"_s << 1900 << QDate(2000, 1, 1); + QTest::newRow("year-match-1999") << u"1999:99"_s << u"yyyy:yy"_s << 1900 << QDate(1999, 1, 1); + QTest::newRow("year-match-2099") << u"2099:99"_s << u"yyyy:yy"_s << 1900 << QDate(2099, 1, 1); + QTest::newRow("year-match-2001") << u"2001:01"_s << u"yyyy:yy"_s << 1900 << QDate(2001, 1, 1); + QTest::newRow("just-yy-1999") << u"99"_s << u"yy"_s << 1900 << QDate(1999, 1, 1); + QTest::newRow("just-yy-1901") << u"01"_s << u"yy"_s << 1900 << QDate(1901, 1, 1); + QTest::newRow("just-yy-2001") << u"01"_s << u"yy"_s << 1970 << QDate(2001, 1, 1); + + QTest::newRow("Monday") << u"Monday"_s << u"dddd"_s << 1900 << QDate(1900, 1, 1); + QTest::newRow("Tuesday") << u"Tuesday"_s << u"dddd"_s << 1900 << QDate(1900, 1, 2); + QTest::newRow("Wednesday") << u"Wednesday"_s << u"dddd"_s << 1900 << QDate(1900, 1, 3); + QTest::newRow("Thursday") << u"Thursday"_s << u"dddd"_s << 1900 << QDate(1900, 1, 4); + QTest::newRow("Friday") << u"Friday"_s << u"dddd"_s << 1900 << QDate(1900, 1, 5); + QTest::newRow("Saturday") << u"Saturday"_s << u"dddd"_s << 1900 << QDate(1900, 1, 6); + QTest::newRow("Sunday") << u"Sunday"_s << u"dddd"_s << 1900 << QDate(1900, 1, 7); + + QTest::newRow("Mon06") << u"Monday 2006"_s << u"dddd yyyy"_s << 1900 << QDate(2006, 1, 2); + QTest::newRow("Tues06") << u"Tuesday 2006"_s << u"dddd yyyy"_s << 1900 << QDate(2006, 1, 3); + QTest::newRow("Wed06") << u"Wednesday 2006"_s << u"dddd yyyy"_s << 1900 << QDate(2006, 1, 4); + QTest::newRow("Thu06") << u"Thursday 2006"_s << u"dddd yyyy"_s << 1900 << QDate(2006, 1, 5); + QTest::newRow("Fri06") << u"Friday 2006"_s << u"dddd yyyy"_s << 1900 << QDate(2006, 1, 6); + QTest::newRow("Sat06") << u"Saturday 2006"_s << u"dddd yyyy"_s << 1900 << QDate(2006, 1, 7); + QTest::newRow("Sun06") << u"Sunday 2006"_s << u"dddd yyyy"_s << 1900 << QDate(2006, 1, 1); + QTest::newRow("Tue07Mar") + << u"Tuesday 2007 March"_s << u"dddd yyyy MMMM"_s << 1900 << QDate(2007, 3, 6); + + QTest::newRow("21May2006") + << u"21052006"_s << u"ddMMyyyy"_s << 1900 << QDate(2006, 5, 21); + QTest::newRow("21May06:19") + << u"210506"_s << u"ddMMyy"_s << 1900 << QDate(1906, 5, 21); + QTest::newRow("21May06:20") + << u"210506"_s << u"ddMMyy"_s << 1970 << QDate(2006, 5, 21); + QTest::newRow("21/May/2006") + << u"21/5/2006"_s << u"d/M/yyyy"_s << 1900 << QDate(2006, 5, 21); + QTest::newRow("21/5/06") + << u"21/5/06"_s << u"d/M/yy"_s << 1900 << QDate(1906, 5, 21); + QTest::newRow("21/5/06:19") + << u"21/5/06"_s << u"d/M/yy"_s << 1900 << QDate(1906, 5, 21); + QTest::newRow("21/5/06:20") + << u"21/5/06"_s << u"d/M/yy"_s << 1910 << QDate(2006, 5, 21); + QTest::newRow("2006May21") + << u"20060521"_s << u"yyyyMMdd"_s << 1900 << QDate(2006, 5, 21); + QTest::newRow("06May21:19") + << u"060521"_s << u"yyMMdd"_s << 1900 << QDate(1906, 5, 21); + QTest::newRow("06May21:20") + << u"060521"_s << u"yyMMdd"_s << 1907 << QDate(2006, 5, 21); + QTest::newRow("lateMarch") + << u"9999-03-06"_s << u"yyyy-MM-dd"_s << 1900 << QDate(9999, 3, 6); + QTest::newRow("late") + << u"9999-12-31"_s << u"yyyy-MM-dd"_s << 1900 << QDate(9999, 12, 31); + + QTest::newRow("quoted-dd") + << u"21dd-05-2006"_s << u"dd'dd'-MM-yyyy"_s << 1900 << QDate(2006, 5, 21); + QTest::newRow("quoted-MM") + << u"21-MM05-2006"_s << u"dd-'MM'MM-yyyy"_s << 1900 << QDate(2006, 5, 21); + QTest::newRow("quotes-empty") + << u"21-'05-2006"_s << u"dd-MM-''yy"_s << 1900 << QDate(2006, 5, 21); // Test unicode handling. QTest::newRow("Unicode in format string") - << QString(u8"2020🤣09🤣21") << QString(u8"yyyy🤣MM🤣dd") << QDate(2020, 9, 21); + << QString(u8"2020🤣09🤣21") << QString(u8"yyyy🤣MM🤣dd") << 1900 << QDate(2020, 9, 21); QTest::newRow("Unicode-in-format-string-quoted-emoji") - << QString(u8"🤣🤣2020👍09🤣21") << QString(u8"'🤣🤣'yyyy👍MM🤣dd") << QDate(2020, 9, 21); + << QString(u8"🤣🤣2020👍09🤣21") << QString(u8"'🤣🤣'yyyy👍MM🤣dd") << 1900 + << QDate(2020, 9, 21); QTest::newRow("Unicode-in-quoted-dd-format-string") - << QString(u8"🤣🤣2020👍09🤣21dd") << QString(u8"🤣🤣yyyy👍MM🤣dd'dd'") << QDate(2020, 9, 21); + << QString(u8"🤣🤣2020👍09🤣21dd") << QString(u8"🤣🤣yyyy👍MM🤣dd'dd'") << 1900 + << QDate(2020, 9, 21); QTest::newRow("Unicode-in-all-formats-quoted-string") << QString(u8"🤣🤣yyyy2020👍MM09🤣21dd") << QString(u8"🤣🤣'yyyy'yyyy👍'MM'MM🤣dd'dd'") - << QDate(2020, 9, 21); + << 1900 << QDate(2020, 9, 21); // QTBUG-84334 QTest::newRow("-ve year: front, nosep") - << QString("-20060521") << QString("yyyyMMdd") << QDate(-2006, 5, 21); + << u"-20060521"_s << u"yyyyMMdd"_s << 1900 << QDate(-2006, 5, 21); QTest::newRow("-ve year: mid, nosep") - << QString("05-200621") << QString("MMyyyydd") << QDate(-2006, 5, 21); + << u"05-200621"_s << u"MMyyyydd"_s << 1900 << QDate(-2006, 5, 21); QTest::newRow("-ve year: back, nosep") - << QString("0521-2006") << QString("MMddyyyy") << QDate(-2006, 5, 21); + << u"0521-2006"_s << u"MMddyyyy"_s << 1900 << QDate(-2006, 5, 21); // - as separator should not interfere with negative year numbers: QTest::newRow("-ve year: front, dash") - << QString("-2006-05-21") << QString("yyyy-MM-dd") << QDate(-2006, 5, 21); + << u"-2006-05-21"_s << u"yyyy-MM-dd"_s << 1900 << QDate(-2006, 5, 21); QTest::newRow("positive year: front, dash") - << QString("-2006-05-21") << QString("-yyyy-MM-dd") << QDate(2006, 5, 21); + << u"-2006-05-21"_s << u"-yyyy-MM-dd"_s << 1900 << QDate(2006, 5, 21); QTest::newRow("-ve year: mid, dash") - << QString("05--2006-21") << QString("MM-yyyy-dd") << QDate(-2006, 5, 21); + << u"05--2006-21"_s << u"MM-yyyy-dd"_s << 1900 << QDate(-2006, 5, 21); QTest::newRow("-ve year: back, dash") - << QString("05-21--2006") << QString("MM-dd-yyyy") << QDate(-2006, 5, 21); + << u"05-21--2006"_s << u"MM-dd-yyyy"_s << 1900 << QDate(-2006, 5, 21); // negative three digit year numbers should be rejected: QTest::newRow("-ve 3digit year: front") - << QString("-206-05-21") << QString("yyyy-MM-dd") << QDate(); + << u"-206-05-21"_s << u"yyyy-MM-dd"_s << 1900 << QDate(); QTest::newRow("-ve 3digit year: mid") - << QString("05--206-21") << QString("MM-yyyy-dd") << QDate(); + << u"05--206-21"_s << u"MM-yyyy-dd"_s << 1900 << QDate(); QTest::newRow("-ve 3digit year: back") - << QString("05-21--206") << QString("MM-dd-yyyy") << QDate(); + << u"05-21--206"_s << u"MM-dd-yyyy"_s << 1900 << QDate(); // negative month numbers should be rejected: QTest::newRow("-ve 2digit month: mid") - << QString("2060--05-21") << QString("yyyy-MM-dd") << QDate(); + << u"2060--05-21"_s << u"yyyy-MM-dd"_s << 1900 << QDate(); QTest::newRow("-ve 2digit month: front") - << QString("-05-2060-21") << QString("MM-yyyy-dd") << QDate(); + << u"-05-2060-21"_s << u"MM-yyyy-dd"_s << 1900 << QDate(); QTest::newRow("-ve 2digit month: back") - << QString("21-2060--05") << QString("dd-yyyy-MM") << QDate(); + << u"21-2060--05"_s << u"dd-yyyy-MM"_s << 1900 << QDate(); // negative single digit month numbers should be rejected: QTest::newRow("-ve 1digit month: mid") - << QString("2060--5-21") << QString("yyyy-MM-dd") << QDate(); + << u"2060--5-21"_s << u"yyyy-MM-dd"_s << 1900 << QDate(); QTest::newRow("-ve 1digit month: front") - << QString("-5-2060-21") << QString("MM-yyyy-dd") << QDate(); + << u"-5-2060-21"_s << u"MM-yyyy-dd"_s << 1900 << QDate(); QTest::newRow("-ve 1digit month: back") - << QString("21-2060--5") << QString("dd-yyyy-MM") << QDate(); + << u"21-2060--5"_s << u"dd-yyyy-MM"_s << 1900 << QDate(); // negative day numbers should be rejected: QTest::newRow("-ve 2digit day: front") - << QString("-21-2060-05") << QString("dd-yyyy-MM") << QDate(); + << u"-21-2060-05"_s << u"dd-yyyy-MM"_s << 1900 << QDate(); QTest::newRow("-ve 2digit day: mid") - << QString("2060--21-05") << QString("yyyy-dd-MM") << QDate(); + << u"2060--21-05"_s << u"yyyy-dd-MM"_s << 1900 << QDate(); QTest::newRow("-ve 2digit day: back") - << QString("05-2060--21") << QString("MM-yyyy-dd") << QDate(); + << u"05-2060--21"_s << u"MM-yyyy-dd"_s << 1900 << QDate(); // negative single digit day numbers should be rejected: QTest::newRow("-ve 1digit day: front") - << QString("-2-2060-05") << QString("dd-yyyy-MM") << QDate(); + << u"-2-2060-05"_s << u"dd-yyyy-MM"_s << 1900 << QDate(); QTest::newRow("-ve 1digit day: mid") - << QString("05--2-2060") << QString("MM-dd-yyyy") << QDate(); + << u"05--2-2060"_s << u"MM-dd-yyyy"_s << 1900 << QDate(); QTest::newRow("-ve 1digit day: back") - << QString("2060-05--2") << QString("yyyy-MM-dd") << QDate(); + << u"2060-05--2"_s << u"yyyy-MM-dd"_s << 1900 << QDate(); // positive three digit year numbers should be rejected: - QTest::newRow("3digit year, front") << QString("206-05-21") << QString("yyyy-MM-dd") << QDate(); - QTest::newRow("3digit year, mid") << QString("05-206-21") << QString("MM-yyyy-dd") << QDate(); - QTest::newRow("3digit year, back") << QString("05-21-206") << QString("MM-dd-yyyy") << QDate(); + QTest::newRow("3digit year, front") << u"206-05-21"_s << u"yyyy-MM-dd"_s << 1900 << QDate(); + QTest::newRow("3digit year, mid") << u"05-206-21"_s << u"MM-yyyy-dd"_s << 1900 << QDate(); + QTest::newRow("3digit year, back") << u"05-21-206"_s << u"MM-dd-yyyy"_s << 1900 << QDate(); // positive five digit year numbers should be rejected: QTest::newRow("5digit year, front") - << QString("00206-05-21") << QString("yyyy-MM-dd") << QDate(); - QTest::newRow("5digit year, mid") << QString("05-00206-21") << QString("MM-yyyy-dd") << QDate(); + << u"00206-05-21"_s << u"yyyy-MM-dd"_s << 1900 << QDate(); + QTest::newRow("5digit year, mid") + << u"05-00206-21"_s << u"MM-yyyy-dd"_s << 1900 << QDate(); QTest::newRow("5digit year, back") - << QString("05-21-00206") << QString("MM-dd-yyyy") << QDate(); + << u"05-21-00206"_s << u"MM-dd-yyyy"_s << 1900 << QDate(); QTest::newRow("dash separator, no year at end") - << QString("05-21-") << QString("dd-MM-yyyy") << QDate(); + << u"05-21-"_s << u"dd-MM-yyyy"_s << 1900 << QDate(); QTest::newRow("slash separator, no year at end") - << QString("11/05/") << QString("d/MM/yyyy") << QDate(); + << u"11/05/"_s << u"d/MM/yyyy"_s << 1900 << QDate(); // QTBUG-84349 - QTest::newRow("+ sign in year field") << QString("+0200322") << QString("yyyyMMdd") << QDate(); - QTest::newRow("+ sign in month field") << QString("2020+322") << QString("yyyyMMdd") << QDate(); - QTest::newRow("+ sign in day field") << QString("202003+1") << QString("yyyyMMdd") << QDate(); + QTest::newRow("+ sign in year field") << u"+0200322"_s << u"yyyyMMdd"_s << 1900 << QDate(); + QTest::newRow("+ sign in month field") << u"2020+322"_s << u"yyyyMMdd"_s << 1900 << QDate(); + QTest::newRow("+ sign in day field") << u"202003+1"_s << u"yyyyMMdd"_s << 1900 << QDate(); } @@ -1458,16 +1481,16 @@ void tst_QDate::fromStringFormat() { QFETCH(QString, string); QFETCH(QString, format); + QFETCH(int, baseYear); QFETCH(QDate, expected); - QDate dt = QDate::fromString(string, format); + QDate dt = QDate::fromString(string, format, baseYear); QEXPECT_FAIL("quotes-empty", "QTBUG-110669: doubled single-quotes in format mishandled", Continue); QCOMPARE(dt, expected); } #endif // datetimeparser -#if QT_CONFIG(datestring) void tst_QDate::toStringFormat_data() { QTest::addColumn<QDate>("t"); @@ -1675,9 +1698,6 @@ void tst_QDate::roundtrip() const loopDate = loopDate.addDays(1); } - qint64 minJd = Q_INT64_C(-784350574879); - qint64 maxJd = Q_INT64_C( 784354017364); - // Test Gregorian round trip at top end of conversion range loopDate = QDate::fromJulianDay(maxJd); while (loopDate.toJulianDay() >= maxJd - 146397) { @@ -1693,6 +1713,38 @@ void tst_QDate::roundtrip() const QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay()); loopDate = loopDate.addDays(1); } + +#if __cpp_lib_chrono >= 201907L + // Test roundtrip for from/to std::chrono conversions. + // Compile-time test, to verify it's all constexpr. + using namespace std::chrono; + { + constexpr sys_days expected{days{minJd}}; + constexpr sys_days actual{QDate::fromStdSysDays(expected).toStdSysDays()}; + static_assert(actual == expected); + } + { + // constexpr year_month_day expected{sys_days{days{maxJd}}}; // Overflow at least on MSVC + constexpr year_month_day expected{1970y, January, 1d}; + constexpr sys_days actual{QDate(expected).toStdSysDays()}; + static_assert(actual == sys_days(expected)); + } + { + constexpr year_month_day_last expected{2001y, {October / last}}; + constexpr sys_days actual{QDate(expected).toStdSysDays()}; + static_assert(actual == sys_days(expected)); + } + { + constexpr year_month_weekday expected{2001y, December, Saturday[1]}; + constexpr sys_days actual{QDate(expected).toStdSysDays()}; + static_assert(actual == sys_days(expected)); + } + { + constexpr year_month_weekday_last expected{2001y, November, Friday[last]}; + constexpr sys_days actual{QDate(expected).toStdSysDays()}; + static_assert(actual == sys_days(expected)); + } +#endif // __cpp_lib_chrono >= 201907L } void tst_QDate::qdebug() const diff --git a/tests/auto/corelib/time/qdatetime/CMakeLists.txt b/tests/auto/corelib/time/qdatetime/CMakeLists.txt index 6d46fcf699..499369c131 100644 --- a/tests/auto/corelib/time/qdatetime/CMakeLists.txt +++ b/tests/auto/corelib/time/qdatetime/CMakeLists.txt @@ -5,6 +5,12 @@ ## tst_qdatetime Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qdatetime LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qdatetime SOURCES tst_qdatetime.cpp @@ -13,6 +19,7 @@ qt_internal_add_test(tst_qdatetime QT_NO_KEYWORDS LIBRARIES Qt::CorePrivate + Qt::TestPrivate ) ## Scopes: diff --git a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp index e4e8d8f7ea..f9c6afc795 100644 --- a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp @@ -1,16 +1,24 @@ // Copyright (C) 2022 The Qt Company Ltd. // Copyright (C) 2016 Intel Corporation. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QDateTime> #include <QTest> #include <QTimeZone> #include <private/qdatetime_p.h> -#include <private/qtenvironmentvariables_p.h> // for qTzSet() +#include <private/qtenvironmentvariables_p.h> // for qTzSet(), qTzName() +#include <private/qcomparisontesthelper_p.h> #ifdef Q_OS_WIN -# include <qt_windows.h> +# include <qt_windows.h> +# if !QT_CONFIG(icu) +// The native MS back-end for time-zones lacks info about historic transitions: +# define INADEQUATE_TZ_DATA +# endif +#endif +#ifdef Q_OS_ANDROID // Also seems to lack full-day zone transitions: +# define INADEQUATE_TZ_DATA #endif using namespace Qt::StringLiterals; @@ -92,8 +100,11 @@ private Q_SLOTS: void secsTo(); void msecsTo_data() { addMSecs_data(); } void msecsTo(); + void orderingCompiles(); void operator_eqeq_data(); void operator_eqeq(); + void ordering_data(); + void ordering(); void operator_insert_extract_data(); void operator_insert_extract(); void currentDateTime(); @@ -151,11 +162,34 @@ private Q_SLOTS: #endif private: - enum { LocalTimeIsUtc = 0, LocalTimeAheadOfUtc = 1, LocalTimeBehindUtc = -1} localTimeType; + /* + Various zones close to UTC (notably Iceland, the WET zones and several in + West Africa) or nominally assigned to it historically (north Canada, the + Antarctic) and those that have crossed the international date-line (by + skipping or repeating a day) don't have a consistent answer to "which side + of UTC is it ?" So the various LocalTimeType members may be different. + */ + enum LocalTimeType { LocalTimeIsUtc = 0, LocalTimeAheadOfUtc = 1, LocalTimeBehindUtc = -1}; + const LocalTimeType solarMeanType, epochTimeType, futureTimeType, distantTimeType; static constexpr auto UTC = QTimeZone::UTC; + static constexpr qint64 epochJd = Q_INT64_C(2440588); int preZoneFix; bool zoneIsCET; + static LocalTimeType timeTypeFor(qint64 jand, qint64 juld) + { + constexpr uint day = 24 * 3600; // in seconds + QDateTime jan = QDateTime::fromSecsSinceEpoch(jand * day); + QDateTime jul = QDateTime::fromSecsSinceEpoch(juld * day); + if (jan.date().toJulianDay() < jand + epochJd || jul.date().toJulianDay() < juld + epochJd) + return LocalTimeBehindUtc; + if (jan.date().toJulianDay() > jand + epochJd || jul.date().toJulianDay() > juld + epochJd + || jan.time().hour() > 0 || jul.time().hour() > 0) { + return LocalTimeAheadOfUtc; + } + return LocalTimeIsUtc; + } + class TimeZoneRollback { const QByteArray prior; @@ -183,7 +217,18 @@ private: Q_DECLARE_METATYPE(Qt::TimeSpec) Q_DECLARE_METATYPE(Qt::DateFormat) -tst_QDateTime::tst_QDateTime() +tst_QDateTime::tst_QDateTime() : + // UTC starts of January and July in the commented years: + solarMeanType(timeTypeFor(-62091, -61910)), // 1800 + epochTimeType(timeTypeFor(0, 181)), // 1970 + // Use stable future, to which current rule is extrapolated, as surrogate for variable current: + futureTimeType(timeTypeFor(24837, 25018)), // 2038 + // The glibc functions only handle DST as far as a 32-bit signed day-count + // from some date in 1970 reaches; the future extreme of that is in the + // second half of 5'881'580 CE. Beyond 5'881'581 CE it treats all zones as + // being in their January state, regardless of time of year. So use data for + // this later year for tests of QDateTime's upper bound. + distantTimeType(timeTypeFor(0x800000adLL, 0x80000162LL)) { /* Due to some jurisdictions changing their zones and rules, it's possible @@ -197,7 +242,6 @@ tst_QDateTime::tst_QDateTime() might not be properly handled by our work-arounds for the MS backend and 32-bit time_t; so don't probe them here. */ - const uint day = 24 * 3600; // in seconds zoneIsCET = (QDateTime(QDate(2038, 1, 19), QTime(4, 14, 7)).toSecsSinceEpoch() == 0x7fffffff // Entries a year apart robustly differ by multiples of day. && QDate(2015, 7, 1).startOfDay().toSecsSinceEpoch() == 1435701600 @@ -227,31 +271,6 @@ tst_QDateTime::tst_QDateTime() Q_ASSERT(preZoneFix > -7200 && preZoneFix < 7200); // So it's OK to add it to a QTime() between 02:00 and 22:00, but otherwise // we must add it to the QDateTime constructed from it. - - /* - Again, rule changes can cause a TZ to look like UTC at some sample dates - but deviate at some date relevant to a test using localTimeType. These - tests mostly use years outside the 1970--2037 range, for which we trust - our TZ data, so we can't helpfully be exhaustive. Instead, scan a sample - of years' starts and middles. - */ - const int sampled = 3; - // UTC starts of months in 2004, 2038 and 1970: - qint64 jans[sampled] = { 12418 * day, 24837 * day, 0 }; - qint64 juls[sampled] = { 12600 * day, 25018 * day, 181 * day }; - localTimeType = LocalTimeIsUtc; - for (int i = sampled; i-- > 0; ) { - QDateTime jan = QDateTime::fromSecsSinceEpoch(jans[i]); - QDateTime jul = QDateTime::fromSecsSinceEpoch(juls[i]); - if (jan.date().year() < 1970 || jul.date().month() < 7) { - localTimeType = LocalTimeBehindUtc; - break; - } else if (jan.time().hour() > 0 || jul.time().hour() > 0 - || jan.date().day() > 1 || jul.date().day() > 1) { - localTimeType = LocalTimeAheadOfUtc; - break; - } - } } void tst_QDateTime::initTestCase() @@ -259,7 +278,7 @@ void tst_QDateTime::initTestCase() // Never construct a message like this in an i18n context... const char *typemsg1 = "exactly"; const char *typemsg2 = "and therefore not"; - switch (localTimeType) { + switch (futureTimeType) { case LocalTimeIsUtc: break; case LocalTimeBehindUtc: @@ -326,7 +345,7 @@ void tst_QDateTime::ctor() void tst_QDateTime::operator_eq() { - QVERIFY(QDateTime() != QDateTime(QDate(1970, 1, 1), QTime(0, 0))); // QTBUG-79006 + QVERIFY(QDateTime() != QDate(1970, 1, 1).startOfDay()); // QTBUG-79006 QDateTime dt1(QDate(2004, 3, 24), QTime(23, 45, 57), UTC); QDateTime dt2(QDate(2005, 3, 11), QTime(0, 0), UTC); dt2 = dt1; @@ -756,6 +775,7 @@ void tst_QDateTime::setMSecsSinceEpoch() QFETCH(qint64, msecs); QFETCH(QDateTime, utc); QFETCH(QDateTime, cet); + using Bound = std::numeric_limits<qint64>; QDateTime dt; dt.setTimeZone(UTC); @@ -789,10 +809,10 @@ void tst_QDateTime::setMSecsSinceEpoch() QCOMPARE(dt1.timeSpec(), Qt::UTC); } - if (zoneIsCET && (msecs == std::numeric_limits<qint64>::max() + if (zoneIsCET && (msecs == Bound::max() // LocalTime will also overflow for min in a CET zone west // of Greenwich (Europe/Madrid): - || (preZoneFix < -3600 && msecs == std::numeric_limits<qint64>::min()))) { + || (preZoneFix < -3600 && msecs == Bound::min()))) { QVERIFY(!cet.isValid()); // overflows } else if (zoneIsCET) { QVERIFY(cet.isValid()); @@ -834,18 +854,22 @@ void tst_QDateTime::setMSecsSinceEpoch() QCOMPARE(dt, reference.addMSecs(msecs)); // Tests that we correctly recognize when we fall off the extremities: - if (msecs == std::numeric_limits<qint64>::max()) { + if (msecs == Bound::max()) { QDateTime off(QDate(1970, 1, 1).startOfDay(QTimeZone::fromSecondsAheadOfUtc(1))); off.setMSecsSinceEpoch(msecs); QVERIFY(!off.isValid()); - } else if (msecs == std::numeric_limits<qint64>::min()) { + } else if (msecs == Bound::min()) { QDateTime off(QDate(1970, 1, 1).startOfDay(QTimeZone::fromSecondsAheadOfUtc(-1))); off.setMSecsSinceEpoch(msecs); QVERIFY(!off.isValid()); } - if ((localTimeType == LocalTimeAheadOfUtc && msecs == std::numeric_limits<qint64>::max()) - || (localTimeType == LocalTimeBehindUtc && msecs == std::numeric_limits<qint64>::min())) { + // Check overflow; only robust if local time is the same at epoch as relevant bound. + // See setting of LocalTimeType values for details. + if (epochTimeType == LocalTimeAheadOfUtc + ? distantTimeType == LocalTimeAheadOfUtc && msecs == Bound::max() + : (solarMeanType == LocalTimeBehindUtc && msecs == Bound::min() + && epochTimeType == LocalTimeBehindUtc)) { QDateTime curt = QDate(1970, 1, 1).startOfDay(); // initially in short-form curt.setMSecsSinceEpoch(msecs); // Overflows due to offset QVERIFY(!curt.isValid()); @@ -867,10 +891,10 @@ void tst_QDateTime::fromMSecsSinceEpoch() // you're East or West of Greenwich. In UTC, we won't overflow. If we're // actually west of Greenwich but (e.g. Europe/Madrid) our zone claims east, // "min" can also overflow (case only caught if local time is CET). - const bool localOverflow = (localTimeType == LocalTimeAheadOfUtc - ? msecs == Bound::max() || preZoneFix < -3600 - : localTimeType == LocalTimeBehindUtc && msecs == Bound::min()); - if (!localOverflow) + const bool localOverflow = + (distantTimeType == LocalTimeAheadOfUtc && (msecs == Bound::max() || preZoneFix < -3600)) + || (solarMeanType == LocalTimeBehindUtc && msecs == Bound::min()); + if (!localOverflow) // Can fail if offset changes sign, e.g. Alaska, Philippines. QCOMPARE(dtLocal, utc); QCOMPARE(dtUtc, utc); @@ -925,7 +949,22 @@ void tst_QDateTime::fromSecsSinceEpoch() QVERIFY(!QDateTime::fromSecsSinceEpoch(-maxSeconds - 1, UTC).isValid()); // Local time: need to adjust for its zone offset - const qint64 last = maxSeconds - qMax(late.addYears(-1).toLocalTime().offsetFromUtc(), 0); + const int lateOffset = late.addYears(-1).toLocalTime().offsetFromUtc(); +#if QT_CONFIG(timezone) + // Check what system zone believes in, as it's used as fall-back to cope + // with times outside the system time_t functions' range, or overflow on the + // results of using those functions. (It seems glibc's handling of + // Australasian zones parts company with the IANA DB after about 5881580 CE, + // leaving NZ in permanent DST after that, for example.) Of course, if + // that's less than lateOffset (as it is for glibc's similar handling of + // MET), the fall-back code will also fail when the primary code fails, so + // use the lesser of these late offsets. + const int lateZone = qMin(QTimeZone::systemTimeZone().offsetFromUtc(late), lateOffset); +#else + const int lateZone = lateOffset; +#endif + + const qint64 last = maxSeconds - qMax(lateZone, 0); QVERIFY(QDateTime::fromSecsSinceEpoch(last).isValid()); QVERIFY(!QDateTime::fromSecsSinceEpoch(last + 1).isValid()); const qint64 first = -maxSeconds - qMin(early.addYears(1).toLocalTime().offsetFromUtc(), 0); @@ -1261,15 +1300,62 @@ void tst_QDateTime::addDays() QCOMPARE(dt2.timeSpec(), Qt::TimeZone); QCOMPARE(dt2.timeZone(), cet); } -#endif +# ifndef INADEQUATE_TZ_DATA + if (const QTimeZone lint("Pacific/Kiritimati"); lint.isValid()) { + // Line Islands Time skipped Sat 1994-12-31: + dt1 = QDateTime(QDate(1994, 12, 30), QTime(12, 0), lint); + dt2 = QDateTime(QDate(1995, 1, 1), QTime(12, 0), lint); + // Trying to step into the hole gets the other side: + QCOMPARE(dt1.addDays(1), dt2); + QCOMPARE(dt2.addDays(-1), dt1); + // But the other side is in fact two days away: + QCOMPARE(dt1.addDays(2), dt2); + QCOMPARE(dt2.addDays(-2), dt1); + QCOMPARE(dt1.daysTo(dt2), 2); + } +# ifndef Q_OS_DARWIN + if (const QTimeZone alaska("America/Anchorage"); alaska.isValid()) { + // On Julian date 1867, Sat Oct 7 (at 14:31 local solar mean time for + // Anchorage, 15:30 LMT in Sitka, which hosted the transfer ceremony) + // Russia sold Alaska to the USA, which changed the calendar to + // Gregorian, hence the date to Fri Oct 18. Compare addSecs:Alaska-Day. + // Friday evening and Saturday morning were repeated, with different dates. + // Friday noon, as described by the Russians: + dt1 = QDateTime(QDate(1867, 10, 6, QCalendar(QCalendar::System::Julian)), + QTime(12, 0), alaska); + // Sunday noon, as described by the Americans: + dt2 = QDateTime(QDate(1867, 10, 20), QTime(12, 0), alaska); + // Three elapsed days, but daysTo() and addDays only see two: + QCOMPARE(dt1.addDays(2), dt2); + QCOMPARE(dt2.addDays(-2), dt1); + QCOMPARE(dt1.daysTo(dt2), 2); + // Stepping into the duplicated day (Julian 7th, Gregorian 19th) gets + // the nearer side, with the same nominal date (and time): + QCOMPARE(dt1.addDays(1).date(), dt2.addDays(-1).date()); + QCOMPARE(dt1.addDays(1).time(), dt2.addDays(-1).time()); + QCOMPARE(dt1.addDays(1).daysTo(dt2.addDays(-1)), 0); + // Yet they differ by a day: + QCOMPARE_NE(dt1.addDays(1), dt2.addDays(-1)); + QCOMPARE(dt1.addDays(1).secsTo(dt2.addDays(-1)), 24 * 60 * 60); + // Stepping from one duplicate one day towards the other jumps it: + QCOMPARE(dt1, dt2.addDays(-1).addDays(-1)); + QCOMPARE(dt1.addDays(1).addDays(1), dt2); + } +# endif // Darwin +# endif // inadequate zone data +#endif // timezone - // Test last UTC second of 1969 *is* valid (despite being time_t(-1)) - dt1 = QDateTime(QDate(1969, 12, 30), QTime(23, 59, 59), UTC).toLocalTime().addDays(1); - QVERIFY(dt1.isValid()); - QCOMPARE(dt1.toSecsSinceEpoch(), -1); - dt2 = QDateTime(QDate(1970, 1, 1), QTime(23, 59, 59), UTC).toLocalTime().addDays(-1); - QVERIFY(dt2.isValid()); - QCOMPARE(dt2.toSecsSinceEpoch(), -1); + // Baja Mexico has a transition at the epoch, see fromStringDateFormat_data(). + if (QDateTime(QDate(1969, 12, 30), QTime(0, 0)).secsTo( + QDateTime(QDate(1970, 1, 2), QTime(0, 0))) == 3 * 24 * 60 * 60) { + // Test last UTC second of 1969 *is* valid (despite being time_t(-1)) + dt1 = QDateTime(QDate(1969, 12, 30), QTime(23, 59, 59), UTC).toLocalTime().addDays(1); + QVERIFY(dt1.isValid()); + QCOMPARE(dt1.toSecsSinceEpoch(), -1); + dt2 = QDateTime(QDate(1970, 1, 1), QTime(23, 59, 59), UTC).toLocalTime().addDays(-1); + QVERIFY(dt2.isValid()); + QCOMPARE(dt2.toSecsSinceEpoch(), -1); + } } void tst_QDateTime::addInvalid() @@ -1525,10 +1611,10 @@ void tst_QDateTime::addMSecs_data() << QDateTime(QDate(2013, 1, 1), QTime(2, 2, 3), QTimeZone::fromSecondsAheadOfUtc(60 * 60)); // Check last second of 1969 QTest::newRow("epoch-1s-utc") - << QDateTime(QDate(1970, 1, 1), QTime(0, 0), UTC) << qint64(-1) + << QDate(1970, 1, 1).startOfDay(UTC) << qint64(-1) << QDateTime(QDate(1969, 12, 31), QTime(23, 59, 59), UTC); QTest::newRow("epoch-1s-local") - << QDateTime(QDate(1970, 1, 1), QTime(0, 0)) << qint64(-1) + << QDate(1970, 1, 1).startOfDay() << qint64(-1) << QDateTime(QDate(1969, 12, 31), QTime(23, 59, 59)); QTest::newRow("epoch-1s-utc-as-local") << QDate(1970, 1, 1).startOfDay(UTC).toLocalTime() << qint64(-1) @@ -1546,6 +1632,43 @@ void tst_QDateTime::addMSecs_data() QTest::newRow("to-first") << QDateTime::fromSecsSinceEpoch(1 - maxSeconds, UTC) << qint64(-1) << QDateTime::fromSecsSinceEpoch(-maxSeconds, UTC); + +#if QT_CONFIG(timezone) + if (const QTimeZone cet("Europe/Oslo"); cet.isValid()) { + QTest::newRow("CET-spring-forward") + << QDateTime(QDate(2023, 3, 26), QTime(1, 30), cet) << qint64(60 * 60) + << QDateTime(QDate(2023, 3, 26), QTime(3, 30), cet); + QTest::newRow("CET-fall-back") + << QDateTime(QDate(2023, 10, 29), QTime(1, 30), cet) << qint64(3 * 60 * 60) + << QDateTime(QDate(2023, 10, 29), QTime(3, 30), cet); + } +# ifndef INADEQUATE_TZ_DATA + const QTimeZone lint("Pacific/Kiritimati"); + if (lint.isValid()) { + // Line Islands Time skipped Sat 1994-12-31: + QTest::newRow("Kiritimati-day-off") + << QDateTime(QDate(1994, 12, 30), QTime(23, 30), lint) << qint64(60 * 60) + << QDateTime(QDate(1995, 1, 1), QTime(0, 30), lint); + } +# ifndef Q_OS_DARWIN + if (const QTimeZone alaska("America/Anchorage"); alaska.isValid()) { + // On Julian date 1867, Sat Oct 7 (at 14:31 local solar mean time for + // Anchorage, 15:30 LMT in Sitka, which hosted the transfer ceremony) + // Russia sold Alaska to the USA, which changed the calendar to + // Gregorian, hence the date to Fri Oct 18. Contrast addDays(). + const QDate sat(1867, 10, 19); + Q_ASSERT(sat == QDate(1867, 10, 7, QCalendar(QCalendar::System::Julian))); + // At the start of the day, it was Sat 7th; by evening it was Fri 18th; + // then the next day was Sat 19th. + QTest::newRow("Alaska-Day") + // The actual morning of the hand-over: + << QDateTime(sat, QTime(6, 0), alaska) << qint64(12 * 60 * 60) + // The evening of the same day. + << QDateTime(sat, QTime(18, 0), alaska).addDays(-1); + } +# endif // Darwin +# endif // inadequate zone data +#endif // timezone } void tst_QDateTime::addSecs_data() @@ -1589,6 +1712,7 @@ void tst_QDateTime::addSecs() QCOMPARE(result - std::chrono::seconds(nsecs), dt); test3 -= std::chrono::seconds(nsecs); QCOMPARE(test3, dt); + QCOMPARE(dt.secsTo(result), nsecs); } } @@ -1609,10 +1733,15 @@ void tst_QDateTime::addMSecs() QCOMPARE(result.addMSecs(qint64(-nsecs) * 1000), dt); } }; - - verify(dt.addMSecs(qint64(nsecs) * 1000)); - verify(dt.addDuration(std::chrono::seconds(nsecs))); - verify(dt.addDuration(std::chrono::milliseconds(nsecs * 1000))); +#define VERIFY(datum) \ + verify(datum); \ + if (QTest::currentTestFailed()) \ + return + + VERIFY(dt.addMSecs(qint64(nsecs) * 1000)); + VERIFY(dt.addDuration(std::chrono::seconds(nsecs))); + VERIFY(dt.addDuration(std::chrono::milliseconds(nsecs * 1000))); +#undef VERIFY } #if QT_DEPRECATED_SINCE(6, 9) @@ -1860,6 +1989,11 @@ void tst_QDateTime::msecsTo() } } +void tst_QDateTime::orderingCompiles() +{ + QTestPrivate::testAllComparisonOperatorsCompile<QDateTime>(); +} + void tst_QDateTime::currentDateTime() { time_t buf1, buf2; @@ -2137,12 +2271,13 @@ void tst_QDateTime::springForward_data() document any such conflicts, if discovered. See http://www.timeanddate.com/time/zones/ for data on more candidates to - test. - */ + test. Note, however, that the IANA DB disagrees with it for some zones, + and is authoritative. + */ - QTimeZone local(QTimeZone::LocalTime); - uint winter = QDate(2015, 1, 1).startOfDay(local).toSecsSinceEpoch(); - uint summer = QDate(2015, 7, 1).startOfDay(local).toSecsSinceEpoch(); + const QTimeZone local(QTimeZone::LocalTime); + const uint winter = QDate(2015, 1, 1).startOfDay(local).toSecsSinceEpoch(); + const uint summer = QDate(2015, 7, 1).startOfDay(local).toSecsSinceEpoch(); if (winter == 1420066800 && summer == 1435701600) { QTest::newRow("Local (CET) from day before") @@ -2150,11 +2285,28 @@ void tst_QDateTime::springForward_data() QTest::newRow("Local (CET) from day after") << local << QDate(2015, 3, 29) << QTime(2, 30) << -1 << 120; } else if (winter == 1420063200 && summer == 1435698000) { - // e.g. Finland, where our CI runs ... + // EET: but there's some variation in the date and time. + // Asia/{Amman,Beirut,Gaza,Hebron}, Europe/Chisinau and Israel: at start of + QDate date(2015, 3, 29); // Sunday by default. + QTime time(0, 30); + if (auto thursday = QDate(2015, 3, 26); thursday.startOfDay(local).time() > time) { + // Asia/Damascus: start of March 26th. + date = thursday; + } else if (auto friday = QDate(2015, 3, 27); friday.startOfDay(local).time() > time) { + // Israel, Asia/{Jerusalem,Tel_Aviv}: start of March 27th (IANA DB). + date = friday; + } else if (friday.startOfDay(local).addSecs(2 * 60 * 60).time() == QTime(3, 0)) { + // Israel, Asia/{Jerusalem,Tel_Aviv} according to glibc at 02:00 on March 27th. + date = friday; + time = QTime(2, 30); + } else if (date.startOfDay(local).time() < time) { + // Most of Europeean EET, e.g. Finland. + time = QTime(3, 30); + } QTest::newRow("Local (EET) from day before") - << local << QDate(2015, 3, 29) << QTime(3, 30) << 1 << 120; + << local << date << time << 1 << 120; QTest::newRow("Local (EET) from day after") - << local << QDate(2015, 3, 29) << QTime(3, 30) << -1 << 180; + << local << date << time << -1 << 180; } else if (winter == 1420070400 && summer == 1435705200) { // Western European Time, WET/WEST; a.k.a. GMT/BST QTest::newRow("Local (WET) from day before") @@ -2163,16 +2315,23 @@ void tst_QDateTime::springForward_data() << local << QDate(2015, 3, 29) << QTime(1, 30) << -1 << 60; } else if (winter == 1420099200 && summer == 1435734000) { // Western USA, Canada: Pacific Time (e.g. US/Pacific) + QDate date(2015, 3, 8); + // America/Ensenada did its transition on April 5th, like the rest of Mexico. + if (QDate(2015, 4, 1).startOfDay().toSecsSinceEpoch() == 1427875200) + date = QDate(2015, 4, 5); QTest::newRow("Local (PT) from day before") - << local << QDate(2015, 3, 8) << QTime(2, 30) << 1 << -480; + << local << date << QTime(2, 30) << 1 << -480; QTest::newRow("Local (PT) from day after") - << local << QDate(2015, 3, 8) << QTime(2, 30) << -1 << -420; + << local << date << QTime(2, 30) << -1 << -420; } else if (winter == 1420088400 && summer == 1435723200) { // Eastern USA, Canada: Eastern Time (e.g. US/Eastern) + // Havana matches offset and date, but at midnight. + const QTime start = QDate(2015, 3, 8).startOfDay(local).time(); + const QTime when = start == QTime(0, 0) ? QTime(2, 30) : QTime(0, 30); QTest::newRow("Local(ET) from day before") - << local << QDate(2015, 3, 8) << QTime(2, 30) << 1 << -300; + << local << QDate(2015, 3, 8) << when << 1 << -300; QTest::newRow("Local(ET) from day after") - << local << QDate(2015, 3, 8) << QTime(2, 30) << -1 << -240; + << local << QDate(2015, 3, 8) << when << -1 << -240; #if !QT_CONFIG(timezone) } else { // Includes the numbers you need to test for your zone, as above: @@ -2224,26 +2383,24 @@ void tst_QDateTime::springForward() QFETCH(int, adjust); QDateTime direct = QDateTime(day.addDays(-step), time, zone).addDays(step); - if (direct.isValid()) { // mktime() may deem a time in the gap invalid - QCOMPARE(direct.date(), day); - QCOMPARE(direct.time().minute(), time.minute()); - QCOMPARE(direct.time().second(), time.second()); - int off = direct.time().hour() - time.hour(); - QVERIFY(off == 1 || off == -1); - // Note: function doc claims always +1, but this should be reviewed ! - } - - // Repeat, but getting there via .toTimeZone(): - QDateTime detour = QDateTime(day.addDays(-step), - time.addSecs(-60 * adjust), - UTC).toTimeZone(zone); + QVERIFY(direct.isValid()); + QCOMPARE(direct.date(), day); + QCOMPARE(direct.time().minute(), time.minute()); + QCOMPARE(direct.time().second(), time.second()); + const int off = step < 0 ? -1 : 1; + QCOMPARE(direct.time().hour() - time.hour(), off); + // adjust is the offset on the other side of the gap: + QCOMPARE(direct.offsetFromUtc(), (adjust + off * 60) * 60); + + // Repeat, but getting there via .toTimeZone(). Apply adjust to datetime, + // not time, as the time wraps round if the adjustment crosses midnight. + QDateTime detour = QDateTime(day.addDays(-step), time, + UTC).addSecs(-60 * adjust).toTimeZone(zone); QCOMPARE(detour.time(), time); detour = detour.addDays(step); // Insist on consistency: - if (direct.isValid()) - QCOMPARE(detour, direct); - else - QVERIFY(!detour.isValid()); + QCOMPARE(detour, direct); + QCOMPARE(detour.offsetFromUtc(), direct.offsetFromUtc()); } void tst_QDateTime::operator_eqeq_data() @@ -2280,7 +2437,7 @@ void tst_QDateTime::operator_eqeq_data() QTest::newRow("data10") << dateTime3 << dateTime3c << true << false; QTest::newRow("data11") << dateTime3 << dateTime3d << true << false; QTest::newRow("data12") << dateTime3c << dateTime3d << true << false; - if (localTimeType == LocalTimeIsUtc) + if (epochTimeType == LocalTimeIsUtc) QTest::newRow("data13") << dateTime3 << dateTime3e << true << false; // ... but a zone (sometimes) ahead of or behind UTC (e.g. Europe/London) // might agree with UTC about the epoch, all the same. @@ -2315,23 +2472,16 @@ void tst_QDateTime::operator_eqeq() QFETCH(bool, expectEqual); QFETCH(bool, checkEuro); - QVERIFY(dt1 == dt1); - QVERIFY(!(dt1 != dt1)); - - QVERIFY(dt2 == dt2); - QVERIFY(!(dt2 != dt2)); + QT_TEST_EQUALITY_OPS(dt1, dt1, true); + QT_TEST_EQUALITY_OPS(dt2, dt2, true); + QT_TEST_EQUALITY_OPS(dt1, dt2, expectEqual); QVERIFY(dt1 != QDateTime::currentDateTime()); QVERIFY(dt2 != QDateTime::currentDateTime()); QVERIFY(dt1.toUTC() == dt1.toUTC()); - bool equal = dt1 == dt2; - QCOMPARE(equal, expectEqual); - bool notEqual = dt1 != dt2; - QCOMPARE(notEqual, !expectEqual); - - if (equal) + if (expectEqual) QVERIFY(qHash(dt1) == qHash(dt2)); if (checkEuro && zoneIsCET) { @@ -2340,6 +2490,64 @@ void tst_QDateTime::operator_eqeq() } } +void tst_QDateTime::ordering_data() +{ + QTest::addColumn<QDateTime>("left"); + QTest::addColumn<QDateTime>("right"); + QTest::addColumn<Qt::weak_ordering>("expectedOrdering"); + + Q_CONSTINIT static const auto constructName = [](const QDateTime &dt) -> QByteArray { + if (dt.isNull()) + return "null"; + if (!dt.isValid()) + return "invalid"; + return dt.toString(Qt::ISODateWithMs).toLatin1(); + }; + + Q_CONSTINIT static const auto generateRow = + [](const QDateTime &left, const QDateTime &right, Qt::weak_ordering ordering) { + const QByteArray leftStr = constructName(left); + const QByteArray rightStr = constructName(right); + QTest::addRow("%s_vs_%s", leftStr.constData(), rightStr.constData()) + << left << right << ordering; + }; + + QDateTime june(QDate(2012, 6, 20), QTime(14, 33, 2, 500)); + QDateTime juneLater = june.addMSecs(1); + QDateTime badDay(QDate(2012, 20, 6), QTime(14, 33, 2, 500)); // Invalid + QDateTime epoch(QDate(1970, 1, 1), QTime(0, 0), UTC); // UTC epoch + QDateTime nextDay = epoch.addDays(1); + QDateTime prevDay = epoch.addDays(-1); + // Ensure that different times may be equal when considering timezone. + QDateTime epochEast1h(epoch.addSecs(3600)); + epochEast1h.setTimeZone(QTimeZone::fromSecondsAheadOfUtc(3600)); + QDateTime epochWest1h(epoch.addSecs(-3600)); + epochWest1h.setTimeZone(QTimeZone::fromSecondsAheadOfUtc(-3600)); + QDateTime local1970(epoch.date(), epoch.time()); // Local time's epoch + + generateRow(june, june, Qt::weak_ordering::equivalent); + generateRow(june, juneLater, Qt::weak_ordering::less); + generateRow(june, badDay, Qt::weak_ordering::greater); + generateRow(badDay, QDateTime(), Qt::weak_ordering::equivalent); + generateRow(june, QDateTime(), Qt::weak_ordering::greater); + generateRow(epoch, nextDay, Qt::weak_ordering::less); + generateRow(epoch, prevDay, Qt::weak_ordering::greater); + generateRow(epoch, epochEast1h, Qt::weak_ordering::equivalent); + generateRow(epoch, epochWest1h, Qt::weak_ordering::equivalent); + generateRow(epochEast1h, epochWest1h, Qt::weak_ordering::equivalent); + if (epochTimeType == LocalTimeIsUtc) + generateRow(epoch, local1970, Qt::weak_ordering::equivalent); +} + +void tst_QDateTime::ordering() +{ + QFETCH(QDateTime, left); + QFETCH(QDateTime, right); + QFETCH(Qt::weak_ordering, expectedOrdering); + + QT_TEST_ALL_COMPARISON_OPS(left, right, expectedOrdering); +} + Q_DECLARE_METATYPE(QDataStream::Version) void tst_QDateTime::operator_insert_extract_data() @@ -2473,6 +2681,10 @@ void tst_QDateTime::fromStringDateFormat_data() QTest::addColumn<Qt::DateFormat>("dateFormat"); QTest::addColumn<QDateTime>("expected"); + // Fails 1970 start dates in western Mexico + // due to changing from PST to MST at the start of 1970. + const bool goodEpochStart = QDateTime(QDate(1970, 1, 1), QTime(0, 0)).isValid(); + // Test Qt::TextDate format. QTest::newRow("text date") << QString::fromLatin1("Tue Jun 17 08:00:10 2003") << Qt::TextDate << QDateTime(QDate(2003, 6, 17), QTime(8, 0, 10)); @@ -2484,22 +2696,25 @@ void tst_QDateTime::fromStringDateFormat_data() << Qt::TextDate << QDateTime(QDate(12345, 6, 17), QTime(8, 0, 10)); QTest::newRow("text date Year -4712") << QString::fromLatin1("Tue Jan 1 00:01:02 -4712") << Qt::TextDate << QDateTime(QDate(-4712, 1, 1), QTime(0, 1, 2)); - QTest::newRow("text epoch") - << QString::fromLatin1("Thu Jan 1 00:00:00 1970") << Qt::TextDate - << QDate(1970, 1, 1).startOfDay(); QTest::newRow("text data1") << QString::fromLatin1("Thu Jan 2 12:34 1970") << Qt::TextDate << QDateTime(QDate(1970, 1, 2), QTime(12, 34)); + if (goodEpochStart) { + QTest::newRow("text epoch year after time") + << QString::fromLatin1("Thu Jan 1 00:00:00 1970") << Qt::TextDate + << QDate(1970, 1, 1).startOfDay(); + QTest::newRow("text epoch spaced") + << QString::fromLatin1(" Thu Jan 1 00:00:00 1970 ") + << Qt::TextDate << QDate(1970, 1, 1).startOfDay(); + QTest::newRow("text epoch time after year") + << QString::fromLatin1("Thu Jan 1 1970 00:00:00") + << Qt::TextDate << QDate(1970, 1, 1).startOfDay(); + } QTest::newRow("text epoch terse") << QString::fromLatin1("Thu Jan 1 00 1970") << Qt::TextDate << QDateTime(); QTest::newRow("text epoch stray :00") << QString::fromLatin1("Thu Jan 1 00:00:00:00 1970") << Qt::TextDate << QDateTime(); - QTest::newRow("text epoch spaced") - << QString::fromLatin1(" Thu Jan 1 00:00:00 1970 ") - << Qt::TextDate << QDate(1970, 1, 1).startOfDay(); QTest::newRow("text data6") << QString::fromLatin1("Thu Jan 1 00:00:00") << Qt::TextDate << QDateTime(); - QTest::newRow("text data7") << QString::fromLatin1("Thu Jan 1 1970 00:00:00") - << Qt::TextDate << QDate(1970, 1, 1).startOfDay(); QTest::newRow("text bad offset") << QString::fromLatin1("Thu Jan 1 00:12:34 1970 UTC+foo") << Qt::TextDate << QDateTime(); QTest::newRow("text UTC early") << QString::fromLatin1("Thu Jan 1 00:12:34 1970 UTC") @@ -2660,10 +2875,16 @@ void tst_QDateTime::fromStringDateFormat_data() QTest::newRow("ISO 24:00") << QString::fromLatin1("2012-06-04T24:00:00") << Qt::ISODate << QDate(2012, 6, 5).startOfDay(); #if QT_CONFIG(timezone) - QTest::newRow("ISO 24:00 in DST") // Only special if TZ=America/Sao_Paulo + const QByteArray sysId = QTimeZone::systemTimeZoneId(); + const bool midnightSkip = sysId == "America/Sao_Paulo" || sysId == "America/Asuncion" + || sysId == "America/Cordoba" || sysId == "America/Argentina/Cordoba" + || sysId == "America/Campo_Grande" + || sysId == "America/Cuiaba" || sysId == "America/Buenos_Aires" + || sysId == "America/Argentina/Buenos_Aires" + || sysId == "America/Argentina/Tucuman" || sysId == "Brazil/East"; + QTest::newRow("ISO 24:00 in DST") // Midnight spring forward in some of South America. << QString::fromLatin1("2008-10-18T24:00") << Qt::ISODate - << QDateTime(QDate(2008, 10, 19), - QTime(QTimeZone::systemTimeZoneId() == "America/Sao_Paulo" ? 1 : 0, 0)); + << QDateTime(QDate(2008, 10, 19), QTime(midnightSkip ? 1 : 0, 0)); #endif QTest::newRow("ISO 24:00 end of month") << QString::fromLatin1("2012-06-30T24:00:00") @@ -2825,8 +3046,6 @@ void tst_QDateTime::fromStringDateFormat_data() << Qt::RFC2822Date << QDateTime(QDate(1987, 2, 13), QTime(14, 24, 51), UTC); QTest::newRow("RFC 850 and 1036 +0000") << QString::fromLatin1("Thu Jan 01 00:12:34 1970 +0000") << Qt::RFC2822Date << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), UTC); - QTest::newRow("RFC 850 and 1036 +0000") << QString::fromLatin1("Thu Jan 01 00:12:34 1970 +0000") - << Qt::RFC2822Date << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), UTC); // No timezone assume UTC QTest::newRow("RFC 850 and 1036 no timezone") << QString::fromLatin1("Thu Jan 01 00:12:34 1970") << Qt::RFC2822Date << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), UTC); @@ -2873,258 +3092,266 @@ void tst_QDateTime::fromStringStringFormat_data() { QTest::addColumn<QString>("string"); QTest::addColumn<QString>("format"); + QTest::addColumn<int>("baseYear"); QTest::addColumn<QDateTime>("expected"); - const QDate defDate(1900, 1, 1); - QTest::newRow("data0") - << QString("101010") << QString("dMyy") << QDate(1910, 10, 10).startOfDay(); - QTest::newRow("data1") << QString("1020") << QString("sss") << QDateTime(); - QTest::newRow("data2") - << QString("1010") << QString("sss") << QDateTime(defDate, QTime(0, 0, 10)); - QTest::newRow("data3") << QString("10hello20") << QString("ss'hello'ss") << QDateTime(); - QTest::newRow("data4") << QString("10") << QString("''") << QDateTime(); - QTest::newRow("data5") << QString("10") << QString("'") << QDateTime(); - QTest::newRow("data6") << QString("pm") << QString("ap") << QDateTime(defDate, QTime(12, 0)); - QTest::newRow("data7") << QString("foo") << QString("ap") << QDateTime(); + // Indian/Cocos had a transition at the start of 1900, so its Jan 1st starts + // at 00:02:20 on that day; this leads to perverse results. QTBUG-77948. + if (const QDate defDate(1900, 1, 1); defDate.startOfDay().time() == QTime(0, 0)) { + QTest::newRow("dMyy-only:19") + << u"101010"_s << u"dMyy"_s << 1900 << QDate(1910, 10, 10).startOfDay(); + QTest::newRow("dMyy-only:20") + << u"101010"_s << u"dMyy"_s << 1911 << QDate(2010, 10, 10).startOfDay(); + QTest::newRow("secs-repeat-valid") + << u"1010"_s << u"sss"_s << 1900 << QDateTime(defDate, QTime(0, 0, 10)); + QTest::newRow("pm-only") + << u"pm"_s << u"ap"_s << 1900 << QDateTime(defDate, QTime(12, 0)); + QTest::newRow("date-only:19") + << u"10 Oct 10"_s << u"dd MMM yy"_s << 1900 << QDate(1910, 10, 10).startOfDay(); + QTest::newRow("date-only:20") + << u"10 Oct 10"_s << u"dd MMM yy"_s << 1950 << QDate(2010, 10, 10).startOfDay(); + QTest::newRow("dow-date-only") + << u"Fri December 3 2004"_s << u"ddd MMMM d yyyy"_s << 1900 + << QDate(2004, 12, 3).startOfDay(); + QTest::newRow("dow-mon-yr-only") + << u"Thu January 2004"_s << u"ddd MMMM yyyy"_s << 1900 + << QDate(2004, 1, 1).startOfDay(); + } + QTest::newRow("yy=24/Mar/20") // QTBUG-123579 + << u"Wed, 20 Mar 24 16:17:00"_s << u"ddd, dd MMM yy HH:mm:ss"_s << 1900 + << QDateTime(QDate(2024, 3, 20), QTime(16, 17)); + QTest::newRow("secs-conflict") << u"1020"_s << u"sss"_s << 1900 << QDateTime(); + QTest::newRow("secs-split-conflict") + << u"10hello20"_s << u"ss'hello'ss"_s << 1900 << QDateTime(); + QTest::newRow("nomatch-quote-twice") << u"10"_s << u"''"_s << 1900 << QDateTime(); + QTest::newRow("momatch-quote") << u"10"_s << u"'"_s << 1900 << QDateTime(); + QTest::newRow("nomatch-am-pm") << u"foo"_s << u"ap"_s << 1900 << QDateTime(); // Day non-conflict should not hide earlier year conflict (1963-03-01 was a // Friday; asking for Thursday moves this, without conflict, to the 7th): - QTest::newRow("data8") - << QString("77 03 1963 Thu") << QString("yy MM yyyy ddd") << QDateTime(); - QTest::newRow("data9") - << QString("101010") << QString("dMyy") << QDate(1910, 10, 10).startOfDay(); - QTest::newRow("data10") - << QString("101010") << QString("dMyy") << QDate(1910, 10, 10).startOfDay(); - QTest::newRow("data11") - << QString("10 Oct 10") << QString("dd MMM yy") << QDate(1910, 10, 10).startOfDay(); - QTest::newRow("data12") - << QString("Fri December 3 2004") << QString("ddd MMMM d yyyy") - << QDate(2004, 12, 3).startOfDay(); - QTest::newRow("data13") << QString("30.02.2004") << QString("dd.MM.yyyy") << QDateTime(); - QTest::newRow("data14") << QString("32.01.2004") << QString("dd.MM.yyyy") << QDateTime(); - QTest::newRow("data15") - << QString("Thu January 2004") << QString("ddd MMMM yyyy") - << QDate(2004, 1, 1).startOfDay(); + QTest::newRow("year-conflict") + << u"77 03 1963 Thu"_s << u"yy MM yyyy ddd"_s << 1900 << QDateTime(); + QTest::newRow("Feb-overflow") << u"30.02.2004"_s << u"dd.MM.yyyy"_s << 1900 << QDateTime(); + QTest::newRow("Jan-overflow") << u"32.01.2004"_s << u"dd.MM.yyyy"_s << 1900 << QDateTime(); QTest::newRow("zulu-time-with-z-centisec") - << QString("2005-06-28T07:57:30.01Z") << QString("yyyy-MM-ddThh:mm:ss.zt") - << QDateTime(QDate(2005, 06, 28), QTime(07, 57, 30, 10), UTC); + << u"2005-06-28T07:57:30.01Z"_s << u"yyyy-MM-ddThh:mm:ss.zt"_s << 1900 + << QDateTime(QDate(2005, 06, 28), QTime(07, 57, 30, 10), UTC); QTest::newRow("zulu-time-with-zz-decisec") - << QString("2005-06-28T07:57:30.1Z") << QString("yyyy-MM-ddThh:mm:ss.zzt") - << QDateTime(QDate(2005, 06, 28), QTime(07, 57, 30, 100), UTC); + << u"2005-06-28T07:57:30.1Z"_s << u"yyyy-MM-ddThh:mm:ss.zzt"_s << 1900 + << QDateTime(QDate(2005, 06, 28), QTime(07, 57, 30, 100), UTC); QTest::newRow("zulu-time-with-zzz-centisec") - << QString("2005-06-28T07:57:30.01Z") << QString("yyyy-MM-ddThh:mm:ss.zzzt") - << QDateTime(); // Invalid because too few digits for zzz + << u"2005-06-28T07:57:30.01Z"_s << u"yyyy-MM-ddThh:mm:ss.zzzt"_s << 1900 + << QDateTime(); // Invalid because too few digits for zzz QTest::newRow("zulu-time-with-z-millisec") - << QString("2005-06-28T07:57:30.001Z") << QString("yyyy-MM-ddThh:mm:ss.zt") - << QDateTime(QDate(2005, 06, 28), QTime(07, 57, 30, 1), UTC); + << u"2005-06-28T07:57:30.001Z"_s << u"yyyy-MM-ddThh:mm:ss.zt"_s << 1900 + << QDateTime(QDate(2005, 06, 28), QTime(07, 57, 30, 1), UTC); QTest::newRow("utc-time-spec-as:UTC+0") - << QString("2005-06-28T07:57:30.001UTC+0") << QString("yyyy-MM-ddThh:mm:ss.zt") - << QDateTime(QDate(2005, 6, 28), QTime(7, 57, 30, 1), UTC); + << u"2005-06-28T07:57:30.001UTC+0"_s << u"yyyy-MM-ddThh:mm:ss.zt"_s << 1900 + << QDateTime(QDate(2005, 6, 28), QTime(7, 57, 30, 1), UTC); QTest::newRow("utc-time-spec-as:UTC-0") - << QString("2005-06-28T07:57:30.001UTC-0") << QString("yyyy-MM-ddThh:mm:ss.zt") - << QDateTime(QDate(2005, 6, 28), QTime(7, 57, 30, 1), UTC); + << u"2005-06-28T07:57:30.001UTC-0"_s << u"yyyy-MM-ddThh:mm:ss.zt"_s << 1900 + << QDateTime(QDate(2005, 6, 28), QTime(7, 57, 30, 1), UTC); QTest::newRow("offset-from-utc:UTC+1") - << QString("2001-09-13T07:33:01.001 UTC+1") << QString("yyyy-MM-ddThh:mm:ss.z t") - << QDateTime(QDate(2001, 9, 13), QTime(7, 33, 1, 1), - QTimeZone::fromSecondsAheadOfUtc(3600)); + << u"2001-09-13T07:33:01.001 UTC+1"_s << u"yyyy-MM-ddThh:mm:ss.z t"_s << 1900 + << QDateTime(QDate(2001, 9, 13), QTime(7, 33, 1, 1), + QTimeZone::fromSecondsAheadOfUtc(3600)); QTest::newRow("offset-from-utc:UTC-11:01") - << QString("2008-09-13T07:33:01.001 UTC-11:01") << QString("yyyy-MM-ddThh:mm:ss.z t") - << QDateTime(QDate(2008, 9, 13), QTime(7, 33, 1, 1), - QTimeZone::fromSecondsAheadOfUtc(-39660)); + << u"2008-09-13T07:33:01.001 UTC-11:01"_s << u"yyyy-MM-ddThh:mm:ss.z t"_s << 1900 + << QDateTime(QDate(2008, 9, 13), QTime(7, 33, 1, 1), + QTimeZone::fromSecondsAheadOfUtc(-39660)); QTest::newRow("offset-from-utc:UTC+02:57") - << QString("2001-09-15T09:33:01.001UTC+02:57") << QString("yyyy-MM-ddThh:mm:ss.zt") - << QDateTime(QDate(2001, 9, 15), QTime(9, 33, 1, 1), - QTimeZone::fromSecondsAheadOfUtc(10620)); + << u"2001-09-15T09:33:01.001UTC+02:57"_s << u"yyyy-MM-ddThh:mm:ss.zt"_s << 1900 + << QDateTime(QDate(2001, 9, 15), QTime(9, 33, 1, 1), + QTimeZone::fromSecondsAheadOfUtc(10620)); QTest::newRow("offset-from-utc:-03:00") // RFC 3339 offset format - << QString("2001-09-15T09:33:01.001-03:00") << QString("yyyy-MM-ddThh:mm:ss.zt") - << QDateTime(QDate(2001, 9, 15), QTime(9, 33, 1, 1), - QTimeZone::fromSecondsAheadOfUtc(-10800)); + << u"2001-09-15T09:33:01.001-03:00"_s << u"yyyy-MM-ddThh:mm:ss.zt"_s << 1900 + << QDateTime(QDate(2001, 9, 15), QTime(9, 33, 1, 1), + QTimeZone::fromSecondsAheadOfUtc(-10800)); QTest::newRow("offset-from-utc:+0205") // ISO 8601 basic offset format - << QString("2001-09-15T09:33:01.001+0205") << QString("yyyy-MM-ddThh:mm:ss.zt") - << QDateTime(QDate(2001, 9, 15), QTime(9, 33, 1, 1), - QTimeZone::fromSecondsAheadOfUtc(7500)); + << u"2001-09-15T09:33:01.001+0205"_s << u"yyyy-MM-ddThh:mm:ss.zt"_s << 1900 + << QDateTime(QDate(2001, 9, 15), QTime(9, 33, 1, 1), + QTimeZone::fromSecondsAheadOfUtc(7500)); QTest::newRow("offset-from-utc:-0401") // ISO 8601 basic offset format - << QString("2001-09-15T09:33:01.001-0401") << QString("yyyy-MM-ddThh:mm:ss.zt") - << QDateTime(QDate(2001, 9, 15), QTime(9, 33, 1, 1), - QTimeZone::fromSecondsAheadOfUtc(-14460)); + << u"2001-09-15T09:33:01.001-0401"_s << u"yyyy-MM-ddThh:mm:ss.zt"_s << 1900 + << QDateTime(QDate(2001, 9, 15), QTime(9, 33, 1, 1), + QTimeZone::fromSecondsAheadOfUtc(-14460)); QTest::newRow("offset-from-utc:+10") // ISO 8601 basic (hour-only) offset format - << QString("2001-09-15T09:33:01.001 +10") << QString("yyyy-MM-ddThh:mm:ss.z t") - << QDateTime(QDate(2001, 9, 15), QTime(9, 33, 1, 1), - QTimeZone::fromSecondsAheadOfUtc(36000)); + << u"2001-09-15T09:33:01.001 +10"_s << u"yyyy-MM-ddThh:mm:ss.z t"_s << 1900 + << QDateTime(QDate(2001, 9, 15), QTime(9, 33, 1, 1), + QTimeZone::fromSecondsAheadOfUtc(36000)); QTest::newRow("offset-from-utc:UTC+10:00") // Time-spec specifier at the beginning - << QString("UTC+10:00 2008-10-13T07:33") << QString("t yyyy-MM-ddThh:mm") - << QDateTime(QDate(2008, 10, 13), QTime(7, 33), QTimeZone::fromSecondsAheadOfUtc(36000)); + << u"UTC+10:00 2008-10-13T07:33"_s << u"t yyyy-MM-ddThh:mm"_s << 1900 + << QDateTime(QDate(2008, 10, 13), QTime(7, 33), + QTimeZone::fromSecondsAheadOfUtc(36000)); QTest::newRow("offset-from-utc:UTC-03:30") // Time-spec specifier in the middle - << QString("2008-10-13 UTC-03:30 11.50") << QString("yyyy-MM-dd t hh.mm") - << QDateTime(QDate(2008, 10, 13), QTime(11, 50), - QTimeZone::fromSecondsAheadOfUtc(-12600)); + << u"2008-10-13 UTC-03:30 11.50"_s << u"yyyy-MM-dd t hh.mm"_s << 1900 + << QDateTime(QDate(2008, 10, 13), QTime(11, 50), + QTimeZone::fromSecondsAheadOfUtc(-12600)); QTest::newRow("offset-from-utc:UTC-2") // Time-spec specifier joined with text/time - << QString("2008-10-13 UTC-2Z11.50") << QString("yyyy-MM-dd tZhh.mm") - << QDateTime(QDate(2008, 10, 13), QTime(11, 50), QTimeZone::fromSecondsAheadOfUtc(-7200)); + << u"2008-10-13 UTC-2Z11.50"_s << u"yyyy-MM-dd tZhh.mm"_s << 1900 + << QDateTime(QDate(2008, 10, 13), QTime(11, 50), + QTimeZone::fromSecondsAheadOfUtc(-7200)); QTest::newRow("offset-from-utc:followed-by-colon") - << QString("2008-10-13 UTC-0100:11.50") << QString("yyyy-MM-dd t:hh.mm") - << QDateTime(QDate(2008, 10, 13), QTime(11, 50), QTimeZone::fromSecondsAheadOfUtc(-3600)); + << u"2008-10-13 UTC-0100:11.50"_s << u"yyyy-MM-dd t:hh.mm"_s << 1900 + << QDateTime(QDate(2008, 10, 13), QTime(11, 50), + QTimeZone::fromSecondsAheadOfUtc(-3600)); QTest::newRow("offset-from-utc:late-colon") - << QString("2008-10-13 UTC+05T:11.50") << QString("yyyy-MM-dd tT:hh.mm") - << QDateTime(QDate(2008, 10, 13), QTime(11, 50), QTimeZone::fromSecondsAheadOfUtc(18000)); + << u"2008-10-13 UTC+05T:11.50"_s << u"yyyy-MM-dd tT:hh.mm"_s << 1900 + << QDateTime(QDate(2008, 10, 13), QTime(11, 50), + QTimeZone::fromSecondsAheadOfUtc(18000)); QTest::newRow("offset-from-utc:merged-with-time") - << QString("2008-10-13 UTC+010011.50") << QString("yyyy-MM-dd thh.mm") - << QDateTime(QDate(2008, 10, 13), QTime(11, 50), QTimeZone::fromSecondsAheadOfUtc(3600)); + << u"2008-10-13 UTC+010011.50"_s << u"yyyy-MM-dd thh.mm"_s << 1900 + << QDateTime(QDate(2008, 10, 13), QTime(11, 50), + QTimeZone::fromSecondsAheadOfUtc(3600)); QTest::newRow("offset-from-utc:double-colon-delimiter") - << QString("2008-10-13 UTC+12::11.50") << QString("yyyy-MM-dd t::hh.mm") - << QDateTime(QDate(2008, 10, 13), QTime(11, 50), QTimeZone::fromSecondsAheadOfUtc(43200)); + << u"2008-10-13 UTC+12::11.50"_s << u"yyyy-MM-dd t::hh.mm"_s << 1900 + << QDateTime(QDate(2008, 10, 13), QTime(11, 50), + QTimeZone::fromSecondsAheadOfUtc(43200)); QTest::newRow("offset-from-utc:3-digit-with-colon") - << QString("2008-10-13 -4:30 11.50") << QString("yyyy-MM-dd t hh.mm") - << QDateTime(QDate(2008, 10, 13), QTime(11, 50), QTimeZone::fromSecondsAheadOfUtc(-16200)); - QTest::newRow("offset-from-utc:merged-with-time") - << QString("2008-10-13 UTC+010011.50") << QString("yyyy-MM-dd thh.mm") - << QDateTime(QDate(2008, 10, 13), QTime(11, 50), QTimeZone::fromSecondsAheadOfUtc(3600)); + << u"2008-10-13 -4:30 11.50"_s << u"yyyy-MM-dd t hh.mm"_s << 1900 + << QDateTime(QDate(2008, 10, 13), QTime(11, 50), + QTimeZone::fromSecondsAheadOfUtc(-16200)); QTest::newRow("offset-from-utc:with-colon-merged-with-time") - << QString("2008-10-13 UTC+01:0011.50") << QString("yyyy-MM-dd thh.mm") - << QDateTime(QDate(2008, 10, 13), QTime(11, 50), QTimeZone::fromSecondsAheadOfUtc(3600)); + << u"2008-10-13 UTC+01:0011.50"_s << u"yyyy-MM-dd thh.mm"_s << 1900 + << QDateTime(QDate(2008, 10, 13), QTime(11, 50), + QTimeZone::fromSecondsAheadOfUtc(3600)); QTest::newRow("invalid-offset-from-utc:out-of-range") - << QString("2001-09-15T09:33:01.001-50") << QString("yyyy-MM-ddThh:mm:ss.zt") - << QDateTime(); + << u"2001-09-15T09:33:01.001-50"_s << u"yyyy-MM-ddThh:mm:ss.zt"_s << 1900 + << QDateTime(); QTest::newRow("invalid-offset-from-utc:single-digit-format") - << QString("2001-09-15T09:33:01.001+5") << QString("yyyy-MM-ddThh:mm:ss.zt") - << QDateTime(); + << u"2001-09-15T09:33:01.001+5"_s << u"yyyy-MM-ddThh:mm:ss.zt"_s << 1900 << QDateTime(); QTest::newRow("invalid-offset-from-utc:three-digit-format") - << QString("2001-09-15T09:33:01.001-701") << QString("yyyy-MM-ddThh:mm:ss.zt") - << QDateTime(); + << u"2001-09-15T09:33:01.001-701"_s << u"yyyy-MM-ddThh:mm:ss.zt"_s << 1900 + << QDateTime(); QTest::newRow("invalid-offset-from-utc:three-digit-minutes") - << QString("2001-09-15T09:33:01.001+11:570") << QString("yyyy-MM-ddThh:mm:ss.zt") - << QDateTime(); + << u"2001-09-15T09:33:01.001+11:570"_s << u"yyyy-MM-ddThh:mm:ss.zt"_s << 1900 + << QDateTime(); QTest::newRow("invalid-offset-from-utc:single-digit-minutes") - << QString("2001-09-15T09:33:01.001+11:5") << QString("yyyy-MM-ddThh:mm:ss.zt") - << QDateTime(); + << u"2001-09-15T09:33:01.001+11:5"_s << u"yyyy-MM-ddThh:mm:ss.zt"_s << 1900 + << QDateTime(); QTest::newRow("invalid-offset-from-utc:invalid-sign-symbol") - << QString("2001-09-15T09:33:01.001 ~11:30") << QString("yyyy-MM-ddThh:mm:ss.z t") - << QDateTime(); + << u"2001-09-15T09:33:01.001 ~11:30"_s << u"yyyy-MM-ddThh:mm:ss.z t"_s << 1900 + << QDateTime(); QTest::newRow("invalid-offset-from-utc:symbol-in-hours") - << QString("2001-09-15T09:33:01.001 UTC+o8:30") << QString("yyyy-MM-ddThh:mm:ss.z t") - << QDateTime(); + << u"2001-09-15T09:33:01.001 UTC+o8:30"_s << u"yyyy-MM-ddThh:mm:ss.z t"_s << 1900 + << QDateTime(); QTest::newRow("invalid-offset-from-utc:symbol-in-minutes") - << QString("2001-09-15T09:33:01.001 UTC+08:3i") << QString("yyyy-MM-ddThh:mm:ss.z t") - << QDateTime(); + << u"2001-09-15T09:33:01.001 UTC+08:3i"_s << u"yyyy-MM-ddThh:mm:ss.z t"_s << 1900 + << QDateTime(); QTest::newRow("invalid-offset-from-utc:UTC+123") // Invalid offset (UTC and 3 digit format) - << QString("2001-09-15T09:33:01.001 UTC+123") << QString("yyyy-MM-ddThh:mm:ss.z t") - << QDateTime(); + << u"2001-09-15T09:33:01.001 UTC+123"_s << u"yyyy-MM-ddThh:mm:ss.z t"_s << 1900 + << QDateTime(); QTest::newRow("invalid-offset-from-utc:UTC+00005") // Invalid offset with leading zeroes - << QString("2001-09-15T09:33:01.001 UTC+00005") << QString("yyyy-MM-ddThh:mm:ss.z t") - << QDateTime(); + << u"2001-09-15T09:33:01.001 UTC+00005"_s << u"yyyy-MM-ddThh:mm:ss.z t"_s << 1900 + << QDateTime(); QTest::newRow("invalid-offset-from-utc:three-digit-with-colon-delimiter") - << QString("2008-10-13 +123:11.50") << QString("yyyy-MM-dd t:hh.mm") - << QDateTime(); + << u"2008-10-13 +123:11.50"_s << u"yyyy-MM-dd t:hh.mm"_s << 1900 << QDateTime(); QTest::newRow("invalid-offset-from-utc:double-colon-as-part-of-offset") - << QString("2008-10-13 UTC+12::11.50") << QString("yyyy-MM-dd thh.mm") - << QDateTime(); + << u"2008-10-13 UTC+12::11.50"_s << u"yyyy-MM-dd thh.mm"_s << 1900 << QDateTime(); QTest::newRow("invalid-offset-from-utc:single-colon-as-part-of-offset") - << QString("2008-10-13 UTC+12::11.50") << QString("yyyy-MM-dd t:hh.mm") - << QDateTime(); + << u"2008-10-13 UTC+12::11.50"_s << u"yyyy-MM-dd t:hh.mm"_s << 1900 << QDateTime(); QTest::newRow("invalid-offset-from-utc:starts-with-colon") - << QString("2008-10-13 UTC+:59 11.50") << QString("yyyy-MM-dd t hh.mm") - << QDateTime(); + << u"2008-10-13 UTC+:59 11.50"_s << u"yyyy-MM-dd t hh.mm"_s << 1900 << QDateTime(); QTest::newRow("invalid-offset-from-utc:empty-offset") - << QString("2008-10-13 UTC+ 11.50") << QString("yyyy-MM-dd t hh.mm") - << QDateTime(); + << u"2008-10-13 UTC+ 11.50"_s << u"yyyy-MM-dd t hh.mm"_s << 1900 << QDateTime(); QTest::newRow("invalid-offset-from-utc:time-section-instead-of-offset") - << QString("2008-10-13 UTC+11.50") << QString("yyyy-MM-dd thh.mm") - << QDateTime(); + << u"2008-10-13 UTC+11.50"_s << u"yyyy-MM-dd thh.mm"_s << 1900 << QDateTime(); QTest::newRow("invalid-offset-from-utc:missing-minutes-if-colon") - << QString("2008-10-13 +05: 11.50") << QString("yyyy-MM-dd t hh.mm") - << QDateTime(); + << u"2008-10-13 +05: 11.50"_s << u"yyyy-MM-dd t hh.mm"_s << 1900 << QDateTime(); QTest::newRow("invalid-offset-from-utc:1-digit-minutes-if-colon") - << QString("2008-10-13 UTC+05:1 11.50") << QString("yyyy-MM-dd t hh.mm") - << QDateTime(); + << u"2008-10-13 UTC+05:1 11.50"_s << u"yyyy-MM-dd t hh.mm"_s << 1900 << QDateTime(); QTest::newRow("invalid-time-spec:random-symbol") - << QString("2001-09-15T09:33:01.001 $") << QString("yyyy-MM-ddThh:mm:ss.z t") - << QDateTime(); + << u"2001-09-15T09:33:01.001 $"_s << u"yyyy-MM-ddThh:mm:ss.z t"_s << 1900 + << QDateTime(); QTest::newRow("invalid-time-spec:random-digit") - << QString("2001-09-15T09:33:01.001 1") << QString("yyyy-MM-ddThh:mm:ss.z t") - << QDateTime(); + << u"2001-09-15T09:33:01.001 1"_s << u"yyyy-MM-ddThh:mm:ss.z t"_s << 1900 + << QDateTime(); QTest::newRow("invalid-offset-from-utc:merged-with-time") - << QString("2008-10-13 UTC+0111.50") << QString("yyyy-MM-dd thh.mm") - << QDateTime(); + << u"2008-10-13 UTC+0111.50"_s << u"yyyy-MM-dd thh.mm"_s << 1900 << QDateTime(); QTest::newRow("invalid-offset-from-utc:with-colon-3-digit-merged-with-time") - << QString("2008-10-13 UTC+01:011.50") << QString("yyyy-MM-dd thh.mm") - << QDateTime(); + << u"2008-10-13 UTC+01:011.50"_s << u"yyyy-MM-dd thh.mm"_s << 1900 << QDateTime(); QTest::newRow("invalid-time-spec:empty") - << QString("2001-09-15T09:33:01.001 ") << QString("yyyy-MM-ddThh:mm:ss.z t") - << QDateTime(); + << u"2001-09-15T09:33:01.001 "_s << u"yyyy-MM-ddThh:mm:ss.z t"_s << 1900 << QDateTime(); #if QT_CONFIG(timezone) QTimeZone southBrazil("America/Sao_Paulo"); if (southBrazil.isValid()) { QTest::newRow("spring-forward-midnight") - // NB: no hour field, so hour takes its default, so that default can - // be over-ridden: - << QString("2008-10-19 23:45.678 America/Sao_Paulo") - << QString("yyyy-MM-dd mm:ss.zzz t") - // That's in the hour skipped - expect the matching time after the - // spring-forward, in DST: - << QDateTime(QDate(2008, 10, 19), QTime(1, 23, 45, 678), southBrazil); + // NB: no hour field, so hour takes its default, 0, so that + // default can be over-ridden: + << u"2008-10-19 23:45.678 America/Sao_Paulo"_s << u"yyyy-MM-dd mm:ss.zzz t"_s << 1900 + // That's in the hour skipped - expect the matching time after + // the spring-forward, in DST: + << QDateTime(QDate(2008, 10, 19), QTime(1, 23, 45, 678), southBrazil); } QTimeZone berlintz("Europe/Berlin"); if (berlintz.isValid()) { QTest::newRow("begin-of-high-summer-time-with-tz") - << QString("1947-05-11 03:23:45.678 Europe/Berlin") - << QString("yyyy-MM-dd hh:mm:ss.zzz t") - // That's in the hour skipped - expecting an invalid DateTime - << QDateTime(QDate(1947, 5, 11), QTime(3, 23, 45, 678), berlintz); + << u"1947-05-11 03:23:45.678 Europe/Berlin"_s << u"yyyy-MM-dd hh:mm:ss.zzz t"_s + // That's in the hour skipped - expecting an invalid DateTime + << 1900 << QDateTime(QDate(1947, 5, 11), QTime(3, 23, 45, 678), berlintz); } #endif - QTest::newRow("late") << QString("9999-12-31T23:59:59.999Z") - << QString("yyyy-MM-ddThh:mm:ss.zZ") - << QDateTime(QDate(9999, 12, 31), QTime(23, 59, 59, 999)); + QTest::newRow("late") + << u"9999-12-31T23:59:59.999Z"_s << u"yyyy-MM-ddThh:mm:ss.zZ"_s << 1900 + << QDateTime(QDate(9999, 12, 31), QTime(23, 59, 59, 999)); // Separators match /([^aAdhHMmstyz]*)/ QTest::newRow("oddly-separated") // To show broken-separator's format is valid. - << QStringLiteral("2018 wilful long working block relief 12-19T21:09 cruel blurb encore flux") - << QStringLiteral("yyyy wilful long working block relief MM-ddThh:mm cruel blurb encore flux") - << QDateTime(QDate(2018, 12, 19), QTime(21, 9)); + << u"2018 wilful long working block relief 12-19T21:09 cruel blurb encore flux"_s + << u"yyyy wilful long working block relief MM-ddThh:mm cruel blurb encore flux"_s + << 1900 << QDateTime(QDate(2018, 12, 19), QTime(21, 9)); QTest::newRow("broken-separator") - << QStringLiteral("2018 wilful") - << QStringLiteral("yyyy wilful long working block relief MM-ddThh:mm cruel blurb encore flux") - << QDateTime(); + << u"2018 wilful"_s + << u"yyyy wilful long working block relief MM-ddThh:mm cruel blurb encore flux"_s + << 1900 << QDateTime(); QTest::newRow("broken-terminator") - << QStringLiteral("2018 wilful long working block relief 12-19T21:09 cruel") - << QStringLiteral("yyyy wilful long working block relief MM-ddThh:mm cruel blurb encore flux") - << QDateTime(); + << u"2018 wilful long working block relief 12-19T21:09 cruel"_s + << u"yyyy wilful long working block relief MM-ddThh:mm cruel blurb encore flux"_s + << 1900 << QDateTime(); // test unicode - QTest::newRow("unicode handling") << QString(u8"2005🤣06🤣28T07🤣57🤣30.001Z") - << QString(u8"yyyy🤣MM🤣ddThh🤣mm🤣ss.zt") - << QDateTime(QDate(2005, 6, 28), QTime(7, 57, 30, 1), UTC); + QTest::newRow("unicode handling") + << QString(u8"2005🤣06🤣28T07🤣57🤣30.001Z") + << QString(u8"yyyy🤣MM🤣ddThh🤣mm🤣ss.zt") << 1900 + << QDateTime(QDate(2005, 6, 28), QTime(7, 57, 30, 1), UTC); // Two tests derived from malformed ASN.1 strings (QTBUG-84349): - QTest::newRow("ASN.1:UTC") - << u"22+221102233Z"_s << u"yyMMddHHmmsst"_s << QDateTime(); - QTest::newRow("ASN.1:Generalized") - << u"9922+221102233Z"_s << u"yyyyMMddHHmmsst"_s << QDateTime(); + QTest::newRow("curft+ASN.1:UTC") + << u"22+221102233Z"_s << u"yyMMddHHmmsst"_s << 1900 << QDateTime(); + QTest::newRow("curft+ASN.1:Generalized") + << u"9922+221102233Z"_s << u"yyyyMMddHHmmsst"_s << 1900 << QDateTime(); + // Verify baseYear needed by plain ASN.1 works: + QTest::newRow("ASN.1:UTC-start") + << u"500101000000Z"_s << u"yyMMddHHmmsst"_s << 1950 + << QDate(1950, 1, 1).startOfDay(QTimeZone::UTC); + QTest::newRow("ASN.1:UTC-end") + << u"491231235959Z"_s << u"yyMMddHHmmsst"_s << 1950 + << QDate(2049, 12, 31).endOfDay(QTimeZone::UTC).addMSecs(-999); // fuzzer test QTest::newRow("integer overflow found by fuzzer") - << QStringLiteral("EEE1200000MUB") << QStringLiteral("t") - << QDateTime(); + << u"EEE1200000MUB"_s << u"t"_s << 1900 << QDateTime(); // Rich time-zone specifiers (QTBUG-95966): const auto east3hours = QTimeZone::fromSecondsAheadOfUtc(10800); QTest::newRow("timezone-tt-with-offset:+0300") - << QString("2008-10-13 +0300 11.50") << QString("yyyy-MM-dd tt hh.mm") - << QDateTime(QDate(2008, 10, 13), QTime(11, 50), east3hours); + << u"2008-10-13 +0300 11.50"_s << u"yyyy-MM-dd tt hh.mm"_s + << 1900 << QDateTime(QDate(2008, 10, 13), QTime(11, 50), east3hours); QTest::newRow("timezone-ttt-with-offset:+03:00") - << QString("2008-10-13 +03:00 11.50") << QString("yyyy-MM-dd ttt hh.mm") - << QDateTime(QDate(2008, 10, 13), QTime(11, 50), east3hours); + << u"2008-10-13 +03:00 11.50"_s << u"yyyy-MM-dd ttt hh.mm"_s + << 1900 << QDateTime(QDate(2008, 10, 13), QTime(11, 50), east3hours); QTest::newRow("timezone-tttt-with-offset:+03:00") - << QString("2008-10-13 +03:00 11.50") << QString("yyyy-MM-dd tttt hh.mm") - << QDateTime(); // Offset not valid when zone name expected. + << u"2008-10-13 +03:00 11.50"_s << u"yyyy-MM-dd tttt hh.mm"_s + << 1900 << QDateTime(); // Offset not valid when zone name expected. } void tst_QDateTime::fromStringStringFormat() { QFETCH(QString, string); QFETCH(QString, format); + QFETCH(int, baseYear); QFETCH(QDateTime, expected); - QDateTime dt = QDateTime::fromString(string, format); + QDateTime dt = QDateTime::fromString(string, format, baseYear); QCOMPARE(dt, expected); if (expected.isValid()) { @@ -3141,6 +3368,7 @@ void tst_QDateTime::fromStringStringFormat_localTimeZone_data() QTest::addColumn<QByteArray>("localTimeZone"); QTest::addColumn<QString>("string"); QTest::addColumn<QString>("format"); + QTest::addColumn<int>("baseYear"); QTest::addColumn<QDateTime>("expected"); #if QT_CONFIG(timezone) @@ -3153,50 +3381,55 @@ void tst_QDateTime::fromStringStringFormat_localTimeZone_data() if (etcGmtWithOffset.isValid()) { lacksRows = false; QTest::newRow("local-timezone-t-with-zone:Etc/GMT+3") - << QByteArrayLiteral("GMT") - << QString("2008-10-13 Etc/GMT+3 11.50") << QString("yyyy-MM-dd t hh.mm") - << QDateTime(QDate(2008, 10, 13), QTime(11, 50), etcGmtWithOffset); + << "GMT"_ba << u"2008-10-13 Etc/GMT+3 11.50"_s << u"yyyy-MM-dd t hh.mm"_s << 1900 + << QDateTime(QDate(2008, 10, 13), QTime(11, 50), etcGmtWithOffset); QTest::newRow("local-timezone-tttt-with-zone:Etc/GMT+3") - << QByteArrayLiteral("GMT") - << QString("2008-10-13 Etc/GMT+3 11.50") << QString("yyyy-MM-dd tttt hh.mm") - << QDateTime(QDate(2008, 10, 13), QTime(11, 50), etcGmtWithOffset); + << "GMT"_ba << u"2008-10-13 Etc/GMT+3 11.50"_s << u"yyyy-MM-dd tttt hh.mm"_s << 1900 + << QDateTime(QDate(2008, 10, 13), QTime(11, 50), etcGmtWithOffset); } QTest::newRow("local-timezone-tt-with-zone:Etc/GMT+3") - << QByteArrayLiteral("GMT") - << QString("2008-10-13 Etc/GMT+3 11.50") << QString("yyyy-MM-dd tt hh.mm") - << QDateTime(); // Zone name not valid when offset expected + << "GMT"_ba << u"2008-10-13 Etc/GMT+3 11.50"_s << u"yyyy-MM-dd tt hh.mm"_s << 1900 + << QDateTime(); // Zone name not valid when offset expected QTest::newRow("local-timezone-ttt-with-zone:Etc/GMT+3") - << QByteArrayLiteral("GMT") - << QString("2008-10-13 Etc/GMT+3 11.50") << QString("yyyy-MM-dd ttt hh.mm") - << QDateTime(); // Zone name not valid when offset expected + << "GMT"_ba << u"2008-10-13 Etc/GMT+3 11.50"_s << u"yyyy-MM-dd ttt hh.mm"_s << 1900 + << QDateTime(); // Zone name not valid when offset expected QTimeZone gmtWithOffset("GMT-2"); if (gmtWithOffset.isValid()) { lacksRows = false; QTest::newRow("local-timezone-with-offset:GMT-2") - << QByteArrayLiteral("GMT") - << QString("2008-10-13 GMT-2 11.50") << QString("yyyy-MM-dd t hh.mm") - << QDateTime(QDate(2008, 10, 13), QTime(11, 50), gmtWithOffset); + << "GMT"_ba << u"2008-10-13 GMT-2 11.50"_s << u"yyyy-MM-dd t hh.mm"_s << 1900 + << QDateTime(QDate(2008, 10, 13), QTime(11, 50), gmtWithOffset); } QTimeZone gmt("GMT"); if (gmt.isValid()) { lacksRows = false; + const bool fullyLocal = ([]() { + TimeZoneRollback useZone("GMT"); + return qTzName(0) == u"GMT"_s; + })(); QTest::newRow("local-timezone-with-offset:GMT") - << QByteArrayLiteral("GMT") - << QString("2008-10-13 GMT 11.50") << QString("yyyy-MM-dd t hh.mm") - << QDateTime(QDate(2008, 10, 13), QTime(11, 50), gmt); + << "GMT"_ba << u"2008-10-13 GMT 11.50"_s << u"yyyy-MM-dd t hh.mm"_s << 1900 + << QDateTime(QDate(2008, 10, 13), QTime(11, 50), + fullyLocal ? QTimeZone(QTimeZone::LocalTime) : gmt); } QTimeZone helsinki("Europe/Helsinki"); if (helsinki.isValid()) { lacksRows = false; - // QTBUG-96861: QAsn1Element::toDateTime() tripped over an assert in - // QTimeZonePrivate::dataForLocalTime() on macOS and iOS. - // The first 20m 11s of 1921-05-01 were skipped, so the parser's attempt - // to construct a local time after scanning yyMM tripped up on the start - // of the day, when the zone backend lacked transition data. - QTest::newRow("Helsinki-joins-EET") - << QByteArrayLiteral("Europe/Helsinki") - << QString("210506000000Z") << QString("yyMMddHHmmsst") - << QDateTime(QDate(1921, 5, 6), QTime(0, 0), UTC); + // QTBUG-96861: QAsn1Element::toDateTime() tripped over an assert due to + // the first 20m 11s of 1921-05-01 being skipped, so the parser's + // attempt to construct a local time after scanning yyMM tripped up on + // the start of the day, when the zone backend lacked transition data. + // (Because QDTP tries to use local time until it reads the final zone + // field, constructing a new QDT after reading each field, hence + // transiently wanting 1921-05-01 00:00:00 before reading the dd field.) + QTest::newRow("Helsinki-joins-EET:19") + << "Europe/Helsinki"_ba << u"210506000000Z"_s << u"yyMMddHHmmsst"_s << 1900 + << QDateTime(QDate(1921, 5, 6), QTime(0, 0), UTC); + // Strictly, ASN.1 wants us to parse that with a different baseYear, so + // check that, too, but tweak to match the 1921 transition's mid-point: + QTest::newRow("Helsinki-joins-EET:20") + << "Europe/Helsinki"_ba << u"210501001006Z"_s << u"yyMMddHHmmsst"_s << 1950 + << QDateTime(QDate(2021, 5, 1), QTime(0, 10, 6), UTC); } if (lacksRows) QSKIP("Testcases all use zones unsupported on this platform"); @@ -3442,16 +3675,34 @@ void tst_QDateTime::timeZoneAbbreviation() if (zoneIsCET) { // Time definitely in Standard Time QDateTime dt4 = QDate(2013, 1, 1).startOfDay(); + /* Note that MET is functionally an alias for CET (their zoneinfo files + differ only in the first letter of the abbreviations), unlike the + various zones that give CET as their abbreviation. + */ + { + const auto abbrev = dt4.timeZoneAbbreviation(); + auto reporter = qScopeGuard([abbrev]() { + qDebug() << "Unexpected abbreviation" << abbrev; + }); #ifdef Q_OS_WIN - QEXPECT_FAIL("", "Windows only reports long name (QTBUG-32759)", Continue); + QEXPECT_FAIL("", "Windows only reports long name (QTBUG-32759)", Continue); #endif - QCOMPARE(dt4.timeZoneAbbreviation(), QStringLiteral("CET")); + QVERIFY(abbrev == u"CET"_s || abbrev == u"MET"_s); + reporter.dismiss(); + } // Time definitely in Daylight Time QDateTime dt5 = QDate(2013, 6, 1).startOfDay(); + { + const auto abbrev = dt5.timeZoneAbbreviation(); + auto reporter = qScopeGuard([abbrev]() { + qDebug() << "Unexpected abbreviation" << abbrev; + }); #ifdef Q_OS_WIN - QEXPECT_FAIL("", "Windows only reports long name (QTBUG-32759)", Continue); + QEXPECT_FAIL("", "Windows only reports long name (QTBUG-32759)", Continue); #endif - QCOMPARE(dt5.timeZoneAbbreviation(), QStringLiteral("CEST")); + QVERIFY(abbrev == u"CEST"_s || abbrev == u"MEST"_s); + reporter.dismiss(); + } } else { qDebug("(Skipped some CET-only tests)"); } @@ -3605,12 +3856,23 @@ void tst_QDateTime::daylightTransitions() const QCOMPARE(before.time(), QTime(1, 59, 59, 999)); QCOMPARE(before.toMSecsSinceEpoch(), spring2012 - 1); - QDateTime missing(QDate(2012, 3, 25), QTime(2, 0)); - QVERIFY(!missing.isValid()); - QCOMPARE(missing.date(), QDate(2012, 3, 25)); - QCOMPARE(missing.time(), QTime(2, 0)); - // datetimeparser relies on toMSecsSinceEpoch to still work: - QCOMPARE(missing.toMSecsSinceEpoch(), spring2012); + QDateTime entering(QDate(2012, 3, 25), QTime(2, 0), + QDateTime::TransitionResolution::PreferBefore); + QVERIFY(entering.isValid()); + QVERIFY(!entering.isDaylightTime()); + QCOMPARE(entering.date(), QDate(2012, 3, 25)); + QCOMPARE(entering.time(), QTime(1, 0)); + // QDateTimeParser relies on toMSecsSinceEpoch() to still work: + QCOMPARE(entering.toMSecsSinceEpoch(), spring2012 - msecsOneHour); + + QDateTime leaving(QDate(2012, 3, 25), QTime(2, 0), + QDateTime::TransitionResolution::PreferAfter); + QVERIFY(leaving.isValid()); + QVERIFY(leaving.isDaylightTime()); + QCOMPARE(leaving.date(), QDate(2012, 3, 25)); + QCOMPARE(leaving.time(), QTime(3, 0)); + // QDateTimeParser relies on toMSecsSinceEpoch to still work: + QCOMPARE(leaving.toMSecsSinceEpoch(), spring2012); QDateTime after(QDate(2012, 3, 25), QTime(3, 0)); QVERIFY(after.isValid()); @@ -3638,14 +3900,14 @@ void tst_QDateTime::daylightTransitions() const QVERIFY(utc.isValid()); QCOMPARE(utc.date(), QDate(2012, 3, 25)); QCOMPARE(utc.time(), QTime(2, 0)); - utc.setTimeZone(QTimeZone::LocalTime); - QVERIFY(!utc.isValid()); + utc.setTimeZone(QTimeZone::LocalTime); // Resolved to RelativeToBefore. + QVERIFY(utc.isValid()); QCOMPARE(utc.date(), QDate(2012, 3, 25)); - QCOMPARE(utc.time(), QTime(2, 0)); - utc.setTimeZone(UTC); + QCOMPARE(utc.time(), QTime(3, 0)); + utc.setTimeZone(UTC); // Preserves the changed time(). QVERIFY(utc.isValid()); QCOMPARE(utc.date(), QDate(2012, 3, 25)); - QCOMPARE(utc.time(), QTime(2, 0)); + QCOMPARE(utc.time(), QTime(3, 0)); // Test date maths, if result falls in missing hour then becomes next // hour (or is always invalid; mktime() may reject gap-times). @@ -3683,19 +3945,17 @@ void tst_QDateTime::daylightTransitions() const #undef CHECK_SPRING_FORWARD // Test for correct behviour for DaylightTime -> StandardTime transition, fall-back - // TODO (QTBUG-79923): Compare to results of direct QDateTime(date, time, fold) - // construction; see Prior/Post commented-out tests. QDateTime autumnMidnight = QDate(2012, 10, 28).startOfDay(); QVERIFY(autumnMidnight.isValid()); - // QCOMPARE(autumnMidnight, QDateTime(QDate(2012, 10, 28), QTime(2, 0), Prior)); QCOMPARE(autumnMidnight.date(), QDate(2012, 10, 28)); QCOMPARE(autumnMidnight.time(), QTime(0, 0)); QCOMPARE(autumnMidnight.toMSecsSinceEpoch(), autumn2012 - 3 * msecsOneHour); QDateTime startFirst = autumnMidnight.addMSecs(2 * msecsOneHour); QVERIFY(startFirst.isValid()); - // QCOMPARE(startFirst, QDateTime(QDate(2012, 10, 28), QTime(2, 0), Prior)); + QCOMPARE(startFirst, QDateTime(QDate(2012, 10, 28), QTime(2, 0), + QDateTime::TransitionResolution::PreferBefore)); QCOMPARE(startFirst.date(), QDate(2012, 10, 28)); QCOMPARE(startFirst.time(), QTime(2, 0)); QCOMPARE(startFirst.toMSecsSinceEpoch(), autumn2012 - msecsOneHour); @@ -3703,7 +3963,9 @@ void tst_QDateTime::daylightTransitions() const // 1 msec before transition is 2:59:59.999 FirstOccurrence QDateTime endFirst = startFirst.addMSecs(msecsOneHour - 1); QVERIFY(endFirst.isValid()); - // QCOMPARE(endFirst, QDateTime(QDate(2012, 10, 28), QTime(2, 59, 59, 999), Prior)); + QCOMPARE(endFirst, + QDateTime(QDate(2012, 10, 28), QTime(2, 59, 59, 999), + QDateTime::TransitionResolution::PreferBefore)); QCOMPARE(endFirst.date(), QDate(2012, 10, 28)); QCOMPARE(endFirst.time(), QTime(2, 59, 59, 999)); QCOMPARE(endFirst.toMSecsSinceEpoch(), autumn2012 - 1); @@ -3711,7 +3973,8 @@ void tst_QDateTime::daylightTransitions() const // At the transition, starting the second pass QDateTime startRepeat = endFirst.addMSecs(1); QVERIFY(startRepeat.isValid()); - // QCOMPARE(startRepeat, QDateTime(QDate(2012, 10, 28), QTime(2, 0), Post)); + QCOMPARE(startRepeat, QDateTime(QDate(2012, 10, 28), QTime(2, 0), + QDateTime::TransitionResolution::PreferAfter)); QCOMPARE(startRepeat.date(), QDate(2012, 10, 28)); QCOMPARE(startRepeat.time(), QTime(2, 0)); QCOMPARE(startRepeat.toMSecsSinceEpoch(), autumn2012); @@ -3719,7 +3982,9 @@ void tst_QDateTime::daylightTransitions() const // 59:59.999 after transition is 2:59:59.999 SecondOccurrence QDateTime endRepeat = endFirst.addMSecs(msecsOneHour); QVERIFY(endRepeat.isValid()); - // QCOMPARE(endRepeat, QDateTime(QDate(2012, 10, 28), QTime(2, 59, 59, 999), Post)); + QCOMPARE(endRepeat, + QDateTime(QDate(2012, 10, 28), QTime(2, 59, 59, 999), + QDateTime::TransitionResolution::PreferAfter)); QCOMPARE(endRepeat.date(), QDate(2012, 10, 28)); QCOMPARE(endRepeat.time(), QTime(2, 59, 59, 999)); QCOMPARE(endRepeat.toMSecsSinceEpoch(), autumn2012 + msecsOneHour - 1); @@ -4004,7 +4269,7 @@ void tst_QDateTime::timeZones() const QCOMPARE(nzStdOffset.date(), QDate(2012, 6, 1)); QCOMPARE(nzStdOffset.time(), QTime(12, 0)); QVERIFY(nzStdOffset.timeZone() == nzTzOffset); - QCOMPARE(nzStdOffset.timeZone().id(), QByteArray("UTC+12")); + QCOMPARE(nzStdOffset.timeZone().id(), QByteArray("UTC+12:00")); QCOMPARE(nzStdOffset.offsetFromUtc(), 43200); QVERIFY(!nzStdOffset.isDaylightTime()); QCOMPARE(nzStdOffset.toMSecsSinceEpoch(), utcStd.toMSecsSinceEpoch()); @@ -4112,14 +4377,53 @@ void tst_QDateTime::timeZones() const QCOMPARE(atGap.toMSecsSinceEpoch(), gapMSecs); // - Test transition hole, setting 02:00:00 is invalid QDateTime inGap = QDateTime(QDate(2013, 3, 31), QTime(2, 0), cet); - QVERIFY(!inGap.isValid()); + QVERIFY(inGap.isValid()); QCOMPARE(inGap.date(), QDate(2013, 3, 31)); - QCOMPARE(inGap.time(), QTime(2, 0)); - // - Test transition hole, setting 02:59:59.999 is invalid + QCOMPARE(inGap.time(), QTime(3, 0)); + QCOMPARE(inGap.offsetFromUtc(), 7200); + // - Test transition hole, 02:59:59.999 was skipped: inGap = QDateTime(QDate(2013, 3, 31), QTime(2, 59, 59, 999), cet); - QVERIFY(!inGap.isValid()); + QVERIFY(inGap.isValid()); QCOMPARE(inGap.date(), QDate(2013, 3, 31)); - QCOMPARE(inGap.time(), QTime(2, 59, 59, 999)); + QCOMPARE(inGap.time(), QTime(3, 59, 59, 999)); + QCOMPARE(inGap.offsetFromUtc(), 7200); + // Test similar for local time, if it's CET: + if (zoneIsCET) { + inGap = QDateTime(QDate(2013, 3, 31), QTime(2, 30)); + QVERIFY(inGap.isValid()); + QCOMPARE(inGap.date(), QDate(2013, 3, 31)); + QCOMPARE(inGap.offsetFromUtc(), 7200); + QCOMPARE(inGap.time(), QTime(3, 30)); + } + + // Test a gap more than 1'141'707.91-years from 1970, outside ShortData's range, + // The zone version is non-short in any case, but check it anyway. + // However, we can only test this if the underlying OS believes CET continues + // exercising DST indefinitely; Darwin, for example, assumes we'll have all + // kicked the habit by the end of 2100. + constexpr int longYear = 1'143'678; + constexpr qint64 millisInWeek = qint64(7) * 24 * 60 * 60 * 1000; + if (QDateTime(QDate(longYear, 3, 24), QTime(12, 0), cet).msecsTo( + QDateTime(QDate(longYear, 3, 31), QTime(12, 0), cet)) < millisInWeek) { + inGap = QDateTime(QDate(longYear, 3, 27), QTime(2, 30), cet); + QVERIFY(inGap.isValid()); + QCOMPARE(inGap.date(), QDate(longYear, 3, 27)); + QCOMPARE(inGap.time(), QTime(3, 30)); + QCOMPARE(inGap.offsetFromUtc(), 7200); + } else { + qDebug("Skipping far-future check beyond zoned end of DST"); + } + if (zoneIsCET && QDateTime(QDate(longYear, 3, 24), QTime(12, 0)).msecsTo( + QDateTime(QDate(longYear, 3, 31), QTime(12, 0))) < millisInWeek) { + inGap = QDateTime(QDate(longYear, 3, 27), QTime(2, 30)); + QVERIFY(inGap.isValid()); + QCOMPARE(inGap.date(), QDate(longYear, 3, 27)); + QCOMPARE(inGap.offsetFromUtc(), 7200); + QCOMPARE(inGap.time(), QTime(3, 30)); + } else { + qDebug(zoneIsCET ? "Skipping far-future check beyond local end of DST" + : "Skipping CET-specific test"); + } // Standard Time to Daylight Time 2013 on 2013-10-27 is 3:00 local time / 1:00 UTC const qint64 replayMSecs = 1382835600000; diff --git a/tests/auto/corelib/time/qdatetime/tst_qdatetime_mac.mm b/tests/auto/corelib/time/qdatetime/tst_qdatetime_mac.mm index ea8051f22b..08379ccb41 100644 --- a/tests/auto/corelib/time/qdatetime/tst_qdatetime_mac.mm +++ b/tests/auto/corelib/time/qdatetime/tst_qdatetime_mac.mm @@ -1,6 +1,6 @@ // Copyright (C) 2020 The Qt Company Ltd. // Copyright (C) 2014 Petroules Corporation. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QtCore/QDateTime> #include <QTest> diff --git a/tests/auto/corelib/time/qdatetimeparser/CMakeLists.txt b/tests/auto/corelib/time/qdatetimeparser/CMakeLists.txt index 392c8d527f..6bdd66ebfc 100644 --- a/tests/auto/corelib/time/qdatetimeparser/CMakeLists.txt +++ b/tests/auto/corelib/time/qdatetimeparser/CMakeLists.txt @@ -5,6 +5,12 @@ ## tst_qdatetimeparser Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qdatetimeparser LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qdatetimeparser SOURCES tst_qdatetimeparser.cpp diff --git a/tests/auto/corelib/time/qdatetimeparser/tst_qdatetimeparser.cpp b/tests/auto/corelib/time/qdatetimeparser/tst_qdatetimeparser.cpp index 0b29ffad73..bfc811eebe 100644 --- a/tests/auto/corelib/time/qdatetimeparser/tst_qdatetimeparser.cpp +++ b/tests/auto/corelib/time/qdatetimeparser/tst_qdatetimeparser.cpp @@ -1,9 +1,11 @@ // Copyright (C) 2020 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> #include <private/qdatetimeparser_p.h> +using namespace Qt::StringLiterals; + QT_BEGIN_NAMESPACE // access to needed members in QDateTimeParser @@ -46,6 +48,7 @@ class tst_QDateTimeParser : public QObject Q_OBJECT private Q_SLOTS: + void reparse(); void parseSection_data(); void parseSection(); @@ -53,6 +56,61 @@ private Q_SLOTS: void intermediateYear(); }; +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 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 == "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'; })) + return Qt::UTC; + if (std::all_of(name.begin() + 1, name.end(), [](QChar ch) { return ch.isDigit(); })) + return Qt::OffsetFromUTC; + // Potential hh:mm offset ? Not yet seen as local tzname[] entry. + } + return Qt::LocalTime; + }); + + const QStringView format = u"dd/MM/yyyy HH:mm t"; + QDateTimeParser who(QMetaType::QDateTime, QDateTimeParser::DateTimeEdit); + QVERIFY(who.parseFormat(format)); + { + // 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() { QTest::addColumn<QString>("format"); @@ -140,10 +198,12 @@ void tst_QDateTimeParser::intermediateYear() QVERIFY(testParser.parseFormat(format)); + // Indian/Cocos has a transition at the start of 1900, so it started this + // day at 00:02:20, throwing a time offset into QDTP. QDateTime val(QDate(1900, 1, 1).startOfDay()); const QDateTimeParser::StateNode tmp = testParser.parse(input, -1, val, false); QCOMPARE(tmp.state, QDateTimeParser::Intermediate); - QCOMPARE(tmp.value, expected.startOfDay()); + QCOMPARE(tmp.value.date(), expected); } QTEST_APPLESS_MAIN(tst_QDateTimeParser) diff --git a/tests/auto/corelib/time/qtime/CMakeLists.txt b/tests/auto/corelib/time/qtime/CMakeLists.txt index 5aafa19723..6fe2968107 100644 --- a/tests/auto/corelib/time/qtime/CMakeLists.txt +++ b/tests/auto/corelib/time/qtime/CMakeLists.txt @@ -5,6 +5,12 @@ ## tst_qtime Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qtime LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qtime SOURCES tst_qtime.cpp @@ -13,4 +19,5 @@ qt_internal_add_test(tst_qtime QT_NO_KEYWORDS LIBRARIES Qt::CorePrivate + Qt::TestPrivate ) diff --git a/tests/auto/corelib/time/qtime/tst_qtime.cpp b/tests/auto/corelib/time/qtime/tst_qtime.cpp index 74631229d1..c0fdb07115 100644 --- a/tests/auto/corelib/time/qtime/tst_qtime.cpp +++ b/tests/auto/corelib/time/qtime/tst_qtime.cpp @@ -1,7 +1,8 @@ // Copyright (C) 2020 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <private/qglobal_p.h> +#include <private/qcomparisontesthelper_p.h> #include <QTest> #include "qdatetime.h" #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) @@ -27,12 +28,11 @@ private Q_SLOTS: void addMSecs(); void addSecs_data(); void addSecs(); + void orderingCompiles(); void operator_eq_eq_data(); void operator_eq_eq(); - void operator_lt(); - void operator_gt(); - void operator_lt_eq(); - void operator_gt_eq(); + void ordering_data(); + void ordering(); #if QT_CONFIG(datestring) # if QT_CONFIG(datetimeparser) void fromStringFormat_data(); @@ -320,6 +320,11 @@ void tst_QTime::msecsTo() QCOMPARE( t1.msecsTo( t2 ), delta ); } +void tst_QTime::orderingCompiles() +{ + QTestPrivate::testAllComparisonOperatorsCompile<QTime>(); +} + void tst_QTime::operator_eq_eq_data() { QTest::addColumn<QTime>("t1"); @@ -345,169 +350,42 @@ void tst_QTime::operator_eq_eq() QFETCH(QTime, t2); QFETCH(bool, expectEqual); - bool equal = t1 == t2; - QCOMPARE(equal, expectEqual); - bool notEqual = t1 != t2; - QCOMPARE(notEqual, !expectEqual); + QT_TEST_EQUALITY_OPS(t1, t2, expectEqual); - if (equal) + if (expectEqual) QVERIFY(qHash(t1) == qHash(t2)); } -void tst_QTime::operator_lt() -{ - QTime t1(0,0,0,0); - QTime t2(0,0,0,0); - QVERIFY( !(t1 < t2) ); - - t1 = QTime(12,34,56,20); - t2 = QTime(12,34,56,30); - QVERIFY( t1 < t2 ); - - t1 = QTime(13,34,46,20); - t2 = QTime(13,34,56,20); - QVERIFY( t1 < t2 ); - - t1 = QTime(13,24,56,20); - t2 = QTime(13,34,56,20); - QVERIFY( t1 < t2 ); - - t1 = QTime(12,34,56,20); - t2 = QTime(13,34,56,20); - QVERIFY( t1 < t2 ); - - t1 = QTime(14,34,56,20); - t2 = QTime(13,34,56,20); - QVERIFY( !(t1 < t2) ); - - t1 = QTime(13,44,56,20); - t2 = QTime(13,34,56,20); - QVERIFY( !(t1 < t2) ); - - t1 = QTime(13,34,56,20); - t2 = QTime(13,34,46,20); - QVERIFY( !(t1 < t2) ); - - t1 = QTime(13,44,56,30); - t2 = QTime(13,44,56,20); - QVERIFY( !(t1 < t2) ); -} - -void tst_QTime::operator_gt() -{ - QTime t1(0,0,0,0); - QTime t2(0,0,0,0); - QVERIFY( !(t1 > t2) ); - - t1 = QTime(12,34,56,20); - t2 = QTime(12,34,56,30); - QVERIFY( !(t1 > t2) ); - - t1 = QTime(13,34,46,20); - t2 = QTime(13,34,56,20); - QVERIFY( !(t1 > t2) ); - - t1 = QTime(13,24,56,20); - t2 = QTime(13,34,56,20); - QVERIFY( !(t1 > t2) ); - - t1 = QTime(12,34,56,20); - t2 = QTime(13,34,56,20); - QVERIFY( !(t1 > t2) ); - - t1 = QTime(14,34,56,20); - t2 = QTime(13,34,56,20); - QVERIFY( t1 > t2 ); - - t1 = QTime(13,44,56,20); - t2 = QTime(13,34,56,20); - QVERIFY( t1 > t2 ); - - t1 = QTime(13,34,56,20); - t2 = QTime(13,34,46,20); - QVERIFY( t1 > t2 ); - - t1 = QTime(13,44,56,30); - t2 = QTime(13,44,56,20); - QVERIFY( t1 > t2 ); -} - -void tst_QTime::operator_lt_eq() +void tst_QTime::ordering_data() { - QTime t1(0,0,0,0); - QTime t2(0,0,0,0); - QVERIFY( t1 <= t2 ); - - t1 = QTime(12,34,56,20); - t2 = QTime(12,34,56,30); - QVERIFY( t1 <= t2 ); - - t1 = QTime(13,34,46,20); - t2 = QTime(13,34,56,20); - QVERIFY( t1 <= t2 ); - - t1 = QTime(13,24,56,20); - t2 = QTime(13,34,56,20); - QVERIFY( t1 <= t2 ); - - t1 = QTime(12,34,56,20); - t2 = QTime(13,34,56,20); - QVERIFY( t1 <= t2 ); - - t1 = QTime(14,34,56,20); - t2 = QTime(13,34,56,20); - QVERIFY( !(t1 <= t2) ); - - t1 = QTime(13,44,56,20); - t2 = QTime(13,34,56,20); - QVERIFY( !(t1 <= t2) ); - - t1 = QTime(13,34,56,20); - t2 = QTime(13,34,46,20); - QVERIFY( !(t1 <= t2) ); - - t1 = QTime(13,44,56,30); - t2 = QTime(13,44,56,20); - QVERIFY( !(t1 <= t2) ); + QTest::addColumn<QTime>("left"); + QTest::addColumn<QTime>("right"); + QTest::addColumn<Qt::strong_ordering>("expectedOrdering"); + + auto generateRow = [](QTime t1, QTime t2, Qt::strong_ordering ordering) { + const QByteArray t1Str = t1.toString("hh:mm:ss.zz").toLatin1(); + const QByteArray t2Str = t2.toString("hh:mm:ss.zz").toLatin1(); + QTest::addRow("%s_vs_%s", t1Str.constData(), t2Str.constData()) << t1 << t2 << ordering; + }; + + generateRow(QTime(0, 0), QTime(0, 0), Qt::strong_ordering::equivalent); + generateRow(QTime(12, 34, 56, 20), QTime(12, 34, 56, 30), Qt::strong_ordering::less); + generateRow(QTime(13, 34, 46, 20), QTime(13, 34, 56, 20), Qt::strong_ordering::less); + generateRow(QTime(13, 24, 56, 20), QTime(13, 34, 56, 20), Qt::strong_ordering::less); + generateRow(QTime(12, 34, 56, 20), QTime(13, 34, 56, 20), Qt::strong_ordering::less); + generateRow(QTime(14, 34, 56, 20), QTime(13, 34, 56, 20), Qt::strong_ordering::greater); + generateRow(QTime(13, 44, 56, 20), QTime(13, 34, 56, 20), Qt::strong_ordering::greater); + generateRow(QTime(13, 34, 56, 20), QTime(13, 34, 46, 20), Qt::strong_ordering::greater); + generateRow(QTime(13, 34, 56, 30), QTime(13, 34, 56, 20), Qt::strong_ordering::greater); } -void tst_QTime::operator_gt_eq() +void tst_QTime::ordering() { - QTime t1(0,0,0,0); - QTime t2(0,0,0,0); - QVERIFY( t1 >= t2 ); - - t1 = QTime(12,34,56,20); - t2 = QTime(12,34,56,30); - QVERIFY( !(t1 >= t2) ); - - t1 = QTime(13,34,46,20); - t2 = QTime(13,34,56,20); - QVERIFY( !(t1 >= t2) ); - - t1 = QTime(13,24,56,20); - t2 = QTime(13,34,56,20); - QVERIFY( !(t1 >= t2) ); - - t1 = QTime(12,34,56,20); - t2 = QTime(13,34,56,20); - QVERIFY( !(t1 >= t2) ); - - t1 = QTime(14,34,56,20); - t2 = QTime(13,34,56,20); - QVERIFY( t1 >= t2 ); - - t1 = QTime(13,44,56,20); - t2 = QTime(13,34,56,20); - QVERIFY( t1 >= t2 ); - - t1 = QTime(13,34,56,20); - t2 = QTime(13,34,46,20); - QVERIFY( t1 >= t2 ); + QFETCH(QTime, left); + QFETCH(QTime, right); + QFETCH(Qt::strong_ordering, expectedOrdering); - t1 = QTime(13,44,56,30); - t2 = QTime(13,44,56,20); - QVERIFY( t1 >= t2 ); + QT_TEST_ALL_COMPARISON_OPS(left, right, expectedOrdering); } #if QT_CONFIG(datestring) diff --git a/tests/auto/corelib/time/qtimezone/CMakeLists.txt b/tests/auto/corelib/time/qtimezone/CMakeLists.txt index 869220ecd2..612bab0db5 100644 --- a/tests/auto/corelib/time/qtimezone/CMakeLists.txt +++ b/tests/auto/corelib/time/qtimezone/CMakeLists.txt @@ -5,6 +5,12 @@ ## tst_qtimezone Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qtimezone LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qtimezone SOURCES tst_qtimezone.cpp @@ -13,6 +19,7 @@ qt_internal_add_test(tst_qtimezone QT_NO_KEYWORDS LIBRARIES Qt::CorePrivate + Qt::TestPrivate ) ## Scopes: diff --git a/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp b/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp index b63e506ad2..e71f32a3ca 100644 --- a/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp +++ b/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp @@ -1,11 +1,13 @@ // Copyright (C) 2022 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> #include <qtimezone.h> #include <private/qtimezoneprivate_p.h> +#include <private/qcomparisontesthelper_p.h> #include <qlocale.h> +#include <qscopeguard.h> #if defined(Q_OS_WIN) #include <QOperatingSystemVersion> @@ -15,6 +17,8 @@ # define USING_WIN_TZ #endif +using namespace Qt::StringLiterals; + class tst_QTimeZone : public QObject { Q_OBJECT @@ -24,6 +28,8 @@ private Q_SLOTS: void createTest(); void nullTest(); void assign(); + void compareCompiles(); + void compare_data(); void compare(); void timespec(); void offset(); @@ -35,6 +41,8 @@ private Q_SLOTS: void availableTimeZoneIds(); void utcOffsetId_data(); void utcOffsetId(); + void aliasMatches_data(); + void aliasMatches(); void specificTransition_data(); void specificTransition(); void transitionEachZone_data(); @@ -331,19 +339,38 @@ void tst_QTimeZone::assign() #endif } -void tst_QTimeZone::compare() +void tst_QTimeZone::compareCompiles() { + QTestPrivate::testEqualityOperatorsCompile<QTimeZone>(); +} + +void tst_QTimeZone::compare_data() +{ + QTest::addColumn<QTimeZone>("left"); + QTest::addColumn<QTimeZone>("right"); + QTest::addColumn<bool>("expectedEqual"); + const QTimeZone local; const QTimeZone utc(QTimeZone::UTC); const auto secondEast = QTimeZone::fromSecondsAheadOfUtc(1); + const auto zeroOffset = QTimeZone::fromSecondsAheadOfUtc(0); + const auto durationEast = QTimeZone::fromDurationAheadOfUtc(std::chrono::seconds{1}); + + QTest::newRow("local vs default-constructed") << local << QTimeZone() << true; + QTest::newRow("local vs UTC") << local << utc << false; + QTest::newRow("local vs secondEast") << local << secondEast << false; + QTest::newRow("secondEast vs UTC") << secondEast << utc << false; + QTest::newRow("UTC vs zeroOffset") << utc << zeroOffset << true; + QTest::newRow("secondEast vs durationEast") << secondEast << durationEast << true; +} - QCOMPARE_NE(local, utc); - QCOMPARE_NE(utc, secondEast); - QCOMPARE_NE(secondEast, local); +void tst_QTimeZone::compare() +{ + QFETCH(QTimeZone, left); + QFETCH(QTimeZone, right); + QFETCH(bool, expectedEqual); - QCOMPARE(local, QTimeZone()); - QCOMPARE(utc, QTimeZone::fromSecondsAheadOfUtc(0)); - QCOMPARE(secondEast, QTimeZone::fromDurationAheadOfUtc(std::chrono::seconds{1})); + QT_TEST_EQUALITY_OPS(left, right, expectedEqual); } void tst_QTimeZone::timespec() @@ -507,7 +534,9 @@ void tst_QTimeZone::asBackendZone() void tst_QTimeZone::systemZone() { const QTimeZone zone = QTimeZone::systemTimeZone(); - QVERIFY(zone.isValid()); + QVERIFY2(zone.isValid(), + "Invalid system zone setting, tests are doomed on misconfigured system."); + // This may fail on Windows if CLDR data doesn't map system MS ID to IANA ID: QCOMPARE(zone.id(), QTimeZone::systemTimeZoneId()); QCOMPARE(zone, QTimeZone(QTimeZone::systemTimeZoneId())); // Check it behaves the same as local-time: @@ -534,7 +563,18 @@ void tst_QTimeZone::isTimeZoneIdAvailable() const QList<QByteArray> available = QTimeZone::availableTimeZoneIds(); for (const QByteArray &id : available) { QVERIFY2(QTimeZone::isTimeZoneIdAvailable(id), id); + const QTimeZone zone(id); + QVERIFY2(zone.isValid(), id); + QVERIFY2(zone.aliasMatches(id), zone.id() + " != " + id); + } + // availableTimeZoneIds() doesn't list all possible offset IDs, but + // isTimeZoneIdAvailable() should accept them. + for (qint32 offset = QTimeZone::MinUtcOffsetSecs; + offset <= QTimeZone::MinUtcOffsetSecs; ++offset) { + const QByteArray id = QTimeZone(offset).id(); + QVERIFY2(QTimeZone::isTimeZoneIdAvailable(id), id); QVERIFY2(QTimeZone(id).isValid(), id); + QCOMPARE(QTimeZone(id).id(), id); } } @@ -552,7 +592,7 @@ void tst_QTimeZone::utcOffsetId_data() #define ROW(name, valid, offset) \ QTest::newRow(name) << QByteArray(name) << valid << offset - // See qtbase/util/locale_database/cldr2qtimezone.py for source + // See qtbase/util/locale_database/zonedata.py for source // CLDR v35.1 IDs: ROW("UTC", true, 0); ROW("UTC-14:00", true, -50400); @@ -599,7 +639,11 @@ void tst_QTimeZone::utcOffsetId_data() ROW("UTC-11", true, -39600); ROW("UTC-09", true, -32400); ROW("UTC-08", true, -28800); + ROW("UTC-8", true, -28800); + ROW("UTC-2:5", true, -7500); ROW("UTC-02", true, -7200); + ROW("UTC+2", true, 7200); + ROW("UTC+2:5", true, 7500); ROW("UTC+12", true, 43200); ROW("UTC+13", true, 46800); // Encountered in bug reports: @@ -647,10 +691,75 @@ void tst_QTimeZone::utcOffsetId() QFETCH(int, offset); QCOMPARE(zone.offsetFromUtc(epoch), offset); QVERIFY(!zone.hasDaylightTime()); + + // zone.id() will be an IANA ID with zero minutes field if original was + // a UTC offset by a whole number of hours. It will also zero-pad a + // single-digit hour or minute to two digits. + if (const qsizetype cut = id.indexOf(':'); cut >= 0) { + if (id.size() == cut + 2) // "...:m" -> "...:0m" + id.insert(cut + 1, '0'); + } else if (zone.id().contains(':')) { + id += ":00"; + } + if (id.indexOf(':') == 5) // UTC±h:mm -> UTC±0h:mm + id.insert(4, '0'); + QCOMPARE(zone.id(), id); } } +void tst_QTimeZone::aliasMatches_data() +{ + QTest::addColumn<QByteArray>("iana"); + QTest::addColumn<QByteArray>("alias"); + + QTest::newRow("Montreal=Toronto") << "America/Toronto"_ba << "America/Montreal"_ba; + QTest::newRow("Asmera=Asmara") << "Africa/Asmara"_ba << "Africa/Asmera"_ba; + QTest::newRow("Argentina/Catamarca") + << "America/Argentina/Catamarca"_ba << "America/Catamarca"_ba; + QTest::newRow("Godthab=Nuuk") << "America/Nuuk"_ba << "America/Godthab"_ba; + QTest::newRow("Indiana/Indianapolis") + << "America/Indiana/Indianapolis"_ba << "America/Indianapolis"_ba; + QTest::newRow("Kentucky/Louisville") + << "America/Kentucky/Louisville"_ba << "America/Louisville"_ba; + QTest::newRow("Calcutta=Kolkata") << "Asia/Kolkata"_ba << "Asia/Calcutta"_ba; + QTest::newRow("Katmandu=Kathmandu") << "Asia/Kathmandu"_ba << "Asia/Katmandu"_ba; + QTest::newRow("Rangoon=Yangon") << "Asia/Yangon"_ba << "Asia/Rangoon"_ba; + QTest::newRow("Saigon=Ho_Chi_Minh") << "Asia/Ho_Chi_Minh"_ba << "Asia/Saigon"_ba; + QTest::newRow("Faeroe=Faroe") << "Atlantic/Faroe"_ba << "Atlantic/Faeroe"_ba; + QTest::newRow("Currie=Hobart") << "Australia/Hobart"_ba << "Australia/Currie"_ba; + QTest::newRow("Kiev=Kyiv") << "Europe/Kyiv"_ba << "Europe/Kiev"_ba; + QTest::newRow("Uzhgorod=Kyiv") << "Europe/Kyiv"_ba << "Europe/Uzhgorod"_ba; + QTest::newRow("Zaporozhye=Kyiv") << "Europe/Kyiv"_ba << "Europe/Zaporozhye"_ba; + QTest::newRow("Fiji=Fiji") << "Pacific/Fiji"_ba << "Pacific/Fiji"_ba; + QTest::newRow("Enderbury=Enderbury") << "Pacific/Enderbury"_ba << "Pacific/Enderbury"_ba; +} + +void tst_QTimeZone::aliasMatches() +{ + QFETCH(const QByteArray, iana); + QFETCH(const QByteArray, alias); + const QTimeZone zone(iana); + const QTimeZone peer(alias); + if (!zone.isValid()) + QSKIP("Backend doesn't support IANA ID"); + + auto report = qScopeGuard([zone, peer]() { + const QByteArray zid = zone.id(), pid = peer.id(); + qDebug("Using %s and %s", zid.constData(), pid.constData()); + }); + QVERIFY2(peer.isValid(), "Construction should have fallen back on IANA ID"); + QVERIFY(zone.aliasMatches(zone.id())); + QVERIFY(zone.aliasMatches(iana)); + QVERIFY(peer.aliasMatches(peer.id())); + QVERIFY(peer.aliasMatches(alias)); + QVERIFY(zone.aliasMatches(peer.id())); + QVERIFY(zone.aliasMatches(alias)); + QVERIFY(peer.aliasMatches(zone.id())); + QVERIFY(peer.aliasMatches(iana)); + report.dismiss(); +} + void tst_QTimeZone::specificTransition_data() { QTest::addColumn<QByteArray>("zone"); @@ -777,17 +886,29 @@ void tst_QTimeZone::transitionEachZone() void tst_QTimeZone::checkOffset_data() { - QTest::addColumn<QByteArray>("zoneName"); + QTest::addColumn<QTimeZone>("zone"); QTest::addColumn<QDateTime>("when"); QTest::addColumn<int>("netOffset"); QTest::addColumn<int>("stdOffset"); QTest::addColumn<int>("dstOffset"); + const QTimeZone UTC = QTimeZone::UTC; + QTest::addRow("UTC") + << UTC << QDate(1970, 1, 1).startOfDay(UTC) << 0 << 0 << 0; + const auto east = QTimeZone::fromSecondsAheadOfUtc(28'800); // 8 hours + QTest::addRow("UTC+8") + << east << QDate(2000, 2, 29).startOfDay(east) << 28'800 << 28'800 << 0; + const auto west = QTimeZone::fromDurationAheadOfUtc(std::chrono::hours{-8}); + QTest::addRow("UTC-8") + << west << QDate(2100, 2, 28).startOfDay(west) << -28'800 << -28'800 << 0; + struct { const char *zone, *nick; int year, month, day, hour, min, sec; int std, dst; } table[] = { + // Exercise the UTC-backend: + { "UTC", "epoch", 1970, 1, 1, 0, 0, 0, 0, 0 }, // Zone with no transitions (QTBUG-74614, QTBUG-74666, when TZ backend uses minimal data) { "Etc/UTC", "epoch", 1970, 1, 1, 0, 0, 0, 0, 0 }, { "Etc/UTC", "pre_int32", 1901, 12, 13, 20, 45, 51, 0, 0 }, @@ -795,42 +916,44 @@ void tst_QTimeZone::checkOffset_data() { "Etc/UTC", "post_uint32", 2106, 2, 7, 6, 28, 17, 0, 0 }, { "Etc/UTC", "initial", -292275056, 5, 16, 16, 47, 5, 0, 0 }, { "Etc/UTC", "final", 292278994, 8, 17, 7, 12, 55, 0, 0 }, - // Kiev: regression test for QTBUG-64122 (on MS): - { "Europe/Kiev", "summer", 2017, 10, 27, 12, 0, 0, 2 * 3600, 3600 }, - { "Europe/Kiev", "winter", 2017, 10, 29, 12, 0, 0, 2 * 3600, 0 } + // Kyiv: regression test for QTBUG-64122 (on MS): + { "Europe/Kyiv", "summer", 2017, 10, 27, 12, 0, 0, 2 * 3600, 3600 }, + { "Europe/Kyiv", "winter", 2017, 10, 29, 12, 0, 0, 2 * 3600, 0 } }; - bool lacksRows = true; for (const auto &entry : table) { QTimeZone zone(entry.zone); if (zone.isValid()) { QTest::addRow("%s@%s", entry.zone, entry.nick) - << QByteArray(entry.zone) + << zone << QDateTime(QDate(entry.year, entry.month, entry.day), QTime(entry.hour, entry.min, entry.sec), zone) << entry.dst + entry.std << entry.std << entry.dst; - lacksRows = false; } else { qWarning("Skipping %s@%s test as zone is invalid", entry.zone, entry.nick); } } - if (lacksRows) - QSKIP("No valid zone info found, skipping test"); } void tst_QTimeZone::checkOffset() { - QFETCH(QByteArray, zoneName); + QFETCH(QTimeZone, zone); QFETCH(QDateTime, when); QFETCH(int, netOffset); QFETCH(int, stdOffset); QFETCH(int, dstOffset); - QTimeZone zone(zoneName); QVERIFY(zone.isValid()); // It was when _data() added the row ! QCOMPARE(zone.offsetFromUtc(when), netOffset); QCOMPARE(zone.standardTimeOffset(when), stdOffset); QCOMPARE(zone.daylightTimeOffset(when), dstOffset); QCOMPARE(zone.isDaylightTime(when), dstOffset != 0); + + // Also test offsetData(), which gets all this data in one go: + const auto data = zone.offsetData(when); + QCOMPARE(data.atUtc, when); + QCOMPARE(data.offsetFromUtc, netOffset); + QCOMPARE(data.standardTimeOffset, stdOffset); + QCOMPARE(data.daylightTimeOffset, dstOffset); } void tst_QTimeZone::availableTimeZoneIds() @@ -861,7 +984,7 @@ void tst_QTimeZone::stressTest() for (const QByteArray &id : idList) { QTimeZone testZone = QTimeZone(id); QCOMPARE(testZone.isValid(), true); - QCOMPARE(testZone.id(), id); + QVERIFY2(testZone.aliasMatches(id), testZone.id() + " != " + id); QDateTime testDate = QDateTime(QDate(2015, 1, 1), QTime(0, 0), UTC); testZone.territory(); testZone.comment(); @@ -909,7 +1032,7 @@ void tst_QTimeZone::windowsId() Current Windows zones for "Central Standard Time": Region IANA Id(s) Default "America/Chicago" - Canada "America/Winnipeg America/Rainy_River America/Rankin_Inlet America/Resolute" + Canada "America/Winnipeg America/Rankin_Inlet America/Resolute" Mexico "America/Matamoros" USA "America/Chicago America/Indiana/Knox America/Indiana/Tell_City America/Menominee" "America/North_Dakota/Beulah America/North_Dakota/Center" @@ -935,46 +1058,54 @@ void tst_QTimeZone::windowsId() QByteArray("CST6CDT")); QCOMPARE(QTimeZone::windowsIdToDefaultIanaId(QByteArray()), QByteArray()); - // No country is sorted list of all zones - QList<QByteArray> list; - list << "America/Chicago" << "America/Indiana/Knox" << "America/Indiana/Tell_City" - << "America/Matamoros" << "America/Menominee" << "America/North_Dakota/Beulah" - << "America/North_Dakota/Center" << "America/North_Dakota/New_Salem" - << "America/Rainy_River" << "America/Rankin_Inlet" << "America/Resolute" - << "America/Winnipeg" << "CST6CDT"; - QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time"), list); - - // Check country with no match returns empty list - list.clear(); - QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time", QLocale::NewZealand), - list); - - // Check valid country returns list in preference order - list.clear(); - list << "America/Winnipeg" << "America/Rainy_River" << "America/Rankin_Inlet" - << "America/Resolute"; - QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time", QLocale::Canada), list); - - list.clear(); - list << "America/Matamoros"; - QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time", QLocale::Mexico), list); - - list.clear(); - list << "America/Chicago" << "America/Indiana/Knox" << "America/Indiana/Tell_City" - << "America/Menominee" << "America/North_Dakota/Beulah" << "America/North_Dakota/Center" - << "America/North_Dakota/New_Salem"; - QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time", QLocale::UnitedStates), - list); - - list.clear(); - list << "CST6CDT"; - QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time", QLocale::AnyTerritory), - list); - - // Check no windowsId return empty - list.clear(); - QCOMPARE(QTimeZone::windowsIdToIanaIds(QByteArray()), list); - QCOMPARE(QTimeZone::windowsIdToIanaIds(QByteArray(), QLocale::AnyTerritory), list); + { + // With no country, expect sorted list of all zones for ID + const QList<QByteArray> list = { + "America/Chicago", "America/Indiana/Knox", "America/Indiana/Tell_City", + "America/Matamoros", "America/Menominee", "America/North_Dakota/Beulah", + "America/North_Dakota/Center", "America/North_Dakota/New_Salem", + "America/Ojinaga", "America/Rankin_Inlet", "America/Resolute", + "America/Winnipeg", "CST6CDT" + }; + QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time"), list); + } + { + // Check country with no match returns empty list + const QList<QByteArray> empty; + QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time", QLocale::NewZealand), + empty); + } + { + // Check valid country returns list in preference order + const QList<QByteArray> list = { + "America/Winnipeg", "America/Rankin_Inlet", "America/Resolute" + }; + QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time", QLocale::Canada), list); + } + { + const QList<QByteArray> list = { "America/Matamoros", "America/Ojinaga" }; + QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time", QLocale::Mexico), list); + } + { + const QList<QByteArray> list = { + "America/Chicago", "America/Indiana/Knox", "America/Indiana/Tell_City", + "America/Menominee", "America/North_Dakota/Beulah", "America/North_Dakota/Center", + "America/North_Dakota/New_Salem" + }; + QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time", QLocale::UnitedStates), + list); + } + { + const QList<QByteArray> list = { "CST6CDT" }; + QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time", QLocale::AnyTerritory), + list); + } + { + // Check empty if given no windowsId: + const QList<QByteArray> empty; + QCOMPARE(QTimeZone::windowsIdToIanaIds(QByteArray()), empty); + QCOMPARE(QTimeZone::windowsIdToIanaIds(QByteArray(), QLocale::AnyTerritory), empty); + } } void tst_QTimeZone::isValidId_data() @@ -1153,18 +1284,25 @@ void tst_QTimeZone::utcTest() QCOMPARE(tzp.hasDaylightTime(), false); QCOMPARE(tzp.hasTransitions(), false); - // Test create from UTC Offset (uses minimal id, skipping minutes if 0) + // Test create from UTC Offset: QDateTime now = QDateTime::currentDateTime(); QTimeZone tz(36000); QVERIFY(tz.isValid()); - QCOMPARE(tz.id(), QByteArray("UTC+10")); + QCOMPARE(tz.id(), QByteArray("UTC+10:00")); QCOMPARE(tz.offsetFromUtc(now), 36000); QCOMPARE(tz.standardTimeOffset(now), 36000); QCOMPARE(tz.daylightTimeOffset(now), 0); - // Test invalid UTC offset, must be in range -14 to +14 hours - int min = -14*60*60; - int max = 14*60*60; + tz = QTimeZone(15 * 3600); // no IANA ID, so uses minimal id, skipping :00 minutes + QVERIFY(tz.isValid()); + QCOMPARE(tz.id(), QByteArray("UTC+15")); + QCOMPARE(tz.offsetFromUtc(now), 15 * 3600); + QCOMPARE(tz.standardTimeOffset(now), 15 * 3600); + QCOMPARE(tz.daylightTimeOffset(now), 0); + + // Test validity range of UTC offsets: + int min = QTimeZone::MinUtcOffsetSecs; + int max = QTimeZone::MaxUtcOffsetSecs; QCOMPARE(QTimeZone(min - 1).isValid(), false); QCOMPARE(QTimeZone(min).isValid(), true); QCOMPARE(QTimeZone(min + 1).isValid(), true); @@ -1202,7 +1340,7 @@ void tst_QTimeZone::utcTest() void tst_QTimeZone::icuTest() { -#if defined(QT_BUILD_INTERNAL) && QT_CONFIG(icu) +#if defined(QT_BUILD_INTERNAL) && QT_CONFIG(icu) && !defined(Q_OS_UNIX) // Known datetimes qint64 std = QDateTime(QDate(2012, 1, 1), QTime(0, 0), QTimeZone::UTC).toMSecsSinceEpoch(); qint64 dst = QDateTime(QDate(2012, 6, 1), QTime(0, 0), QTimeZone::UTC).toMSecsSinceEpoch(); @@ -1211,7 +1349,9 @@ void tst_QTimeZone::icuTest() QIcuTimeZonePrivate tzpd; QVERIFY(tzpd.isValid()); - // Test invalid constructor + // Test invalid is not available: + QVERIFY(!tzpd.isTimeZoneIdAvailable("Gondwana/Erewhon")); + // and construction gives an invalid result: QIcuTimeZonePrivate tzpi("Gondwana/Erewhon"); QCOMPARE(tzpi.isValid(), false); @@ -1243,7 +1383,7 @@ void tst_QTimeZone::icuTest() if (QTest::currentTestFailed()) return; testEpochTranPrivate(QIcuTimeZonePrivate("America/Toronto")); -#endif // icu +#endif // ICU not on Unix } void tst_QTimeZone::tzTest() @@ -1260,7 +1400,7 @@ void tst_QTimeZone::tzTest() // Test invalid constructor QTzTimeZonePrivate tzpi("Gondwana/Erewhon"); - QCOMPARE(tzpi.isValid(), false); + QVERIFY(!tzpi.isValid()); // Test named constructor QTzTimeZonePrivate tzp("Europe/Berlin"); @@ -1427,9 +1567,12 @@ void tst_QTimeZone::tzTest() QCOMPARE(datatz1.offsetFromUtc, datautc1.offsetFromUtc); // Test TZ timezone vs UTC timezone for non-whole-hour positive offset: - QTzTimeZonePrivate tztz2("Asia/Calcutta"); + QTzTimeZonePrivate tztz2k("Asia/Kolkata"); // New name + QTzTimeZonePrivate tztz2c("Asia/Calcutta"); // Legacy name + // Can't assign QtzTZP, so use a reference; prefer new name. + QTzTimeZonePrivate &tztz2 = tztz2k.isValid() ? tztz2k : tztz2c; QUtcTimeZonePrivate tzutc2("UTC+05:30"); - QVERIFY(tztz2.isValid()); + QVERIFY2(tztz2.isValid(), tztz2.id().constData()); QVERIFY(tzutc2.isValid()); QTzTimeZonePrivate::Data datatz2 = tztz2.data(std); QTzTimeZonePrivate::Data datautc2 = tzutc2.data(std); @@ -1444,7 +1587,7 @@ void tst_QTimeZone::tzTest() QDateTime dt(QDate(2016, 3, 28), QTime(0, 0), UTC); QCOMPARE(tzBarnaul.data(dt.toMSecsSinceEpoch()).abbreviation, QString("+07")); } -#endif // QT_BUILD_INTERNAL && Q_OS_UNIX && !Q_OS_DARWIN +#endif // QT_BUILD_INTERNAL && Q_OS_UNIX && !Q_OS_DARWIN && !Q_OS_ANDROID } void tst_QTimeZone::macTest() @@ -1602,6 +1745,8 @@ void tst_QTimeZone::localeSpecificDisplayName() QVERIFY(zone.isValid()); const QString localeName = zone.displayName(timeType, QTimeZone::LongName, locale); + if (localeName.isEmpty()) // Backend doesn't know how to localize this zone's name + QEXPECT_FAIL("", "QTBUG-115158 zone name localization unknown", Continue); QCOMPARE(localeName, expectedName); } @@ -1774,18 +1919,10 @@ void tst_QTimeZone::stdCompatibility() QFETCH(const std::chrono::time_zone *, timeZone); QByteArrayView zoneName = QByteArrayView(timeZone->name()); QTimeZone tz = QTimeZone::fromStdTimeZonePtr(timeZone); - if (tz.isValid()) { - QCOMPARE(tz.id(), zoneName); - } else { - // QTBUG-102187: a few timezones reported by tzdb might not be - // recognized by QTimeZone. This happens for instance on Windows, where - // tzdb is using ICU, whose database does not match QTimeZone's. - const bool isKnownUnknown = - !zoneName.contains('/') - || zoneName == "Antarctica/Troll" - || zoneName.startsWith("SystemV/"); - QVERIFY(isKnownUnknown); - } + if (tz.isValid()) + QVERIFY2(tz.aliasMatches(zoneName), tz.id().constData()); + else + QVERIFY(!QTimeZone::isTimeZoneIdAvailable(zoneName.toByteArray())); #else QSKIP("This test requires C++20's <chrono>."); #endif diff --git a/tests/auto/corelib/time/qtimezone/tst_qtimezone_darwin.mm b/tests/auto/corelib/time/qtimezone/tst_qtimezone_darwin.mm index 0bdd00edc9..f4ef15036d 100644 --- a/tests/auto/corelib/time/qtimezone/tst_qtimezone_darwin.mm +++ b/tests/auto/corelib/time/qtimezone/tst_qtimezone_darwin.mm @@ -1,5 +1,5 @@ // Copyright (C) 2017 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QtCore/QTimeZone> #include <QTest> @@ -9,7 +9,7 @@ void tst_QTimeZone_darwinTypes() { -#if !defined(QT_NO_SYSTEMLOCALE) +#if QT_CONFIG(timezone) // QTimeZone <-> CFTimeZone { QTimeZone qtTimeZone("America/Los_Angeles"); @@ -39,5 +39,5 @@ void tst_QTimeZone_darwinTypes() QVERIFY([qtTimeZone.toNSTimeZone() isEqual:nsTimeZone]); [autoreleasepool release]; } -#endif +#endif // feature timezone } |