summaryrefslogtreecommitdiffstats
path: root/src/corelib/time/qtimezoneprivate_tz.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/time/qtimezoneprivate_tz.cpp')
-rw-r--r--src/corelib/time/qtimezoneprivate_tz.cpp342
1 files changed, 193 insertions, 149 deletions
diff --git a/src/corelib/time/qtimezoneprivate_tz.cpp b/src/corelib/time/qtimezoneprivate_tz.cpp
index a9d6d06125..b6a7d1418c 100644
--- a/src/corelib/time/qtimezoneprivate_tz.cpp
+++ b/src/corelib/time/qtimezoneprivate_tz.cpp
@@ -1,58 +1,27 @@
-/****************************************************************************
-**
-** Copyright (C) 2021 The Qt Company Ltd.
-** Copyright (C) 2019 Crimson AS <info@crimson.no>
-** Copyright (C) 2013 John Layt <jlayt@kde.org>
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2022 The Qt Company Ltd.
+// Copyright (C) 2019 Crimson AS <info@crimson.no>
+// Copyright (C) 2013 John Layt <jlayt@kde.org>
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qtimezone.h"
#include "qtimezoneprivate_p.h"
#include "private/qlocale_tools_p.h"
+#include "private/qlocking_p.h"
#include <QtCore/QDataStream>
#include <QtCore/QDateTime>
+#include <QtCore/QDirListing>
#include <QtCore/QFile>
#include <QtCore/QCache>
+#include <QtCore/QMap>
#include <QtCore/QMutex>
#include <qdebug.h>
#include <qplatformdefs.h>
#include <algorithm>
+#include <memory>
+
#include <errno.h>
#include <limits.h>
#ifndef Q_OS_INTEGRITY
@@ -62,6 +31,12 @@
QT_BEGIN_NAMESPACE
+using namespace Qt::StringLiterals;
+
+#if QT_CONFIG(icu)
+Q_CONSTINIT static QBasicMutex s_icu_mutex;
+#endif
+
/*
Private
@@ -69,22 +44,50 @@ QT_BEGIN_NAMESPACE
*/
struct QTzTimeZone {
- QLocale::Territory territory;
+ QLocale::Territory territory = QLocale::AnyTerritory;
QByteArray comment;
};
// Define as a type as Q_GLOBAL_STATIC doesn't like it
typedef QHash<QByteArray, QTzTimeZone> QTzTimeZoneHash;
-// Parse zone.tab table, assume lists all installed zones, if not will need to read directories
-static QTzTimeZoneHash loadTzTimeZones()
+static bool isTzFile(const QString &name);
+
+// Open a named file under the zone info directory:
+static bool openZoneInfo(QString name, QFile *file)
{
- QString path = QStringLiteral("/usr/share/zoneinfo/zone.tab");
- if (!QFile::exists(path))
- path = QStringLiteral("/usr/lib/zoneinfo/zone.tab");
+ // At least on Linux / glibc (see man 3 tzset), $TZDIR overrides the system
+ // default location for zone info:
+ const QString tzdir = qEnvironmentVariable("TZDIR");
+ if (!tzdir.isEmpty()) {
+ file->setFileName(QDir(tzdir).filePath(name));
+ if (file->open(QIODevice::ReadOnly))
+ return true;
+ }
+ // Try modern system path first:
+ constexpr auto zoneShare = "/usr/share/zoneinfo/"_L1;
+ if (tzdir != zoneShare && tzdir != zoneShare.chopped(1)) {
+ file->setFileName(zoneShare + name);
+ if (file->open(QIODevice::ReadOnly))
+ return true;
+ }
+ // Fall back to legacy system path:
+ constexpr auto zoneLib = "/usr/lib/zoneinfo/"_L1;
+ if (tzdir != zoneLib && tzdir != zoneLib.chopped(1)) {
+ file->setFileName(zoneShare + name);
+ if (file->open(QIODevice::ReadOnly))
+ return true;
+ }
+ return false;
+}
- QFile tzif(path);
- if (!tzif.open(QIODevice::ReadOnly))
+// Parse zone.tab table for territory information, read directories to ensure we
+// find all installed zones (many are omitted from zone.tab; even more from
+// zone1970.tab).
+static QTzTimeZoneHash loadTzTimeZones()
+{
+ QFile tzif;
+ if (!openZoneInfo("zone.tab"_L1, &tzif))
return QTzTimeZoneHash();
QTzTimeZoneHash zonesHash;
@@ -113,11 +116,32 @@ static QTzTimeZoneHash loadTzTimeZones()
}
}
}
+
+ const QString path = tzif.fileName();
+ const qsizetype cut = path.lastIndexOf(u'/');
+ Q_ASSERT(cut > 0);
+ const QDir zoneDir = QDir(path.first(cut));
+ for (const auto &info : QDirListing(zoneDir, QDirListing::IteratorFlag::Recursive)) {
+ if (!(info.isFile() || info.isSymLink()))
+ continue;
+ const QString name = zoneDir.relativeFilePath(info.filePath());
+ // Two sub-directories containing (more or less) copies of the zoneinfo tree.
+ if (info.isDir() ? name == "posix"_L1 || name == "right"_L1
+ : name.startsWith("posix/"_L1) || name.startsWith("right/"_L1)) {
+ continue;
+ }
+ // We could filter out *.* and leapseconds instead of doing the
+ // isTzFile() check; in practice current (2023) zoneinfo/ contains only
+ // actual zone files and matches to that filter.
+ const QByteArray id = QFile::encodeName(name);
+ if (!zonesHash.contains(id) && isTzFile(zoneDir.absoluteFilePath(name)))
+ zonesHash.insert(id, QTzTimeZone());
+ }
return zonesHash;
}
// Hash of available system tz files as loaded by loadTzTimeZones()
-Q_GLOBAL_STATIC_WITH_ARGS(const QTzTimeZoneHash, tzZones, (loadTzTimeZones()));
+Q_GLOBAL_STATIC(const QTzTimeZoneHash, tzZones, loadTzTimeZones());
/*
The following is copied and modified from tzfile.h which is in the public domain.
@@ -156,6 +180,11 @@ struct QTzType {
};
Q_DECLARE_TYPEINFO(QTzType, Q_PRIMITIVE_TYPE);
+static bool isTzFile(const QString &name)
+{
+ QFile file(name);
+ return file.open(QFile::ReadOnly) && file.read(strlen(TZ_MAGIC)) == TZ_MAGIC;
+}
// TZ File parsing
@@ -380,13 +409,15 @@ static QDate calculateDowDate(int year, int month, int dayOfWeek, int week)
static QDate calculatePosixDate(const QByteArray &dateRule, int year)
{
+ Q_ASSERT(!dateRule.isEmpty());
bool ok;
// Can start with M, J, or a digit
if (dateRule.at(0) == 'M') {
// nth week in month format "Mmonth.week.dow"
QList<QByteArray> dateParts = dateRule.split('.');
- if (dateParts.count() > 2) {
- int month = dateParts.at(0).mid(1).toInt(&ok);
+ if (dateParts.size() > 2) {
+ Q_ASSERT(!dateParts.at(0).isEmpty()); // the 'M' is its [0].
+ int month = QByteArrayView{ dateParts.at(0) }.sliced(1).toInt(&ok);
int week = ok ? dateParts.at(1).toInt(&ok) : 0;
int dow = ok ? dateParts.at(2).toInt(&ok) : 0;
if (ok)
@@ -395,7 +426,7 @@ static QDate calculatePosixDate(const QByteArray &dateRule, int year)
} else if (dateRule.at(0) == 'J') {
// Day of Year 1...365, ignores Feb 29.
// So March always starts on day 60.
- int doy = dateRule.mid(1).toInt(&ok);
+ int doy = QByteArrayView{ dateRule }.sliced(1).toInt(&ok);
if (ok && doy > 0 && doy < 366) {
// Subtract 1 because we're adding days *after* the first of
// January, unless it's after February in a leap year, when the leap
@@ -420,27 +451,28 @@ static int parsePosixTime(const char *begin, const char *end)
int hour, min = 0, sec = 0;
const int maxHour = 137; // POSIX's extended range.
- bool ok = false;
- const char *cut = begin;
- hour = qstrntoll(begin, end - begin, &cut, 10, &ok);
- if (!ok || hour < -maxHour || hour > maxHour || cut > begin + 2)
+ auto r = qstrntoll(begin, end - begin, 10);
+ hour = r.result;
+ if (!r.ok() || hour < -maxHour || hour > maxHour || r.used > 2)
return INT_MIN;
- begin = cut;
+ begin += r.used;
if (begin < end && *begin == ':') {
// minutes
++begin;
- min = qstrntoll(begin, end - begin, &cut, 10, &ok);
- if (!ok || min < 0 || min > 59 || cut > begin + 2)
+ r = qstrntoll(begin, end - begin, 10);
+ min = r.result;
+ if (!r.ok() || min < 0 || min > 59 || r.used > 2)
return INT_MIN;
- begin = cut;
+ begin += r.used;
if (begin < end && *begin == ':') {
// seconds
++begin;
- sec = qstrntoll(begin, end - begin, &cut, 10, &ok);
- if (!ok || sec < 0 || sec > 59 || cut > begin + 2)
+ r = qstrntoll(begin, end - begin, 10);
+ sec = r.result;
+ if (!r.ok() || sec < 0 || sec > 59 || r.used > 2)
return INT_MIN;
- begin = cut;
+ begin += r.used;
}
}
@@ -511,7 +543,7 @@ PosixZone PosixZone::parse(const char *&pos, const char *end)
Q_ASSERT(pos < end);
if (*pos == '<') {
- nameBegin = pos + 1; // skip the '<'
+ ++nameBegin; // skip the '<'
nameEnd = nameBegin;
while (nameEnd < end && *nameEnd != '>') {
// POSIX says only alphanumeric, but we allow anything
@@ -519,7 +551,6 @@ PosixZone PosixZone::parse(const char *&pos, const char *end)
}
pos = nameEnd + 1; // skip the '>'
} else {
- nameBegin = pos;
nameEnd = nameBegin;
while (nameEnd < end && asciiIsLetter(*nameEnd))
++nameEnd;
@@ -544,32 +575,41 @@ PosixZone PosixZone::parse(const char *&pos, const char *end)
pos = zoneEnd;
// UTC+hh:mm:ss or GMT+hh:mm:ss should be read as offsets from UTC, not as a
// POSIX rule naming a zone as UTC or GMT and specifying a non-zero offset.
- if (offset != 0 && (name == QLatin1String("UTC") || name == QLatin1String("GMT")))
+ if (offset != 0 && (name =="UTC"_L1 || name == "GMT"_L1))
return invalid();
return {std::move(name), offset};
}
-static auto validatePosixRule(const QByteArray &posixRule)
+/* Parse and check a POSIX rule.
+
+ By default a simple zone abbreviation with no offset information is accepted.
+ Set \a requireOffset to \c true to require that there be offset data present.
+*/
+static auto validatePosixRule(const QByteArray &posixRule, bool requireOffset = false)
{
// Format is described here:
// http://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
// See also calculatePosixTransition()'s reference.
const auto parts = posixRule.split(',');
- const struct { bool isValid, hasDst; } fail{false, false}, good{true, parts.count() > 1};
+ const struct { bool isValid, hasDst; } fail{false, false}, good{true, parts.size() > 1};
const QByteArray &zoneinfo = parts.at(0);
if (zoneinfo.isEmpty())
return fail;
const char *begin = zoneinfo.begin();
-
- // Updates begin to point after the name and offset it parses:
- if (PosixZone::parse(begin, zoneinfo.end()).name.isEmpty())
- return fail;
+ {
+ // Updates begin to point after the name and offset it parses:
+ const auto posix = PosixZone::parse(begin, zoneinfo.end());
+ if (posix.name.isEmpty())
+ return fail;
+ if (requireOffset && !posix.hasValidOffset())
+ return fail;
+ }
if (good.hasDst) {
if (begin >= zoneinfo.end())
return fail;
- // Expect a second name and offset after the first:
+ // Expect a second name (and optional offset) after the first:
if (PosixZone::parse(begin, zoneinfo.end()).name.isEmpty())
return fail;
}
@@ -577,13 +617,13 @@ static auto validatePosixRule(const QByteArray &posixRule)
return fail;
if (good.hasDst) {
- if (parts.count() != 3 || parts.at(1).isEmpty() || parts.at(2).isEmpty())
+ if (parts.size() != 3 || parts.at(1).isEmpty() || parts.at(2).isEmpty())
return fail;
for (int i = 1; i < 3; ++i) {
const auto tran = parts.at(i).split('/');
if (!calculatePosixDate(tran.at(0), 1972).isValid())
return fail;
- if (tran.count() > 1) {
+ if (tran.size() > 1) {
const auto time = tran.at(1);
if (parsePosixTime(time.begin(), time.end()) == INT_MIN)
return fail;
@@ -624,7 +664,7 @@ static QList<QTimeZonePrivate::Data> calculatePosixTransitions(const QByteArray
}
// If only the name part, or no DST specified, then no transitions
- if (parts.count() == 1 || !dstZone.hasValidOffset()) {
+ if (parts.size() == 1 || !dstZone.hasValidOffset()) {
QTimeZonePrivate::Data data;
data.atMSecsSinceEpoch = lastTranMSecs;
data.offsetFromUtc = stdZone.offset;
@@ -634,19 +674,19 @@ static QList<QTimeZonePrivate::Data> calculatePosixTransitions(const QByteArray
result << data;
return result;
}
- if (parts.count() < 3 || parts.at(1).isEmpty() || parts.at(2).isEmpty())
+ if (parts.size() < 3 || parts.at(1).isEmpty() || parts.at(2).isEmpty())
return result; // Malformed.
// Get the std to dst transition details
const int twoOClock = 7200; // Default transition time, when none specified
const auto dstParts = parts.at(1).split('/');
const QByteArray dstDateRule = dstParts.at(0);
- const int dstTime = dstParts.count() < 2 ? twoOClock : parsePosixTransitionTime(dstParts.at(1));
+ const int dstTime = dstParts.size() < 2 ? twoOClock : parsePosixTransitionTime(dstParts.at(1));
// Get the dst to std transition details
const auto stdParts = parts.at(2).split('/');
const QByteArray stdDateRule = stdParts.at(0);
- const int stdTime = stdParts.count() < 2 ? twoOClock : parsePosixTransitionTime(stdParts.at(1));
+ const int stdTime = stdParts.size() < 2 ? twoOClock : parsePosixTransitionTime(stdParts.at(1));
if (dstDateRule.isEmpty() || stdDateRule.isEmpty() || dstTime == INT_MIN || stdTime == INT_MIN)
return result; // Malformed.
@@ -659,20 +699,22 @@ static QList<QTimeZonePrivate::Data> calculatePosixTransitions(const QByteArray
Q_ASSERT(startYear <= endYear);
for (int year = startYear; year <= endYear; ++year) {
- // Note: std and dst, despite being QDateTime(,, Qt::UTC), have the
+ // Note: std and dst, despite being QDateTime(,, UTC), have the
// date() and time() of the *zone*'s description of the transition
// moments; the atMSecsSinceEpoch values computed from them are
// correctly offse to be UTC-based.
QTimeZonePrivate::Data dstData; // Transition to DST
- QDateTime dst(calculatePosixDate(dstDateRule, year).startOfDay(Qt::UTC).addSecs(dstTime));
+ QDateTime dst(calculatePosixDate(dstDateRule, year)
+ .startOfDay(QTimeZone::UTC).addSecs(dstTime));
dstData.atMSecsSinceEpoch = dst.toMSecsSinceEpoch() - stdZone.offset * 1000;
dstData.offsetFromUtc = dstZone.offset;
dstData.standardTimeOffset = stdZone.offset;
dstData.daylightTimeOffset = dstZone.offset - stdZone.offset;
dstData.abbreviation = dstZone.name;
QTimeZonePrivate::Data stdData; // Transition to standard time
- QDateTime std(calculatePosixDate(stdDateRule, year).startOfDay(Qt::UTC).addSecs(stdTime));
+ QDateTime std(calculatePosixDate(stdDateRule, year)
+ .startOfDay(QTimeZone::UTC).addSecs(stdTime));
stdData.atMSecsSinceEpoch = std.toMSecsSinceEpoch() - dstZone.offset * 1000;
stdData.offsetFromUtc = stdZone.offset;
stdData.standardTimeOffset = stdZone.offset;
@@ -683,16 +725,16 @@ static QList<QTimeZonePrivate::Data> calculatePosixTransitions(const QByteArray
// Handle the special case of fixed state, which may be represented
// by fake transitions at start and end of each year:
if (dstData.atMSecsSinceEpoch < stdData.atMSecsSinceEpoch) {
- if (dst <= QDate(year, 1, 1).startOfDay(Qt::UTC)
- && std >= QDate(year, 12, 31).endOfDay(Qt::UTC)) {
+ if (dst <= QDate(year, 1, 1).startOfDay(QTimeZone::UTC)
+ && std >= QDate(year, 12, 31).endOfDay(QTimeZone::UTC)) {
// Permanent DST:
dstData.atMSecsSinceEpoch = lastTranMSecs;
result << dstData;
return result;
}
} else {
- if (std <= QDate(year, 1, 1).startOfDay(Qt::UTC)
- && dst >= QDate(year, 12, 31).endOfDay(Qt::UTC)) {
+ if (std <= QDate(year, 1, 1).startOfDay(QTimeZone::UTC)
+ && dst >= QDate(year, 12, 31).endOfDay(QTimeZone::UTC)) {
// Permanent Standard time, perversely described:
stdData.atMSecsSinceEpoch = lastTranMSecs;
result << stdData;
@@ -719,14 +761,8 @@ static QList<QTimeZonePrivate::Data> calculatePosixTransitions(const QByteArray
// Create the system default time zone
QTzTimeZonePrivate::QTzTimeZonePrivate()
+ : QTzTimeZonePrivate(staticSystemTimeZoneId())
{
- init(systemTimeZoneId());
-}
-
-// Create a named time zone
-QTzTimeZonePrivate::QTzTimeZonePrivate(const QByteArray &ianaId)
-{
- init(ianaId);
}
QTzTimeZonePrivate::~QTzTimeZonePrivate()
@@ -735,6 +771,9 @@ QTzTimeZonePrivate::~QTzTimeZonePrivate()
QTzTimeZonePrivate *QTzTimeZonePrivate::clone() const
{
+#if QT_CONFIG(icu)
+ const auto lock = qt_scoped_lock(s_icu_mutex);
+#endif
return new QTzTimeZonePrivate(*this);
}
@@ -744,7 +783,7 @@ public:
QTzTimeZoneCacheEntry fetchEntry(const QByteArray &ianaId);
private:
- QTzTimeZoneCacheEntry findEntry(const QByteArray &ianaId);
+ static QTzTimeZoneCacheEntry findEntry(const QByteArray &ianaId);
QCache<QByteArray, QTzTimeZoneCacheEntry> m_cache;
QMutex m_mutex;
};
@@ -758,21 +797,14 @@ QTzTimeZoneCacheEntry QTzTimeZoneCache::findEntry(const QByteArray &ianaId)
tzif.setFileName(QStringLiteral("/etc/localtime"));
if (!tzif.open(QIODevice::ReadOnly))
return ret;
- } else {
- // Open named tz, try modern path first, if fails try legacy path
- tzif.setFileName(QLatin1String("/usr/share/zoneinfo/") + QString::fromLocal8Bit(ianaId));
- if (!tzif.open(QIODevice::ReadOnly)) {
- tzif.setFileName(QLatin1String("/usr/lib/zoneinfo/") + QString::fromLocal8Bit(ianaId));
- if (!tzif.open(QIODevice::ReadOnly)) {
- // ianaId may be a POSIX rule, taken from $TZ or /etc/TZ
- auto check = validatePosixRule(ianaId);
- if (check.isValid) {
- ret.m_hasDst = check.hasDst;
- ret.m_posixRule = ianaId;
- }
- return ret;
- }
+ } else if (!openZoneInfo(QString::fromLocal8Bit(ianaId), &tzif)) {
+ // ianaId may be a POSIX rule, taken from $TZ or /etc/TZ
+ auto check = validatePosixRule(ianaId);
+ if (check.isValid) {
+ ret.m_hasDst = check.hasDst;
+ ret.m_posixRule = ianaId;
}
+ return ret;
}
QDataStream ds(&tzif);
@@ -858,7 +890,7 @@ QTzTimeZoneCacheEntry QTzTimeZoneCache::findEntry(const QByteArray &ianaId)
// Offsets are stored as total offset, want to know separate UTC and DST offsets
// so find the first non-dst transition to use as base UTC Offset
int utcOffset = ret.m_preZoneRule.stdOffset;
- for (const QTzTransition &tran : qAsConst(tranList)) {
+ for (const QTzTransition &tran : std::as_const(tranList)) {
if (!typeList.at(tran.tz_typeind).tz_isdst) {
utcOffset = typeList.at(tran.tz_typeind).tz_gmtoff;
break;
@@ -866,7 +898,7 @@ QTzTimeZoneCacheEntry QTzTimeZoneCache::findEntry(const QByteArray &ianaId)
}
// Now for each transition time calculate and store our rule:
- const int tranCount = tranList.count();;
+ const int tranCount = tranList.size();
ret.m_tranTimes.reserve(tranCount);
// The DST offset when in effect: usually stable, usually an hour:
int lastDstOff = 3600;
@@ -925,8 +957,8 @@ QTzTimeZoneCacheEntry QTzTimeZoneCache::findEntry(const QByteArray &ianaId)
if (ruleIndex == -1) {
if (rule.dstOffset != 0)
ret.m_hasDst = true;
+ tran.ruleIndex = ret.m_tranRules.size();
ret.m_tranRules.append(rule);
- tran.ruleIndex = ret.m_tranRules.size() - 1;
} else {
tran.ruleIndex = ruleIndex;
}
@@ -948,13 +980,24 @@ QTzTimeZoneCacheEntry QTzTimeZoneCache::fetchEntry(const QByteArray &ianaId)
return *obj;
// ... or build a new entry from scratch
+
+ locker.unlock(); // don't parse files under mutex lock
+
QTzTimeZoneCacheEntry ret = findEntry(ianaId);
- m_cache.insert(ianaId, new QTzTimeZoneCacheEntry(ret));
+ auto ptr = std::make_unique<QTzTimeZoneCacheEntry>(ret);
+
+ locker.relock();
+ m_cache.insert(ianaId, ptr.release()); // may overwrite if another thread was faster
+ locker.unlock();
+
return ret;
}
-void QTzTimeZonePrivate::init(const QByteArray &ianaId)
+// Create a named time zone
+QTzTimeZonePrivate::QTzTimeZonePrivate(const QByteArray &ianaId)
{
+ if (!isTimeZoneIdAvailable(ianaId)) // Avoid pointlessly creating cache entries
+ return;
static QTzTimeZoneCache tzCache;
auto entry = tzCache.fetchEntry(ianaId);
if (entry.m_tranTimes.isEmpty() && entry.m_posixRule.isEmpty())
@@ -989,42 +1032,27 @@ QString QTzTimeZonePrivate::comment() const
return QString::fromUtf8(tzZones->value(m_id).comment);
}
-QString QTzTimeZonePrivate::displayName(qint64 atMSecsSinceEpoch,
- QTimeZone::NameType nameType,
- const QLocale &locale) const
-{
-#if QT_CONFIG(icu)
- if (!m_icu)
- m_icu = new QIcuTimeZonePrivate(m_id);
- // TODO small risk may not match if tran times differ due to outdated files
- // TODO Some valid TZ names are not valid ICU names, use translation table?
- if (m_icu->isValid())
- return m_icu->displayName(atMSecsSinceEpoch, nameType, locale);
-#else
- Q_UNUSED(nameType);
- Q_UNUSED(locale);
-#endif
- // Fall back to base-class:
- return QTimeZonePrivate::displayName(atMSecsSinceEpoch, nameType, locale);
-}
-
QString QTzTimeZonePrivate::displayName(QTimeZone::TimeType timeType,
QTimeZone::NameType nameType,
const QLocale &locale) const
{
+ // TZ DB lacks localized names (it only has IANA IDs), so delegate to ICU
+ // for those, when available.
#if QT_CONFIG(icu)
- if (!m_icu)
- m_icu = new QIcuTimeZonePrivate(m_id);
- // TODO small risk may not match if tran times differ due to outdated files
- // TODO Some valid TZ names are not valid ICU names, use translation table?
- if (m_icu->isValid())
- return m_icu->displayName(timeType, nameType, locale);
+ {
+ auto lock = qt_scoped_lock(s_icu_mutex);
+ // TODO Some valid TZ names are not valid ICU names, use translation table?
+ if (!m_icu)
+ m_icu = new QIcuTimeZonePrivate(m_id);
+ if (m_icu->isValid())
+ return m_icu->displayName(timeType, nameType, locale);
+ }
#else
Q_UNUSED(timeType);
Q_UNUSED(nameType);
Q_UNUSED(locale);
#endif
- // If no ICU available then have to use abbreviations instead
+ // If ICU is unavailable, fall back to abbreviations.
// Abbreviations don't have GenericTime
if (timeType == QTimeZone::GenericTime)
timeType = QTimeZone::StandardTime;
@@ -1119,7 +1147,7 @@ QTimeZonePrivate::Data QTzTimeZonePrivate::dataFromRule(QTzTransitionRule rule,
QList<QTimeZonePrivate::Data> QTzTimeZonePrivate::getPosixTransitions(qint64 msNear) const
{
- const int year = QDateTime::fromMSecsSinceEpoch(msNear, Qt::UTC).date().year();
+ const int year = QDateTime::fromMSecsSinceEpoch(msNear, QTimeZone::UTC).date().year();
// The Data::atMSecsSinceEpoch of the single entry if zone is constant:
qint64 atTime = tranCache().isEmpty() ? msNear : tranCache().last().atMSecsSinceEpoch;
return calculatePosixTransitions(cached_data.m_posixRule, year - 1, year + 1, atTime);
@@ -1213,7 +1241,11 @@ QTimeZonePrivate::Data QTzTimeZonePrivate::previousTransition(qint64 beforeMSecs
bool QTzTimeZonePrivate::isTimeZoneIdAvailable(const QByteArray &ianaId) const
{
- return tzZones->contains(ianaId);
+ // Allow a POSIX rule as long as it has offset data. (This needs to reject a
+ // plain abbreviation, without offset, since claiming to support such zones
+ // would prevent the custom QTimeZone constructor from accepting such a
+ // name, as it doesn't want a custom zone to over-ride a "real" one.)
+ return tzZones->contains(ianaId) || validatePosixRule(ianaId, true).isValid;
}
QList<QByteArray> QTzTimeZonePrivate::availableTimeZoneIds() const
@@ -1238,7 +1270,7 @@ QList<QByteArray> QTzTimeZonePrivate::availableTimeZoneIds(QLocale::Territory te
// Getting the system zone's ID:
namespace {
-class ZoneNameReader : public QObject
+class ZoneNameReader
{
public:
QByteArray name()
@@ -1291,7 +1323,7 @@ private:
{
static constexpr unsigned long bad = ~0ul;
unsigned long m_dev, m_ino;
- StatIdent() : m_dev(bad), m_ino(bad) {}
+ constexpr StatIdent() : m_dev(bad), m_ino(bad) {}
StatIdent(const QT_STATBUF &data) : m_dev(data.st_dev), m_ino(data.st_ino) {}
bool isValid() { return m_dev != bad || m_ino != bad; }
bool operator==(const StatIdent &other)
@@ -1309,7 +1341,8 @@ private:
{
// On most distros /etc/localtime is a symlink to a real file so extract
// name from the path
- const QLatin1String zoneinfo("/zoneinfo/");
+ const QString tzdir = qEnvironmentVariable("TZDIR");
+ constexpr auto zoneinfo = "/zoneinfo/"_L1;
QString path = QStringLiteral("/etc/localtime");
long iteration = getSymloopMax();
// Symlink may point to another symlink etc. before being under zoneinfo/
@@ -1317,9 +1350,15 @@ private:
// symlink, like America/Montreal pointing to America/Toronto
do {
path = QFile::symLinkTarget(path);
- int index = path.indexOf(zoneinfo);
- if (index >= 0) // Found zoneinfo file; extract zone name from path:
- return QStringView{ path }.mid(index + zoneinfo.size()).toUtf8();
+ // If it's a zoneinfo file, extract the zone name from its path:
+ int index = tzdir.isEmpty() ? -1 : path.indexOf(tzdir);
+ if (index >= 0) {
+ const auto tail = QStringView{ path }.sliced(index + tzdir.size()).toUtf8();
+ return tail.startsWith(u'/') ? tail.sliced(1) : tail;
+ }
+ index = path.indexOf(zoneinfo);
+ if (index >= 0)
+ return QStringView{ path }.sliced(index + zoneinfo.size()).toUtf8();
} while (!path.isEmpty() && --iteration > 0);
return QByteArray();
@@ -1361,6 +1400,11 @@ private:
QByteArray QTzTimeZonePrivate::systemTimeZoneId() const
{
+ return staticSystemTimeZoneId();
+}
+
+QByteArray QTzTimeZonePrivate::staticSystemTimeZoneId()
+{
// Check TZ env var first, if not populated try find it
QByteArray ianaId = qgetenv("TZ");
@@ -1371,10 +1415,10 @@ QByteArray QTzTimeZonePrivate::systemTimeZoneId() const
if (ianaId == ":/etc/localtime")
ianaId.clear();
else if (ianaId.startsWith(':'))
- ianaId = ianaId.mid(1);
+ ianaId = ianaId.sliced(1);
if (ianaId.isEmpty()) {
- thread_local static ZoneNameReader reader;
+ Q_CONSTINIT thread_local static ZoneNameReader reader;
ianaId = reader.name();
}