From 83421e320b8b0b2ddd528a280609cbb7f2cb7781 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Mon, 22 Feb 2021 16:35:44 +0100 Subject: [PATCH] Try again if mktime() fails when we thought we knew DST-ness MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When refreshing a QDateTime(,, Qt::LocalTime) we call mktime on data obtained from it, passing in the DST status (when known; this keeps two otherwise identical times in a fall-back distinct). One of the tests relies on changing zone under the feet of such a date-time, created in Hawaiian standard time; it serializes it, the reads it back in Western Australian Daylight-saving time and expects the results to be equal. However, the two differ in DST-ness, which leads to mktime() failing for the Hawaiian original, with unwelcome results. Notice this case, failure with DST-ness claimed known, and retry without the claim, so as to correct the DST-ness. Change-Id: Id0278df53130f76fc587769efe946ca9af1adc26 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Thiago Macieira --- src/corelib/time/qdatetime.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index ea09120e30..26e25afd38 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -2457,6 +2457,12 @@ static qint64 qt_mktime(QDate *date, QTime *time, QDateTimePrivate::DaylightStat int hh = local.tm_hour; #endif // Q_OS_WIN time_t secsSinceEpoch = qMkTime(&local); + // That can fail if we thought we knew DST-ness, but were wrong: + if (secsSinceEpoch == time_t(-1) && local.tm_isdst >= 0) { + local.tm_isdst = -1; + secsSinceEpoch = qMkTime(&local); + } + if (secsSinceEpoch != time_t(-1)) { *date = QDate(local.tm_year + 1900, local.tm_mon + 1, local.tm_mday); *time = QTime(local.tm_hour, local.tm_min, local.tm_sec, msec);