From 29af4b1833e76ae1eeeca6ed9f0b9f8a9cb8c03f Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Fri, 11 Aug 2023 16:12:47 +0200 Subject: [PATCH] Sort out an anomaly in tst_QDateTime::springForward() It noted that an unspecified function claimed the offset it was checking should be +1, while testing it against that or -1. The function turns out to be QDateTime::addDays(), whose doc did indeed, misleadingly, say that it lands after a gap it would have hit. It in fact overshoots the gap in the direction of its change. Amend its docs, likewise those of addMonths() and addYears(), to reflect the true behavior. Amend the test to look at the direction of the step its taking and anticipate that the adjustment will be in the same direction; then compare the actual adjustment to that. Change-Id: I9ab918fac0ab2195ef014983f37fccc435bf0498 Reviewed-by: Ivan Solovev Reviewed-by: Thiago Macieira Reviewed-by: Qt CI Bot --- src/corelib/time/qdatetime.cpp | 33 ++++++++++--------- .../corelib/time/qdatetime/tst_qdatetime.cpp | 9 ++--- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index f338201f67..779fc1b0a5 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -4296,11 +4296,12 @@ static inline void massageAdjustedDateTime(QDateTimeData &d, QDate date, QTime t later than the datetime of this object (or earlier if \a ndays is negative). - If the timeSpec() is Qt::LocalTime or Qt::TimeZone and the resulting - date and time fall in the Standard Time to Daylight-Saving Time transition - hour then the result will be adjusted accordingly, i.e. if the transition - is at 2am and the clock goes forward to 3am and the result falls between - 2am and 3am then the result will be adjusted to fall after 3am. + If the timeSpec() is Qt::LocalTime or Qt::TimeZone and the resulting date + and time fall in the Standard Time to Daylight-Saving Time transition hour + then the result will be just beyond this gap, in the direction of change. + If the transition is at 2am and the clock goes forward to 3am, the result of + aiming between 2am and 3am will be adjusted to fall before 2am (if \c{ndays + < 0}) or after 3am (otherwise). \sa daysTo(), addMonths(), addYears(), addSecs() */ @@ -4321,11 +4322,12 @@ QDateTime QDateTime::addDays(qint64 ndays) const later than the datetime of this object (or earlier if \a nmonths is negative). - If the timeSpec() is Qt::LocalTime or Qt::TimeZone and the resulting - date and time fall in the Standard Time to Daylight-Saving Time transition - hour then the result will be adjusted accordingly, i.e. if the transition - is at 2am and the clock goes forward to 3am and the result falls between - 2am and 3am then the result will be adjusted to fall after 3am. + If the timeSpec() is Qt::LocalTime or Qt::TimeZone and the resulting date + and time fall in the Standard Time to Daylight-Saving Time transition hour + then the result will be just beyond this gap, in the direction of change. + If the transition is at 2am and the clock goes forward to 3am, the result of + aiming between 2am and 3am will be adjusted to fall before 2am (if + \c{nmonths < 0}) or after 3am (otherwise). \sa daysTo(), addDays(), addYears(), addSecs() */ @@ -4346,11 +4348,12 @@ QDateTime QDateTime::addMonths(int nmonths) const later than the datetime of this object (or earlier if \a nyears is negative). - If the timeSpec() is Qt::LocalTime or Qt::TimeZone and the resulting - date and time fall in the Standard Time to Daylight-Saving Time transition - hour then the result will be adjusted accordingly, i.e. if the transition - is at 2am and the clock goes forward to 3am and the result falls between - 2am and 3am then the result will be adjusted to fall after 3am. + If the timeSpec() is Qt::LocalTime or Qt::TimeZone and the resulting date + and time fall in the Standard Time to Daylight-Saving Time transition hour + then the result will be just beyond this gap, in the direction of change. + If the transition is at 2am and the clock goes forward to 3am, the result of + aiming between 2am and 3am will be adjusted to fall before 2am (if \c{nyears + < 0}) or after 3am (otherwise). \sa daysTo(), addDays(), addMonths(), addSecs() */ diff --git a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp index 71719a183e..4cf8c3d5f7 100644 --- a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp @@ -2290,13 +2290,8 @@ void tst_QDateTime::springForward() QCOMPARE(direct.date(), day); QCOMPARE(direct.time().minute(), time.minute()); QCOMPARE(direct.time().second(), time.second()); - // Note: function doc claims always +1, but this should be reviewed ! - int off = direct.time().hour() - time.hour(); - auto report = qScopeGuard([off]() { - qDebug("Offset %d found where 1 or -1 expected", off); - }); - QVERIFY(off == 1 || off == -1); - report.dismiss(); + 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); }