Skip to content

Commit

Permalink
QTimeZone: short-cut the empty-name case for the system zone and its id
Browse files Browse the repository at this point in the history
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.

Pick-to: 6.8
Change-Id: I8541e74f378e92af5a2a7187a49a9eb14a66c744
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
ediosyncratic committed Oct 10, 2024
1 parent 5f48395 commit 2068645
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/corelib/time/qtimezone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class QTimeZoneSingleton
// the host resources are insufficient. A simple UTC backend is used if no
// alternative is available.
QExplicitlySharedDataPointer<QTimeZonePrivate> backend;
// TODO QTBUG-56899: refresh should update this backend.
};

Q_GLOBAL_STATIC(QTimeZoneSingleton, global_tz);
Expand Down Expand Up @@ -1405,7 +1406,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();
}

/*!
Expand All @@ -1428,9 +1429,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) {
Expand Down

0 comments on commit 2068645

Please sign in to comment.