Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtimezoneprivate_tz.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// Copyright (C) 2019 Crimson AS <info@crimson.no>
3// Copyright (C) 2013 John Layt <jlayt@kde.org>
4// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
5// Qt-Security score:critical reason:data-parser
6
7#include "qtimezone.h"
9#include "private/qlocale_tools_p.h"
10#include "private/qlocking_p.h"
11
12#include <QtCore/QDataStream>
13#include <QtCore/QDateTime>
14#include <QtCore/QDirListing>
15#include <QtCore/QDir>
16#include <QtCore/QFile>
17#include <QtCore/QCache>
18#include <QtCore/QMap>
19#include <QtCore/QMutex>
20
21#include <qdebug.h>
22#include <qplatformdefs.h>
23
24#include <algorithm>
25#include <memory>
26
27#include <errno.h>
28#include <limits.h>
29#ifndef Q_OS_INTEGRITY
30#include <sys/param.h> // to use MAXSYMLINKS constant
31#endif
32#include <unistd.h> // to use _SC_SYMLOOP_MAX constant
33
34QT_BEGIN_NAMESPACE
35
36using namespace Qt::StringLiterals;
37
38/*
39 Private
40
41 tz file implementation
42*/
43
45 // TODO: for zone1970.tab we'll need a set of territories:
48};
49
50// Define as a type as Q_GLOBAL_STATIC doesn't like it
52
53static bool isTzFile(const QString &name);
54
55// Open a named file under the zone info directory:
56static bool openZoneInfo(const QString &name, QFile *file)
57{
58 // At least on Linux / glibc (see man 3 tzset), $TZDIR overrides the system
59 // default location for zone info:
60 const QString tzdir = qEnvironmentVariable("TZDIR");
61 if (!tzdir.isEmpty()) {
62 file->setFileName(QDir(tzdir).filePath(name));
63 if (file->open(QIODevice::ReadOnly))
64 return true;
65 }
66 // Try modern system path first:
67 constexpr auto zoneShare = "/usr/share/zoneinfo/"_L1;
68 if (tzdir != zoneShare && tzdir != zoneShare.chopped(1)) {
69 file->setFileName(zoneShare + name);
70 if (file->open(QIODevice::ReadOnly))
71 return true;
72 }
73 // Fall back to legacy system path:
74 constexpr auto zoneLib = "/usr/lib/zoneinfo/"_L1;
75 if (tzdir != zoneLib && tzdir != zoneLib.chopped(1)) {
76 file->setFileName(zoneShare + name);
77 if (file->open(QIODevice::ReadOnly))
78 return true;
79 }
80 return false;
81}
82
83// Parse zone.tab table for territory information, read directories to ensure we
84// find all installed zones (many are omitted from zone.tab; even more from
85// zone1970.tab; see also QTBUG-64941).
87{
88 QFile tzif;
89 if (!openZoneInfo("zone.tab"_L1, &tzif))
90 return QTzTimeZoneHash();
91
92 QTzTimeZoneHash zonesHash;
93 QByteArray line;
94 while (tzif.readLineInto(&line)) {
95 QByteArrayView text = QByteArrayView(line).trimmed();
96 if (text.isEmpty() || text.at(0) == '#') // Ignore empty or comment
97 continue;
98 // Data rows are tab-separated columns Region, Coordinates, ID, Optional Comments
99 int cut = text.indexOf('\t');
100 if (Q_LIKELY(cut > 0)) {
101 QTzTimeZone zone;
102 // TODO: QLocale & friends could do this look-up without UTF8-conversion:
103 zone.territory = QLocalePrivate::codeToTerritory(QString::fromUtf8(text.first(cut)));
104 text = text.sliced(cut + 1);
105 cut = text.indexOf('\t');
106 if (Q_LIKELY(cut >= 0)) { // Skip over Coordinates, read ID and comment
107 text = text.sliced(cut + 1);
108 cut = text.indexOf('\t'); // < 0 if line has no comment
109 if (Q_LIKELY(cut)) {
110 const QByteArray id = (cut > 0 ? text.first(cut) : text).toByteArray();
111 if (cut > 0)
112 zone.comment = text.sliced(cut + 1).toByteArray();
113 zonesHash.insert(id, zone);
114 }
115 }
116 }
117 }
118
119 QString path = tzif.fileName();
120 const qsizetype cut = path.lastIndexOf(u'/');
121 Q_ASSERT(cut > 0);
122 path.truncate(cut + 1);
123 const qsizetype prefixLen = path.size();
124 for (const auto &info : QDirListing(path, QDirListing::IteratorFlag::Recursive)) {
125 if (!(info.isFile() || info.isSymLink()))
126 continue;
127 const QString infoAbsolutePath = info.absoluteFilePath();
128 const QString name = infoAbsolutePath.sliced(prefixLen);
129 // Two sub-directories containing (more or less) copies of the zoneinfo tree.
130 if (info.isDir() ? name == "posix"_L1 || name == "right"_L1
131 : name.startsWith("posix/"_L1) || name.startsWith("right/"_L1)) {
132 continue;
133 }
134 // We could filter out *.* and leapseconds instead of doing the
135 // isTzFile() check; in practice current (2023) zoneinfo/ contains only
136 // actual zone files and matches to that filter.
137 const QByteArray id = QFile::encodeName(name);
138 if (!zonesHash.contains(id) && isTzFile(infoAbsolutePath))
139 zonesHash.insert(id, QTzTimeZone());
140 }
141 return zonesHash;
142}
143
144// Hash of available system tz files as loaded by loadTzTimeZones()
145Q_GLOBAL_STATIC(const QTzTimeZoneHash, tzZones, loadTzTimeZones());
146
147/*
148 The following is copied and modified from tzfile.h which is in the public domain.
149 Copied as no compatibility guarantee and is never system installed.
150 See https://github.com/eggert/tz/blob/master/tzfile.h
151*/
152
153#define TZ_MAGIC "TZif"
154#define TZ_MAX_TIMES 1200
155#define TZ_MAX_TYPES 256 // Limited by what (unsigned char)'s can hold
156#define TZ_MAX_CHARS 50 // Maximum number of abbreviation characters
157#define TZ_MAX_LEAPS 50 // Maximum number of leap second corrections
158
159struct QTzHeader {
160 char tzh_magic[4]; // TZ_MAGIC
161 char tzh_version; // '\0' or '2' as of 2005
162 char tzh_reserved[15]; // reserved--must be zero
163 quint32 tzh_ttisgmtcnt; // number of trans. time flags
164 quint32 tzh_ttisstdcnt; // number of trans. time flags
165 quint32 tzh_leapcnt; // number of leap seconds
166 quint32 tzh_timecnt; // number of transition times
167 quint32 tzh_typecnt; // number of local time types
168 quint32 tzh_charcnt; // number of abbr. chars
169};
170
172 qint64 tz_time; // Transition time
173 quint8 tz_typeind; // Type Index
174};
176
177struct QTzType {
178 int tz_gmtoff; // UTC offset in seconds
179 bool tz_isdst; // Is DST
180 quint8 tz_abbrind; // abbreviation list index
181};
183
184static bool isTzFile(const QString &name)
185{
186 QFile file(name);
187 return file.open(QFile::ReadOnly) && file.read(strlen(TZ_MAGIC)) == TZ_MAGIC;
188}
189
190// TZ File parsing
191
192static QTzHeader parseTzHeader(QDataStream &ds, bool *ok)
193{
194 QTzHeader hdr;
195 quint8 ch;
196 *ok = false;
197
198 // Parse Magic, 4 bytes
199 ds.readRawData(hdr.tzh_magic, 4);
200
201 if (memcmp(hdr.tzh_magic, TZ_MAGIC, 4) != 0 || ds.status() != QDataStream::Ok)
202 return hdr;
203
204 // Parse Version, 1 byte, before 2005 was '\0', since 2005 a '2', since 2013 a '3'
205 ds >> ch;
206 hdr.tzh_version = ch;
207 if (ds.status() != QDataStream::Ok
208 || (hdr.tzh_version != '2' && hdr.tzh_version != '\0' && hdr.tzh_version != '3')) {
209 return hdr;
210 }
211
212 // Parse reserved space, 15 bytes
213 ds.readRawData(hdr.tzh_reserved, 15);
214 if (ds.status() != QDataStream::Ok)
215 return hdr;
216
217 // Parse rest of header, 6 x 4-byte transition counts
218 ds >> hdr.tzh_ttisgmtcnt >> hdr.tzh_ttisstdcnt >> hdr.tzh_leapcnt >> hdr.tzh_timecnt
219 >> hdr.tzh_typecnt >> hdr.tzh_charcnt;
220
221 // Check defined maximums
222 if (ds.status() != QDataStream::Ok
223 || hdr.tzh_timecnt > TZ_MAX_TIMES
224 || hdr.tzh_typecnt > TZ_MAX_TYPES
225 || hdr.tzh_charcnt > TZ_MAX_CHARS
226 || hdr.tzh_leapcnt > TZ_MAX_LEAPS
227 || hdr.tzh_ttisgmtcnt > hdr.tzh_typecnt
228 || hdr.tzh_ttisstdcnt > hdr.tzh_typecnt) {
229 return hdr;
230 }
231
232 *ok = true;
233 return hdr;
234}
235
236static QList<QTzTransition> parseTzTransitions(QDataStream &ds, int tzh_timecnt, bool longTran)
237{
238 QList<QTzTransition> transitions(tzh_timecnt);
239
240 if (longTran) {
241 // Parse tzh_timecnt x 8-byte transition times
242 for (int i = 0; i < tzh_timecnt && ds.status() == QDataStream::Ok; ++i) {
243 ds >> transitions[i].tz_time;
244 if (ds.status() != QDataStream::Ok)
245 transitions.resize(i);
246 }
247 } else {
248 // Parse tzh_timecnt x 4-byte transition times
249 qint32 val;
250 for (int i = 0; i < tzh_timecnt && ds.status() == QDataStream::Ok; ++i) {
251 ds >> val;
252 transitions[i].tz_time = val;
253 if (ds.status() != QDataStream::Ok)
254 transitions.resize(i);
255 }
256 }
257
258 // Parse tzh_timecnt x 1-byte transition type index
259 for (int i = 0; i < tzh_timecnt && ds.status() == QDataStream::Ok; ++i) {
260 quint8 typeind;
261 ds >> typeind;
262 if (ds.status() == QDataStream::Ok)
263 transitions[i].tz_typeind = typeind;
264 }
265
266 return transitions;
267}
268
269static QList<QTzType> parseTzTypes(QDataStream &ds, int tzh_typecnt)
270{
271 QList<QTzType> types(tzh_typecnt);
272
273 // Parse tzh_typecnt x transition types
274 for (int i = 0; i < tzh_typecnt && ds.status() == QDataStream::Ok; ++i) {
275 QTzType &type = types[i];
276 // Parse UTC Offset, 4 bytes
277 ds >> type.tz_gmtoff;
278 // Parse Is DST flag, 1 byte
279 if (ds.status() == QDataStream::Ok)
280 ds >> type.tz_isdst;
281 // Parse Abbreviation Array Index, 1 byte
282 if (ds.status() == QDataStream::Ok)
283 ds >> type.tz_abbrind;
284 if (ds.status() != QDataStream::Ok)
285 types.resize(i);
286 }
287
288 return types;
289}
290
291static QMap<int, QByteArray> parseTzAbbreviations(QDataStream &ds, int tzh_charcnt, const QList<QTzType> &types)
292{
293 // Parse the abbreviation list which is tzh_charcnt long with '\0' separated strings. The
294 // QTzType.tz_abbrind index points to the first char of the abbreviation in the array, not the
295 // occurrence in the list. It can also point to a partial string so we need to use the actual typeList
296 // index values when parsing. By using a map with tz_abbrind as ordered key we get both index
297 // methods in one data structure and can convert the types afterwards.
298 QMap<int, QByteArray> map;
299 quint8 ch;
300 QByteArray input;
301 // First parse the full abbrev string
302 for (int i = 0; i < tzh_charcnt && ds.status() == QDataStream::Ok; ++i) {
303 ds >> ch;
304 if (ds.status() == QDataStream::Ok)
305 input.append(char(ch));
306 else
307 return map;
308 }
309 // Then extract all the substrings pointed to by types
310 for (const QTzType &type : types) {
311 QByteArray abbrev;
312 for (int i = type.tz_abbrind; input.at(i) != '\0'; ++i)
313 abbrev.append(input.at(i));
314 // Have reached end of an abbreviation, so add to map
315 map[type.tz_abbrind] = abbrev;
316 }
317 return map;
318}
319
320static void parseTzLeapSeconds(QDataStream &ds, int tzh_leapcnt, bool longTran)
321{
322 // Parse tzh_leapcnt x pairs of leap seconds
323 // We don't use leap seconds, so only read and don't store
324 qint32 val;
325 if (longTran) {
326 // v2 file format, each entry is 12 bytes long
327 qint64 time;
328 for (int i = 0; i < tzh_leapcnt && ds.status() == QDataStream::Ok; ++i) {
329 // Parse Leap Occurrence Time, 8 bytes
330 ds >> time;
331 // Parse Leap Seconds To Apply, 4 bytes
332 if (ds.status() == QDataStream::Ok)
333 ds >> val;
334 }
335 } else {
336 // v0 file format, each entry is 8 bytes long
337 for (int i = 0; i < tzh_leapcnt && ds.status() == QDataStream::Ok; ++i) {
338 // Parse Leap Occurrence Time, 4 bytes
339 ds >> val;
340 // Parse Leap Seconds To Apply, 4 bytes
341 if (ds.status() == QDataStream::Ok)
342 ds >> val;
343 }
344 }
345}
346
347static QList<QTzType> parseTzIndicators(QDataStream &ds, const QList<QTzType> &types, int tzh_ttisstdcnt,
348 int tzh_ttisgmtcnt)
349{
350 QList<QTzType> result = types;
351 bool temp;
352 /*
353 Scan and discard indicators.
354
355 These indicators are only of use (by the date program) when "handling
356 POSIX-style time zone environment variables". The flags here say whether
357 the *specification* of the zone gave the time in UTC, local standard time
358 or local wall time; but whatever was specified has been digested for us,
359 already, by the zone-info compiler (zic), so that the tz_time values read
360 from the file (by parseTzTransitions) are all in UTC.
361 */
362
363 // Scan tzh_ttisstdcnt x 1-byte standard/wall indicators
364 for (int i = 0; i < tzh_ttisstdcnt && ds.status() == QDataStream::Ok; ++i)
365 ds >> temp;
366
367 // Scan tzh_ttisgmtcnt x 1-byte UTC/local indicators
368 for (int i = 0; i < tzh_ttisgmtcnt && ds.status() == QDataStream::Ok; ++i)
369 ds >> temp;
370
371 return result;
372}
373
374static QByteArray parseTzPosixRule(QDataStream &ds)
375{
376 // Parse POSIX rule, variable length '\n' enclosed
377 QByteArray rule;
378
379 quint8 ch;
380 ds >> ch;
381 if (ch != '\n' || ds.status() != QDataStream::Ok)
382 return rule;
383 ds >> ch;
384 while (ch != '\n' && ds.status() == QDataStream::Ok) {
385 rule.append((char)ch);
386 ds >> ch;
387 }
388
389 return rule;
390}
391
392static QDate calculateDowDate(int year, int month, int dayOfWeek, int week)
393{
394 if (dayOfWeek == 0) // Sunday; we represent it as 7, POSIX uses 0
395 dayOfWeek = 7;
396 else if (dayOfWeek & ~7 || month < 1 || month > 12 || week < 1 || week > 5)
397 return QDate();
398
399 QDate date(year, month, 1);
400 int startDow = date.dayOfWeek();
401 if (startDow <= dayOfWeek)
402 date = date.addDays(dayOfWeek - startDow - 7);
403 else
404 date = date.addDays(dayOfWeek - startDow);
405 date = date.addDays(week * 7);
406 while (date.month() != month)
407 date = date.addDays(-7);
408 return date;
409}
410
411static QDate calculatePosixDate(QLatin1StringView dateRule, int year)
412{
413 Q_ASSERT(!dateRule.isEmpty());
414 bool ok;
415 // Can start with M, J, or a digit
416 if (dateRule.at(0) == 'M') {
417 // nth week in month format "Mmonth.week.dow"
418 const auto dateParts = dateRule.tokenize(u'.');
419 auto token = dateParts.begin();
420 Q_ASSERT(token != dateParts.end());
421 Q_ASSERT(!token->isEmpty()); // the 'M' is its [0].
422 const int month = token->sliced(1).toInt(&ok);
423 if (ok && ++token != dateParts.end()) {
424 const int week = token->toInt(&ok);
425 if (ok && ++token != dateParts.end()) {
426 const int dow = token->toInt(&ok);
427 if (ok)
428 return calculateDowDate(year, month, dow, week);
429 }
430 }
431 } else if (dateRule.at(0) == 'J') {
432 // Day of Year 1...365, ignores Feb 29.
433 // So March always starts on day 60.
434 int doy = dateRule.sliced(1).toInt(&ok);
435 if (ok && doy > 0 && doy < 366) {
436 // Subtract 1 because we're adding days *after* the first of
437 // January, unless it's after February in a leap year, when the leap
438 // day cancels that out:
439 if (!QDate::isLeapYear(year) || doy < 60)
440 --doy;
441 return QDate(year, 1, 1).addDays(doy);
442 }
443 } else {
444 // Day of Year 0...365, includes Feb 29
445 int doy = dateRule.toInt(&ok);
446 if (ok && doy >= 0 && doy < 366)
447 return QDate(year, 1, 1).addDays(doy);
448 }
449 return QDate();
450}
451
452// returns the time in seconds, INT_MIN if we failed to parse
453static int parsePosixTime(const char *begin, const char *end)
454{
455 // Format "hh[:mm[:ss]]"
456 int hour, min = 0, sec = 0;
457
458 const int maxHour = 137; // POSIX's extended range.
459 auto r = qstrntoll(begin, end - begin, 10);
460 hour = r.result;
461 if (!r.ok() || hour < -maxHour || hour > maxHour || r.used > 2)
462 return INT_MIN;
463 begin += r.used;
464 if (begin < end && *begin == ':') {
465 // minutes
466 ++begin;
467 r = qstrntoll(begin, end - begin, 10);
468 min = r.result;
469 if (!r.ok() || min < 0 || min > 59 || r.used > 2)
470 return INT_MIN;
471
472 begin += r.used;
473 if (begin < end && *begin == ':') {
474 // seconds
475 ++begin;
476 r = qstrntoll(begin, end - begin, 10);
477 sec = r.result;
478 if (!r.ok() || sec < 0 || sec > 59 || r.used > 2)
479 return INT_MIN;
480 begin += r.used;
481 }
482 }
483
484 // we must have consumed everything
485 if (begin != end)
486 return INT_MIN;
487
488 return (hour * 60 + min) * 60 + sec;
489}
490
491static int parsePosixTransitionTime(QLatin1StringView timeRule)
492{
493 return parsePosixTime(timeRule.begin(), timeRule.end());
494}
495
496static int parsePosixOffset(const char *begin, const char *end)
497{
498 // Format "[+|-]hh[:mm[:ss]]"
499 // note that the sign is inverted because POSIX counts in hours West of GMT
500 bool negate = true;
501 if (*begin == '+') {
502 ++begin;
503 } else if (*begin == '-') {
504 negate = false;
505 ++begin;
506 }
507
508 int value = parsePosixTime(begin, end);
509 if (value == INT_MIN)
510 return value;
511 return negate ? -value : value;
512}
513
514static inline bool asciiIsLetter(char ch)
515{
516 ch |= 0x20; // lowercases if it is a letter, otherwise just corrupts ch
517 return ch >= 'a' && ch <= 'z';
518}
519
520namespace {
521
522struct PosixZone // TODO: QTBUG-112006 - make this cross-platform.
523{
524 enum {
525 InvalidOffset = INT_MIN,
526 };
527
528 QString name;
529 int offset = InvalidOffset;
530 bool hasValidOffset() const noexcept { return offset != InvalidOffset; }
531 QTimeZonePrivate::Data dataAt(qint64 when)
532 {
533 Q_ASSERT(hasValidOffset());
534 return QTimeZonePrivate::Data(name, when, offset, offset);
535 }
536 QTimeZonePrivate::Data dataAtOffset(qint64 when, int standard)
537 {
538 Q_ASSERT(hasValidOffset());
539 return QTimeZonePrivate::Data(name, when, offset, standard);
540 }
541
542 static PosixZone parse(const char *&pos, const char *end);
543};
544
545} // unnamed namespace
546
547// Returns the zone name, the offset (in seconds) and advances \a begin to
548// where the parsing ended. Returns a zone of INT_MIN in case an offset
549// couldn't be read.
550PosixZone PosixZone::parse(const char *&pos, const char *end)
551{
552 static const char offsetChars[] = "0123456789:";
553
554 const char *nameBegin = pos;
555 const char *nameEnd;
556 Q_ASSERT(pos < end);
557
558 if (*pos == '<') {
559 ++nameBegin; // skip the '<'
560 nameEnd = nameBegin;
561 while (nameEnd < end && *nameEnd != '>') {
562 // POSIX says only alphanumeric, but we allow anything
563 ++nameEnd;
564 }
565 pos = nameEnd + 1; // skip the '>'
566 } else {
567 nameEnd = nameBegin;
568 while (nameEnd < end && asciiIsLetter(*nameEnd))
569 ++nameEnd;
570 pos = nameEnd;
571 }
572 if (nameEnd - nameBegin < 3)
573 return {}; // name must be at least 3 characters long
574
575 // zone offset, form [+-]hh:mm:ss
576 const char *zoneBegin = pos;
577 const char *zoneEnd = pos;
578 if (zoneEnd < end && (zoneEnd[0] == '+' || zoneEnd[0] == '-'))
579 ++zoneEnd;
580 while (zoneEnd < end) {
581 if (strchr(offsetChars, char(*zoneEnd)) == nullptr)
582 break;
583 ++zoneEnd;
584 }
585
586 QString name = QString::fromUtf8(nameBegin, nameEnd - nameBegin);
587 const int offset = zoneEnd > zoneBegin ? parsePosixOffset(zoneBegin, zoneEnd) : InvalidOffset;
588 pos = zoneEnd;
589 // UTC+hh:mm:ss or GMT+hh:mm:ss should be read as offsets from UTC, not as a
590 // POSIX rule naming a zone as UTC or GMT and specifying a non-zero offset.
591 if (offset != 0 && (name =="UTC"_L1 || name == "GMT"_L1))
592 return {};
593 return {std::move(name), offset};
594}
595
596/* Parse and check a POSIX rule.
597
598 By default a simple zone abbreviation with no offset information is accepted.
599 Set \a requireOffset to \c true to require that there be offset data present.
600*/
601static auto validatePosixRule(QByteArrayView posixRule, bool requireOffset = false)
602{
603 // Format is described here:
604 // https://sourceware.org/glibc/manual/latest/html_node/Proleptic-TZ.html
605 // See also calculatePosixTransition()'s reference.
606 QLatin1StringView zoneinfo, startDate, endDate;
607 int parts = 1;
608 {
609 const auto tokens = QLatin1StringView(posixRule).tokenize(u',');
610 auto token = tokens.begin();
611 Q_ASSERT(token != tokens.end());
612 zoneinfo = token->trimmed();
613 if (++token != tokens.end()) {
614 ++parts;
615 startDate = *token;
616 if (++token != tokens.end()) {
617 ++parts;
618 endDate = *token;
619 if (++token != tokens.end())
620 ++parts; // So we know if we have too many
621 }
622 }
623 }
624 const struct { bool isValid, hasDst; } fail{false, false}, good{true, parts > 1};
625 if (zoneinfo.isEmpty())
626 return fail;
627
628 const char *begin = zoneinfo.begin();
629 {
630 // Updates begin to point after the name and offset it parses:
631 const auto posix = PosixZone::parse(begin, zoneinfo.end());
632 if (posix.name.isEmpty())
633 return fail;
634 if (requireOffset && !posix.hasValidOffset())
635 return fail;
636 }
637
638 if (good.hasDst) {
639 if (begin >= zoneinfo.end())
640 return fail;
641 // Expect a second name (and optional offset) after the first:
642 if (PosixZone::parse(begin, zoneinfo.end()).name.isEmpty())
643 return fail;
644 }
645 if (begin < zoneinfo.end())
646 return fail;
647
648 if (good.hasDst) {
649 if (parts != 3 || startDate.isEmpty() || endDate.isEmpty())
650 return fail;
651 for (int i = 0; i < 2; ++i) {
652 const auto tokens = (i ? endDate : startDate).tokenize(u'/');
653 auto token = tokens.begin();
654 Q_ASSERT(token != tokens.end());
655 if (!calculatePosixDate(*token, 1972).isValid())
656 return fail;
657 if (++token != tokens.end() && parsePosixTime(token->begin(), token->end()) == INT_MIN)
658 return fail;
659 }
660 }
661 return good;
662}
663
664static QList<QTimeZonePrivate::Data> calculatePosixTransitions(const QByteArray &posixRule,
665 int startYear, int endYear,
666 qint64 lastTranMSecs)
667{
668 QList<QTimeZonePrivate::Data> result;
669
670 // POSIX Format is like "TZ=CST6CDT,M3.2.0/2:00:00,M11.1.0/2:00:00"
671 // i.e. "std offset dst [offset],start[/time],end[/time]"
672 // See the section about TZ at the end of
673 // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html
674 // and the link in validatePosixRule(), above.
675 const auto tokens = QLatin1String(posixRule).tokenize(u',');
676 auto token = tokens.begin();
677 Q_ASSERT(token != tokens.end());
678
679 PosixZone stdZone, dstZone;
680 QLatin1StringView zoneText = *token;
681 {
682 QLatin1StringView zoneinfo = zoneText.trimmed();
683 const char *begin = zoneinfo.begin();
684
685 stdZone = PosixZone::parse(begin, zoneinfo.end());
686 if (!stdZone.hasValidOffset()) {
687 stdZone.offset = 0; // reset to UTC if we failed to parse
688 } else if (begin < zoneinfo.end()) {
689 dstZone = PosixZone::parse(begin, zoneinfo.end());
690 if (!dstZone.hasValidOffset()) {
691 // if the dst offset isn't provided, it is 1 hour ahead of the standard offset
692 dstZone.offset = stdZone.offset + (60 * 60);
693 }
694 }
695 }
696
697 // If only the name part, or no DST specified, then no transitions
698 if (++token == tokens.end() || !dstZone.hasValidOffset()) {
699 result.emplaceBack(
700 stdZone.name.isEmpty() ? QString(zoneText) : stdZone.name,
701 lastTranMSecs, stdZone.offset, stdZone.offset);
702 return result;
703 }
704 QLatin1StringView dstRule = *token;
705 if (++token == tokens.end() || dstRule.isEmpty() || token->isEmpty())
706 return result; // Malformed.
707 QLatin1StringView stdRule = *token;
708
709 // Get the std to dst transition details
710 const int twoOClock = 7200; // Default transition time, when none specified
711 const auto dstParts = dstRule.tokenize(u'/');
712 auto subtok = dstParts.begin();
713 Q_ASSERT(subtok != dstParts.end());
714 QLatin1StringView dstDateRule = *subtok;
715 const int dstTime = ++subtok == dstParts.end() ? twoOClock : parsePosixTransitionTime(*subtok);
716
717 // Get the dst to std transition details
718 const auto stdParts = stdRule.tokenize(u'/');
719 subtok = stdParts.begin();
720 Q_ASSERT(subtok != stdParts.end());
721 QLatin1StringView stdDateRule = *subtok;
722 const int stdTime = ++subtok == stdParts.end() ? twoOClock : parsePosixTransitionTime(*subtok);
723
724 if (dstDateRule.isEmpty() || stdDateRule.isEmpty() || dstTime == INT_MIN || stdTime == INT_MIN)
725 return result; // Malformed.
726
727 // Limit year to the range QDateTime can represent:
728 const int minYear = int(QDateTime::YearRange::First);
729 const int maxYear = int(QDateTime::YearRange::Last);
730 startYear = qBound(minYear, startYear, maxYear);
731 endYear = qBound(minYear, endYear, maxYear);
732 Q_ASSERT(startYear <= endYear);
733
734 for (int year = startYear; year <= endYear; ++year) {
735 // Note: std and dst, despite being QDateTime(,, UTC), have the
736 // date() and time() of the *zone*'s description of the transition
737 // moments; the atMSecsSinceEpoch values computed from them are
738 // correctly offse to be UTC-based.
739
740 // Transition to daylight-saving time:
741 QDateTime dst(calculatePosixDate(dstDateRule, year)
742 .startOfDay(QTimeZone::UTC).addSecs(dstTime));
743 auto saving = dstZone.dataAtOffset(dst.toMSecsSinceEpoch() - stdZone.offset * 1000,
744 stdZone.offset);
745 // Transition to standard time:
746 QDateTime std(calculatePosixDate(stdDateRule, year)
747 .startOfDay(QTimeZone::UTC).addSecs(stdTime));
748 auto standard = stdZone.dataAt(std.toMSecsSinceEpoch() - dstZone.offset * 1000);
749
750 if (year == startYear) {
751 // Handle the special case of fixed state, which may be represented
752 // by fake transitions at start and end of each year:
753 if (saving.atMSecsSinceEpoch < standard.atMSecsSinceEpoch) {
754 if (dst <= QDate(year, 1, 1).startOfDay(QTimeZone::UTC)
755 && std >= QDate(year, 12, 31).endOfDay(QTimeZone::UTC)) {
756 // Permanent DST:
757 saving.atMSecsSinceEpoch = lastTranMSecs;
758 result.emplaceBack(std::move(saving));
759 return result;
760 }
761 } else {
762 if (std <= QDate(year, 1, 1).startOfDay(QTimeZone::UTC)
763 && dst >= QDate(year, 12, 31).endOfDay(QTimeZone::UTC)) {
764 // Permanent Standard time, perversely described:
765 standard.atMSecsSinceEpoch = lastTranMSecs;
766 result.emplaceBack(std::move(standard));
767 return result;
768 }
769 }
770 }
771
772 const bool useStd = std.isValid() && std.date().year() == year && !stdZone.name.isEmpty();
773 const bool useDst = dst.isValid() && dst.date().year() == year && !dstZone.name.isEmpty();
774 if (useStd && useDst) {
775 if (dst < std) {
776 result.emplaceBack(std::move(saving));
777 result.emplaceBack(std::move(standard));
778 } else {
779 result.emplaceBack(std::move(standard));
780 result.emplaceBack(std::move(saving));
781 }
782 } else if (useStd) {
783 result.emplaceBack(std::move(standard));
784 } else if (useDst) {
785 result.emplaceBack(std::move(saving));
786 }
787 }
788 return result;
789}
790
791// Create the system default time zone
792QTzTimeZonePrivate::QTzTimeZonePrivate()
793 : QTzTimeZonePrivate(staticSystemTimeZoneId())
794{
795}
796
797QTzTimeZonePrivate::~QTzTimeZonePrivate()
798{
799}
800
801QTzTimeZonePrivate *QTzTimeZonePrivate::clone() const
802{
803 return new QTzTimeZonePrivate(*this);
804}
805
807{
808public:
809 QTzTimeZoneCacheEntry fetchEntry(const QByteArray &ianaId);
810 // Set max cost to hold whole IANA DB, since some code iterates it !
812
813private:
814 static QTzTimeZoneCacheEntry findEntry(const QByteArray &ianaId);
815 QCache<QByteArray, QTzTimeZoneCacheEntry> m_cache;
816 QMutex m_mutex;
817};
818
819QTzTimeZoneCacheEntry QTzTimeZoneCache::findEntry(const QByteArray &ianaId)
820{
821 QTzTimeZoneCacheEntry ret;
822 QFile tzif;
823 if (ianaId.isEmpty()) {
824 // Open system tz
825 tzif.setFileName(QStringLiteral("/etc/localtime"));
826 if (!tzif.open(QIODevice::ReadOnly))
827 return ret;
828 } else if (!openZoneInfo(QString::fromLocal8Bit(ianaId), &tzif)) {
829 // ianaId may be a POSIX rule, taken from $TZ or /etc/TZ
830 auto check = validatePosixRule(ianaId);
831 if (check.isValid) {
832 ret.m_hasDst = check.hasDst;
833 ret.m_posixRule = ianaId;
834 }
835 return ret;
836 }
837
838 QDataStream ds(&tzif);
839
840 // Parse the old version block of data
841 bool ok = false;
842 QByteArray posixRule;
843 QTzHeader hdr = parseTzHeader(ds, &ok);
844 if (!ok || ds.status() != QDataStream::Ok)
845 return ret;
846 QList<QTzTransition> tranList = parseTzTransitions(ds, hdr.tzh_timecnt, false);
847 if (ds.status() != QDataStream::Ok)
848 return ret;
849 QList<QTzType> typeList = parseTzTypes(ds, hdr.tzh_typecnt);
850 if (ds.status() != QDataStream::Ok)
851 return ret;
852 QMap<int, QByteArray> abbrevMap = parseTzAbbreviations(ds, hdr.tzh_charcnt, typeList);
853 if (ds.status() != QDataStream::Ok)
854 return ret;
855 parseTzLeapSeconds(ds, hdr.tzh_leapcnt, false);
856 if (ds.status() != QDataStream::Ok)
857 return ret;
858 typeList = parseTzIndicators(ds, typeList, hdr.tzh_ttisstdcnt, hdr.tzh_ttisgmtcnt);
859 if (ds.status() != QDataStream::Ok)
860 return ret;
861
862 // If version 2 then parse the second block of data
863 if (hdr.tzh_version == '2' || hdr.tzh_version == '3') {
864 ok = false;
865 QTzHeader hdr2 = parseTzHeader(ds, &ok);
866 if (!ok || ds.status() != QDataStream::Ok)
867 return ret;
868 tranList = parseTzTransitions(ds, hdr2.tzh_timecnt, true);
869 if (ds.status() != QDataStream::Ok)
870 return ret;
871 typeList = parseTzTypes(ds, hdr2.tzh_typecnt);
872 if (ds.status() != QDataStream::Ok)
873 return ret;
874 abbrevMap = parseTzAbbreviations(ds, hdr2.tzh_charcnt, typeList);
875 if (ds.status() != QDataStream::Ok)
876 return ret;
877 parseTzLeapSeconds(ds, hdr2.tzh_leapcnt, true);
878 if (ds.status() != QDataStream::Ok)
879 return ret;
880 typeList = parseTzIndicators(ds, typeList, hdr2.tzh_ttisstdcnt, hdr2.tzh_ttisgmtcnt);
881 if (ds.status() != QDataStream::Ok)
882 return ret;
883 posixRule = parseTzPosixRule(ds);
884 if (ds.status() != QDataStream::Ok)
885 return ret;
886 }
887 // Translate the TZ file's raw data into our internal form:
888
889 if (!posixRule.isEmpty()) {
890 auto check = validatePosixRule(posixRule);
891 if (!check.isValid) // We got a POSIX rule, but it was malformed:
892 return ret;
893 ret.m_posixRule = posixRule;
894 ret.m_hasDst = check.hasDst;
895 }
896
897 // Translate the array-index-based tz_abbrind into list index
898 const int size = abbrevMap.size();
899 ret.m_abbreviations.clear();
900 ret.m_abbreviations.reserve(size);
901 QList<int> abbrindList;
902 abbrindList.reserve(size);
903 for (auto it = abbrevMap.cbegin(), end = abbrevMap.cend(); it != end; ++it) {
904 ret.m_abbreviations.append(it.value());
905 abbrindList.append(it.key());
906 }
907 // Map tz_abbrind from map's keys (as initially read) to abbrindList's
908 // indices (used hereafter):
909 for (int i = 0; i < typeList.size(); ++i)
910 typeList[i].tz_abbrind = abbrindList.indexOf(typeList.at(i).tz_abbrind);
911
912 // TODO: is typeList[0] always the "before zones" data ? It seems to be ...
913 if (typeList.size())
914 ret.m_preZoneRule = { typeList.at(0).tz_gmtoff, 0, typeList.at(0).tz_abbrind };
915
916 // Offsets are stored as total offset, want to know separate UTC and DST offsets
917 // so find the first non-dst transition to use as base UTC Offset
918 int utcOffset = ret.m_preZoneRule.stdOffset;
919 for (const QTzTransition &tran : std::as_const(tranList)) {
920 if (!typeList.at(tran.tz_typeind).tz_isdst) {
921 utcOffset = typeList.at(tran.tz_typeind).tz_gmtoff;
922 break;
923 }
924 }
925
926 // Now for each transition time calculate and store our rule:
927 const int tranCount = tranList.size();
928 ret.m_tranTimes.reserve(tranCount);
929 // The DST offset when in effect: usually stable, usually an hour:
930 int lastDstOff = 3600;
931 for (int i = 0; i < tranCount; i++) {
932 const QTzTransition &tz_tran = tranList.at(i);
933 QTzTransitionTime tran;
934 QTzTransitionRule rule;
935 const QTzType tz_type = typeList.at(tz_tran.tz_typeind);
936
937 // Calculate the associated Rule
938 if (!tz_type.tz_isdst) {
939 utcOffset = tz_type.tz_gmtoff;
940 } else if (Q_UNLIKELY(tz_type.tz_gmtoff != utcOffset + lastDstOff)) {
941 /*
942 This might be a genuine change in DST offset, but could also be
943 DST starting at the same time as the standard offset changed. See
944 if DST's end gives a more plausible utcOffset (i.e. one closer to
945 the last we saw, or a simple whole hour):
946 */
947 // Standard offset inferred from net offset and expected DST offset:
948 const int inferStd = tz_type.tz_gmtoff - lastDstOff; // != utcOffset
949 for (int j = i + 1; j < tranCount; j++) {
950 const QTzType new_type = typeList.at(tranList.at(j).tz_typeind);
951 if (!new_type.tz_isdst) {
952 const int newUtc = new_type.tz_gmtoff;
953 if (newUtc == utcOffset) {
954 // DST-end can't help us, avoid lots of messy checks.
955 // else: See if the end matches the familiar DST offset:
956 } else if (newUtc == inferStd) {
957 utcOffset = newUtc;
958 // else: let either end shift us to one hour as DST offset:
959 } else if (tz_type.tz_gmtoff - 3600 == utcOffset) {
960 // Start does it
961 } else if (tz_type.tz_gmtoff - 3600 == newUtc) {
962 utcOffset = newUtc; // End does it
963 // else: prefer whichever end gives DST offset closer to
964 // last, but consider any offset > 0 "closer" than any <= 0:
965 } else if (newUtc < tz_type.tz_gmtoff
966 ? (utcOffset >= tz_type.tz_gmtoff
967 || qAbs(newUtc - inferStd) < qAbs(utcOffset - inferStd))
968 : (utcOffset >= tz_type.tz_gmtoff
969 && qAbs(newUtc - inferStd) < qAbs(utcOffset - inferStd))) {
970 utcOffset = newUtc;
971 }
972 break;
973 }
974 }
975 lastDstOff = tz_type.tz_gmtoff - utcOffset;
976 }
977 rule.stdOffset = utcOffset;
978 rule.dstOffset = tz_type.tz_gmtoff - utcOffset;
979 rule.abbreviationIndex = tz_type.tz_abbrind;
980
981 // If the rule already exist then use that, otherwise add it
982 int ruleIndex = ret.m_tranRules.indexOf(rule);
983 if (ruleIndex == -1) {
984 if (rule.dstOffset != 0)
985 ret.m_hasDst = true;
986 tran.ruleIndex = ret.m_tranRules.size();
987 ret.m_tranRules.append(rule);
988 } else {
989 tran.ruleIndex = ruleIndex;
990 }
991
992 tran.atMSecsSinceEpoch = tz_tran.tz_time * 1000;
993 ret.m_tranTimes.append(tran);
994 }
995
996 return ret;
997}
998
1000{
1001 QMutexLocker locker(&m_mutex);
1002
1003 // search the cache...
1004 QTzTimeZoneCacheEntry *obj = m_cache.object(ianaId);
1005 if (obj)
1006 return *obj;
1007
1008 // ... or build a new entry from scratch
1009
1010 locker.unlock(); // don't parse files under mutex lock
1011
1012 QTzTimeZoneCacheEntry ret = findEntry(ianaId);
1013 if (ret.m_tranTimes.isEmpty() && ret.m_posixRule.isEmpty())
1014 return ret; // Don't use up cache space with invalid IDs.
1015
1016 auto ptr = std::make_unique<QTzTimeZoneCacheEntry>(ret);
1017
1018 locker.relock();
1019 m_cache.insert(ianaId, ptr.release()); // may overwrite if another thread was faster
1020 locker.unlock();
1021
1022 return ret;
1023}
1024
1025// Create a named time zone
1026QTzTimeZonePrivate::QTzTimeZonePrivate(const QByteArray &ianaId)
1027{
1028 if (!isTimeZoneIdAvailable(ianaId)) // Avoid pointlessly creating cache entries
1029 return;
1030 static QTzTimeZoneCache tzCache;
1031 auto entry = tzCache.fetchEntry(ianaId);
1032 if (entry.m_tranTimes.isEmpty() && entry.m_posixRule.isEmpty())
1033 return; // Invalid after all !
1034
1035 cached_data = std::move(entry);
1036 m_id = ianaId;
1037 // Avoid empty ID, if we have an abbreviation to use instead
1038 if (m_id.isEmpty()) {
1039 // This can only happen for the system zone, when we've read the
1040 // contents of /etc/localtime because it wasn't a symlink.
1041 // TODO: use CLDR generic abbreviation for the zone.
1042 m_id = abbreviation(QDateTime::currentMSecsSinceEpoch()).toUtf8();
1043 }
1044}
1045
1046QLocale::Territory QTzTimeZonePrivate::territory() const
1047{
1048 return tzZones->value(m_id).territory;
1049}
1050
1051QString QTzTimeZonePrivate::comment() const
1052{
1053 return QString::fromUtf8(tzZones->value(m_id).comment);
1054}
1055
1056QString QTzTimeZonePrivate::displayName(QTimeZone::TimeType timeType,
1057 QTimeZone::NameType nameType,
1058 const QLocale &locale) const
1059{
1060 // TZ only provides C-locale abbreviations and offset:
1061 if (nameType != QTimeZone::LongName && isDataLocale(locale)) {
1062 Data tran = data(timeType);
1063 if (tran.atMSecsSinceEpoch != invalidMSecs()) {
1064 if (nameType == QTimeZone::ShortName)
1065 return tran.abbreviation;
1066 // Save base class repeating the data(timeType) query:
1067 if (isAnglicLocale(locale))
1068 return isoOffsetFormat(tran.offsetFromUtc);
1069 }
1070 }
1071 // Otherwise, fall back to base class (and qtimezonelocale.cpp):
1072 return QTimeZonePrivate::displayName(timeType, nameType, locale);
1073}
1074
1075QString QTzTimeZonePrivate::abbreviation(qint64 atMSecsSinceEpoch) const
1076{
1077 return data(atMSecsSinceEpoch).abbreviation;
1078}
1079
1080int QTzTimeZonePrivate::offsetFromUtc(qint64 atMSecsSinceEpoch) const
1081{
1082 const Data tran = data(atMSecsSinceEpoch);
1083 return tran.offsetFromUtc; // == tran.standardTimeOffset + tran.daylightTimeOffset
1084}
1085
1086int QTzTimeZonePrivate::standardTimeOffset(qint64 atMSecsSinceEpoch) const
1087{
1088 return data(atMSecsSinceEpoch).standardTimeOffset;
1089}
1090
1091int QTzTimeZonePrivate::daylightTimeOffset(qint64 atMSecsSinceEpoch) const
1092{
1093 return data(atMSecsSinceEpoch).daylightTimeOffset;
1094}
1095
1096bool QTzTimeZonePrivate::hasDaylightTime() const
1097{
1098 return cached_data.m_hasDst;
1099}
1100
1101bool QTzTimeZonePrivate::isDaylightTime(qint64 atMSecsSinceEpoch) const
1102{
1103 return (daylightTimeOffset(atMSecsSinceEpoch) != 0);
1104}
1105
1106QTimeZonePrivate::Data QTzTimeZonePrivate::dataForTzTransition(QTzTransitionTime tran) const
1107{
1108 return dataFromRule(cached_data.m_tranRules.at(tran.ruleIndex), tran.atMSecsSinceEpoch);
1109}
1110
1111QTimeZonePrivate::Data QTzTimeZonePrivate::dataFromRule(QTzTransitionRule rule,
1112 qint64 msecsSinceEpoch) const
1113{
1114 return Data(QString::fromUtf8(cached_data.m_abbreviations.at(rule.abbreviationIndex)),
1115 msecsSinceEpoch, rule.stdOffset + rule.dstOffset, rule.stdOffset);
1116}
1117
1118QList<QTimeZonePrivate::Data> QTzTimeZonePrivate::getPosixTransitions(qint64 msNear) const
1119{
1120 const int year = QDateTime::fromMSecsSinceEpoch(msNear, QTimeZone::UTC).date().year();
1121 // The Data::atMSecsSinceEpoch of the single entry if zone is constant:
1122 qint64 atTime = tranCache().isEmpty() ? msNear : tranCache().last().atMSecsSinceEpoch;
1123 return calculatePosixTransitions(cached_data.m_posixRule, year - 1, year + 1, atTime);
1124}
1125
1126QTimeZonePrivate::Data QTzTimeZonePrivate::data(qint64 forMSecsSinceEpoch) const
1127{
1128 // If the required time is after the last transition (or there were none)
1129 // and we have a POSIX rule, then use it:
1130 if (!cached_data.m_posixRule.isEmpty()
1131 && (tranCache().isEmpty() || tranCache().last().atMSecsSinceEpoch < forMSecsSinceEpoch)) {
1132 QList<Data> posixTrans = getPosixTransitions(forMSecsSinceEpoch);
1133 auto it = std::partition_point(posixTrans.cbegin(), posixTrans.cend(),
1134 [forMSecsSinceEpoch] (const Data &at) {
1135 return at.atMSecsSinceEpoch <= forMSecsSinceEpoch;
1136 });
1137 // Use most recent, if any in the past; or the first if we have no other rules:
1138 if (it > posixTrans.cbegin() || (tranCache().isEmpty() && it < posixTrans.cend())) {
1139 Data data = *(it > posixTrans.cbegin() ? it - 1 : it);
1140 data.atMSecsSinceEpoch = forMSecsSinceEpoch;
1141 return data;
1142 }
1143 }
1144 if (tranCache().isEmpty()) // Only possible if !isValid()
1145 return {};
1146
1147 // Otherwise, use the rule for the most recent or first transition:
1148 auto last = std::partition_point(tranCache().cbegin(), tranCache().cend(),
1149 [forMSecsSinceEpoch] (QTzTransitionTime at) {
1150 return at.atMSecsSinceEpoch <= forMSecsSinceEpoch;
1151 });
1152 if (last == tranCache().cbegin())
1153 return dataFromRule(cached_data.m_preZoneRule, forMSecsSinceEpoch);
1154
1155 --last;
1156 return dataFromRule(cached_data.m_tranRules.at(last->ruleIndex), forMSecsSinceEpoch);
1157}
1158
1159// Overridden because the final iteration over transitions only needs to look
1160// forward and backwards one transition within the POSIX rule (when there is
1161// one, as is common) to settle the whole period it covers, so we can then skip
1162// all other transitions of the POSIX rule and iterate tranCache() backwards
1163// from its most recent transition.
1164QTimeZonePrivate::Data QTzTimeZonePrivate::data(QTimeZone::TimeType timeType) const
1165{
1166 // True if tran is valid and has the DST-ness to match timeType:
1167 const auto validMatch = [timeType](const Data &tran) {
1168 return tran.atMSecsSinceEpoch != invalidMSecs()
1169 && ((timeType == QTimeZone::DaylightTime) != (tran.daylightTimeOffset == 0));
1170 };
1171
1172 // Get current tran, use if suitable:
1173 const qint64 currentMSecs = QDateTime::currentMSecsSinceEpoch();
1174 Data tran = data(currentMSecs);
1175 if (validMatch(tran))
1176 return tran;
1177
1178 // Otherwise, next tran probably flips DST-ness:
1179 tran = nextTransition(currentMSecs);
1180 if (validMatch(tran))
1181 return tran;
1182
1183 // Failing that, prev (or present, if current MSecs is eactly a transition
1184 // moment) tran defines what data() got us and the one before that probably
1185 // flips DST-ness:
1186 tran = previousTransition(currentMSecs + 1);
1187 if (tran.atMSecsSinceEpoch != invalidMSecs())
1188 tran = previousTransition(tran.atMSecsSinceEpoch);
1189 if (validMatch(tran))
1190 return tran;
1191
1192 // Otherwise, we can look backwards through transitions for a match; if we
1193 // have a POSIX rule, it clearly doesn't do DST (or we'd have hit it by
1194 // now), so we only need to look in the tranCache() up to now.
1195 const auto untilNow = [currentMSecs](QTzTransitionTime at) {
1196 return at.atMSecsSinceEpoch <= currentMSecs;
1197 };
1198 auto it = std::partition_point(tranCache().cbegin(), tranCache().cend(), untilNow);
1199 // That's the end or first future transition; we don't want to look at it,
1200 // but at all those before it.
1201 while (it != tranCache().cbegin()) {
1202 --it;
1203 tran = dataForTzTransition(*it);
1204 if ((timeType == QTimeZone::DaylightTime) != (tran.daylightTimeOffset == 0))
1205 return tran;
1206 }
1207
1208 return {};
1209}
1210
1211bool QTzTimeZonePrivate::isDataLocale(const QLocale &locale) const
1212{
1213 // TZ data uses en-Latn-* / C locale names:
1214 return isAnglicLocale(locale);
1215}
1216
1217bool QTzTimeZonePrivate::hasTransitions() const
1218{
1219 return true;
1220}
1221
1222QTimeZonePrivate::Data QTzTimeZonePrivate::nextTransition(qint64 afterMSecsSinceEpoch) const
1223{
1224 // If the required time is after the last transition (or there were none)
1225 // and we have a POSIX rule, then use it:
1226 if (!cached_data.m_posixRule.isEmpty()
1227 && (tranCache().isEmpty() || tranCache().last().atMSecsSinceEpoch < afterMSecsSinceEpoch)) {
1228 QList<Data> posixTrans = getPosixTransitions(afterMSecsSinceEpoch);
1229 auto it = std::partition_point(posixTrans.cbegin(), posixTrans.cend(),
1230 [afterMSecsSinceEpoch] (const Data &at) {
1231 return at.atMSecsSinceEpoch <= afterMSecsSinceEpoch;
1232 });
1233
1234 return it == posixTrans.cend() ? Data{} : *it;
1235 }
1236
1237 // Otherwise, if we can find a valid tran, use its rule:
1238 auto last = std::partition_point(tranCache().cbegin(), tranCache().cend(),
1239 [afterMSecsSinceEpoch] (QTzTransitionTime at) {
1240 return at.atMSecsSinceEpoch <= afterMSecsSinceEpoch;
1241 });
1242 return last != tranCache().cend() ? dataForTzTransition(*last) : Data{};
1243}
1244
1245QTimeZonePrivate::Data QTzTimeZonePrivate::previousTransition(qint64 beforeMSecsSinceEpoch) const
1246{
1247 // If the required time is after the last transition (or there were none)
1248 // and we have a POSIX rule, then use it:
1249 if (!cached_data.m_posixRule.isEmpty()
1250 && (tranCache().isEmpty() || tranCache().last().atMSecsSinceEpoch < beforeMSecsSinceEpoch)) {
1251 QList<Data> posixTrans = getPosixTransitions(beforeMSecsSinceEpoch);
1252 auto it = std::partition_point(posixTrans.cbegin(), posixTrans.cend(),
1253 [beforeMSecsSinceEpoch] (const Data &at) {
1254 return at.atMSecsSinceEpoch < beforeMSecsSinceEpoch;
1255 });
1256 if (it > posixTrans.cbegin())
1257 return *--it;
1258 // It fell between the last transition (if any) and the first of the POSIX rule:
1259 return tranCache().isEmpty() ? Data{} : dataForTzTransition(tranCache().last());
1260 }
1261
1262 // Otherwise if we can find a valid tran then use its rule
1263 auto last = std::partition_point(tranCache().cbegin(), tranCache().cend(),
1264 [beforeMSecsSinceEpoch] (QTzTransitionTime at) {
1265 return at.atMSecsSinceEpoch < beforeMSecsSinceEpoch;
1266 });
1267 return last > tranCache().cbegin() ? dataForTzTransition(*--last) : Data{};
1268}
1269
1270bool QTzTimeZonePrivate::isTimeZoneIdAvailable(QByteArrayView ianaId) const
1271{
1272 // Allow a POSIX rule as long as it has offset data. (This needs to reject a
1273 // plain abbreviation, without offset, since claiming to support such zones
1274 // would prevent the custom QTimeZone constructor from accepting such a
1275 // name, as it doesn't want a custom zone to over-ride a "real" one.)
1276 return tzZones->contains(ianaId) || validatePosixRule(ianaId, true).isValid;
1277}
1278
1279QList<QByteArray> QTzTimeZonePrivate::availableTimeZoneIds() const
1280{
1281 return uniqueSortedAliasPadded(tzZones->keys());
1282}
1283
1284QList<QByteArray> QTzTimeZonePrivate::availableTimeZoneIds(QLocale::Territory territory) const
1285{
1286 QList<QByteArray> result;
1287 for (auto it = tzZones->cbegin(), end = tzZones->cend(); it != end; ++it) {
1288 if (it.value().territory == territory)
1289 result << it.key();
1290 // We'll pick up any CLDR-standard names below, so don't try to map here.
1291 }
1292 std::sort(result.begin(), result.end());
1293
1294 // Since zone.tab only knows about one territory per zone, and is somewhat
1295 // incomplete, we may well miss some zones that CLDR associates with the
1296 // territory. So merge with those from CLDR that we do support.
1297 const auto unWantedZone = [territory](QByteArrayView id) {
1298 // We only want to add zones if they are known and we don't already have them:
1299 auto it = tzZones->constFind(id);
1300 return it == tzZones->end() || it->territory == territory;
1301 };
1302 QList<QByteArrayView> cldrViews = matchingTimeZoneIds(territory);
1303 std::sort(cldrViews.begin(), cldrViews.end());
1304 const auto uniqueEnd = std::unique(cldrViews.begin(), cldrViews.end());
1305 const auto prunedEnd = std::remove_if(cldrViews.begin(), uniqueEnd, unWantedZone);
1306 const auto cldrSize = std::distance(cldrViews.begin(), prunedEnd);
1307 if (cldrSize) {
1308 QList<QByteArray> cldrList;
1309 cldrList.reserve(cldrSize);
1310 for (auto it = cldrViews.begin(); it != prunedEnd; ++it)
1311 cldrList.emplace_back(it->toByteArray());
1312 QList<QByteArray> joined;
1313 joined.reserve(result.size() + cldrSize);
1314 std::set_union(result.begin(), result.end(), cldrList.begin(), cldrList.end(),
1315 std::back_inserter(joined));
1316 result = joined;
1317 }
1318
1319 return result;
1320}
1321
1322// Getting the system zone's ID:
1323
1324namespace {
1325class ZoneNameReader
1326{
1327public:
1328 QByteArray name()
1329 {
1330 /* Assumptions:
1331 a) Systems don't change which of localtime and TZ they use without a
1332 reboot.
1333 b) When they change, they use atomic renames, hence a new device and
1334 inode for the new file.
1335 c) If we change which *name* is used for a zone, while referencing
1336 the same final zoneinfo file, we don't care about the change of
1337 name (e.g. if Europe/Oslo and Europe/Berlin are both symlinks to
1338 the same CET file, continuing to use the old name, after
1339 /etc/localtime changes which of the two it points to, is
1340 harmless).
1341
1342 The alternative would be to use a file-system watcher, but they are a
1343 scarce resource.
1344 */
1345 const StatIdent local = identify("/etc/localtime");
1346 const StatIdent tz = identify("/etc/TZ");
1347 const StatIdent timezone = identify("/etc/timezone");
1348 if (!m_name.isEmpty() && m_last.isValid()
1349 && (m_last == local || m_last == tz || m_last == timezone)) {
1350 return m_name;
1351 }
1352
1353 m_name = etcLocalTime();
1354 if (!m_name.isEmpty()) {
1355 m_last = local;
1356 return m_name;
1357 }
1358
1359 // Some systems (e.g. uClibc) have a default value for $TZ in /etc/TZ:
1360 m_name = etcContent(QStringLiteral("/etc/TZ"));
1361 if (!m_name.isEmpty()) {
1362 m_last = tz;
1363 return m_name;
1364 }
1365
1366 // Gentoo still (2020, QTBUG-87326) uses this:
1367 m_name = etcContent(QStringLiteral("/etc/timezone"));
1368 m_last = m_name.isEmpty() ? StatIdent() : timezone;
1369 return m_name;
1370 }
1371
1372private:
1373 QByteArray m_name;
1374 struct StatIdent
1375 {
1376 static constexpr unsigned long bad = ~0ul;
1377 unsigned long m_dev, m_ino;
1378 constexpr StatIdent() : m_dev(bad), m_ino(bad) {}
1379 StatIdent(const QT_STATBUF &data) : m_dev(data.st_dev), m_ino(data.st_ino) {}
1380 bool isValid() { return m_dev != bad || m_ino != bad; }
1381 friend constexpr bool operator==(StatIdent lhs, StatIdent rhs)
1382 { return lhs.m_dev == rhs.m_dev && lhs.m_ino == rhs.m_ino; }
1383 };
1384 StatIdent m_last;
1385
1386 static StatIdent identify(const char *path)
1387 {
1388 QT_STATBUF data;
1389 return QT_STAT(path, &data) == -1 ? StatIdent() : StatIdent(data);
1390 }
1391
1392 static QByteArray etcLocalTime()
1393 {
1394 // On most distros /etc/localtime is a symlink to a real file so extract
1395 // name from the path
1396 const QString tzdir = qEnvironmentVariable("TZDIR");
1397 constexpr auto zoneinfo = "/zoneinfo/"_L1;
1398 QString path = QStringLiteral("/etc/localtime");
1399 long iteration = getSymloopMax();
1400 // Symlink may point to another symlink etc. before being under zoneinfo/
1401 // We stop on the first path under /zoneinfo/, even if it is itself a
1402 // symlink, like America/Montreal pointing to America/Toronto
1403 do {
1404 path = QFile::symLinkTarget(path);
1405 // If it's a zoneinfo file, extract the zone name from its path:
1406 int index = tzdir.isEmpty() ? -1 : path.indexOf(tzdir);
1407 if (index >= 0) {
1408 const auto tail = QStringView{ path }.sliced(index + tzdir.size()).toUtf8();
1409 return tail.startsWith(u'/') ? tail.sliced(1) : tail;
1410 }
1411 index = path.indexOf(zoneinfo);
1412 if (index >= 0)
1413 return QStringView{ path }.sliced(index + zoneinfo.size()).toUtf8();
1414 } while (!path.isEmpty() && --iteration > 0);
1415
1416 return QByteArray();
1417 }
1418
1419 static QByteArray etcContent(const QString &path)
1420 {
1421 QFile zone(path);
1422 if (zone.open(QIODevice::ReadOnly))
1423 return zone.readAll().trimmed();
1424
1425 return QByteArray();
1426 }
1427
1428 // Any chain of symlinks longer than this is assumed to be a loop:
1429 static long getSymloopMax()
1430 {
1431#ifdef SYMLOOP_MAX
1432 // If defined, at runtime it can only be greater than this, so this is a safe bet:
1433 return SYMLOOP_MAX;
1434#else
1435 errno = 0;
1436 long result = sysconf(_SC_SYMLOOP_MAX);
1437 if (result >= 0)
1438 return result;
1439 // result is -1, meaning either error or no limit
1440 Q_ASSERT(!errno); // ... but it can't be an error, POSIX mandates _SC_SYMLOOP_MAX
1441
1442 // therefore we can make up our own limit
1443# ifdef MAXSYMLINKS
1444 return MAXSYMLINKS;
1445# else
1446 return 8;
1447# endif
1448#endif
1449 }
1450};
1451}
1452
1453QByteArray QTzTimeZonePrivate::systemTimeZoneId() const
1454{
1455 return staticSystemTimeZoneId();
1456}
1457
1458QByteArray QTzTimeZonePrivate::staticSystemTimeZoneId()
1459{
1460 // Check TZ env var first, if not populated try find it
1461 QByteArray ianaId = qgetenv("TZ");
1462
1463 // The TZ value can be ":/etc/localtime" which libc considers
1464 // to be a "default timezone", in which case it will be read
1465 // by one of the blocks below, so unset it here so it is not
1466 // considered as a valid/found ianaId
1467 if (ianaId == ":/etc/localtime")
1468 ianaId.clear();
1469 else if (ianaId.startsWith(':'))
1470 ianaId = ianaId.sliced(1);
1471
1472 if (ianaId.isEmpty()) {
1473 Q_CONSTINIT thread_local static ZoneNameReader reader;
1474 ianaId = reader.name();
1475 }
1476
1477 return ianaId;
1478}
1479
1480QT_END_NAMESPACE
\inmodule QtCore\reentrant
Definition qdatastream.h:50
Definition qlist.h:81
QTzTimeZoneCacheEntry fetchEntry(const QByteArray &ianaId)
static int parsePosixTime(const char *begin, const char *end)
static auto validatePosixRule(QByteArrayView posixRule, bool requireOffset=false)
#define TZ_MAGIC
static QMap< int, QByteArray > parseTzAbbreviations(QDataStream &ds, int tzh_charcnt, const QList< QTzType > &types)
static QTzTimeZoneHash loadTzTimeZones()
Q_GLOBAL_STATIC(const QTzTimeZoneHash, tzZones, loadTzTimeZones())
QHash< QByteArray, QTzTimeZone > QTzTimeZoneHash
static int parsePosixTransitionTime(QLatin1StringView timeRule)
static QDate calculatePosixDate(QLatin1StringView dateRule, int year)
#define TZ_MAX_CHARS
static bool isTzFile(const QString &name)
static void parseTzLeapSeconds(QDataStream &ds, int tzh_leapcnt, bool longTran)
static QTzHeader parseTzHeader(QDataStream &ds, bool *ok)
static bool asciiIsLetter(char ch)
static QDate calculateDowDate(int year, int month, int dayOfWeek, int week)
#define TZ_MAX_TYPES
static bool openZoneInfo(const QString &name, QFile *file)
Q_DECLARE_TYPEINFO(QTzType, Q_PRIMITIVE_TYPE)
#define TZ_MAX_TIMES
static QList< QTzType > parseTzIndicators(QDataStream &ds, const QList< QTzType > &types, int tzh_ttisstdcnt, int tzh_ttisgmtcnt)
#define TZ_MAX_LEAPS
Q_DECLARE_TYPEINFO(QTzTransition, Q_PRIMITIVE_TYPE)
static QList< QTzType > parseTzTypes(QDataStream &ds, int tzh_typecnt)
static QList< QTimeZonePrivate::Data > calculatePosixTransitions(const QByteArray &posixRule, int startYear, int endYear, qint64 lastTranMSecs)
static int parsePosixOffset(const char *begin, const char *end)
static QList< QTzTransition > parseTzTransitions(QDataStream &ds, int tzh_timecnt, bool longTran)
static QByteArray parseTzPosixRule(QDataStream &ds)
QLocale::Territory territory