diff options
Diffstat (limited to 'tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp')
-rw-r--r-- | tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp | 92 |
1 files changed, 73 insertions, 19 deletions
diff --git a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp index df9089057d..228ce73c6b 100644 --- a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp @@ -68,7 +68,10 @@ private slots: void timeSpec(); void toTime_t_data(); void toTime_t(); + void daylightSavingsTimeChange_data(); void daylightSavingsTimeChange(); + void springForward_data(); + void springForward(); void setDate(); void setTime_data(); void setTime(); @@ -188,10 +191,10 @@ tst_QDateTime::tst_QDateTime() void tst_QDateTime::initTestCase() { // Never construct a message like this in an i18n context... - const char *typemsg1, *typemsg2 = "and therefore not"; + const char *typemsg1 = "exactly"; + const char *typemsg2 = "and therefore not"; switch (localTimeType) { case LocalTimeIsUtc: - typemsg1 = "exactly"; break; case LocalTimeBehindUtc: typemsg1 = "behind"; @@ -1561,36 +1564,87 @@ void tst_QDateTime::toTime_t() } } +void tst_QDateTime::daylightSavingsTimeChange_data() +{ + QTest::addColumn<QDate>("inDST"); + QTest::addColumn<QDate>("outDST"); + QTest::newRow("Autumn") << QDate(2006, 8, 1) << QDate(2006, 12, 1); + QTest::newRow("Spring") << QDate(2006, 5, 1) << QDate(2006, 2, 1); +} + void tst_QDateTime::daylightSavingsTimeChange() { - // This is a regression test for an old bug where starting with a date in - // DST and then moving to a date outside it (or vice-versa) caused 1-hour - // jumps in time when addSecs() was called. + // This has grown from a regression test for an old bug where starting with + // a date in DST and then moving to a date outside it (or vice-versa) caused + // 1-hour jumps in time when addSecs() was called. // // The bug was caused by QDateTime knowing more than it lets show. // Internally, if it knows, QDateTime stores a flag indicating if the time is // DST or not. If it doesn't, it sets to "LocalUnknown". The problem happened // because some functions did not reset the flag when moving in or out of DST. - // WARNING: This test only works if there's a Daylight Savings Time change - // in the current locale between 2006-11-06 and 2006-10-16 - // This is true for Central European Time + // WARNING: This only tests anything if there's a Daylight Savings Time change + // in the current locale between inDST and outDST. + // This is true for Central European Time and may be elsewhere. + + QFETCH(QDate, inDST); + QFETCH(QDate, outDST); - if (!europeanTimeZone) - QSKIP("Not tested with timezone other than Central European (CET/CEST)"); + // First with simple construction + QDateTime dt = QDateTime(outDST, QTime(0, 0, 0), Qt::LocalTime); + int outDSTsecs = dt.toTime_t(); - QDateTime dt = QDateTime(QDate(2006, 11, 6), QTime(0, 0, 0), Qt::LocalTime); - dt.setDate(QDate(2006, 10, 16)); + dt.setDate(inDST); dt = dt.addSecs(1); - QCOMPARE(dt.date(), QDate(2006, 10, 16)); - QCOMPARE(dt.time(), QTime(0, 0, 1)); + QCOMPARE(dt, QDateTime(inDST, QTime(0, 0, 1))); // now using fromTime_t - dt = QDateTime::fromTime_t(1162767600); // 2006-11-06 00:00:00 +0100 - dt.setDate(QDate(2006, 10, 16)); - dt = dt.addSecs (1); - QCOMPARE(dt.date(), QDate(2006, 10, 16)); - QCOMPARE(dt.time(), QTime(0, 0, 1)); + dt = QDateTime::fromTime_t(outDSTsecs); + QCOMPARE(dt, QDateTime(outDST, QTime(0, 0, 0))); + + dt.setDate(inDST); + dt = dt.addSecs(60); + QCOMPARE(dt, QDateTime(inDST, QTime(0, 1, 0))); +} + +void tst_QDateTime::springForward_data() +{ + QTest::addColumn<QDate>("day"); // day of DST transition + QTest::addColumn<QTime>("time"); // in the "missing hour" + QTest::addColumn<int>("step"); // days to step; +ve from before, -ve from after + QTest::addColumn<int>("adjust"); // minutes ahead of UTC on day stepped from + + if (europeanTimeZone) { + QTest::newRow("Europe from day before") << QDate(2015, 3, 29) << QTime(2, 30, 0) << 1 << 60; + QTest::newRow("Europe from day after") << QDate(2015, 3, 29) << QTime(2, 30, 0) << -1 << 120; + // } else if (otherZone) { + } else { + QSKIP("No spring forward test data for this TZ"); + } +} + +void tst_QDateTime::springForward() +{ + QFETCH(QDate, day); + QFETCH(QTime, time); + QFETCH(int, step); + QFETCH(int, adjust); + + QDateTime direct = QDateTime(day.addDays(-step), time, Qt::LocalTime).addDays(step); + 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 .toLocalTime(): + QDateTime detour = QDateTime(day.addDays(-step), + time.addSecs(-60 * adjust), + Qt::UTC).toLocalTime(); + QCOMPARE(detour.time(), time); + detour = detour.addDays(step); + QCOMPARE(detour, direct); // Insist on consistency. } void tst_QDateTime::operator_eqeq_data() |