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 <ivan.solovev@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
bb10
Edward Welbourne 2023-08-11 16:12:47 +02:00
parent c23d00078c
commit 29af4b1833
2 changed files with 20 additions and 22 deletions

View File

@ -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()
*/

View File

@ -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);
}