From e3fcc5403cead363d855e7b85315aae33ef19944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Tue, 10 Jun 2025 13:49:58 +0200 Subject: [PATCH] QDateTime::currentDateTime: fix time zone offset for repeated hours Because we were consulting GetLocalTime we didn't get disambiguated times for repeated hours. Drop that code-path and simple rely on GetSystemTime instead. Done-with: Mate Barany Done-with: Edward Welbourne Fixes: QTBUG-133656 Pick-to: 6.9 6.8 Change-Id: I3f1f09edfd9fb3135ccc12bf98e06254327e76fe Reviewed-by: Mate Barany (cherry picked from commit 1bcfbfa3bc75d2a91752497e59dd6c1b7a287e2b) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/time/qdatetime.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index a5ac987c701..b85c252cd30 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -5512,13 +5512,14 @@ QDateTime QDateTime::currentDateTime(const QTimeZone &zone) // convert, which is most efficiently done from UTC. const Qt::TimeSpec spec = zone.timeSpec(); SYSTEMTIME st = {}; - // GetSystemTime()'s page links to its partner page for GetLocalTime(). // https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtime - (spec == Qt::LocalTime ? GetLocalTime : GetSystemTime)(&st); + // We previously used GetLocalTime for spec == LocalTime but it didn't provide enough + // information to differentiate between repeated hours of a tradition and would report the same + // timezone (eg always CEST, never CET) for both. But toTimeZone handles it correctly, given + // the UTC time. + GetSystemTime(&st); QDate d(st.wYear, st.wMonth, st.wDay); QTime t(msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds)); - if (spec == Qt::LocalTime) - return QDateTime(d, t); QDateTime utc(d, t, QTimeZone::UTC); return spec == Qt::UTC ? utc : utc.toTimeZone(zone); }