summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2021-06-03 16:10:53 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2021-06-11 17:12:12 +0200
commit063b4212510eed1ed3e8f81ffe381007b6f4e932 (patch)
tree1782250d83e9751651cf957ef63c74cee140c002 /tests
parente069f06da70e4dfaa63a782a8634c5a6ec87a05e (diff)
Correct handling of last second of 1969 (again)
In my prior attempt to handle the last second of 1969, I forgot that the QTime we're describing is a local time, so whether *it* thinks we're at the last second of the day is beside the point. Fortunately, preceding second should get -2 as return if mktime()'s initial -1 actually meant the last second of 1969, so we can test via that, after a cheap pre-test to save doing this too often (albeit we only even attempt the check if mktime() returned -1 in any case). Restructured qt_mktime() in the process to deal with the error case's early return promptly instead of doing it in an else clause. Also repackage the calls to mktime to isolate various quirks and simplify the logic in qt_mktime(). This also prepares for setting tm_isdst as a hint when we know when we came from, in massageAdjustedDateTime(). Refined one test, added two more test cases. These didn't fail before this fix, but a judiciously-placed qDebug() in testing revealed that localMSecsToEpochMSecs() resorted to its fall-back handling - as if the date-time were outside the time_t range - due to qt_mktime() failing, for these test-cases (and several others). This fix evades that fall-back behavior; a judiciously-placed qDebug() shows none of our test-cases now fail callMkTime(). Change-Id: I11aa5015191dc4a565c28482307f7bc341c207e7 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp15
1 files changed, 12 insertions, 3 deletions
diff --git a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp
index 9a15acbcb8..d8fa3436b6 100644
--- a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp
+++ b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp
@@ -1133,10 +1133,13 @@ void tst_QDateTime::addDays()
QCOMPARE(dt2.timeSpec(), Qt::OffsetFromUTC);
QCOMPARE(dt2.offsetFromUtc(), 60 * 60);
- // test last second of 1969 *is* valid (despite being time_t(-1))
- dt1 = QDateTime(QDate(1970, 1, 1), QTime(23, 59, 59));
- dt2 = dt1.addDays(-1);
+ // Test last UTC second of 1969 *is* valid (despite being time_t(-1))
+ dt1 = QDateTime(QDate(1969, 12, 30), QTime(23, 59, 59), Qt::UTC).toLocalTime().addDays(1);
+ QVERIFY(dt1.isValid());
+ QCOMPARE(dt1.toSecsSinceEpoch(), -1);
+ dt2 = QDateTime(QDate(1970, 1, 1), QTime(23, 59, 59), Qt::UTC).toLocalTime().addDays(-1);
QVERIFY(dt2.isValid());
+ QCOMPARE(dt2.toSecsSinceEpoch(), -1);
}
void tst_QDateTime::addInvalid()
@@ -1397,6 +1400,9 @@ void tst_QDateTime::addMSecs_data()
QTest::newRow("epoch-1s-local")
<< QDateTime(QDate(1970, 1, 1), QTime(0, 0)) << qint64(-1)
<< QDateTime(QDate(1969, 12, 31), QTime(23, 59, 59));
+ QTest::newRow("epoch-1s-utc-as-local")
+ << QDate(1970, 1, 1).startOfDay(Qt::UTC).toLocalTime() << qint64(-1)
+ << QDateTime(QDate(1969, 12, 31), QTime(23, 59, 59), Qt::UTC).toLocalTime();
// Overflow and Underflow
const qint64 maxSeconds = std::numeric_limits<qint64>::max() / 1000;
@@ -1485,6 +1491,9 @@ void tst_QDateTime::toTimeSpec_data()
QTest::newRow("1969/12/31 23:00 UTC")
<< QDateTime(QDate(1969, 12, 31), QTime(23, 0), Qt::UTC)
<< QDateTime(QDate(1970, 1, 1), QTime(0, 0), Qt::LocalTime);
+ QTest::newRow("1969/12/31 23:59:59 UTC")
+ << QDateTime(QDate(1969, 12, 31), QTime(23, 59, 59), Qt::UTC)
+ << QDateTime(QDate(1970, 1, 1), QTime(0, 59, 59), Qt::LocalTime);
QTest::newRow("2037/12/31 23:00 UTC")
<< QDateTime(QDate(2037, 12, 31), QTime(23, 0), Qt::UTC)
<< QDateTime(QDate(2038, 1, 1), QTime(0, 0), Qt::LocalTime);