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
811private:
812 static QTzTimeZoneCacheEntry findEntry(const QByteArray &ianaId);
813 QCache<QByteArray, QTzTimeZoneCacheEntry> m_cache;
814 QMutex m_mutex;
815};
816
817QTzTimeZoneCacheEntry QTzTimeZoneCache::findEntry(const QByteArray &ianaId)
818{
819 QTzTimeZoneCacheEntry ret;
820 QFile tzif;
821 if (ianaId.isEmpty()) {
822 // Open system tz
823 tzif.setFileName(QStringLiteral("/etc/localtime"));
824 if (!tzif.open(QIODevice::ReadOnly))
825 return ret;
826 } else if (!openZoneInfo(QString::fromLocal8Bit(ianaId), &tzif)) {
827 // ianaId may be a POSIX rule, taken from $TZ or /etc/TZ
828 auto check = validatePosixRule(ianaId);
829 if (check.isValid) {
830 ret.m_hasDst = check.hasDst;
831 ret.m_posixRule = ianaId;
832 }
833 return ret;
834 }
835
836 QDataStream ds(&tzif);
837
838 // Parse the old version block of data
839 bool ok = false;
840 QByteArray posixRule;
841 QTzHeader hdr = parseTzHeader(ds, &ok);
842 if (!ok || ds.status() != QDataStream::Ok)
843 return ret;
844 QList<QTzTransition> tranList = parseTzTransitions(ds, hdr.tzh_timecnt, false);
845 if (ds.status() != QDataStream::Ok)
846 return ret;
847 QList<QTzType> typeList = parseTzTypes(ds, hdr.tzh_typecnt);
848 if (ds.status() != QDataStream::Ok)
849 return ret;
850 QMap<int, QByteArray> abbrevMap = parseTzAbbreviations(ds, hdr.tzh_charcnt, typeList);
851 if (ds.status() != QDataStream::Ok)
852 return ret;
853 parseTzLeapSeconds(ds, hdr.tzh_leapcnt, false);
854 if (ds.status() != QDataStream::Ok)
855 return ret;
856 typeList = parseTzIndicators(ds, typeList, hdr.tzh_ttisstdcnt, hdr.tzh_ttisgmtcnt);
857 if (ds.status() != QDataStream::Ok)
858 return ret;
859
860 // If version 2 then parse the second block of data
861 if (hdr.tzh_version == '2' || hdr.tzh_version == '3') {
862 ok = false;
863 QTzHeader hdr2 = parseTzHeader(ds, &ok);
864 if (!ok || ds.status() != QDataStream::Ok)
865 return ret;
866 tranList = parseTzTransitions(ds, hdr2.tzh_timecnt, true);
867 if (ds.status() != QDataStream::Ok)
868 return ret;
869 typeList = parseTzTypes(ds, hdr2.tzh_typecnt);
870 if (ds.status() != QDataStream::Ok)
871 return ret;
872 abbrevMap = parseTzAbbreviations(ds, hdr2.tzh_charcnt, typeList);
873 if (ds.status() != QDataStream::Ok)
874 return ret;
875 parseTzLeapSeconds(ds, hdr2.tzh_leapcnt, true);
876 if (ds.status() != QDataStream::Ok)
877 return ret;
878 typeList = parseTzIndicators(ds, typeList, hdr2.tzh_ttisstdcnt, hdr2.tzh_ttisgmtcnt);
879 if (ds.status() != QDataStream::Ok)
880 return ret;
881 posixRule = parseTzPosixRule(ds);
882 if (ds.status() != QDataStream::Ok)
883 return ret;
884 }
885 // Translate the TZ file's raw data into our internal form:
886
887 if (!posixRule.isEmpty()) {
888 auto check = validatePosixRule(posixRule);
889 if (!check.isValid) // We got a POSIX rule, but it was malformed:
890 return ret;
891 ret.m_posixRule = posixRule;
892 ret.m_hasDst = check.hasDst;
893 }
894
895 // Translate the array-index-based tz_abbrind into list index
896 const int size = abbrevMap.size();
897 ret.m_abbreviations.clear();
898 ret.m_abbreviations.reserve(size);
899 QList<int> abbrindList;
900 abbrindList.reserve(size);
901 for (auto it = abbrevMap.cbegin(), end = abbrevMap.cend(); it != end; ++it) {
902 ret.m_abbreviations.append(it.value());
903 abbrindList.append(it.key());
904 }
905 // Map tz_abbrind from map's keys (as initially read) to abbrindList's
906 // indices (used hereafter):
907 for (int i = 0; i < typeList.size(); ++i)
908 typeList[i].tz_abbrind = abbrindList.indexOf(typeList.at(i).tz_abbrind);
909
910 // TODO: is typeList[0] always the "before zones" data ? It seems to be ...
911 if (typeList.size())
912 ret.m_preZoneRule = { typeList.at(0).tz_gmtoff, 0, typeList.at(0).tz_abbrind };
913
914 // Offsets are stored as total offset, want to know separate UTC and DST offsets
915 // so find the first non-dst transition to use as base UTC Offset
916 int utcOffset = ret.m_preZoneRule.stdOffset;
917 for (const QTzTransition &tran : std::as_const(tranList)) {
918 if (!typeList.at(tran.tz_typeind).tz_isdst) {
919 utcOffset = typeList.at(tran.tz_typeind).tz_gmtoff;
920 break;
921 }
922 }
923
924 // Now for each transition time calculate and store our rule:
925 const int tranCount = tranList.size();
926 ret.m_tranTimes.reserve(tranCount);
927 // The DST offset when in effect: usually stable, usually an hour:
928 int lastDstOff = 3600;
929 for (int i = 0; i < tranCount; i++) {
930 const QTzTransition &tz_tran = tranList.at(i);
931 QTzTransitionTime tran;
932 QTzTransitionRule rule;
933 const QTzType tz_type = typeList.at(tz_tran.tz_typeind);
934
935 // Calculate the associated Rule
936 if (!tz_type.tz_isdst) {
937 utcOffset = tz_type.tz_gmtoff;
938 } else if (Q_UNLIKELY(tz_type.tz_gmtoff != utcOffset + lastDstOff)) {
939 /*
940 This might be a genuine change in DST offset, but could also be
941 DST starting at the same time as the standard offset changed. See
942 if DST's end gives a more plausible utcOffset (i.e. one closer to
943 the last we saw, or a simple whole hour):
944 */
945 // Standard offset inferred from net offset and expected DST offset:
946 const int inferStd = tz_type.tz_gmtoff - lastDstOff; // != utcOffset
947 for (int j = i + 1; j < tranCount; j++) {
948 const QTzType new_type = typeList.at(tranList.at(j).tz_typeind);
949 if (!new_type.tz_isdst) {
950 const int newUtc = new_type.tz_gmtoff;
951 if (newUtc == utcOffset) {
952 // DST-end can't help us, avoid lots of messy checks.
953 // else: See if the end matches the familiar DST offset:
954 } else if (newUtc == inferStd) {
955 utcOffset = newUtc;
956 // else: let either end shift us to one hour as DST offset:
957 } else if (tz_type.tz_gmtoff - 3600 == utcOffset) {
958 // Start does it
959 } else if (tz_type.tz_gmtoff - 3600 == newUtc) {
960 utcOffset = newUtc; // End does it
961 // else: prefer whichever end gives DST offset closer to
962 // last, but consider any offset > 0 "closer" than any <= 0:
963 } else if (newUtc < tz_type.tz_gmtoff
964 ? (utcOffset >= tz_type.tz_gmtoff
965 || qAbs(newUtc - inferStd) < qAbs(utcOffset - inferStd))
966 : (utcOffset >= tz_type.tz_gmtoff
967 && qAbs(newUtc - inferStd) < qAbs(utcOffset - inferStd))) {
968 utcOffset = newUtc;
969 }
970 break;
971 }
972 }
973 lastDstOff = tz_type.tz_gmtoff - utcOffset;
974 }
975 rule.stdOffset = utcOffset;
976 rule.dstOffset = tz_type.tz_gmtoff - utcOffset;
977 rule.abbreviationIndex = tz_type.tz_abbrind;
978
979 // If the rule already exist then use that, otherwise add it
980 int ruleIndex = ret.m_tranRules.indexOf(rule);
981 if (ruleIndex == -1) {
982 if (rule.dstOffset != 0)
983 ret.m_hasDst = true;
984 tran.ruleIndex = ret.m_tranRules.size();
985 ret.m_tranRules.append(rule);
986 } else {
987 tran.ruleIndex = ruleIndex;
988 }
989
990 tran.atMSecsSinceEpoch = tz_tran.tz_time * 1000;
991 ret.m_tranTimes.append(tran);
992 }
993
994 return ret;
995}
996
998{
999 QMutexLocker locker(&m_mutex);
1000
1001 // search the cache...
1002 QTzTimeZoneCacheEntry *obj = m_cache.object(ianaId);
1003 if (obj)
1004 return *obj;
1005
1006 // ... or build a new entry from scratch
1007
1008 locker.unlock(); // don't parse files under mutex lock
1009
1010 QTzTimeZoneCacheEntry ret = findEntry(ianaId);
1011 auto ptr = std::make_unique<QTzTimeZoneCacheEntry>(ret);
1012
1013 locker.relock();
1014 m_cache.insert(ianaId, ptr.release()); // may overwrite if another thread was faster
1015 locker.unlock();
1016
1017 return ret;
1018}
1019
1020// Create a named time zone
1021QTzTimeZonePrivate::QTzTimeZonePrivate(const QByteArray &ianaId)
1022{
1023 if (!isTimeZoneIdAvailable(ianaId)) // Avoid pointlessly creating cache entries
1024 return;
1025 static QTzTimeZoneCache tzCache;
1026 auto entry = tzCache.fetchEntry(ianaId);
1027 if (entry.m_tranTimes.isEmpty() && entry.m_posixRule.isEmpty())
1028 return; // Invalid after all !
1029
1030 cached_data = std::move(entry);
1031 m_id = ianaId;
1032 // Avoid empty ID, if we have an abbreviation to use instead
1033 if (m_id.isEmpty()) {
1034 // This can only happen for the system zone, when we've read the
1035 // contents of /etc/localtime because it wasn't a symlink.
1036 // TODO: use CLDR generic abbreviation for the zone.
1037 m_id = abbreviation(QDateTime::currentMSecsSinceEpoch()).toUtf8();
1038 }
1039}
1040
1041QLocale::Territory QTzTimeZonePrivate::territory() const
1042{
1043 return tzZones->value(m_id).territory;
1044}
1045
1046QString QTzTimeZonePrivate::comment() const
1047{
1048 return QString::fromUtf8(tzZones->value(m_id).comment);
1049}
1050
1051QString QTzTimeZonePrivate::displayName(QTimeZone::TimeType timeType,
1052 QTimeZone::NameType nameType,
1053 const QLocale &locale) const
1054{
1055 // TZ only provides C-locale abbreviations and offset:
1056 if (nameType != QTimeZone::LongName && isDataLocale(locale)) {
1057 Data tran = data(timeType);
1058 if (tran.atMSecsSinceEpoch != invalidMSecs()) {
1059 if (nameType == QTimeZone::ShortName)
1060 return tran.abbreviation;
1061 // Save base class repeating the data(timeType) query:
1062 if (isAnglicLocale(locale))
1063 return isoOffsetFormat(tran.offsetFromUtc);
1064 }
1065 }
1066 // Otherwise, fall back to base class (and qtimezonelocale.cpp):
1067 return QTimeZonePrivate::displayName(timeType, nameType, locale);
1068}
1069
1070QString QTzTimeZonePrivate::abbreviation(qint64 atMSecsSinceEpoch) const
1071{
1072 return data(atMSecsSinceEpoch).abbreviation;
1073}
1074
1075int QTzTimeZonePrivate::offsetFromUtc(qint64 atMSecsSinceEpoch) const
1076{
1077 const Data tran = data(atMSecsSinceEpoch);
1078 return tran.offsetFromUtc; // == tran.standardTimeOffset + tran.daylightTimeOffset
1079}
1080
1081int QTzTimeZonePrivate::standardTimeOffset(qint64 atMSecsSinceEpoch) const
1082{
1083 return data(atMSecsSinceEpoch).standardTimeOffset;
1084}
1085
1086int QTzTimeZonePrivate::daylightTimeOffset(qint64 atMSecsSinceEpoch) const
1087{
1088 return data(atMSecsSinceEpoch).daylightTimeOffset;
1089}
1090
1091bool QTzTimeZonePrivate::hasDaylightTime() const
1092{
1093 return cached_data.m_hasDst;
1094}
1095
1096bool QTzTimeZonePrivate::isDaylightTime(qint64 atMSecsSinceEpoch) const
1097{
1098 return (daylightTimeOffset(atMSecsSinceEpoch) != 0);
1099}
1100
1101QTimeZonePrivate::Data QTzTimeZonePrivate::dataForTzTransition(QTzTransitionTime tran) const
1102{
1103 return dataFromRule(cached_data.m_tranRules.at(tran.ruleIndex), tran.atMSecsSinceEpoch);
1104}
1105
1106QTimeZonePrivate::Data QTzTimeZonePrivate::dataFromRule(QTzTransitionRule rule,
1107 qint64 msecsSinceEpoch) const
1108{
1109 return Data(QString::fromUtf8(cached_data.m_abbreviations.at(rule.abbreviationIndex)),
1110 msecsSinceEpoch, rule.stdOffset + rule.dstOffset, rule.stdOffset);
1111}
1112
1113QList<QTimeZonePrivate::Data> QTzTimeZonePrivate::getPosixTransitions(qint64 msNear) const
1114{
1115 const int year = QDateTime::fromMSecsSinceEpoch(msNear, QTimeZone::UTC).date().year();
1116 // The Data::atMSecsSinceEpoch of the single entry if zone is constant:
1117 qint64 atTime = tranCache().isEmpty() ? msNear : tranCache().last().atMSecsSinceEpoch;
1118 return calculatePosixTransitions(cached_data.m_posixRule, year - 1, year + 1, atTime);
1119}
1120
1121QTimeZonePrivate::Data QTzTimeZonePrivate::data(qint64 forMSecsSinceEpoch) const
1122{
1123 // If the required time is after the last transition (or there were none)
1124 // and we have a POSIX rule, then use it:
1125 if (!cached_data.m_posixRule.isEmpty()
1126 && (tranCache().isEmpty() || tranCache().last().atMSecsSinceEpoch < forMSecsSinceEpoch)) {
1127 QList<Data> posixTrans = getPosixTransitions(forMSecsSinceEpoch);
1128 auto it = std::partition_point(posixTrans.cbegin(), posixTrans.cend(),
1129 [forMSecsSinceEpoch] (const Data &at) {
1130 return at.atMSecsSinceEpoch <= forMSecsSinceEpoch;
1131 });
1132 // Use most recent, if any in the past; or the first if we have no other rules:
1133 if (it > posixTrans.cbegin() || (tranCache().isEmpty() && it < posixTrans.cend())) {
1134 Data data = *(it > posixTrans.cbegin() ? it - 1 : it);
1135 data.atMSecsSinceEpoch = forMSecsSinceEpoch;
1136 return data;
1137 }
1138 }
1139 if (tranCache().isEmpty()) // Only possible if !isValid()
1140 return {};
1141
1142 // Otherwise, use the rule for the most recent or first transition:
1143 auto last = std::partition_point(tranCache().cbegin(), tranCache().cend(),
1144 [forMSecsSinceEpoch] (QTzTransitionTime at) {
1145 return at.atMSecsSinceEpoch <= forMSecsSinceEpoch;
1146 });
1147 if (last == tranCache().cbegin())
1148 return dataFromRule(cached_data.m_preZoneRule, forMSecsSinceEpoch);
1149
1150 --last;
1151 return dataFromRule(cached_data.m_tranRules.at(last->ruleIndex), forMSecsSinceEpoch);
1152}
1153
1154// Overridden because the final iteration over transitions only needs to look
1155// forward and backwards one transition within the POSIX rule (when there is
1156// one, as is common) to settle the whole period it covers, so we can then skip
1157// all other transitions of the POSIX rule and iterate tranCache() backwards
1158// from its most recent transition.
1159QTimeZonePrivate::Data QTzTimeZonePrivate::data(QTimeZone::TimeType timeType) const
1160{
1161 // True if tran is valid and has the DST-ness to match timeType:
1162 const auto validMatch = [timeType](const Data &tran) {
1163 return tran.atMSecsSinceEpoch != invalidMSecs()
1164 && ((timeType == QTimeZone::DaylightTime) != (tran.daylightTimeOffset == 0));
1165 };
1166
1167 // Get current tran, use if suitable:
1168 const qint64 currentMSecs = QDateTime::currentMSecsSinceEpoch();
1169 Data tran = data(currentMSecs);
1170 if (validMatch(tran))
1171 return tran;
1172
1173 // Otherwise, next tran probably flips DST-ness:
1174 tran = nextTransition(currentMSecs);
1175 if (validMatch(tran))
1176 return tran;
1177
1178 // Failing that, prev (or present, if current MSecs is eactly a transition
1179 // moment) tran defines what data() got us and the one before that probably
1180 // flips DST-ness:
1181 tran = previousTransition(currentMSecs + 1);
1182 if (tran.atMSecsSinceEpoch != invalidMSecs())
1183 tran = previousTransition(tran.atMSecsSinceEpoch);
1184 if (validMatch(tran))
1185 return tran;
1186
1187 // Otherwise, we can look backwards through transitions for a match; if we
1188 // have a POSIX rule, it clearly doesn't do DST (or we'd have hit it by
1189 // now), so we only need to look in the tranCache() up to now.
1190 const auto untilNow = [currentMSecs](QTzTransitionTime at) {
1191 return at.atMSecsSinceEpoch <= currentMSecs;
1192 };
1193 auto it = std::partition_point(tranCache().cbegin(), tranCache().cend(), untilNow);
1194 // That's the end or first future transition; we don't want to look at it,
1195 // but at all those before it.
1196 while (it != tranCache().cbegin()) {
1197 --it;
1198 tran = dataForTzTransition(*it);
1199 if ((timeType == QTimeZone::DaylightTime) != (tran.daylightTimeOffset == 0))
1200 return tran;
1201 }
1202
1203 return {};
1204}
1205
1206bool QTzTimeZonePrivate::isDataLocale(const QLocale &locale) const
1207{
1208 // TZ data uses en-Latn-* / C locale names:
1209 return isAnglicLocale(locale);
1210}
1211
1212bool QTzTimeZonePrivate::hasTransitions() const
1213{
1214 return true;
1215}
1216
1217QTimeZonePrivate::Data QTzTimeZonePrivate::nextTransition(qint64 afterMSecsSinceEpoch) const
1218{
1219 // If the required time is after the last transition (or there were none)
1220 // and we have a POSIX rule, then use it:
1221 if (!cached_data.m_posixRule.isEmpty()
1222 && (tranCache().isEmpty() || tranCache().last().atMSecsSinceEpoch < afterMSecsSinceEpoch)) {
1223 QList<Data> posixTrans = getPosixTransitions(afterMSecsSinceEpoch);
1224 auto it = std::partition_point(posixTrans.cbegin(), posixTrans.cend(),
1225 [afterMSecsSinceEpoch] (const Data &at) {
1226 return at.atMSecsSinceEpoch <= afterMSecsSinceEpoch;
1227 });
1228
1229 return it == posixTrans.cend() ? Data{} : *it;
1230 }
1231
1232 // Otherwise, if we can find a valid tran, use its rule:
1233 auto last = std::partition_point(tranCache().cbegin(), tranCache().cend(),
1234 [afterMSecsSinceEpoch] (QTzTransitionTime at) {
1235 return at.atMSecsSinceEpoch <= afterMSecsSinceEpoch;
1236 });
1237 return last != tranCache().cend() ? dataForTzTransition(*last) : Data{};
1238}
1239
1240QTimeZonePrivate::Data QTzTimeZonePrivate::previousTransition(qint64 beforeMSecsSinceEpoch) const
1241{
1242 // If the required time is after the last transition (or there were none)
1243 // and we have a POSIX rule, then use it:
1244 if (!cached_data.m_posixRule.isEmpty()
1245 && (tranCache().isEmpty() || tranCache().last().atMSecsSinceEpoch < beforeMSecsSinceEpoch)) {
1246 QList<Data> posixTrans = getPosixTransitions(beforeMSecsSinceEpoch);
1247 auto it = std::partition_point(posixTrans.cbegin(), posixTrans.cend(),
1248 [beforeMSecsSinceEpoch] (const Data &at) {
1249 return at.atMSecsSinceEpoch < beforeMSecsSinceEpoch;
1250 });
1251 if (it > posixTrans.cbegin())
1252 return *--it;
1253 // It fell between the last transition (if any) and the first of the POSIX rule:
1254 return tranCache().isEmpty() ? Data{} : dataForTzTransition(tranCache().last());
1255 }
1256
1257 // Otherwise if we can find a valid tran then use its rule
1258 auto last = std::partition_point(tranCache().cbegin(), tranCache().cend(),
1259 [beforeMSecsSinceEpoch] (QTzTransitionTime at) {
1260 return at.atMSecsSinceEpoch < beforeMSecsSinceEpoch;
1261 });
1262 return last > tranCache().cbegin() ? dataForTzTransition(*--last) : Data{};
1263}
1264
1265bool QTzTimeZonePrivate::isTimeZoneIdAvailable(QByteArrayView ianaId) const
1266{
1267 // Allow a POSIX rule as long as it has offset data. (This needs to reject a
1268 // plain abbreviation, without offset, since claiming to support such zones
1269 // would prevent the custom QTimeZone constructor from accepting such a
1270 // name, as it doesn't want a custom zone to over-ride a "real" one.)
1271 return tzZones->contains(ianaId) || validatePosixRule(ianaId, true).isValid;
1272}
1273
1274QList<QByteArray> QTzTimeZonePrivate::availableTimeZoneIds() const
1275{
1276 return uniqueSortedAliasPadded(tzZones->keys());
1277}
1278
1279QList<QByteArray> QTzTimeZonePrivate::availableTimeZoneIds(QLocale::Territory territory) const
1280{
1281 QList<QByteArray> result;
1282 for (auto it = tzZones->cbegin(), end = tzZones->cend(); it != end; ++it) {
1283 if (it.value().territory == territory)
1284 result << it.key();
1285 // We'll pick up any CLDR-standard names below, so don't try to map here.
1286 }
1287 std::sort(result.begin(), result.end());
1288
1289 // Since zone.tab only knows about one territory per zone, and is somewhat
1290 // incomplete, we may well miss some zones that CLDR associates with the
1291 // territory. So merge with those from CLDR that we do support.
1292 const auto unWantedZone = [territory](QByteArrayView id) {
1293 // We only want to add zones if they are known and we don't already have them:
1294 auto it = tzZones->constFind(id);
1295 return it == tzZones->end() || it->territory == territory;
1296 };
1297 QList<QByteArrayView> cldrViews = matchingTimeZoneIds(territory);
1298 std::sort(cldrViews.begin(), cldrViews.end());
1299 const auto uniqueEnd = std::unique(cldrViews.begin(), cldrViews.end());
1300 const auto prunedEnd = std::remove_if(cldrViews.begin(), uniqueEnd, unWantedZone);
1301 const auto cldrSize = std::distance(cldrViews.begin(), prunedEnd);
1302 if (cldrSize) {
1303 QList<QByteArray> cldrList;
1304 cldrList.reserve(cldrSize);
1305 for (auto it = cldrViews.begin(); it != prunedEnd; ++it)
1306 cldrList.emplace_back(it->toByteArray());
1307 QList<QByteArray> joined;
1308 joined.reserve(result.size() + cldrSize);
1309 std::set_union(result.begin(), result.end(), cldrList.begin(), cldrList.end(),
1310 std::back_inserter(joined));
1311 result = joined;
1312 }
1313
1314 return result;
1315}
1316
1317// Getting the system zone's ID:
1318
1319namespace {
1320class ZoneNameReader
1321{
1322public:
1323 QByteArray name()
1324 {
1325 /* Assumptions:
1326 a) Systems don't change which of localtime and TZ they use without a
1327 reboot.
1328 b) When they change, they use atomic renames, hence a new device and
1329 inode for the new file.
1330 c) If we change which *name* is used for a zone, while referencing
1331 the same final zoneinfo file, we don't care about the change of
1332 name (e.g. if Europe/Oslo and Europe/Berlin are both symlinks to
1333 the same CET file, continuing to use the old name, after
1334 /etc/localtime changes which of the two it points to, is
1335 harmless).
1336
1337 The alternative would be to use a file-system watcher, but they are a
1338 scarce resource.
1339 */
1340 const StatIdent local = identify("/etc/localtime");
1341 const StatIdent tz = identify("/etc/TZ");
1342 const StatIdent timezone = identify("/etc/timezone");
1343 if (!m_name.isEmpty() && m_last.isValid()
1344 && (m_last == local || m_last == tz || m_last == timezone)) {
1345 return m_name;
1346 }
1347
1348 m_name = etcLocalTime();
1349 if (!m_name.isEmpty()) {
1350 m_last = local;
1351 return m_name;
1352 }
1353
1354 // Some systems (e.g. uClibc) have a default value for $TZ in /etc/TZ:
1355 m_name = etcContent(QStringLiteral("/etc/TZ"));
1356 if (!m_name.isEmpty()) {
1357 m_last = tz;
1358 return m_name;
1359 }
1360
1361 // Gentoo still (2020, QTBUG-87326) uses this:
1362 m_name = etcContent(QStringLiteral("/etc/timezone"));
1363 m_last = m_name.isEmpty() ? StatIdent() : timezone;
1364 return m_name;
1365 }
1366
1367private:
1368 QByteArray m_name;
1369 struct StatIdent
1370 {
1371 static constexpr unsigned long bad = ~0ul;
1372 unsigned long m_dev, m_ino;
1373 constexpr StatIdent() : m_dev(bad), m_ino(bad) {}
1374 StatIdent(const QT_STATBUF &data) : m_dev(data.st_dev), m_ino(data.st_ino) {}
1375 bool isValid() { return m_dev != bad || m_ino != bad; }
1376 friend constexpr bool operator==(StatIdent lhs, StatIdent rhs)
1377 { return lhs.m_dev == rhs.m_dev && lhs.m_ino == rhs.m_ino; }
1378 };
1379 StatIdent m_last;
1380
1381 static StatIdent identify(const char *path)
1382 {
1383 QT_STATBUF data;
1384 return QT_STAT(path, &data) == -1 ? StatIdent() : StatIdent(data);
1385 }
1386
1387 static QByteArray etcLocalTime()
1388 {
1389 // On most distros /etc/localtime is a symlink to a real file so extract
1390 // name from the path
1391 const QString tzdir = qEnvironmentVariable("TZDIR");
1392 constexpr auto zoneinfo = "/zoneinfo/"_L1;
1393 QString path = QStringLiteral("/etc/localtime");
1394 long iteration = getSymloopMax();
1395 // Symlink may point to another symlink etc. before being under zoneinfo/
1396 // We stop on the first path under /zoneinfo/, even if it is itself a
1397 // symlink, like America/Montreal pointing to America/Toronto
1398 do {
1399 path = QFile::symLinkTarget(path);
1400 // If it's a zoneinfo file, extract the zone name from its path:
1401 int index = tzdir.isEmpty() ? -1 : path.indexOf(tzdir);
1402 if (index >= 0) {
1403 const auto tail = QStringView{ path }.sliced(index + tzdir.size()).toUtf8();
1404 return tail.startsWith(u'/') ? tail.sliced(1) : tail;
1405 }
1406 index = path.indexOf(zoneinfo);
1407 if (index >= 0)
1408 return QStringView{ path }.sliced(index + zoneinfo.size()).toUtf8();
1409 } while (!path.isEmpty() && --iteration > 0);
1410
1411 return QByteArray();
1412 }
1413
1414 static QByteArray etcContent(const QString &path)
1415 {
1416 QFile zone(path);
1417 if (zone.open(QIODevice::ReadOnly))
1418 return zone.readAll().trimmed();
1419
1420 return QByteArray();
1421 }
1422
1423 // Any chain of symlinks longer than this is assumed to be a loop:
1424 static long getSymloopMax()
1425 {
1426#ifdef SYMLOOP_MAX
1427 // If defined, at runtime it can only be greater than this, so this is a safe bet:
1428 return SYMLOOP_MAX;
1429#else
1430 errno = 0;
1431 long result = sysconf(_SC_SYMLOOP_MAX);
1432 if (result >= 0)
1433 return result;
1434 // result is -1, meaning either error or no limit
1435 Q_ASSERT(!errno); // ... but it can't be an error, POSIX mandates _SC_SYMLOOP_MAX
1436
1437 // therefore we can make up our own limit
1438# ifdef MAXSYMLINKS
1439 return MAXSYMLINKS;
1440# else
1441 return 8;
1442# endif
1443#endif
1444 }
1445};
1446}
1447
1448QByteArray QTzTimeZonePrivate::systemTimeZoneId() const
1449{
1450 return staticSystemTimeZoneId();
1451}
1452
1453QByteArray QTzTimeZonePrivate::staticSystemTimeZoneId()
1454{
1455 // Check TZ env var first, if not populated try find it
1456 QByteArray ianaId = qgetenv("TZ");
1457
1458 // The TZ value can be ":/etc/localtime" which libc considers
1459 // to be a "default timezone", in which case it will be read
1460 // by one of the blocks below, so unset it here so it is not
1461 // considered as a valid/found ianaId
1462 if (ianaId == ":/etc/localtime")
1463 ianaId.clear();
1464 else if (ianaId.startsWith(':'))
1465 ianaId = ianaId.sliced(1);
1466
1467 if (ianaId.isEmpty()) {
1468 Q_CONSTINIT thread_local static ZoneNameReader reader;
1469 ianaId = reader.name();
1470 }
1471
1472 return ianaId;
1473}
1474
1475QT_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