7# include "qtimezoneprivate_p.h"
10#include <QtCore/qdatastream.h>
11#include <QtCore/qdatetime.h>
19static_assert(!std::is_constructible_v<QTimeZone, Qt::TimeSpec>);
20using namespace Qt::StringLiterals;
22#if QT_CONFIG(timezone)
24static QTimeZonePrivate *newBackendTimeZone()
26#if QT_CONFIG(timezone_tzdb)
27 return new QChronoTimeZonePrivate();
28#elif defined(Q_OS_DARWIN)
29 return new QMacTimeZonePrivate();
30#elif defined(Q_OS_ANDROID)
31 return new QAndroidTimeZonePrivate();
32#elif defined(Q_OS_UNIX)
33 return new QTzTimeZonePrivate();
35 return new QIcuTimeZonePrivate();
36#elif defined(Q_OS_WIN)
37 return new QWinTimeZonePrivate();
39 return new QUtcTimeZonePrivate();
44static QTimeZonePrivate *newBackendTimeZone(
const QByteArray &ianaId)
46 Q_ASSERT(!ianaId.isEmpty());
47#if QT_CONFIG(timezone_tzdb)
48 return new QChronoTimeZonePrivate(ianaId);
49#elif defined(Q_OS_DARWIN)
50 return new QMacTimeZonePrivate(ianaId);
51#elif defined(Q_OS_ANDROID)
52 return new QAndroidTimeZonePrivate(ianaId);
53#elif defined(Q_OS_UNIX)
54 return new QTzTimeZonePrivate(ianaId);
56 return new QIcuTimeZonePrivate(ianaId);
57#elif defined(Q_OS_WIN)
58 return new QWinTimeZonePrivate(ianaId);
60 return new QUtcTimeZonePrivate(ianaId);
64class QTimeZoneSingleton
67 QTimeZoneSingleton() : backend(newBackendTimeZone()) {}
74 QExplicitlySharedDataPointer<QTimeZonePrivate> backend;
78Q_GLOBAL_STATIC(QTimeZoneSingleton, global_tz);
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
266
267
268
269
270
271
272
273
274
275
276
277
278
280#if QT_CONFIG(timezone)
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
366
367
368
369
370
371
374QTimeZone::Data::Data()
noexcept : d(
nullptr)
377 static_assert(
int(Qt::TimeZone) == 3);
380QTimeZone::Data::Data(
const Data &other)
noexcept
382#if QT_CONFIG(timezone)
383 if (!other.isShort() && other.d)
389QTimeZone::Data::Data(QTimeZonePrivate *dptr)
noexcept
392#if QT_CONFIG(timezone)
398QTimeZone::Data::~Data()
400#if QT_CONFIG(timezone)
401 if (!isShort() && d && !d->ref.deref())
407QTimeZone::Data &QTimeZone::Data::operator=(
const Data &other)
noexcept
409#if QT_CONFIG(timezone)
410 if (!other.isShort())
411 return *
this = other.d;
412 if (!isShort() && d && !d->ref.deref())
420
421
423QTimeZone::QTimeZone()
noexcept
426 static_assert(
sizeof(ShortData) <=
sizeof(Data::d));
428 static_assert(qintptr(1) << (
sizeof(
void *) * 8 - 2) >= MaxUtcOffsetSecs);
431#if QT_CONFIG(timezone)
432QTimeZone::Data &QTimeZone::Data::operator=(QTimeZonePrivate *dptr)
noexcept
437 if (d && !d->ref.deref())
443 Q_ASSERT(!isShort());
448
449
450
451
452
453
454
455
456
457
458
460QTimeZone::QTimeZone(
const QByteArray &ianaId)
464 d =
new QUtcTimeZonePrivate(ianaId);
467 if (ianaId.isEmpty()) {
468 d = newBackendTimeZone();
470 d = newBackendTimeZone(ianaId);
473 const QByteArray name = QTimeZonePrivate::aliasToIana(ianaId);
474 if (!name.isEmpty() && name != ianaId)
475 d = newBackendTimeZone(name);
482 qint64 offset = QUtcTimeZonePrivate::offsetFromUtcString(ianaId);
483 if (offset != QTimeZonePrivate::invalidSeconds()) {
485 qint32 seconds = qint32(offset);
486 Q_ASSERT(qint64(seconds) == offset);
488 d =
new QUtcTimeZonePrivate(seconds);
494
495
496
497
498
499
500
501
502
503
504
505
507QTimeZone::QTimeZone(
int offsetSeconds)
508 : d((offsetSeconds >= MinUtcOffsetSecs && offsetSeconds <= MaxUtcOffsetSecs)
509 ?
new QUtcTimeZonePrivate(offsetSeconds) :
nullptr)
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
537QTimeZone::QTimeZone(
const QByteArray &zoneId,
int offsetSeconds,
const QString &name,
538 const QString &abbreviation, QLocale::Territory territory,
const QString &comment)
539 : d(QUtcTimeZonePrivate().isTimeZoneIdAvailable(zoneId)
540 || global_tz->backend->isTimeZoneIdAvailable(zoneId)
542 :
new QUtcTimeZonePrivate(zoneId, offsetSeconds, name, abbreviation, territory, comment))
547
548
549
550
551
552
554QTimeZone::QTimeZone(QTimeZonePrivate &dd)
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
582QTimeZone QTimeZone::asBackendZone()
const
584 switch (timeSpec()) {
588 return systemTimeZone();
591 case Qt::OffsetFromUTC:
592 return QTimeZone(*
new QUtcTimeZonePrivate(
int(d.s.offset)));
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
619
620
621
622
623
624
625
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
650
651
652
653
654
655
656
657
658
659
660
661
664
665
666
667
668
669
670
671
674
675
676
677
678
681
682
683
684
685
686
687
688
689
690
693
694
696QTimeZone::QTimeZone(
const QTimeZone &other)
noexcept
702
703
704
705
708
709
711QTimeZone::~QTimeZone()
716
717
718
721
722
724QTimeZone &QTimeZone::operator=(
const QTimeZone &other)
731
732
733
734
735
738
739
740
741
742
743
744
745
746
749
750
751
752
753
754
755
756
757
762 return rhs.d.isShort() && lhs.d.s == rhs.d.s;
764 if (!rhs.d.isShort()) {
765 if (lhs.d.d == rhs.d.d)
767#if QT_CONFIG(timezone)
768 return lhs.d.d && rhs.d.d && *lhs.d.d == *rhs.d.d;
776
777
779bool QTimeZone::isValid()
const
781#if QT_CONFIG(timezone)
783 return d.d && d->isValid();
788#if QT_CONFIG(timezone)
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
820QByteArray QTimeZone::id()
const
823 switch (d.s.spec()) {
825 return QTimeZonePrivate::utcQByteArray();
827 return systemTimeZoneId();
828 case Qt::OffsetFromUTC:
829 return QUtcTimeZonePrivate(d.s.offset).id();
841
842
843
844
845
846
847
848
849
850
851
852
853
854bool QTimeZone::hasAlternativeName(QByteArrayView alias)
const
858 QByteArray mine = QTimeZonePrivate::aliasToIana(id());
860 if (!mine.isEmpty() && alias == mine)
862 QByteArray its = QTimeZonePrivate::aliasToIana(alias);
865 return !its.isEmpty() && its == (mine.isEmpty() ? id() : mine);
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883QLocale::Territory QTimeZone::territory()
const
886 if (d.s.spec() == Qt::LocalTime)
887 return systemTimeZone().territory();
888 }
else if (isValid()) {
889 return d->territory();
891 return QLocale::AnyTerritory;
894#if QT_DEPRECATED_SINCE(6
, 6
)
896
897
898
899
900
901
903QLocale::Country QTimeZone::country()
const
910
911
912
913
914
915
916
917
919QString QTimeZone::comment()
const
923 }
else if (isValid()) {
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
952QString QTimeZone::displayName(
const QDateTime &atDateTime, NameType nameType,
953 const QLocale &locale)
const
956 switch (d.s.spec()) {
958 return systemTimeZone().displayName(atDateTime, nameType, locale);
960 case Qt::OffsetFromUTC:
961 return QUtcTimeZonePrivate(d.s.offset).displayName(
962 atDateTime.toMSecsSinceEpoch(), nameType, locale);
967 }
else if (isValid()) {
968 return d->displayName(atDateTime.toMSecsSinceEpoch(), nameType, locale);
975
976
977
978
979
980
981
982
983
984
986QString QTimeZone::displayName(TimeType timeType, NameType nameType,
987 const QLocale &locale)
const
990 switch (d.s.spec()) {
992 return systemTimeZone().displayName(timeType, nameType, locale);
994 case Qt::OffsetFromUTC:
995 return QUtcTimeZonePrivate(d.s.offset).displayName(timeType, nameType, locale);
1000 }
else if (isValid()) {
1001 return d->displayName(timeType, nameType, locale);
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1022QString QTimeZone::abbreviation(
const QDateTime &atDateTime)
const
1025 switch (d.s.spec()) {
1027 return systemTimeZone().abbreviation(atDateTime);
1029 case Qt::OffsetFromUTC:
1030 return QUtcTimeZonePrivate(d.s.offset).abbreviation(atDateTime.toMSecsSinceEpoch());
1035 }
else if (isValid()) {
1036 return d->abbreviation(atDateTime.toMSecsSinceEpoch());
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1058int QTimeZone::offsetFromUtc(
const QDateTime &atDateTime)
const
1061 switch (d.s.spec()) {
1063 return systemTimeZone().offsetFromUtc(atDateTime);
1065 case Qt::OffsetFromUTC:
1071 }
else if (isValid()) {
1072 const int offset = d->offsetFromUtc(atDateTime.toMSecsSinceEpoch());
1073 if (offset != QTimeZonePrivate::invalidSeconds())
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1093int QTimeZone::standardTimeOffset(
const QDateTime &atDateTime)
const
1096 switch (d.s.spec()) {
1098 return systemTimeZone().standardTimeOffset(atDateTime);
1100 case Qt::OffsetFromUTC:
1106 }
else if (isValid()) {
1107 const int offset = d->standardTimeOffset(atDateTime.toMSecsSinceEpoch());
1108 if (offset != QTimeZonePrivate::invalidSeconds())
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1128int QTimeZone::daylightTimeOffset(
const QDateTime &atDateTime)
const
1131 switch (d.s.spec()) {
1133 return systemTimeZone().daylightTimeOffset(atDateTime);
1135 case Qt::OffsetFromUTC:
1141 }
else if (hasDaylightTime()) {
1142 const int offset = d->daylightTimeOffset(atDateTime.toMSecsSinceEpoch());
1143 if (offset != QTimeZonePrivate::invalidSeconds())
1150
1151
1152
1153
1154
1155
1157bool QTimeZone::hasDaylightTime()
const
1160 switch (d.s.spec()) {
1162 return systemTimeZone().hasDaylightTime();
1164 case Qt::OffsetFromUTC:
1170 }
else if (isValid()) {
1171 return d->hasDaylightTime();
1177
1178
1179
1180
1181
1182
1184bool QTimeZone::isDaylightTime(
const QDateTime &atDateTime)
const
1187 switch (d.s.spec()) {
1189 return systemTimeZone().isDaylightTime(atDateTime);
1191 case Qt::OffsetFromUTC:
1197 }
else if (hasDaylightTime()) {
1198 return d->isDaylightTime(atDateTime.toMSecsSinceEpoch());
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1217QTimeZone::OffsetData QTimeZone::offsetData(
const QDateTime &forDateTime)
const
1220 switch (d.s.spec()) {
1222 return systemTimeZone().offsetData(forDateTime);
1224 case Qt::OffsetFromUTC:
1225 return { abbreviation(forDateTime), forDateTime,
int(d.s.offset),
int(d.s.offset), 0 };
1232 return QTimeZonePrivate::toOffsetData(d->data(forDateTime.toMSecsSinceEpoch()));
1234 return QTimeZonePrivate::invalidOffsetData();
1238
1239
1240
1241
1242
1243
1244
1245
1246
1248bool QTimeZone::hasTransitions()
const
1251 switch (d.s.spec()) {
1253 return systemTimeZone().hasTransitions();
1255 case Qt::OffsetFromUTC:
1261 }
else if (isValid()) {
1262 return d->hasTransitions();
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1282QTimeZone::OffsetData QTimeZone::nextTransition(
const QDateTime &afterDateTime)
const
1285 switch (d.s.spec()) {
1287 return systemTimeZone().nextTransition(afterDateTime);
1289 case Qt::OffsetFromUTC:
1295 }
else if (hasTransitions()) {
1296 return QTimeZonePrivate::toOffsetData(d->nextTransition(afterDateTime.toMSecsSinceEpoch()));
1299 return QTimeZonePrivate::invalidOffsetData();
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1317QTimeZone::OffsetData QTimeZone::previousTransition(
const QDateTime &beforeDateTime)
const
1320 switch (d.s.spec()) {
1322 return systemTimeZone().previousTransition(beforeDateTime);
1324 case Qt::OffsetFromUTC:
1330 }
else if (hasTransitions()) {
1331 return QTimeZonePrivate::toOffsetData(
1332 d->previousTransition(beforeDateTime.toMSecsSinceEpoch()));
1335 return QTimeZonePrivate::invalidOffsetData();
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1350QTimeZone::OffsetDataList QTimeZone::transitions(
const QDateTime &fromDateTime,
1351 const QDateTime &toDateTime)
const
1353 OffsetDataList list;
1355 switch (d.s.spec()) {
1357 return systemTimeZone().transitions(fromDateTime, toDateTime);
1359 case Qt::OffsetFromUTC:
1365 }
else if (hasTransitions()) {
1366 const QTimeZonePrivate::DataList plist = d->transitions(fromDateTime.toMSecsSinceEpoch(),
1367 toDateTime.toMSecsSinceEpoch());
1368 list.reserve(plist.size());
1369 for (
const QTimeZonePrivate::Data &pdata : plist)
1370 list.append(QTimeZonePrivate::toOffsetData(pdata));
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1402QByteArray QTimeZone::systemTimeZoneId()
1404 QByteArray sys = global_tz->backend->systemTimeZoneId();
1408 return global_tz->backend->id();
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429QTimeZone QTimeZone::systemTimeZone()
1432 const QByteArray sysId = global_tz->backend->systemTimeZoneId();
1433 const auto sys = sysId.isEmpty() ? QTimeZone(global_tz->backend) : QTimeZone(sysId);
1434 if (!sys.isValid()) {
1435 static bool neverWarned =
true;
1438 neverWarned =
false;
1439 qWarning(
"Unable to determine system time zone: "
1440 "please check your system configuration.");
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458QTimeZone QTimeZonePrivate::utcQTimeZone()
1460 return QTimeZone(*
new QUtcTimeZonePrivate());
1463Q_GLOBAL_STATIC(QTimeZone, utcTimeZone, QTimeZonePrivate::utcQTimeZone());
1465QTimeZone QTimeZone::utc()
1467 if (Q_UNLIKELY(utcTimeZone.isDestroyed()))
1468 return QTimeZonePrivate::utcQTimeZone();
1469 return *utcTimeZone;
1473
1474
1475
1476
1477
1478
1479
1480
1481
1483bool QTimeZone::isTimeZoneIdAvailable(
const QByteArray &ianaId)
1485#if defined(Q_OS_UNIX) && !(defined(Q_OS_ANDROID) || defined(Q_OS_DARWIN))
1492 if (!QTimeZonePrivate::isValidId(ianaId))
1495 return QUtcTimeZonePrivate().isTimeZoneIdAvailable(ianaId)
1496 || QUtcTimeZonePrivate::offsetFromUtcString(ianaId) != QTimeZonePrivate::invalidSeconds()
1497 || global_tz->backend->isTimeZoneIdAvailable(ianaId);
1500[[maybe_unused]]
static bool isUniqueSorted(
const QList<QByteArray> &seq)
1505 return std::is_sorted(seq.begin(), seq.end(), std::less_equal<QByteArray>());
1508static QList<QByteArray> set_union(
const QList<QByteArray> &l1,
const QList<QByteArray> &l2)
1510 Q_ASSERT(isUniqueSorted(l1));
1511 Q_ASSERT(isUniqueSorted(l2));
1512 QList<QByteArray> result;
1513 result.reserve(l1.size() + l2.size());
1514 std::set_union(l1.begin(), l1.end(),
1515 l2.begin(), l2.end(),
1516 std::back_inserter(result));
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1532QList<QByteArray> QTimeZone::availableTimeZoneIds()
1536 return set_union(QUtcTimeZonePrivate().availableTimeZoneIds(),
1537 global_tz->backend->availableTimeZoneIds());
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1554QList<QByteArray> QTimeZone::availableTimeZoneIds(QLocale::Territory territory)
1556 return set_union(QUtcTimeZonePrivate().availableTimeZoneIds(territory),
1557 global_tz->backend->availableTimeZoneIds(territory));
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1573QList<QByteArray> QTimeZone::availableTimeZoneIds(
int offsetSeconds)
1575 return set_union(QUtcTimeZonePrivate().availableTimeZoneIds(offsetSeconds),
1576 global_tz->backend->availableTimeZoneIds(offsetSeconds));
1580
1581
1582
1583
1584
1585
1587QByteArray QTimeZone::ianaIdToWindowsId(
const QByteArray &ianaId)
1589 return QTimeZonePrivate::ianaIdToWindowsId(ianaId);
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1605QByteArray QTimeZone::windowsIdToDefaultIanaId(
const QByteArray &windowsId)
1607 return QTimeZonePrivate::windowsIdToDefaultIanaId(windowsId);
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1630QByteArray QTimeZone::windowsIdToDefaultIanaId(
const QByteArray &windowsId,
1631 QLocale::Territory territory)
1633 return QTimeZonePrivate::windowsIdToDefaultIanaId(windowsId, territory);
1637
1638
1639
1640
1641
1642
1643
1644
1646QList<QByteArray> QTimeZone::windowsIdToIanaIds(
const QByteArray &windowsId)
1648 return QTimeZonePrivate::windowsIdToIanaIds(windowsId);
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1667QList<QByteArray> QTimeZone::windowsIdToIanaIds(
const QByteArray &windowsId,
1668 QLocale::Territory territory)
1670 return QTimeZonePrivate::windowsIdToIanaIds(windowsId, territory);
1674
1675
1676
1677
1678
1679
1680
1681
1682
1685template <
typename Stream,
typename Wrap>
1686void QTimeZone::Data::serialize(Stream &out,
const Wrap &wrap)
const
1691 out << wrap(
"QTimeZone::UTC");
1694 out << wrap(
"QTimeZone::LocalTime");
1696 case Qt::OffsetFromUTC:
1697 out << wrap(
"AheadOfUtcBy") <<
int(s.offset);
1705#if QT_CONFIG(timezone)
1706 if constexpr (std::is_same<Stream, QDataStream>::value) {
1711 out << QString::fromUtf8(d ? QByteArrayView(d->id()) : QByteArrayView());
1716#ifndef QT_NO_DATASTREAM
1722 const auto toQString = [](
const char *text) {
1723 return QString(QLatin1StringView(text));
1726 tz.d.serialize(ds, toQString);
1737 if (ianaId == invalidId()) {
1739 }
else if (ianaId ==
"OffsetFromUtc"_L1) {
1742 QString abbreviation;
1745 ds >> ianaId >> utcOffset >> name >> abbreviation >> territory >> comment;
1746#if QT_CONFIG(timezone)
1750 tz = QTimeZone(ianaId.toUtf8());
1751 if (!tz.isValid() || tz.hasDaylightTime()
1752 || tz.offsetFromUtc(QDateTime::fromMSecsSinceEpoch(0, QTimeZone::UTC)) != utcOffset) {
1754 tz = QTimeZone(ianaId.toUtf8(), utcOffset, name, abbreviation,
1755 QLocale::Territory(territory), comment);
1758 tz = QTimeZone::fromSecondsAheadOfUtc(utcOffset);
1760 }
else if (ianaId ==
"AheadOfUtcBy"_L1) {
1763 tz = QTimeZone::fromSecondsAheadOfUtc(utcOffset);
1764 }
else if (ianaId ==
"QTimeZone::UTC"_L1) {
1765 tz = QTimeZone(QTimeZone::UTC);
1766 }
else if (ianaId ==
"QTimeZone::LocalTime"_L1) {
1767 tz = QTimeZone(QTimeZone::LocalTime);
1768#if QT_CONFIG(timezone)
1770 tz = QTimeZone(ianaId.toUtf8());
1777#ifndef QT_NO_DEBUG_STREAM
1780 QDebugStateSaver saver(dbg);
1781 const auto asIs = [](
const char *text) {
return text; };
1783 dbg.nospace() <<
"QTimeZone(";
1784 tz.d.serialize(dbg, asIs);
1785 dbg.nospace() <<
')';
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
bool comparesEqual(const QFileInfo &lhs, const QFileInfo &rhs)
static QString invalidId()
QDataStream & operator<<(QDataStream &stream, const QImage &image)
[0]
QDataStream & operator>>(QDataStream &stream, QImage &image)