From 92d24e52f18534fbf9bc4b768bc4f91d5c758331 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Mon, 7 Oct 2024 17:44:55 +0200 Subject: [PATCH] Add systemTimeZoneId() to the C++20 tzdb backend of QTimeZone It's not strictly necessary, but it should be a little more efficient. Without it, QTZ::systemTimeZoneId() falls back to using the id() of a default-constructed backend, QChronoTZP(), which will get the same answer returned here, but we can avoid the need for that fall-back by doing the right lookup to begin with. Change-Id: I6fe5beba048b5e9817b6bfed6eba785b13c2c206 Reviewed-by: Thiago Macieira --- src/corelib/time/qtimezoneprivate_chrono.cpp | 9 +++++++++ src/corelib/time/qtimezoneprivate_p.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/src/corelib/time/qtimezoneprivate_chrono.cpp b/src/corelib/time/qtimezoneprivate_chrono.cpp index 6c8a7218df7..c77c6eb5260 100644 --- a/src/corelib/time/qtimezoneprivate_chrono.cpp +++ b/src/corelib/time/qtimezoneprivate_chrono.cpp @@ -62,6 +62,15 @@ QChronoTimeZonePrivate::QChronoTimeZonePrivate(QByteArrayView id) QChronoTimeZonePrivate::~QChronoTimeZonePrivate() = default; +QByteArray QChronoTimeZonePrivate::systemTimeZoneId() const +{ + if (const time_zone *zone = std::chrono::current_zone()) { + std::string_view name = zone->name(); + return {name.begin(), qsizetype(name.size())}; + } + return {}; +} + QString QChronoTimeZonePrivate::abbreviation(qint64 atMSecsSinceEpoch) const { if (auto info = infoAtEpochMillis(m_timeZone, atMSecsSinceEpoch)) diff --git a/src/corelib/time/qtimezoneprivate_p.h b/src/corelib/time/qtimezoneprivate_p.h index 27bbe5b6222..b3a86e9cdbd 100644 --- a/src/corelib/time/qtimezoneprivate_p.h +++ b/src/corelib/time/qtimezoneprivate_p.h @@ -241,6 +241,8 @@ public: QChronoTimeZonePrivate(QByteArrayView id); ~QChronoTimeZonePrivate() override; + QByteArray systemTimeZoneId() const override; + QString abbreviation(qint64 atMSecsSinceEpoch) const override; int offsetFromUtc(qint64 atMSecsSinceEpoch) const override; int standardTimeOffset(qint64 atMSecsSinceEpoch) const override;