From acb9db32b20344c74109ecbe5b4da493c9d7afc3 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 4 Nov 2015 12:20:34 +0100 Subject: Add spring-forward test Test that stepping into the missing hour lands us somewhere sane. Check that raw instance and product of .toLocalTime() agree. Task-number: QTBUG-49008 Change-Id: I430382ae223bcb43b151d2d6054ecbdd7edc8a47 Reviewed-by: Thiago Macieira --- .../auto/corelib/tools/qdatetime/tst_qdatetime.cpp | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'tests/auto/corelib') diff --git a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp index 6cf73c85de..833dfe87d7 100644 --- a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp @@ -69,6 +69,8 @@ private slots: void toTime_t_data(); void toTime_t(); void daylightSavingsTimeChange(); + void springForward_data(); + void springForward(); void setDate(); void setTime_data(); void setTime(); @@ -1593,6 +1595,48 @@ void tst_QDateTime::daylightSavingsTimeChange() QCOMPARE(dt.time(), QTime(0, 0, 1)); } +void tst_QDateTime::springForward_data() +{ + QTest::addColumn("day"); // day of DST transition + QTest::addColumn("time"); // in the "missing hour" + QTest::addColumn("step"); // days to step; +ve from before, -ve from after + QTest::addColumn("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; +#if 0 // FIXME: fails + QTest::newRow("Europe from day after") << QDate(2015, 3, 29) << QTime(2, 30, 0) << -1 << 120; +#endif + // } 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() { QTest::addColumn("dt1"); -- cgit v1.2.3