From 63fbabde2378a57c0f144786de089530d1d43fa8 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 23 Feb 2021 12:16:06 +0100 Subject: [PATCH] Only store offsetFromUtc when sane (and assert sanity) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The addition of a sanity-assertion revealed that, for an invalid time-zone, refreshZonedDateTime() left epochMSecs unset but computed offsetFromUtc from it none the less. Leave it as zero in that case, or any other where the conversion to UTC didn't give valid date and time. Change-Id: I0ebd955798532e91e7e211bf065667e313ee5c2d Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Thiago Macieira --- src/corelib/time/qdatetime.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index 6818837e8c..ea09120e30 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -2868,10 +2868,14 @@ static void refreshZonedDateTime(QDateTimeData &d, Qt::TimeSpec spec) msecs, d->m_timeZone, dstStatus, &testDate, &testTime); #endif // timezone } // else: testDate, testTime haven't been set, so are invalid. - // Cache the offset to use in offsetFromUtc() &c. - offsetFromUtc = (msecs - epochMSecs) / MSECS_PER_SEC; - if (testDate.isValid() && testTime.isValid() - && timeToMSecs(testDate, testTime) == msecs) { + const bool ok = testDate.isValid() && testTime.isValid(); + // Cache the offset to use in offsetFromUtc() &c., even if the next + // check marks invalid; this lets fromMSecsSinceEpoch() give a useful + // fallback for times in spring-forward gaps. + if (ok) + offsetFromUtc = (msecs - epochMSecs) / MSECS_PER_SEC; + Q_ASSERT(offsetFromUtc >= -SECS_PER_DAY && offsetFromUtc <= SECS_PER_DAY); + if (ok && timeToMSecs(testDate, testTime) == msecs) { status = mergeDaylightStatus(status, dstStatus); status |= QDateTimePrivate::ValidDateTime; } else {