Only store offsetFromUtc when sane (and assert sanity)

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 <marten.nordheim@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2021-02-23 12:16:06 +01:00
parent 5af6b59b6e
commit 63fbabde23

View File

@ -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 {