From 9b6a585f71703076a0939c247299ecabd084af11 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Mon, 7 Oct 2024 17:48:04 +0200 Subject: [PATCH] QTimeZone: short-cut the empty-name case for the system zone and its id The systemTimeZoneId() delegated to systemTimeZone() in a way that worked - but wasn't easy to follow - in the case where the backend's systemTimeZoneId() failed but its default constructor succeeds. This went via the QTimeZone(id) constructor, relying on its handling of empty id (which gets the system zone, unlike the default constructor which gets an invalid zone). When it gets an empty id, it falls back to newBackendTimeZone(), which uses the default constructor of the backend; and we have (and are even accessing the backend methods via) a default-constructed backend, so can simply share that with global_tz. This, if nothing else, saves repeating the systemTimeZone() look-up for the system ID when delegated to by systemTimeZoneId(). There's a risk that this might miss a change to the system configuration since the global_tz was initialized, but it's only used in a fallback and backends tend to cache this information anyway. When we come to address QTBUG-56899, we can have the refresh function update the global_tz object. Change-Id: I8541e74f378e92af5a2a7187a49a9eb14a66c744 Reviewed-by: Thiago Macieira (cherry picked from commit 2068645f07ab9108fc54fa2c499b1b2661a35bfd) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/time/qtimezone.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/corelib/time/qtimezone.cpp b/src/corelib/time/qtimezone.cpp index 9318e31b9a7..0caf36e7549 100644 --- a/src/corelib/time/qtimezone.cpp +++ b/src/corelib/time/qtimezone.cpp @@ -67,6 +67,7 @@ public: // the host resources are insufficient. A simple UTC backend is used if no // alternative is available. QExplicitlySharedDataPointer backend; + // TODO QTBUG-56899: refresh should update this backend. }; Q_GLOBAL_STATIC(QTimeZoneSingleton, global_tz); @@ -1401,7 +1402,7 @@ QByteArray QTimeZone::systemTimeZoneId() if (!sys.isEmpty()) return sys; // The system zone, despite the empty ID, may know its real ID anyway: - return systemTimeZone().id(); + return global_tz->backend->id(); } /*! @@ -1424,9 +1425,9 @@ QByteArray QTimeZone::systemTimeZoneId() */ QTimeZone QTimeZone::systemTimeZone() { - // Use ID even if empty, as default constructor is invalid but empty-ID - // constructor goes to backend's default constructor, which may succeed. - const auto sys = QTimeZone(global_tz->backend->systemTimeZoneId()); + // Short-cut constructor's handling of empty ID: + const QByteArray sysId = global_tz->backend->systemTimeZoneId(); + const auto sys = sysId.isEmpty() ? QTimeZone(global_tz->backend) : QTimeZone(sysId); if (!sys.isValid()) { static bool neverWarned = true; if (neverWarned) {