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 <mate.barany@qt.io>
Done-with: Edward Welbourne <edward.welbourne@qt.io>
Fixes: QTBUG-133656
Pick-to: 6.8
Change-Id: I3f1f09edfd9fb3135ccc12bf98e06254327e76fe
Reviewed-by: Mate Barany <mate.barany@qt.io>
(cherry picked from commit 1bcfbfa3bc75d2a91752497e59dd6c1b7a287e2b)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit e3fcc5403cead363d855e7b85315aae33ef19944)
This commit is contained in:
Mårten Nordheim 2025-06-10 13:49:58 +02:00 committed by Qt Cherry-pick Bot
parent 3d4f972d10
commit c72fc4695e

View File

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