Skip to content

Commit

Permalink
Move QTimeZone's CLDR-derived data into a namespace
Browse files Browse the repository at this point in the history
Introduce namespace QtTimeZoneCldr instead of having a Q prefix on
each class name used for the data.

Change-Id: Icb22a91340b67f9cc93173b77374a70f69f81bbe
Reviewed-by: Ivan Solovev <[email protected]>
  • Loading branch information
ediosyncratic committed Feb 8, 2024
1 parent a736e61 commit b0f4bd7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
27 changes: 14 additions & 13 deletions src/corelib/time/qtimezoneprivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
QT_BEGIN_NAMESPACE

using namespace QtMiscUtils;
using namespace QtTimeZoneCldr;

// For use with std::is_sorted() in assertions:
[[maybe_unused]]
constexpr bool earlierZoneData(const QZoneData &less, const QZoneData &more) noexcept
constexpr bool earlierZoneData(const ZoneData &less, const ZoneData &more) noexcept
{
return less.windowsIdKey < more.windowsIdKey
|| (less.windowsIdKey == more.windowsIdKey && less.territory < more.territory);
}

[[maybe_unused]]
static bool earlierWinData(const QWindowsData &less, const QWindowsData &more) noexcept
static bool earlierWinData(const WindowsData &less, const WindowsData &more) noexcept
{
// Actually only tested in the negative, to check more < less never happens,
// so should be true if more < less in either part; hence || not && combines.
Expand All @@ -38,22 +39,22 @@ static bool earlierWinData(const QWindowsData &less, const QWindowsData &more) n
}

// For use with std::lower_bound():
constexpr bool atLowerUtcOffset(const QUtcData &entry, qint32 offsetSeconds) noexcept
constexpr bool atLowerUtcOffset(const UtcData &entry, qint32 offsetSeconds) noexcept
{
return entry.offsetFromUtc < offsetSeconds;
}

constexpr bool atLowerWindowsKey(const QWindowsData &entry, qint16 winIdKey) noexcept
constexpr bool atLowerWindowsKey(const WindowsData &entry, qint16 winIdKey) noexcept
{
return entry.windowsIdKey < winIdKey;
}

static bool earlierWindowsId(const QWindowsData &entry, QByteArrayView winId) noexcept
static bool earlierWindowsId(const WindowsData &entry, QByteArrayView winId) noexcept
{
return entry.windowsId().compare(winId, Qt::CaseInsensitive) < 0;
}

constexpr bool zoneAtLowerWindowsKey(const QZoneData &entry, qint16 winIdKey) noexcept
constexpr bool zoneAtLowerWindowsKey(const ZoneData &entry, qint16 winIdKey) noexcept
{
return entry.windowsIdKey < winIdKey;
}
Expand Down Expand Up @@ -151,7 +152,7 @@ QLocale::Territory QTimeZonePrivate::territory() const
{
// Default fall-back mode, use the zoneTable to find Region of known Zones
const QLatin1StringView sought(m_id.data(), m_id.size());
for (const QZoneData &data : zoneDataTable) {
for (const ZoneData &data : zoneDataTable) {
for (QLatin1StringView token : data.ids()) {
if (token == sought)
return QLocale::Territory(data.territory);
Expand Down Expand Up @@ -534,7 +535,7 @@ QList<QByteArray> QTimeZonePrivate::availableTimeZoneIds(QLocale::Territory terr
QList<QByteArray> regions;

// First get all Zones in the Zones table belonging to the Region
for (const QZoneData &data : zoneDataTable) {
for (const ZoneData &data : zoneDataTable) {
if (data.territory == territory) {
for (auto l1 : data.ids())
regions << QByteArray(l1.data(), l1.size());
Expand All @@ -558,7 +559,7 @@ QList<QByteArray> QTimeZonePrivate::availableTimeZoneIds(int offsetFromUtc) cons
// Default fall-back mode, use the zoneTable to find Offset of know Zones
QList<QByteArray> offsets;
// First get all Zones in the table using the Offset
for (const QWindowsData &winData : windowsDataTable) {
for (const WindowsData &winData : windowsDataTable) {
if (winData.offsetFromUtc == offsetFromUtc) {
for (auto data = zoneStartForWindowsId(winData.windowsIdKey);
data != std::end(zoneDataTable) && data->windowsIdKey == winData.windowsIdKey;
Expand Down Expand Up @@ -727,7 +728,7 @@ QByteArray QTimeZonePrivate::ianaIdToWindowsId(const QByteArray &id)
// so we have to allocate here...
const auto idUtf8 = QString::fromUtf8(id);

for (const QZoneData &data : zoneDataTable) {
for (const ZoneData &data : zoneDataTable) {
for (auto l1 : data.ids()) {
if (l1 == idUtf8)
return toWindowsIdLiteral(data.windowsIdKey);
Expand Down Expand Up @@ -829,7 +830,7 @@ QUtcTimeZonePrivate::QUtcTimeZonePrivate()
QUtcTimeZonePrivate::QUtcTimeZonePrivate(const QByteArray &id)
{
// Look for the name in the UTC list, if found set the values
for (const QUtcData &data : utcDataTable) {
for (const UtcData &data : utcDataTable) {
if (isEntryInIanaList(id, data.id())) {
QString name = QString::fromUtf8(id);
init(id, data.offsetFromUtc, name, name, QLocale::AnyTerritory, name);
Expand Down Expand Up @@ -993,7 +994,7 @@ QByteArray QUtcTimeZonePrivate::systemTimeZoneId() const
bool QUtcTimeZonePrivate::isTimeZoneIdAvailable(const QByteArray &ianaId) const
{
// Only the zone IDs supplied by CLDR and recognized by constructor.
for (const QUtcData &data : utcDataTable) {
for (const UtcData &data : utcDataTable) {
if (isEntryInIanaList(ianaId, data.id()))
return true;
}
Expand All @@ -1008,7 +1009,7 @@ QList<QByteArray> QUtcTimeZonePrivate::availableTimeZoneIds() const
// Only the zone IDs supplied by CLDR and recognized by constructor.
QList<QByteArray> result;
result.reserve(std::size(utcDataTable));
for (const QUtcData &data : utcDataTable) {
for (const UtcData &data : utcDataTable) {
QByteArrayView id = data.id();
qsizetype cut;
while ((cut = id.indexOf(' ')) >= 0) {
Expand Down
23 changes: 13 additions & 10 deletions src/corelib/time/qtimezoneprivate_data_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

QT_BEGIN_NAMESPACE

namespace QtTimeZoneCldr {
/*
Recognized UTC-offset zones and CLDR-derived data on Windows IDs.
Expand All @@ -43,7 +44,7 @@ QT_BEGIN_NAMESPACE
of its last update and how to update it.
*/

struct QZoneData
struct ZoneData
{
// Keys (table is sorted in Windows ID, then on territory enum value):
quint16 windowsIdKey; // Windows ID sequence number
Expand All @@ -54,7 +55,7 @@ struct QZoneData
constexpr auto ids() const { return id().tokenize(u' '); } // Iterate IANA IDs
};

struct QWindowsData
struct WindowsData
{
// Table is sorted on key and this puts the windowsId()s in ascending order.
quint16 windowsIdKey; // Windows ID sequence number
Expand All @@ -66,7 +67,7 @@ struct QWindowsData
constexpr QByteArrayView ianaId() const; // Space-joined list of IANA IDs
};

struct QUtcData
struct UtcData
{
quint16 ianaIdIndex; // Index in ianaIdData of space-joined IANA IDs
qint32 offsetFromUtc; // Offset form UTC in seconds
Expand Down Expand Up @@ -107,7 +108,7 @@ struct QUtcData
*/

// Windows ID Key, Territory Enum, IANA ID Index
static constexpr QZoneData zoneDataTable[] = {
static constexpr ZoneData zoneDataTable[] = {
{ 1, 1, 0 }, // Afghanistan Standard Time / Afghanistan
{ 2, 248, 11 }, // Alaskan Standard Time / United States
{ 3, 248, 106 }, // Aleutian Standard Time / United States
Expand Down Expand Up @@ -478,7 +479,7 @@ static constexpr QZoneData zoneDataTable[] = {
};

// Windows ID Key, Windows ID Index, IANA ID Index, UTC Offset
static constexpr QWindowsData windowsDataTable[] = {
static constexpr WindowsData windowsDataTable[] = {
{ 1, 0, 0, 16200 }, // Afghanistan Standard Time
{ 2, 26, 7325,-32400 }, // Alaskan Standard Time
{ 3, 48, 106,-36000 }, // Aleutian Standard Time
Expand Down Expand Up @@ -621,7 +622,7 @@ static constexpr QWindowsData windowsDataTable[] = {
};

// IANA ID Index, UTC Offset
static constexpr QUtcData utcDataTable[] = {
static constexpr UtcData utcDataTable[] = {
{ 7788,-50400 }, // UTC-14:00
{ 7798,-46800 }, // UTC-13:00
{ 7808,-43200 }, // UTC-12:00
Expand Down Expand Up @@ -1383,13 +1384,15 @@ static constexpr char ianaIdData[] = {
};
// GENERATED PART ENDS HERE

constexpr QByteArrayView QWindowsData::windowsId() const { return windowsIdData + windowsIdIndex; }
constexpr QByteArrayView WindowsData::windowsId() const { return windowsIdData + windowsIdIndex; }
// Each of the following returns a space-joined sequence of IANA IDs:
constexpr QByteArrayView QWindowsData::ianaId() const { return ianaIdData + ianaIdIndex; }
constexpr QByteArrayView QUtcData::id() const { return ianaIdData + ianaIdIndex; }
constexpr QLatin1StringView QZoneData::id() const
constexpr QByteArrayView WindowsData::ianaId() const { return ianaIdData + ianaIdIndex; }
constexpr QByteArrayView UtcData::id() const { return ianaIdData + ianaIdIndex; }
constexpr QLatin1StringView ZoneData::id() const
{ return QLatin1StringView(ianaIdData + ianaIdIndex); }

} // namespace QtTimeZoneCldr

QT_END_NAMESPACE

#endif // QTIMEZONEPRIVATE_DATA_P_H
7 changes: 4 additions & 3 deletions util/locale_database/cldr2qtimezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ def write(self, out, name):
out('\n};\n')

class ZoneIdWriter (SourceFileEditor):
# All the output goes into namespace QtTimeZoneCldr.
def write(self, version, defaults, windowsIds):
self.__writeWarning(version)
windows, iana = self.__writeTables(self.writer.write, defaults, windowsIds)
Expand All @@ -284,7 +285,7 @@ def __writeTables(out, defaults, windowsIds):

# Write Windows/IANA table
out('// Windows ID Key, Territory Enum, IANA ID Index\n')
out('static constexpr QZoneData zoneDataTable[] = {\n')
out('static constexpr ZoneData zoneDataTable[] = {\n')
# Sorted by (Windows ID Key, territory enum)
for index, data in sorted(windowsIds.items()):
out(' {{ {:6d},{:6d},{:6d} }}, // {} / {}\n'.format(
Expand All @@ -295,7 +296,7 @@ def __writeTables(out, defaults, windowsIds):

# Write Windows ID key table
out('// Windows ID Key, Windows ID Index, IANA ID Index, UTC Offset\n')
out('static constexpr QWindowsData windowsDataTable[] = {\n')
out('static constexpr WindowsData windowsDataTable[] = {\n')
# Sorted by Windows ID key; sorting case-insensitively by
# Windows ID must give the same order.
winIdNames = [x.lower() for x, y in windowsIdList]
Expand All @@ -314,7 +315,7 @@ def __writeTables(out, defaults, windowsIds):
offsetMap[pair[1]] = offsetMap.get(pair[1], ()) + (pair[0],)
# Write UTC ID key table
out('// IANA ID Index, UTC Offset\n')
out('static constexpr QUtcData utcDataTable[] = {\n')
out('static constexpr UtcData utcDataTable[] = {\n')
for offset in sorted(offsetMap.keys()): # Sort so C++ can binary-chop.
names = offsetMap[offset];
out(' {{ {:6d},{:6d} }}, // {}\n'.format(
Expand Down

0 comments on commit b0f4bd7

Please sign in to comment.