14#include "private/qcalendarmath_p.h"
15#include "private/qdatetime_p.h"
17#include "private/qcore_mac_p.h"
19#include "private/qgregoriancalendar_p.h"
20#include "private/qlocale_tools_p.h"
21#include "private/qlocaltime_p.h"
22#include "private/qnumeric_p.h"
23#include "private/qstringconverter_p.h"
24#include "private/qstringiterator_p.h"
25#if QT_CONFIG(timezone)
26#include "private/qtimezoneprivate_p.h"
28#if QT_CONFIG(datestring)
29# include "private/qttemporalpattern_p.h"
34# include <qt_windows.h>
37#include <private/qtools_p.h>
41using namespace Qt::StringLiterals;
42using namespace QtPrivate::DateTimeConstants;
43using namespace QtMiscUtils;
46
47
50
51
52static_assert(std::is_trivially_copyable_v<QCalendar::YearMonthDay>);
54static inline QDate
fixedDate(QCalendar::YearMonthDay parts, QCalendar cal)
56 if ((parts.year < 0 && !cal.isProleptic()) || (parts.year == 0 && !cal.hasYearZero()))
59 parts.day = qMin(parts.day, cal.daysInMonth(parts.month, parts.year));
60 return cal.dateFromParts(parts);
63static inline QDate
fixedDate(QCalendar::YearMonthDay parts)
66 parts.day = qMin(parts.day, QGregorianCalendar::monthLength(parts.month, parts.year));
67 const auto jd = QGregorianCalendar::julianFromParts(parts.year, parts.month, parts.day);
69 return QDate::fromJulianDay(*jd);
75
76
78#if QT_CONFIG(textdate)
79static const char qt_shortMonthNames[][4] = {
80 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
81 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
84static int fromShortMonthName(QStringView monthName)
86 for (
unsigned int i = 0; i <
sizeof(qt_shortMonthNames) /
sizeof(qt_shortMonthNames[0]); ++i) {
87 if (monthName == QLatin1StringView(qt_shortMonthNames[i], 3))
94#if QT_CONFIG(datestring)
96using ParsedInt = QSimpleParsedNumber<qulonglong>;
99
100
101ParsedInt readInt(QLatin1StringView text)
107 if (text.isEmpty() || !isAsciiDigit(text.front().toLatin1()))
110 QSimpleParsedNumber res = qstrntoull(text.data(), text.size(), 10);
111 return res.used == text.size() ? res : ParsedInt{};
114ParsedInt readInt(QStringView text)
124 QVarLengthArray<
char> latin1(text.size());
125 QLatin1::convertFromUnicode(latin1.data(), text);
126 return readInt(QLatin1StringView{latin1.data(), latin1.size()});
131struct ParsedRfcDateTime {
137static int shortDayFromName(QStringView name)
139 const char16_t shortDayNames[] = u"MonTueWedThuFriSatSun";
140 for (
int i = 0; i < 7; i++) {
141 if (name == QStringView(shortDayNames + 3 * i, 3))
147static ParsedRfcDateTime rfcDateImpl(QStringView s)
151 ParsedRfcDateTime result;
153 QVarLengthArray<QStringView, 6> words;
155 auto tokens = s.tokenize(u' ', Qt::SkipEmptyParts);
156 auto it = tokens.begin();
157 for (
int i = 0; i < 6 && it != tokens.end(); ++i, ++it)
158 words.emplace_back(*it);
160 if (words.size() < 3 || it != tokens.end())
162 const QChar colon(u':');
166 const auto isShortName = [](QStringView name) {
167 return (name.size() == 3 && name[0].isUpper()
168 && name[1].isLower() && name[2].isLower());
172
173
178 const QStringView maybeDayName = words.front();
179 if (maybeDayName.endsWith(u',')) {
180 dayName = maybeDayName.chopped(1);
181 words.erase(words.begin());
182 }
else if (!maybeDayName.front().isDigit()) {
183 dayName = maybeDayName;
184 words.erase(words.begin());
187 if (words.size() < 3 || words.size() > 5)
191 int dayIndex, monthIndex;
201 yearIndex = words.size() > 3 && words.at(2).contains(colon) ? 3 : 2;
203 if (words.at(yearIndex).size() != 4)
207 if (!dayName.isEmpty()) {
208 if (!isShortName(dayName))
210 dayOfWeek = shortDayFromName(dayName);
215 const int day = words.at(dayIndex).toInt(&ok);
218 const int year = words.at(yearIndex).toInt(&ok);
221 const QStringView monthName = words.at(monthIndex);
222 if (!isShortName(monthName))
224 int month = fromShortMonthName(monthName);
228 date = QDate(year, month, day);
229 if (dayOfWeek && date.dayOfWeek() != dayOfWeek)
232 words.remove(yearIndex);
237 if (words.size() && words.at(0).contains(colon)) {
238 const QStringView when = words.front();
239 words.erase(words.begin());
240 if (when.size() < 5 || when[2] != colon
241 || (when.size() == 8 ? when[5] != colon : when.size() > 5)) {
244 const int hour = when.first(2).toInt(&ok);
247 const int minute = when.sliced(3, 2).toInt(&ok);
250 const auto secs = when.size() == 8 ? when.last(2).toInt(&ok) : 0;
253 time = QTime(hour, minute, secs);
259 const QStringView zone = words.front();
260 words.erase(words.begin());
261 if (words.size() || !(zone.size() == 3 || zone.size() == 5))
266 else if (zone[0] != u'+')
268 const int hour = zone.sliced(1, 2).toInt(&ok);
271 const auto minute = zone.size() == 5 ? zone.last(2).toInt(&ok) : 0;
274 offset = (hour * 60 + minute) * 60;
281 result.utcOffset = offset;
289 return QString::asprintf(
"%c%02d%s%02d",
290 offset >= 0 ?
'+' :
'-',
291 qAbs(offset) /
int(SECS_PER_HOUR),
293 format == Qt::TextDate ?
"" :
":",
294 (qAbs(offset) / 60) % 60);
297#if QT_CONFIG(datestring)
299static int fromOffsetString(QStringView offsetString,
bool *valid)
noexcept
303 const qsizetype size = offsetString.size();
304 if (size < 2 || size > 6)
311 const QChar signChar = offsetString[0];
312 if (signChar == u'+')
314 else if (signChar == u'-')
320 const QStringView time = offsetString.sliced(1);
321 qsizetype hhLen = time.indexOf(u':');
328 const QStringView hhRef = time.first(qMin(hhLen, time.size()));
330 const int hour = hhRef.toInt(&ok);
331 if (!ok || hour > 23)
334 const QStringView mmRef = time.sliced(qMin(mmIndex, time.size()));
335 const int minute = mmRef.isEmpty() ? 0 : mmRef.toInt(&ok);
336 if (!ok || minute < 0 || minute > 59)
340 return sign * ((hour * 60) + minute) * 60;
345
346
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
430
431
432
433
434
435
438
439
440
441
442
443
444
445
446
448QDate::QDate(
int y,
int m,
int d)
450 static_assert(maxJd() == JulianDayMax);
451 static_assert(minJd() == JulianDayMin);
452 jd = QGregorianCalendar::julianFromParts(y, m, d).value_or(nullJd());
455QDate::QDate(
int y,
int m,
int d, QCalendar cal)
457 *
this = cal.dateFromParts(y, m, d);
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
485
486
487
488
489
490
491
492
493
494
497
498
499
500
501
502
503
504
505
506
509
510
511
512
513
514
515
516
517
520
521
522
523
524
525
526
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
552int QDate::year(QCalendar cal)
const
555 const auto parts = cal.partsFromDate(*
this);
563
564
566int QDate::year()
const
569 const auto parts = QGregorianCalendar::partsFromJulian(jd);
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
606int QDate::month(QCalendar cal)
const
609 const auto parts = cal.partsFromDate(*
this);
617
618
620int QDate::month()
const
623 const auto parts = QGregorianCalendar::partsFromJulian(jd);
631
632
633
634
635
636
637
638
639
641int QDate::day(QCalendar cal)
const
644 const auto parts = cal.partsFromDate(*
this);
652
653
655int QDate::day()
const
658 const auto parts = QGregorianCalendar::partsFromJulian(jd);
666
667
668
669
670
671
672
673
674
675
677int QDate::dayOfWeek(QCalendar cal)
const
682 return cal.dayOfWeek(*
this);
686
687
689int QDate::dayOfWeek()
const
691 return isValid() ? QGregorianCalendar::weekDayOfJulian(jd) : 0;
695
696
697
698
699
700
701
702
703
705int QDate::dayOfYear(QCalendar cal)
const
708 QDate firstDay = cal.dateFromParts(year(cal), 1, 1);
709 if (firstDay.isValid())
710 return firstDay.daysTo(*
this) + 1;
716
719int QDate::dayOfYear()
const
722 if (
const auto first = QGregorianCalendar::julianFromParts(year(), 1, 1))
723 return jd - *first + 1;
729
730
731
732
733
734
735
736
737
738
740int QDate::daysInMonth(QCalendar cal)
const
743 const auto parts = cal.partsFromDate(*
this);
745 return cal.daysInMonth(parts.month, parts.year);
751
752
754int QDate::daysInMonth()
const
757 const auto parts = QGregorianCalendar::partsFromJulian(jd);
759 return QGregorianCalendar::monthLength(parts.month, parts.year);
765
766
767
768
769
770
771
772
773
775int QDate::daysInYear(QCalendar cal)
const
780 return cal.daysInYear(year(cal));
784
785
787int QDate::daysInYear()
const
789 return isValid() ? QGregorianCalendar::leapTest(year()) ? 366 : 365 : 0;
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
811int QDate::weekNumber(
int *yearNumber)
const
818 const QDate thursday(addDays(4 - dayOfWeek()));
820 *yearNumber = thursday.year();
823 return (thursday.dayOfYear() + 6) / 7;
826#if QT_DEPRECATED_SINCE(6
, 9
)
828static QTimeZone asTimeZone(Qt::TimeSpec spec,
int offset,
const char *warner)
833 qWarning(
"%s: Pass a QTimeZone instead of Qt::TimeZone.", warner);
837 qWarning(
"%s: Ignoring offset (%d seconds) passed with Qt::LocalTime",
843 qWarning(
"%s: Ignoring offset (%d seconds) passed with Qt::UTC",
848 case Qt::OffsetFromUTC:
852 return QTimeZone::isUtcOrFixedOffset(spec)
853 ? QTimeZone::fromSecondsAheadOfUtc(offset)
854 : QTimeZone(QTimeZone::LocalTime);
862 using Bounds = std::numeric_limits<qint64>;
863 if (jd < Bounds::min() + JULIAN_DAY_FOR_EPOCH)
865 jd -= JULIAN_DAY_FOR_EPOCH;
866 const qint64 maxDay = Bounds::max() / MSECS_PER_DAY;
867 const qint64 minDay = Bounds::min() / MSECS_PER_DAY - 1;
873 return jd > minDay && jd <= maxDay;
875 return jd >= minDay && jd < maxDay;
877 Q_UNREACHABLE_RETURN(
false);
882 Q_ASSERT(!zone.isUtcOrFixedOffset());
884 const auto moment = [=](QTime time) {
885 return QDateTime(day, time, zone, QDateTime::TransitionResolution::Reject);
888 QDateTime when = moment(QTime(2, 0));
889 if (!when.isValid()) {
891 when = moment(QTime(12, 0));
892 if (!when.isValid()) {
894 when = moment(QTime(23, 59, 59, 999));
899 int high = when.time().msecsSinceStartOfDay() / 60000;
902 while (high > low + 1) {
903 const int mid = (high + low) / 2;
904 const QDateTime probe = QDateTime(day, QTime(mid / 60, mid % 60), zone,
905 QDateTime::TransitionResolution::PreferBefore);
906 if (probe.isValid() && probe.date() == day) {
916 if (QDateTime p = moment(when.time().addSecs(-1)); Q_UNLIKELY(p.isValid() && p.date() == day)) {
919 while (high > low + 1) {
920 const int mid = (high + low) / 2;
921 const int min = mid / 60;
922 const QDateTime probe = moment(QTime(min / 60, min % 60, mid % 60));
923 if (probe.isValid() && probe.date() == day) {
931 return when.isValid() ? when : QDateTime();
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963QDateTime QDate::startOfDay(
const QTimeZone &zone)
const
965 if (!inDateTimeRange(jd, DaySide::Start) || !zone.isValid())
968 QDateTime when(*
this, QTime(0, 0), zone,
969 QDateTime::TransitionResolution::RelativeToBefore);
970 if (Q_UNLIKELY(!when.isValid() || when.date() != *
this)) {
971#if QT_CONFIG(timezone)
973 if (zone.timeSpec() == Qt::TimeZone && zone.hasTransitions()) {
974 QTimeZone::OffsetData tran
977 = zone.previousTransition(QDateTime(addDays(1), QTime(12, 0), zone));
978 const QDateTime &at = tran.atUtc.toTimeZone(zone);
979 if (at.isValid() && at.date() == *
this)
984 when = toEarliest(*
this, zone);
991
992
994QDateTime QDate::startOfDay()
const
996 return startOfDay(QTimeZone::LocalTime);
999#if QT_DEPRECATED_SINCE(6
, 9
)
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028QDateTime QDate::startOfDay(Qt::TimeSpec spec,
int offsetSeconds)
const
1030 QTimeZone zone = asTimeZone(spec, offsetSeconds,
"QDate::startOfDay");
1032 return zone.timeSpec() == spec ? startOfDay(zone) : QDateTime();
1036static QDateTime
toLatest(QDate day,
const QTimeZone &zone)
1038 Q_ASSERT(!zone.isUtcOrFixedOffset());
1040 const auto moment = [=](QTime time) {
1041 return QDateTime(day, time, zone, QDateTime::TransitionResolution::Reject);
1044 QDateTime when = moment(QTime(21, 59, 59, 999));
1045 if (!when.isValid()) {
1047 when = moment(QTime(12, 0));
1048 if (!when.isValid()) {
1050 when = moment(QTime(0, 0));
1051 if (!when.isValid())
1056 int low = when.time().msecsSinceStartOfDay() / 60000;
1058 while (high > low + 1) {
1059 const int mid = (high + low) / 2;
1060 const QDateTime probe = QDateTime(day, QTime(mid / 60, mid % 60, 59, 999), zone,
1061 QDateTime::TransitionResolution::PreferAfter);
1062 if (probe.isValid() && probe.date() == day) {
1072 if (QDateTime p = moment(when.time().addSecs(1)); Q_UNLIKELY(p.isValid() && p.date() == day)) {
1075 while (high > low + 1) {
1076 const int mid = (high + low) / 2;
1077 const int min = mid / 60;
1078 const QDateTime probe = moment(QTime(min / 60, min % 60, mid % 60, 999));
1079 if (probe.isValid() && probe.date() == day) {
1087 return when.isValid() ? when : QDateTime();
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120QDateTime QDate::endOfDay(
const QTimeZone &zone)
const
1122 if (!inDateTimeRange(jd, DaySide::End) || !zone.isValid())
1125 QDateTime when(*
this, QTime(23, 59, 59, 999), zone,
1126 QDateTime::TransitionResolution::RelativeToAfter);
1127 if (Q_UNLIKELY(!when.isValid() || when.date() != *
this)) {
1128#if QT_CONFIG(timezone)
1130 if (zone.timeSpec() == Qt::TimeZone && zone.hasTransitions()) {
1131 QTimeZone::OffsetData tran
1134 = zone.nextTransition(QDateTime(addDays(-1), QTime(12, 0), zone));
1135 const QDateTime &at = tran.atUtc.toTimeZone(zone);
1136 if (at.isValid() && at.date() == *
this)
1141 when = toLatest(*
this, zone);
1147
1148
1150QDateTime QDate::endOfDay()
const
1152 return endOfDay(QTimeZone::LocalTime);
1155#if QT_DEPRECATED_SINCE(6
, 9
)
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184QDateTime QDate::endOfDay(Qt::TimeSpec spec,
int offsetSeconds)
const
1186 QTimeZone zone = asTimeZone(spec, offsetSeconds,
"QDate::endOfDay");
1188 return endOfDay(zone);
1192#if QT_CONFIG(datestring)
1194static QString toStringTextDate(QDate date)
1196 if (date.isValid()) {
1198 const auto parts = cal.partsFromDate(date);
1199 if (parts.isValid()) {
1200 const QLatin1Char sp(
' ');
1201 return QLocale::c().dayName(cal.dayOfWeek(date), QLocale::ShortFormat) + sp
1202 + cal.monthName(QLocale::c(), parts.month, parts.year, QLocale::ShortFormat)
1204 + sp + QString::asprintf(
"%d %04d", parts.day, parts.year);
1210static QString toStringIsoDate(QDate date)
1212 const auto parts = QCalendar().partsFromDate(date);
1213 if (parts.isValid() && parts.year >= 0 && parts.year <= 9999)
1214 return QString::asprintf(
"%04d-%02d-%02d", parts.year, parts.month, parts.day);
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246QString QDate::toString(Qt::DateFormat format)
const
1252 case Qt::RFC2822Date:
1253 return QLocale::c().toString(*
this, u"dd MMM yyyy");
1256 return toStringTextDate(*
this);
1258 case Qt::ISODateWithMs:
1260 return toStringIsoDate(*
this);
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332QString QDate::toString(QStringView format, QCalendar cal)
const
1334 return QLocale::c().toString(*
this, format, cal);
1339
1340
1341
1342QString QDate::toString(QStringView format)
const
1344 return QLocale::c().toString(*
this, format, QCalendar());
1348
1349
1350
1351QString QDate::toString(
const QString &format)
const
1353 return QLocale::c().toString(*
this, qToStringViewIgnoringNull(format), QCalendar());
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367bool QDate::setDate(
int year,
int month,
int day)
1369 const auto maybe = QGregorianCalendar::julianFromParts(year, month, day);
1370 jd = maybe.value_or(nullJd());
1375
1376
1377
1378
1379
1380
1381
1382
1383
1385bool QDate::setDate(
int year,
int month,
int day, QCalendar cal)
1387 *
this = QDate(year, month, day, cal);
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403void QDate::getDate(
int *year,
int *month,
int *day)
const
1405 QCalendar::YearMonthDay parts;
1407 parts = QGregorianCalendar::partsFromJulian(jd);
1409 const bool ok = parts.isValid();
1411 *year = ok ? parts.year : 0;
1413 *month = ok ? parts.month : 0;
1415 *day = ok ? parts.day : 0;
1419
1420
1421
1422
1423
1424
1425
1426
1428QDate QDate::addDays(qint64 ndays)
const
1433 if (qint64 r; Q_UNLIKELY(qAddOverflow(jd, ndays, &r)))
1436 return fromJulianDay(r);
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1475QDate QDate::addMonths(
int nmonths, QCalendar cal)
const
1483 auto parts = cal.partsFromDate(*
this);
1485 if (!parts.isValid())
1487 Q_ASSERT(parts.year || cal.hasYearZero());
1489 parts.month += nmonths;
1490 while (parts.month <= 0) {
1491 if (--parts.year || cal.hasYearZero())
1492 parts.month += cal.monthsInYear(parts.year);
1494 int count = cal.monthsInYear(parts.year);
1495 while (parts.month > count) {
1496 parts.month -= count;
1497 count = (++parts.year || cal.hasYearZero()) ? cal.monthsInYear(parts.year) : 0;
1500 return fixedDate(parts, cal);
1504
1505
1507QDate QDate::addMonths(
int nmonths)
const
1515 auto parts = QGregorianCalendar::partsFromJulian(jd);
1517 if (!parts.isValid())
1519 Q_ASSERT(parts.year);
1521 parts.month += nmonths;
1522 while (parts.month <= 0) {
1526 while (parts.month > 12) {
1532 return fixedDate(parts);
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1551QDate QDate::addYears(
int nyears, QCalendar cal)
const
1556 auto parts = cal.partsFromDate(*
this);
1557 if (!parts.isValid())
1560 int old_y = parts.year;
1561 parts.year += nyears;
1564 if (!cal.hasYearZero() && ((old_y > 0) != (parts.year > 0) || !parts.year))
1565 parts.year += nyears > 0 ? +1 : -1;
1567 return fixedDate(parts, cal);
1571
1572
1574QDate QDate::addYears(
int nyears)
const
1579 auto parts = QGregorianCalendar::partsFromJulian(jd);
1580 if (!parts.isValid())
1583 int old_y = parts.year;
1584 parts.year += nyears;
1587 if ((old_y > 0) != (parts.year > 0) || !parts.year)
1588 parts.year += nyears > 0 ? +1 : -1;
1590 return fixedDate(parts);
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1606qint64 QDate::daysTo(QDate d)
const
1608 if (isNull() || d.isNull())
1617
1618
1619
1620
1621
1624
1625
1626
1627
1628
1629
1630
1633
1634
1635
1636
1639
1640
1641
1642
1643
1646
1647
1648
1649
1652
1653
1654
1655
1656
1659
1660
1661
1662
1663
1665#if QT_CONFIG(datestring)
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1682
1683
1684
1685QDate QDate::fromString(QStringView string, Qt::DateFormat format)
1687 if (string.isEmpty())
1691 case Qt::RFC2822Date:
1692 return rfcDateImpl(string).date;
1694 case Qt::TextDate: {
1696 QVarLengthArray<QStringView, 4> parts;
1697 auto tokens = string.tokenize(u' ', Qt::SkipEmptyParts);
1698 auto it = tokens.begin();
1699 for (
int i = 0; i < 4 && it != tokens.end(); ++i, ++it)
1700 parts.emplace_back(*it);
1702 if (parts.size() != 4 || it != tokens.end())
1706 int year = parts.at(3).toInt(&ok);
1707 int day = ok ? parts.at(2).toInt(&ok) : 0;
1711 const int month = fromShortMonthName(parts.at(1));
1715 return QDate(year, month, day);
1719 if (string.size() == 10 && string[4].isPunct() && string[7].isPunct()) {
1720 const ParsedInt year = readInt(string.first(4));
1721 const ParsedInt month = readInt(string.sliced(5, 2));
1722 const ParsedInt day = readInt(string.sliced(8, 2));
1723 if (year.ok() && year.result > 0 && year.result <= 9999 && month.ok() && day.ok())
1724 return QDate(year.result, month.result, day.result);
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1879
1880
1881
1882
1885
1886
1887
1888QDate QDate::fromString(
const QString &string, QStringView format,
int baseYear, QCalendar cal)
1890#if QT_CONFIG(datetimeparser)
1891 QDatePattern pattern = QDatePattern::fromQtFormat(format);
1892 if (pattern.isNull() && !format.isEmpty())
1894 pattern.setLocale(QLocale::c());
1895 pattern.setCalendar(cal);
1896 pattern.setBaseYear(baseYear);
1897 if (
auto match = pattern.parse(string, QDate(baseYear, 1, 1, cal));
1898 match.size == string.size()) {
1899 return std::move(match.payload);
1911
1912
1913
1914
1917
1918
1919
1920
1923
1924
1925
1926
1929
1930
1931
1932
1933
1934
1937
1938
1939
1940
1941
1942QDate QDate::fromString(
const QString &string, QStringView format,
int baseYear)
1944 return fromString(string, format, baseYear, QCalendar());
1948
1949
1950
1951
1952
1953
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1968bool QDate::isValid(
int year,
int month,
int day)
1970 return QGregorianCalendar::validParts(year, month, day);
1974
1975
1976
1977
1978
1979
1980
1982bool QDate::isLeapYear(
int y)
1984 return QGregorianCalendar::leapTest(y);
1988
1989
1990
1991
1992
1995
1996
1997
1998
1999
2002
2003
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2052
2053
2054
2055
2056
2057
2058
2059
2062
2063
2064
2065
2066
2067
2068
2069
2071QTime::QTime(
int h,
int m,
int s,
int ms)
2073 setHMS(h, m, s, ms);
2078
2079
2080
2081
2082
2083
2084
2085
2088
2089
2090
2091
2092
2093
2094
2096bool QTime::isValid()
const
2098 return mds > NullTime && mds < MSECS_PER_DAY;
2103
2104
2105
2106
2107
2108
2110int QTime::hour()
const
2115 return ds() / MSECS_PER_HOUR;
2119
2120
2121
2122
2123
2124
2126int QTime::minute()
const
2131 return (ds() % MSECS_PER_HOUR) / MSECS_PER_MIN;
2135
2136
2137
2138
2139
2140
2142int QTime::second()
const
2147 return (ds() / MSECS_PER_SEC) % SECS_PER_MIN;
2151
2152
2153
2154
2155
2156
2158int QTime::msec()
const
2163 return ds() % MSECS_PER_SEC;
2166#if QT_CONFIG(datestring)
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2191QString QTime::toString(Qt::DateFormat format)
const
2197 case Qt::ISODateWithMs:
2198 return QString::asprintf(
"%02d:%02d:%02d.%03d", hour(), minute(), second(), msec());
2199 case Qt::RFC2822Date:
2203 return QString::asprintf(
"%02d:%02d:%02d", hour(), minute(), second());
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310QString QTime::toString(QStringView format)
const
2312 return QLocale::c().toString(*
this, format);
2318
2319
2320
2321
2322
2323
2324
2325
2326
2328bool QTime::setHMS(
int h,
int m,
int s,
int ms)
2330 if (!isValid(h,m,s,ms)) {
2334 mds = ((h * MINS_PER_HOUR + m) * SECS_PER_MIN + s) * MSECS_PER_SEC + ms;
2335 Q_ASSERT(mds >= 0 && mds < MSECS_PER_DAY);
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2354QTime QTime::addSecs(
int s)
const
2357 return addMSecs(s * MSECS_PER_SEC);
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2375int QTime::secsTo(QTime t)
const
2377 if (!isValid() || !t.isValid())
2381 int ourSeconds = ds() / MSECS_PER_SEC;
2382 int theirSeconds = t.ds() / MSECS_PER_SEC;
2383 return theirSeconds - ourSeconds;
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2398QTime QTime::addMSecs(
int ms)
const
2402 t.mds = QRoundingDown::qMod<MSECS_PER_DAY>(ds() + ms);
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2420int QTime::msecsTo(QTime t)
const
2422 if (!isValid() || !t.isValid())
2424 return t.ds() - ds();
2429
2430
2431
2432
2435
2436
2437
2438
2441
2442
2443
2444
2447
2448
2449
2450
2451
2454
2455
2456
2457
2460
2461
2462
2463
2464
2467
2468
2469
2470
2471
2472
2473
2474
2475
2478
2479
2480
2481
2482
2483
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2500#if QT_CONFIG(datestring)
2502static QTime fromIsoTimeString(QStringView string, Qt::DateFormat format,
bool *isMidnight24)
2504 Q_ASSERT(format == Qt::TextDate || format == Qt::ISODate || format == Qt::ISODateWithMs);
2506 *isMidnight24 =
false;
2512 const qsizetype dot = string.indexOf(u'.'), comma = string.indexOf(u',');
2514 tail = string.sliced(dot + 1);
2515 if (tail.indexOf(u'.') != -1)
2517 string = string.first(dot);
2518 }
else if (comma != -1) {
2519 tail = string.sliced(comma + 1);
2520 string = string.first(comma);
2522 if (tail.indexOf(u',') != -1)
2525 double fraction = 0.0;
2526 bool hasFraction =
false;
2527 if (tail.isEmpty()) {
2528 if (dot != -1 || comma != -1)
2534 const auto isDigit = [](QChar ch) {
return ch >= u'0' && ch <= u'9'; };
2535 constexpr qsizetype MaxFractionDigits = std::numeric_limits<
double>::digits10;
2536 const qsizetype fractionDigits = qMin(tail.size(), MaxFractionDigits);
2538 qulonglong digits = 0;
2540 for (qsizetype i = 0; i < fractionDigits; ++i) {
2541 if (!isDigit(tail[i]))
2543 digits = digits * 10 + (tail[i].unicode() - u'0');
2546 if (!std::all_of(tail.begin() + fractionDigits, tail.end(), isDigit))
2548 fraction = digits / scale;
2550 Q_ASSERT(!(fraction < 0.0) && fraction < 1.0);
2552 Q_ASSERT(hasFraction ^ tail.isEmpty());
2554 const qsizetype size = string.size();
2555 if (size < 2 || size > 8)
2558 ParsedInt hour = readInt(string.first(2));
2559 if (!hour.ok() || hour.result > (format == Qt::TextDate ? 23 : 24))
2563 if (string.size() > 2) {
2564 if (string[2] == u':' && string.size() > 4)
2565 minute = readInt(string.sliced(3, 2));
2566 if (!minute.ok() || minute.result >= MINS_PER_HOUR)
2568 }
else if (format == Qt::TextDate) {
2570 }
else if (hasFraction) {
2571 fraction *= MINS_PER_HOUR;
2572 minute.result = qulonglong(fraction);
2573 fraction -= minute.result;
2577 if (string.size() > 5) {
2578 if (string[5] == u':' && string.size() == 8)
2579 second = readInt(string.sliced(6, 2));
2580 if (!second.ok() || second.result >= SECS_PER_MIN)
2582 }
else if (hasFraction) {
2583 if (format == Qt::TextDate)
2585 Q_ASSERT(!(fraction < 0.0) && fraction < 1.0);
2586 fraction *= SECS_PER_MIN;
2587 second.result = qulonglong(fraction);
2588 fraction -= second.result;
2591 Q_ASSERT(!(fraction < 0.0) && fraction < 1.0);
2593 int msec = hasFraction ? qRound(MSECS_PER_SEC * fraction) : 0;
2595 if (msec == MSECS_PER_SEC) {
2598 if (isMidnight24 || hour.result < 23 || minute.result < 59 || second.result < 59) {
2600 if (++second.result == SECS_PER_MIN) {
2602 if (++minute.result == MINS_PER_HOUR) {
2611 msec = MSECS_PER_SEC - 1;
2616 if (hour.result == 24 && minute.result == 0 && second.result == 0 && msec == 0) {
2617 Q_ASSERT(format != Qt::TextDate);
2619 *isMidnight24 =
true;
2623 return QTime(hour.result, minute.result, second.result, msec);
2627
2628
2629
2630
2631
2632
2633
2634
2637
2638
2639
2640QTime QTime::fromString(QStringView string, Qt::DateFormat format)
2642 if (string.isEmpty())
2646 case Qt::RFC2822Date:
2647 return rfcDateImpl(string).time;
2649 case Qt::ISODateWithMs:
2652 return fromIsoTimeString(string, format,
nullptr);
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2730
2731
2732
2733
2736
2737
2738
2739QTime QTime::fromString(
const QString &string, QStringView format)
2741#if QT_CONFIG(datetimeparser)
2742 QTimePattern pattern = QTimePattern::fromQtFormat(format);
2743 if (pattern.isNull() && !format.isEmpty())
2745 pattern.setLocale(QLocale::c());
2746 if (
auto match = pattern.parse(string, QTime(0, 0)); match.size == string.size())
2747 return std::move(match.payload);
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2771bool QTime::isValid(
int h,
int m,
int s,
int ms)
2773 return (uint(h) < 24 && uint(m) < MINS_PER_HOUR && uint(s) < SECS_PER_MIN
2774 && uint(ms) < MSECS_PER_SEC);
2778
2779
2788 return JULIAN_DAY_FOR_EPOCH + QRoundingDown::qDiv<MSECS_PER_DAY>(msecs);
2793 return QDate::fromJulianDay(msecsToJulianDay(msecs));
2798 return QTime::fromMSecsSinceStartOfDay(QRoundingDown::qMod<MSECS_PER_DAY>(msecs));
2805 return qMulOverflow(days, std::integral_constant<qint64, MSECS_PER_DAY>(), sumMillis)
2806 || qAddOverflow(*sumMillis, millisInDay, sumMillis);
2812 qint64 days = date.toJulianDay() - JULIAN_DAY_FOR_EPOCH;
2813 qint64 msecs, dayms = time.msecsSinceStartOfDay();
2814 if (days < 0 && dayms > 0) {
2816 dayms -= MSECS_PER_DAY;
2818 if (daysAndMillisOverflow(days, dayms, &msecs)) {
2819 using Bound = std::numeric_limits<qint64>;
2820 return days < 0 ? Bound::min() : Bound::max();
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2844 static const auto bounds = QLocalTime::computeSystemMillisRange();
2845 return (bounds.minClip || millis >= bounds.min - slack)
2846 && (bounds.maxClip || millis <= bounds.max + slack);
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2867#if defined(Q_OS_WIN) || defined(Q_OS_WASM)
2868 static constexpr int forLeapEarly[] = { 1984, 1996, 1980, 1992, 1976, 1988, 1972 };
2869 static constexpr int regularEarly[] = { 1978, 1973, 1974, 1975, 1970, 1971, 1977 };
2871 static constexpr int forLeapEarly[] = { 1928, 1912, 1924, 1908, 1920, 1904, 1916 };
2872 static constexpr int regularEarly[] = { 1905, 1906, 1907, 1902, 1903, 1909, 1910 };
2874 static constexpr int forLeapLate[] = { 2012, 2024, 2036, 2020, 2032, 2016, 2028 };
2875 static constexpr int regularLate[] = { 2034, 2035, 2030, 2031, 2037, 2027, 2033 };
2876 const int dow = QGregorianCalendar::yearStartWeekDay(year);
2877 Q_ASSERT(dow == QDate(year, 1, 1).dayOfWeek());
2878 const int res = (QGregorianCalendar::leapTest(year)
2879 ? (year < 1970 ? forLeapEarly : forLeapLate)
2880 : (year < 1970 ? regularEarly : regularLate))[dow == 7 ? 0 : dow];
2881 Q_ASSERT(QDate(res, 1, 1).dayOfWeek() == dow);
2882 Q_ASSERT(QDate(res, 12, 31).dayOfWeek() == QDate(year, 12, 31).dayOfWeek());
2887QDateTimePrivate::ZoneState QDateTimePrivate::expressUtcAsLocal(qint64 utcMSecs)
2889 ZoneState result{utcMSecs};
2891 if (millisInSystemRange(utcMSecs)) {
2892 result = QLocalTime::utcToLocal(utcMSecs);
2899#if QT_CONFIG(timezone)
2900 if (
const auto sys = QTimeZone::systemTimeZone(); sys.isValid()) {
2901 result.offset = sys.d->offsetFromUtc(utcMSecs);
2902 if (result.offset != QTimeZonePrivate::invalidSeconds()) {
2903 if (qAddOverflow(utcMSecs, result.offset * MSECS_PER_SEC, &result.when))
2905 result.dst = sys.d->isDaylightTime(utcMSecs) ? DaylightTime : StandardTime;
2906 result.valid =
true;
2915 const qint64 jd = msecsToJulianDay(utcMSecs);
2916 const auto ymd = QGregorianCalendar::partsFromJulian(jd);
2917 qint64 diffMillis, fakeUtc;
2918 const auto fakeJd = QGregorianCalendar::julianFromParts(systemTimeYearMatching(ymd.year),
2919 ymd.month, ymd.day);
2920 if (Q_UNLIKELY(!fakeJd
2921 || qMulOverflow(jd - *fakeJd, std::integral_constant<qint64, MSECS_PER_DAY>(),
2923 || qSubOverflow(utcMSecs, diffMillis, &fakeUtc))) {
2927 result = QLocalTime::utcToLocal(fakeUtc);
2929 if (!result.valid || qAddOverflow(result.when, diffMillis, &result.when)) {
2932 result.when = utcMSecs;
2933 result.valid =
false;
2944 qint64 jd = msecsToJulianDay(millis);
2945 auto ymd = QGregorianCalendar::partsFromJulian(jd);
2946 const auto fakeJd = QGregorianCalendar::julianFromParts(systemTimeYearMatching(ymd.year),
2947 ymd.month, ymd.day);
2948 result.good = fakeJd && !daysAndMillisOverflow(*fakeJd - jd, millis, &result.shifted);
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3010 case QDateTime::TransitionResolution::RelativeToBefore:
3011 return QDateTimePrivate::GapUseAfter | QDateTimePrivate::FoldUseBefore;
3012 case QDateTime::TransitionResolution::RelativeToAfter:
3013 return QDateTimePrivate::GapUseBefore | QDateTimePrivate::FoldUseAfter;
3014 case QDateTime::TransitionResolution::PreferBefore:
3015 return QDateTimePrivate::GapUseBefore | QDateTimePrivate::FoldUseBefore;
3016 case QDateTime::TransitionResolution::PreferAfter:
3017 return QDateTimePrivate::GapUseAfter | QDateTimePrivate::FoldUseAfter;
3018 case QDateTime::TransitionResolution::PreferStandard:
3019 return QDateTimePrivate::GapUseBefore
3020 | QDateTimePrivate::FoldUseAfter
3021 | QDateTimePrivate::FlipForReverseDst;
3022 case QDateTime::TransitionResolution::PreferDaylightSaving:
3023 return QDateTimePrivate::GapUseAfter
3024 | QDateTimePrivate::FoldUseBefore
3025 | QDateTimePrivate::FlipForReverseDst;
3026 case QDateTime::TransitionResolution::Reject:
break;
3034 return toTransitionOptions(dst == QDateTimePrivate::DaylightTime
3035 ? QDateTime::TransitionResolution::PreferDaylightSaving
3036 : QDateTime::TransitionResolution::PreferStandard);
3039QString QDateTimePrivate::localNameAtMillis(qint64 millis, DaylightStatus dst)
3041 const QDateTimePrivate::TransitionOptions resolve = toTransitionOptions(dst);
3042 QString abbreviation;
3043 if (millisInSystemRange(millis, MSECS_PER_DAY)) {
3044 abbreviation = QLocalTime::localTimeAbbreviationAt(millis, resolve);
3045 if (!abbreviation.isEmpty())
3046 return abbreviation;
3050#if QT_CONFIG(timezone)
3052 const auto sys = QTimeZone::systemTimeZone();
3053 if (sys.isValid()) {
3054 ZoneState state = zoneStateAtMillis(sys, millis, resolve);
3056 return sys.d->abbreviation(state.when - state.offset * MSECS_PER_SEC);
3062 auto fake = millisToWithinRange(millis);
3063 if (Q_LIKELY(fake.good))
3064 return QLocalTime::localTimeAbbreviationAt(fake.shifted, resolve);
3071QDateTimePrivate::ZoneState QDateTimePrivate::localStateAtMillis(
3072 qint64 millis, QDateTimePrivate::TransitionOptions resolve)
3076 if (millisInSystemRange(millis, MSECS_PER_DAY)) {
3077 auto result = QLocalTime::mapLocalTime(millis, resolve);
3083#if QT_CONFIG(timezone)
3085 const auto sys = QTimeZone::systemTimeZone();
3087 return zoneStateAtMillis(sys, millis, resolve);
3092 auto fake = millisToWithinRange(millis);
3093 if (Q_LIKELY(fake.good)) {
3094 auto result = QLocalTime::mapLocalTime(fake.shifted, resolve);
3097 if (Q_UNLIKELY(qAddOverflow(result.when, millis - fake.shifted, &adjusted))) {
3098 using Bound = std::numeric_limits<qint64>;
3099 adjusted = millis < fake.shifted ? Bound::min() : Bound::max();
3101 result.when = adjusted;
3103 result.when = millis;
3111#if QT_CONFIG(timezone)
3115QDateTimePrivate::ZoneState QDateTimePrivate::zoneStateAtMillis(
3116 const QTimeZone &zone, qint64 millis, QDateTimePrivate::TransitionOptions resolve)
3118 Q_ASSERT(zone.isValid());
3119 Q_ASSERT(zone.timeSpec() == Qt::TimeZone);
3120 return zone.d->stateAtZoneTime(millis, resolve);
3125 QDateTimePrivate::TransitionOptions resolve)
3127 if (zone.timeSpec() == Qt::LocalTime)
3128 return QDateTimePrivate::localStateAtMillis(millis, resolve);
3129#if QT_CONFIG(timezone)
3130 if (zone.timeSpec() == Qt::TimeZone && zone.isValid())
3131 return QDateTimePrivate::zoneStateAtMillis(zone, millis, resolve);
3138 return spec == Qt::LocalTime || spec == Qt::UTC;
3143 if constexpr (!QDateTimeData::CanBeSmall)
3147 sd.msecs = qintptr(msecs);
3148 return sd.msecs == msecs;
3151static constexpr inline
3154 status &= ~QDateTimePrivate::TimeSpecMask;
3155 status |= QDateTimePrivate::StatusFlags::fromInt(
int(spec) << QDateTimePrivate::TimeSpecShift);
3161 return Qt::TimeSpec((status & QDateTimePrivate::TimeSpecMask).toInt() >> QDateTimePrivate::TimeSpecShift);
3168 sf &= ~QDateTimePrivate::DaylightMask;
3169 if (status == QDateTimePrivate::DaylightTime) {
3170 sf |= QDateTimePrivate::SetToDaylightTime;
3171 }
else if (status == QDateTimePrivate::StandardTime) {
3172 sf |= QDateTimePrivate::SetToStandardTime;
3178static constexpr inline
3181 if (status.testFlag(QDateTimePrivate::SetToDaylightTime))
3182 return QDateTimePrivate::DaylightTime;
3183 if (status.testFlag(QDateTimePrivate::SetToStandardTime))
3184 return QDateTimePrivate::StandardTime;
3185 return QDateTimePrivate::UnknownDaylightTime;
3193 return qintptr(d.d) >> 8;
3203 return QDateTimePrivate::StatusFlag(qintptr(d.d) & 0xFF);
3210 return extractSpec(getStatus(d));
3214
3215
3216
3217
3220 const auto status = getStatus(a);
3221 if (status != getStatus(b))
3225 switch (extractSpec(status)) {
3232
3233
3234
3235
3236 case Qt::OffsetFromUTC:
3237 Q_ASSERT(!a.isShort() && !b.isShort());
3238 return a->m_offsetFromUtc == b->m_offsetFromUtc;
3240 Q_UNREACHABLE_RETURN(
false);
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3257 constexpr quint64 UtcOffsetMillisRange
3258 = quint64(QTimeZone::MaxUtcOffsetSecs - QTimeZone::MinUtcOffsetSecs) * MSECS_PER_SEC;
3260 return qSubOverflow(leftMillis, rightMillis, &gap) || QtPrivate::qUnsignedAbs(gap) > UtcOffsetMillisRange;
3265 QDateTimePrivate::TransitionOptions resolve)
3267 Q_ASSERT(zone.timeSpec() == Qt::TimeZone || zone.timeSpec() == Qt::LocalTime);
3268 auto status = getStatus(d);
3269 Q_ASSERT(extractSpec(status) == zone.timeSpec());
3270 int offsetFromUtc = 0;
3272
3273
3274
3275
3276
3277
3278
3279
3282 if (!status.testFlags(QDateTimePrivate::ValidDate | QDateTimePrivate::ValidTime)) {
3283 status.setFlag(QDateTimePrivate::ValidDateTime,
false);
3287 qint64 msecs = getMSecs(d);
3288 QDateTimePrivate::ZoneState state = stateAtMillis(zone, msecs, resolve);
3289 Q_ASSERT(!state.valid || (state.offset >= -SECS_PER_DAY && state.offset <= SECS_PER_DAY));
3290 if (state.dst == QDateTimePrivate::UnknownDaylightTime) {
3291 status.setFlag(QDateTimePrivate::ValidDateTime,
false);
3292 }
else if (state.valid) {
3293 status = mergeDaylightStatus(status, state.dst);
3294 offsetFromUtc = state.offset;
3295 status.setFlag(QDateTimePrivate::ValidDateTime,
true);
3296 if (Q_UNLIKELY(msecs != state.when)) {
3298 if (status.testFlag(QDateTimePrivate::ShortData)) {
3299 if (msecsCanBeSmall(state.when)) {
3300 d.data.msecs = qintptr(state.when);
3303 status.setFlag(QDateTimePrivate::ShortData,
false);
3307 if (!status.testFlag(QDateTimePrivate::ShortData))
3308 d->m_msecs = state.when;
3311 status.setFlag(QDateTimePrivate::ValidDateTime,
false);
3315 if (status.testFlag(QDateTimePrivate::ShortData)) {
3316 d.data.status = status.toInt();
3318 d->m_status = status;
3319 d->m_offsetFromUtc = offsetFromUtc;
3326 auto status = getStatus(d);
3327 Q_ASSERT(QTimeZone::isUtcOrFixedOffset(extractSpec(status)));
3328 status.setFlag(QDateTimePrivate::ValidDateTime,
3329 status.testFlags(QDateTimePrivate::ValidDate | QDateTimePrivate::ValidTime));
3331 if (status.testFlag(QDateTimePrivate::ShortData))
3332 d.data.status = status.toInt();
3334 d->m_status = status;
3340 auto spec = extractSpec(getStatus(d));
3342 case Qt::OffsetFromUTC:
3351 refreshZonedDateTime(d, d.timeZone(), toTransitionOptions(resolve));
3357 QDateTime::TransitionResolution resolve)
3359 Qt::TimeSpec spec = zone.timeSpec();
3360 auto status = mergeSpec(getStatus(d), spec);
3361 bool reuse = d.isShort();
3366 Q_ASSERT(zone.fixedSecondsAheadOfUtc() == 0);
3368 case Qt::OffsetFromUTC:
3370 offset = zone.fixedSecondsAheadOfUtc();
3380 status &= ~(QDateTimePrivate::ValidDateTime | QDateTimePrivate::DaylightMask);
3382 d.data.status = status.toInt();
3385 d->m_status = status & ~QDateTimePrivate::ShortData;
3386 d->m_offsetFromUtc = offset;
3387#if QT_CONFIG(timezone)
3388 if (spec == Qt::TimeZone)
3389 d->m_timeZone = zone;
3393 if (QTimeZone::isUtcOrFixedOffset(spec))
3396 refreshZonedDateTime(d, zone, toTransitionOptions(resolve));
3402 if (!time.isValid() && date.isValid())
3403 time = QTime::fromMSecsSinceStartOfDay(0);
3405 QDateTimePrivate::StatusFlags newStatus = { };
3409 if (date.isValid()) {
3410 days = date.toJulianDay() - JULIAN_DAY_FOR_EPOCH;
3411 newStatus = QDateTimePrivate::ValidDate;
3416 if (time.isValid()) {
3417 ds = time.msecsSinceStartOfDay();
3418 newStatus |= QDateTimePrivate::ValidTime;
3420 Q_ASSERT(ds < MSECS_PER_DAY);
3423 if (days < 0 && ds > 0) {
3425 ds -= MSECS_PER_DAY;
3430 if (daysAndMillisOverflow(days, qint64(ds), &msecs)) {
3431 newStatus = QDateTimePrivate::StatusFlags{};
3436 if (msecsCanBeSmall(msecs)) {
3438 d.data.msecs = qintptr(msecs);
3439 d.data.status &= ~(QDateTimePrivate::ValidityMask | QDateTimePrivate::DaylightMask).toInt();
3440 d.data.status |= newStatus.toInt();
3449 d->m_status &= ~(QDateTimePrivate::ValidityMask | QDateTimePrivate::DaylightMask);
3450 d->m_status |= newStatus;
3456 auto status = getStatus(d);
3457 const qint64 msecs = getMSecs(d);
3458 const auto dayMilli = QRoundingDown::qDivMod<MSECS_PER_DAY>(msecs);
3459 return { status.testFlag(QDateTimePrivate::ValidDate)
3460 ? QDate::fromJulianDay(JULIAN_DAY_FOR_EPOCH + dayMilli.quotient)
3462 status.testFlag(QDateTimePrivate::ValidTime)
3463 ? QTime::fromMSecsSinceStartOfDay(dayMilli.remainder)
3468
3469
3471inline QDateTime::Data::Data()
noexcept
3476 quintptr value = mergeSpec(QDateTimePrivate::ShortData, Qt::LocalTime).toInt();
3477 d =
reinterpret_cast<QDateTimePrivate *>(value);
3480inline QDateTime::Data::Data(
const QTimeZone &zone)
3482 Qt::TimeSpec spec = zone.timeSpec();
3483 if (CanBeSmall && Q_LIKELY(specCanBeSmall(spec))) {
3484 quintptr value = mergeSpec(QDateTimePrivate::ShortData, spec).toInt();
3485 d =
reinterpret_cast<QDateTimePrivate *>(value);
3486 Q_ASSERT(isShort());
3489 d =
new QDateTimePrivate;
3491 d->m_status = mergeSpec({}, spec);
3492 if (spec == Qt::OffsetFromUTC)
3493 d->m_offsetFromUtc = zone.fixedSecondsAheadOfUtc();
3494 else if (spec == Qt::TimeZone)
3495 d->m_timeZone = zone;
3496 Q_ASSERT(!isShort());
3500inline QDateTime::Data::Data(
const Data &other)
noexcept
3505 if (specCanBeSmall(extractSpec(d->m_status)) && msecsCanBeSmall(d->m_msecs)) {
3507 sd.msecs = qintptr(d->m_msecs);
3508 sd.status = (d->m_status | QDateTimePrivate::ShortData).toInt();
3517inline QDateTime::Data::Data(Data &&other)
noexcept
3522 Q_ASSERT(dummy.isShort());
3523 other.data = dummy.data;
3526inline QDateTime::Data &QDateTime::Data::operator=(
const Data &other)
noexcept
3528 if (isShort() ? data == other.data : d == other.d)
3533 if (!other.isShort()) {
3535 if (specCanBeSmall(extractSpec(other.d->m_status)) && msecsCanBeSmall(other.d->m_msecs)) {
3537 sd.msecs = qintptr(other.d->m_msecs);
3538 sd.status = (other.d->m_status | QDateTimePrivate::ShortData).toInt();
3546 if (!(quintptr(x) & QDateTimePrivate::ShortData) && !x->ref.deref())
3551inline QDateTime::Data::~Data()
3553 if (!isShort() && !d->ref.deref())
3557inline bool QDateTime::Data::isShort()
const
3559 bool b = quintptr(d) & QDateTimePrivate::ShortData;
3562 Q_ASSERT(b || !d->m_status.testFlag(QDateTimePrivate::ShortData));
3566 if constexpr (CanBeSmall)
3568 return Q_UNLIKELY(b);
3571inline void QDateTime::Data::detach()
3573 QDateTimePrivate *x;
3574 bool wasShort = isShort();
3577 x =
new QDateTimePrivate;
3578 x->m_status = QDateTimePrivate::StatusFlags::fromInt(data.status) & ~QDateTimePrivate::ShortData;
3579 x->m_msecs = data.msecs;
3581 if (d->ref.loadRelaxed() == 1)
3584 x =
new QDateTimePrivate(*d);
3587 x->ref.storeRelaxed(1);
3588 if (!wasShort && !d->ref.deref())
3593void QDateTime::Data::invalidate()
3596 data.status &= ~
int(QDateTimePrivate::ValidityMask);
3599 d->m_status &= ~QDateTimePrivate::ValidityMask;
3603QTimeZone QDateTime::Data::timeZone()
const
3605 switch (getSpec(*
this)) {
3607 return QTimeZone::UTC;
3608 case Qt::OffsetFromUTC:
3609 return QTimeZone::fromSecondsAheadOfUtc(d->m_offsetFromUtc);
3611#if QT_CONFIG(timezone)
3612 if (d->m_timeZone.isValid())
3613 return d->m_timeZone;
3617 return QTimeZone::LocalTime;
3622inline const QDateTimePrivate *QDateTime::Data::operator->()
const
3624 Q_ASSERT(!isShort());
3628inline QDateTimePrivate *QDateTime::Data::operator->()
3631 Q_ASSERT(!isShort());
3632 Q_ASSERT(d->ref.loadRelaxed() == 1);
3637
3638
3641QDateTime::Data QDateTimePrivate::create(QDate toDate, QTime toTime,
const QTimeZone &zone,
3642 QDateTime::TransitionResolution resolve)
3644 QDateTime::Data result(zone);
3645 setDateTime(result, toDate, toTime);
3646 if (zone.isUtcOrFixedOffset())
3647 refreshSimpleDateTime(result);
3649 refreshZonedDateTime(result, zone, toTransitionOptions(resolve));
3654
3655
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4040
4041
4042
4043
4044
4045
4046QDateTime::QDateTime()
noexcept
4048#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
) || defined(QT_BOOTSTRAPPED) || QT_POINTER_SIZE == 8
4049 static_assert(
sizeof(ShortData) ==
sizeof(qint64));
4050 static_assert(
sizeof(Data) ==
sizeof(qint64));
4052 static_assert(
sizeof(ShortData) >=
sizeof(
void*),
"oops, Data::swap() is broken!");
4055#if QT_DEPRECATED_SINCE(6
, 9
)
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076QDateTime::QDateTime(QDate date, QTime time, Qt::TimeSpec spec,
int offsetSeconds)
4077 : d(QDateTimePrivate::create(date, time, asTimeZone(spec, offsetSeconds,
"QDateTime"),
4078 TransitionResolution::LegacyBehavior))
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4101QDateTime::QDateTime(QDate date, QTime time,
const QTimeZone &timeZone, TransitionResolution resolve)
4102 : d(QDateTimePrivate::create(date, time, timeZone, resolve))
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4119QDateTime::QDateTime(QDate date, QTime time, TransitionResolution resolve)
4120 : d(QDateTimePrivate::create(date, time, QTimeZone::LocalTime, resolve))
4125
4126
4127QDateTime::QDateTime(
const QDateTime &other)
noexcept
4133
4134
4135
4136
4137QDateTime::QDateTime(QDateTime &&other)
noexcept
4138 : d(std::move(other.d))
4143
4144
4145QDateTime::~QDateTime()
4150
4151
4153QDateTime &QDateTime::operator=(
const QDateTime &other)
noexcept
4159
4160
4161
4162
4165
4166
4167
4168
4169
4171bool QDateTime::isNull()
const
4174 return !getStatus(d).testAnyFlag(QDateTimePrivate::ValidityMask);
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4191bool QDateTime::isValid()
const
4193 return getStatus(d).testFlag(QDateTimePrivate::ValidDateTime);
4197
4198
4199
4200
4202QDate QDateTime::date()
const
4204 return getStatus(d).testFlag(QDateTimePrivate::ValidDate) ? msecsToDate(getMSecs(d)) : QDate();
4208
4209
4210
4211
4213QTime QDateTime::time()
const
4215 return getStatus(d).testFlag(QDateTimePrivate::ValidTime) ? msecsToTime(getMSecs(d)) : QTime();
4219
4220
4221
4222
4223
4224
4225
4226
4227
4229Qt::TimeSpec QDateTime::timeSpec()
const
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4248QTimeZone QDateTime::timeRepresentation()
const
4250 return d.timeZone();
4253#if QT_CONFIG(timezone)
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4269QTimeZone QDateTime::timeZone()
const
4271 return d.timeZone().asBackendZone();
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4297int QDateTime::offsetFromUtc()
const
4299 const auto status = getStatus(d);
4300 if (!status.testFlags(QDateTimePrivate::ValidDate | QDateTimePrivate::ValidTime))
4304 return d->m_offsetFromUtc;
4306 auto spec = extractSpec(status);
4307 if (spec == Qt::LocalTime) {
4309 const auto resolve = toTransitionOptions(extractDaylightStatus(status));
4310 return QDateTimePrivate::localStateAtMillis(getMSecs(d), resolve).offset;
4313 Q_ASSERT(spec == Qt::UTC);
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4338QString QDateTime::timeZoneAbbreviation()
const
4343 switch (getSpec(d)) {
4346 case Qt::OffsetFromUTC:
4347 return "UTC"_L1 + toOffsetString(Qt::ISODate, d->m_offsetFromUtc);
4349#if !QT_CONFIG(timezone)
4352 Q_ASSERT(d->m_timeZone.isValid());
4353 return d->m_timeZone.abbreviation(*
this);
4356#if defined(Q_OS_WIN) && QT_CONFIG(timezone)
4358 if (QString sys = QTimeZone::systemTimeZone().abbreviation(*
this); !sys.isEmpty())
4362 return QDateTimePrivate::localNameAtMillis(getMSecs(d),
4363 extractDaylightStatus(getStatus(d)));
4369
4370
4371
4372
4373
4374
4375
4376
4377
4379bool QDateTime::isDaylightTime()
const
4384 switch (getSpec(d)) {
4386 case Qt::OffsetFromUTC:
4389#if !QT_CONFIG(timezone)
4392 Q_ASSERT(d->m_timeZone.isValid());
4393 if (
auto dst = extractDaylightStatus(getStatus(d));
4394 dst != QDateTimePrivate::UnknownDaylightTime) {
4395 return dst == QDateTimePrivate::DaylightTime;
4397 return d->m_timeZone.d->isDaylightTime(toMSecsSinceEpoch());
4399 case Qt::LocalTime: {
4400 auto dst = extractDaylightStatus(getStatus(d));
4401 if (dst == QDateTimePrivate::UnknownDaylightTime) {
4402 dst = QDateTimePrivate::localStateAtMillis(
4403 getMSecs(d), toTransitionOptions(TransitionResolution::LegacyBehavior)).dst;
4405 return dst == QDateTimePrivate::DaylightTime;
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4426void QDateTime::setDate(QDate date, TransitionResolution resolve)
4428 setDateTime(d, date, time());
4429 checkValidDateTime(d, resolve);
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4451void QDateTime::setTime(QTime time, TransitionResolution resolve)
4453 setDateTime(d, date(), time);
4454 checkValidDateTime(d, resolve);
4457#if QT_DEPRECATED_SINCE(6
, 9
)
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4476void QDateTime::setTimeSpec(Qt::TimeSpec spec)
4478 reviseTimeZone(d, asTimeZone(spec, 0,
"QDateTime::setTimeSpec"),
4479 TransitionResolution::LegacyBehavior);
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4498void QDateTime::setOffsetFromUtc(
int offsetSeconds)
4500 reviseTimeZone(d, QTimeZone::fromSecondsAheadOfUtc(offsetSeconds),
4501 TransitionResolution::Reject);
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4525void QDateTime::setTimeZone(
const QTimeZone &toZone, TransitionResolution resolve)
4527 reviseTimeZone(d, toZone, resolve);
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545qint64 QDateTime::toMSecsSinceEpoch()
const
4552 const auto status = getStatus(d);
4553 if (!status.testFlags(QDateTimePrivate::ValidDate | QDateTimePrivate::ValidTime))
4556 switch (extractSpec(status)) {
4560 case Qt::OffsetFromUTC:
4561 Q_ASSERT(!d.isShort());
4562 return d->m_msecs - d->m_offsetFromUtc * MSECS_PER_SEC;
4565 if (status.testFlag(QDateTimePrivate::ShortData)) {
4567 const auto resolve = toTransitionOptions(extractDaylightStatus(getStatus(d)));
4568 const auto state = QDateTimePrivate::localStateAtMillis(getMSecs(d), resolve);
4569 return state.when - state.offset * MSECS_PER_SEC;
4572 return d->m_msecs - d->m_offsetFromUtc * MSECS_PER_SEC;
4575 Q_ASSERT(!d.isShort());
4576#if QT_CONFIG(timezone)
4578 if (d->m_timeZone.isValid())
4579 return d->m_msecs - d->m_offsetFromUtc * MSECS_PER_SEC;
4583 Q_UNREACHABLE_RETURN(0);
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601qint64 QDateTime::toSecsSinceEpoch()
const
4603 return toMSecsSinceEpoch() / MSECS_PER_SEC;
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621void QDateTime::setMSecsSinceEpoch(qint64 msecs)
4623 auto status = getStatus(d);
4624 const auto spec = extractSpec(status);
4625 Q_ASSERT(specCanBeSmall(spec) || !d.isShort());
4626 QDateTimePrivate::ZoneState state(msecs);
4628 status &= ~QDateTimePrivate::ValidityMask;
4629 if (QTimeZone::isUtcOrFixedOffset(spec)) {
4630 if (spec == Qt::OffsetFromUTC)
4631 state.offset = d->m_offsetFromUtc;
4632 if (!state.offset || !qAddOverflow(msecs, state.offset * MSECS_PER_SEC, &state.when))
4633 status |= QDateTimePrivate::ValidityMask;
4634 }
else if (spec == Qt::LocalTime) {
4635 state = QDateTimePrivate::expressUtcAsLocal(msecs);
4637 status = mergeDaylightStatus(status | QDateTimePrivate::ValidityMask, state.dst);
4638#if QT_CONFIG(timezone)
4639 }
else if (spec == Qt::TimeZone && (d.detach(), d->m_timeZone.isValid())) {
4640 const auto data = d->m_timeZone.d->data(msecs);
4641 if (Q_LIKELY(data.offsetFromUtc != QTimeZonePrivate::invalidSeconds())) {
4642 state.offset = data.offsetFromUtc;
4643 Q_ASSERT(state.offset >= -SECS_PER_DAY && state.offset <= SECS_PER_DAY);
4645 || !Q_UNLIKELY(qAddOverflow(msecs, state.offset * MSECS_PER_SEC, &state.when))) {
4646 d->m_status = mergeDaylightStatus(status | QDateTimePrivate::ValidityMask,
4647 data.daylightTimeOffset
4648 ? QDateTimePrivate::DaylightTime
4649 : QDateTimePrivate::StandardTime);
4650 d->m_msecs = state.when;
4651 d->m_offsetFromUtc = state.offset;
4657 Q_ASSERT(!status.testFlag(QDateTimePrivate::ValidDateTime)
4658 || (state.offset >= -SECS_PER_DAY && state.offset <= SECS_PER_DAY));
4660 if (msecsCanBeSmall(state.when) && d.isShort()) {
4662 d.data.msecs = qintptr(state.when);
4663 d.data.status = status.toInt();
4666 d->m_status = status & ~QDateTimePrivate::ShortData;
4667 d->m_msecs = state.when;
4668 d->m_offsetFromUtc = state.offset;
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683void QDateTime::setSecsSinceEpoch(qint64 secs)
4686 if (!qMulOverflow(secs, std::integral_constant<qint64, MSECS_PER_SEC>(), &msecs))
4687 setMSecsSinceEpoch(msecs);
4692#if QT_CONFIG(datestring)
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723QString QDateTime::toString(Qt::DateFormat format)
const
4730 case Qt::RFC2822Date:
4731 buf = QLocale::c().toString(*
this, u"dd MMM yyyy hh:mm:ss ");
4732 buf += toOffsetString(Qt::TextDate, offsetFromUtc());
4735 case Qt::TextDate: {
4736 const std::pair<QDate, QTime> p = getDateTime(d);
4737 buf = toStringTextDate(p.first);
4739 buf.insert(buf.lastIndexOf(u' '),
4740 u' ' + p.second.toString(Qt::TextDate));
4742 switch (timeSpec()) {
4745#if QT_CONFIG(timezone)
4747 buf += u' ' + d->m_timeZone.displayName(
4748 *
this, QTimeZone::OffsetName, QLocale::c());
4757 if (getSpec(d) == Qt::OffsetFromUTC)
4758 buf += toOffsetString(Qt::TextDate, offsetFromUtc());
4763 case Qt::ISODateWithMs: {
4764 const std::pair<QDate, QTime> p = getDateTime(d);
4765 buf = toStringIsoDate(p.first);
4768 buf += u'T' + p.second.toString(format);
4769 switch (getSpec(d)) {
4773 case Qt::OffsetFromUTC:
4775 buf += toOffsetString(Qt::ISODate, offsetFromUtc());
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825QString QDateTime::toString(QStringView format, QCalendar cal)
const
4827 return QLocale::c().toString(*
this, format, cal);
4832
4833
4834
4835QString QDateTime::toString(QStringView format)
const
4837 return QLocale::c().toString(*
this, format, QCalendar());
4841
4842
4843
4844QString QDateTime::toString(
const QString &format)
const
4846 return QLocale::c().toString(*
this, qToStringViewIgnoringNull(format), QCalendar());
4852 const QDateTimePrivate::TransitionOptions resolve = toTransitionOptions(
4853 forward ? QDateTime::TransitionResolution::RelativeToBefore
4854 : QDateTime::TransitionResolution::RelativeToAfter);
4855 auto status = getStatus(d);
4856 Q_ASSERT(status.testFlags(QDateTimePrivate::ValidDate | QDateTimePrivate::ValidTime
4857 | QDateTimePrivate::ValidDateTime));
4858 auto spec = extractSpec(status);
4859 if (QTimeZone::isUtcOrFixedOffset(spec)) {
4860 setDateTime(d, date, time);
4864 qint64 local = timeToMSecs(date, time);
4865 const QDateTimePrivate::ZoneState state = stateAtMillis(d.timeZone(), local, resolve);
4866 Q_ASSERT(state.valid || state.dst == QDateTimePrivate::UnknownDaylightTime);
4867 if (state.dst == QDateTimePrivate::UnknownDaylightTime)
4868 status.setFlag(QDateTimePrivate::ValidDateTime,
false);
4870 status = mergeDaylightStatus(status | QDateTimePrivate::ValidDateTime, state.dst);
4872 if (status & QDateTimePrivate::ShortData) {
4873 d.data.msecs = state.when;
4874 d.data.status = status.toInt();
4877 d->m_status = status;
4879 d->m_msecs = state.when;
4880 d->m_offsetFromUtc = state.offset;
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4900QDateTime QDateTime::addDays(qint64 ndays)
const
4905 QDateTime dt(*
this);
4906 std::pair<QDate, QTime> p = getDateTime(d);
4907 massageAdjustedDateTime(dt.d, p.first.addDays(ndays), p.second, ndays >= 0);
4912
4913
4914
4915
4916
4917
4918
4919
4922
4923
4924
4925
4926
4927
4928
4929
4932
4933
4934
4935
4936
4937
4938
4939
4942
4943
4944
4945
4946
4947
4948
4949
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4966QDateTime QDateTime::addMonths(
int nmonths)
const
4971 QDateTime dt(*
this);
4972 std::pair<QDate, QTime> p = getDateTime(d);
4973 massageAdjustedDateTime(dt.d, p.first.addMonths(nmonths), p.second, nmonths >= 0);
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4992QDateTime QDateTime::addYears(
int nyears)
const
4997 QDateTime dt(*
this);
4998 std::pair<QDate, QTime> p = getDateTime(d);
4999 massageAdjustedDateTime(dt.d, p.first.addYears(nyears), p.second, nyears >= 0);
5004
5005
5006
5007
5008
5009
5010
5011
5013QDateTime QDateTime::addSecs(qint64 s)
const
5016 if (qMulOverflow(s, std::integral_constant<qint64, MSECS_PER_SEC>(), &msecs))
5018 return addMSecs(msecs);
5022
5023
5024
5025
5026
5027
5028
5029
5030QDateTime QDateTime::addMSecs(qint64 msecs)
const
5035 QDateTime dt(*
this);
5036 switch (getSpec(d)) {
5040 if (!qAddOverflow(toMSecsSinceEpoch(), msecs, &msecs))
5041 dt.setMSecsSinceEpoch(msecs);
5046 case Qt::OffsetFromUTC:
5048 if (qAddOverflow(getMSecs(d), msecs, &msecs)) {
5050 }
else if (d.isShort() && msecsCanBeSmall(msecs)) {
5051 dt.d.data.msecs = qintptr(msecs);
5054 dt.d->m_msecs = msecs;
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5097qint64 QDateTime::daysTo(
const QDateTime &other)
const
5099 return date().daysTo(other.date());
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5118qint64 QDateTime::secsTo(
const QDateTime &other)
const
5120 return msecsTo(other) / MSECS_PER_SEC;
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5137qint64 QDateTime::msecsTo(
const QDateTime &other)
const
5139 if (!isValid() || !other.isValid())
5142 return other.toMSecsSinceEpoch() - toMSecsSinceEpoch();
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5212#if QT_DEPRECATED_SINCE(6
, 9
)
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5232QDateTime QDateTime::toTimeSpec(Qt::TimeSpec spec)
const
5234 return toTimeZone(asTimeZone(spec, 0,
"toTimeSpec"));
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5252QDateTime QDateTime::toOffsetFromUtc(
int offsetSeconds)
const
5254 return toTimeZone(QTimeZone::fromSecondsAheadOfUtc(offsetSeconds));
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268QDateTime QDateTime::toLocalTime()
const
5270 return toTimeZone(QTimeZone::LocalTime);
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284QDateTime QDateTime::toUTC()
const
5286 return toTimeZone(QTimeZone::UTC);
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5307QDateTime QDateTime::toTimeZone(
const QTimeZone &timeZone)
const
5309 if (timeRepresentation() == timeZone)
5313 QDateTime ret = *
this;
5314 ret.setTimeZone(timeZone);
5318 return fromMSecsSinceEpoch(toMSecsSinceEpoch(), timeZone);
5322
5323
5324
5325
5326
5327
5329bool QDateTime::equals(
const QDateTime &other)
const
5332 return !other.isValid();
5333 if (!other.isValid())
5336 const qint64 thisMs = getMSecs(d);
5337 const qint64 yourMs = getMSecs(other.d);
5338 if (usesSameOffset(d, other.d) || areFarEnoughApart(thisMs, yourMs))
5339 return thisMs == yourMs;
5342 return toMSecsSinceEpoch() == other.toMSecsSinceEpoch();
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5367
5368
5369
5370
5371
5372
5373
5374
5375
5380 return rhs.isValid() ? Qt::weak_ordering::less : Qt::weak_ordering::equivalent;
5383 return Qt::weak_ordering::greater;
5385 const qint64 lhms = getMSecs(lhs.d), rhms = getMSecs(rhs.d);
5386 if (usesSameOffset(lhs.d, rhs.d) || areFarEnoughApart(lhms, rhms))
5387 return Qt::compareThreeWay(lhms, rhms);
5390 return Qt::compareThreeWay(lhs.toMSecsSinceEpoch(), rhs.toMSecsSinceEpoch());
5394
5395
5396
5397
5398
5399
5400
5401
5402
5405
5406
5407
5408
5409
5410
5411
5412
5413
5416
5417
5418
5419
5420
5421
5422
5423
5426
5427
5428
5429
5430
5431
5432
5433
5434
5437
5438
5439
5440
5441
5442
5443
5444
5445
5448
5449
5451QDateTime QDateTime::currentDateTime()
5453 return currentDateTime(QTimeZone::LocalTime);
5457
5458
5459
5460
5461
5462
5463
5464
5466QDateTime QDateTime::currentDateTimeUtc()
5468 return currentDateTime(QTimeZone::UTC);
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5484
5485
5486
5487
5488
5489
5490
5491
5492
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5520
5521
5523
5524
5525
5526QDateTime QDateTime::fromStdTimePoint(
5527 std::chrono::time_point<
5528 std::chrono::system_clock,
5529 std::chrono::milliseconds
5532 return fromMSecsSinceEpoch(time.time_since_epoch().count(), QTimeZone::UTC);
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5599#if defined(Q_OS_WIN)
5600static inline uint msecsFromDecomposed(
int hour,
int minute,
int sec,
int msec = 0)
5602 return MSECS_PER_HOUR * hour + MSECS_PER_MIN * minute + MSECS_PER_SEC * sec + msec;
5605QDate QDate::currentDate()
5609 return QDate(st.wYear, st.wMonth, st.wDay);
5612QTime QTime::currentTime()
5617 ct.setHMS(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
5621QDateTime QDateTime::currentDateTime(
const QTimeZone &zone)
5625 const Qt::TimeSpec spec = zone.timeSpec();
5633 QDate d(st.wYear, st.wMonth, st.wDay);
5634 QTime t(msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds));
5635 QDateTime utc(d, t, QTimeZone::UTC);
5636 return spec == Qt::UTC ? utc : utc.toTimeZone(zone);
5639qint64 QDateTime::currentMSecsSinceEpoch()
noexcept
5643 const qint64 daysAfterEpoch = QDate(1970, 1, 1).daysTo(QDate(st.wYear, st.wMonth, st.wDay));
5645 return msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds) +
5646 daysAfterEpoch * MSECS_PER_DAY;
5649qint64 QDateTime::currentSecsSinceEpoch()
noexcept
5653 const qint64 daysAfterEpoch = QDate(1970, 1, 1).daysTo(QDate(st.wYear, st.wMonth, st.wDay));
5655 return st.wHour * SECS_PER_HOUR + st.wMinute * SECS_PER_MIN + st.wSecond +
5656 daysAfterEpoch * SECS_PER_DAY;
5659#elif defined(Q_OS_UNIX)
5660QDate QDate::currentDate()
5662 return QDateTime::currentDateTime().date();
5665QTime QTime::currentTime()
5667 return QDateTime::currentDateTime().time();
5670QDateTime QDateTime::currentDateTime(
const QTimeZone &zone)
5672 return fromMSecsSinceEpoch(currentMSecsSinceEpoch(), zone);
5675qint64 QDateTime::currentMSecsSinceEpoch()
noexcept
5677 struct timespec when;
5678 if (clock_gettime(CLOCK_REALTIME, &when) == 0)
5679 return when.tv_sec * MSECS_PER_SEC + (when.tv_nsec + 500'000) / 1'000'000;
5680 Q_UNREACHABLE_RETURN(0);
5683qint64 QDateTime::currentSecsSinceEpoch()
noexcept
5685 struct timespec when;
5686 if (clock_gettime(CLOCK_REALTIME, &when) == 0)
5688 Q_UNREACHABLE_RETURN(0);
5691#error "What system is this?"
5694#if QT_DEPRECATED_SINCE(6
, 9
)
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec,
int offsetSeconds)
5720 return fromMSecsSinceEpoch(msecs,
5721 asTimeZone(spec, offsetSeconds,
"QDateTime::fromMSecsSinceEpoch"));
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747QDateTime QDateTime::fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec,
int offsetSeconds)
5749 return fromSecsSinceEpoch(secs,
5750 asTimeZone(spec, offsetSeconds,
"QDateTime::fromSecsSinceEpoch"));
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs,
const QTimeZone &timeZone)
5771 reviseTimeZone(dt.d, timeZone, TransitionResolution::Reject);
5772 if (timeZone.isValid())
5773 dt.setMSecsSinceEpoch(msecs);
5778
5780QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs)
5782 return fromMSecsSinceEpoch(msecs, QTimeZone::LocalTime);
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799QDateTime QDateTime::fromSecsSinceEpoch(qint64 secs,
const QTimeZone &timeZone)
5802 reviseTimeZone(dt.d, timeZone, TransitionResolution::Reject);
5803 if (timeZone.isValid())
5804 dt.setSecsSinceEpoch(secs);
5809
5811QDateTime QDateTime::fromSecsSinceEpoch(qint64 secs)
5813 return fromSecsSinceEpoch(secs, QTimeZone::LocalTime);
5816#if QT_CONFIG(datestring)
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5832
5833
5834
5835QDateTime QDateTime::fromString(QStringView string, Qt::DateFormat format)
5837 if (string.isEmpty())
5841 case Qt::RFC2822Date: {
5842 const ParsedRfcDateTime rfc = rfcDateImpl(string);
5844 if (!rfc.date.isValid() || !rfc.time.isValid())
5847 QDateTime dateTime(rfc.date, rfc.time, QTimeZone::UTC);
5848 dateTime.setTimeZone(QTimeZone::fromSecondsAheadOfUtc(rfc.utcOffset));
5852 case Qt::ISODateWithMs: {
5853 const int size = string.size();
5857 QDate date = QDate::fromString(string.first(10), Qt::ISODate);
5858 if (!date.isValid())
5861 return date.startOfDay();
5863 QTimeZone zone = QTimeZone::LocalTime;
5864 QStringView isoString = string.sliced(10);
5867 if (isoString.size() < 2
5868 || !(isoString.startsWith(u'T', Qt::CaseInsensitive)
5872 || isoString.startsWith(u' '))) {
5875 isoString = isoString.sliced(1);
5878 if (isoString.endsWith(u'Z', Qt::CaseInsensitive)) {
5879 zone = QTimeZone::UTC;
5884 int signIndex = isoString.size() - 1;
5885 Q_ASSERT(signIndex >= 0);
5888 QChar character(isoString[signIndex]);
5889 found = character == u'+' || character == u'-';
5890 }
while (!found && --signIndex >= 0);
5894 int offset = fromOffsetString(isoString.sliced(signIndex), &ok);
5897 isoString = isoString.first(signIndex);
5898 zone = QTimeZone::fromSecondsAheadOfUtc(offset);
5904 bool isMidnight24 =
false;
5905 QTime time = fromIsoTimeString(isoString, format, &isMidnight24);
5906 if (!time.isValid())
5909 return date.addDays(1).startOfDay(zone);
5910 return QDateTime(date, time, zone);
5912 case Qt::TextDate: {
5913 QVarLengthArray<QStringView, 6> parts;
5915 auto tokens = string.tokenize(u' ', Qt::SkipEmptyParts);
5916 auto it = tokens.begin();
5917 for (
int i = 0; i < 6 && it != tokens.end(); ++i, ++it)
5918 parts.emplace_back(*it);
5922 if (parts.size() < 5 || it != tokens.end())
5929 if (parts.at(3).contains(u':'))
5931 else if (parts.at(4).contains(u':'))
5937 int day = parts.at(2).toInt(&ok);
5938 int year = ok ? parts.at(yearPart).toInt(&ok) : 0;
5939 int month = fromShortMonthName(parts.at(1));
5940 if (!ok || year == 0 || day == 0 || month < 1)
5943 const QDate date(year, month, day);
5944 if (!date.isValid())
5947 const QTime time = fromIsoTimeString(parts.at(timePart), format,
nullptr);
5948 if (!time.isValid())
5951 if (parts.size() == 5)
5952 return QDateTime(date, time);
5954 QStringView tz = parts.at(5);
5955 if (tz.startsWith(
"UTC"_L1)
5957 || tz.startsWith(
"GMT"_L1, Qt::CaseInsensitive)) {
5960 return QDateTime(date, time, QTimeZone::UTC);
5962 int offset = fromOffsetString(tz, &ok);
5963 return ok ? QDateTime(date, time, QTimeZone::fromSecondsAheadOfUtc(offset))
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6047
6048
6049
6050
6053
6054
6055
6056QDateTime QDateTime::fromString(
const QString &string, QStringView format,
int baseYear,
6059#if QT_CONFIG(datetimeparser)
6060 QDateTimePattern pattern = QDateTimePattern::fromQtFormat(format);
6061 if (pattern.isNull() && !format.isEmpty())
6063 pattern.setLocale(QLocale::c());
6064 pattern.setCalendar(cal);
6065 pattern.setBaseYear(baseYear);
6066 if (
auto match = pattern.parse(string, QDate(baseYear, 1, 1, cal).startOfDay());
6067 match.size == string.size()) {
6068 return std::move(match.payload);
6080
6081
6082
6083
6086
6087
6088
6089
6092
6093
6094
6095
6098
6099
6100
6101
6102
6103
6106
6107
6108
6109
6110
6111QDateTime QDateTime::fromString(
const QString &string, QStringView format,
int baseYear)
6113 return fromString(string, format, baseYear, QCalendar());
6117
6118
6119
6120
6121
6122
6126
6127
6129#ifndef QT_NO_DATASTREAM
6131
6132
6133
6134
6135
6136
6138QDataStream &operator<<(QDataStream &out, QDate date)
6140 if (out.version() < QDataStream::Qt_5_0)
6141 return out << quint32(date.jd);
6143 return out << date.jd;
6147
6148
6149
6150
6151
6152
6154QDataStream &operator>>(QDataStream &in, QDate &date)
6157 if (in.version() < QDataStream::Qt_5_0) {
6161 jd = (jd32 != 0 ? qint64(jd32) : QDate::nullJd());
6165 date = QDate::fromJulianDay(jd);
6170
6171
6172
6173
6174
6175
6177QDataStream &operator<<(QDataStream &out, QTime time)
6179 if (out.version() >= QDataStream::Qt_4_0) {
6180 return out << quint32(time.mds);
6183 return out << quint32(time.isNull() ? 0 : time.mds);
6188
6189
6190
6191
6192
6193
6195QDataStream &operator>>(QDataStream &in, QTime &time)
6199 if (in.version() >= QDataStream::Qt_4_0) {
6203 time.mds = (ds == 0) ? QTime::NullTime :
int(ds);
6205 if (time.mds != QTime::NullTime && (time.mds < 0 || time.mds >= MSECS_PER_DAY))
6206 time.mds = QTime::NullTime;
6211
6212
6213
6214
6215
6216
6217QDataStream &operator<<(QDataStream &out,
const QDateTime &dateTime)
6219 std::pair<QDate, QTime> dateAndTime;
6222 if (out.version() >= QDataStream::Qt_5_2) {
6225 dateAndTime = getDateTime(dateTime.d);
6226 out << dateAndTime << qint8(dateTime.timeSpec());
6227 if (dateTime.timeSpec() == Qt::OffsetFromUTC)
6228 out << qint32(dateTime.offsetFromUtc());
6229#if QT_CONFIG(timezone)
6230 else if (dateTime.timeSpec() == Qt::TimeZone)
6231 out << dateTime.timeZone();
6234 }
else if (out.version() == QDataStream::Qt_5_0) {
6240 dateAndTime = getDateTime((dateTime.isValid() ? dateTime.toUTC() : dateTime).d);
6241 out << dateAndTime << qint8(dateTime.timeSpec());
6243 }
else if (out.version() >= QDataStream::Qt_4_0) {
6246 dateAndTime = getDateTime(dateTime.d);
6248 switch (dateTime.timeSpec()) {
6250 out << (qint8)QDateTimePrivate::UTC;
6252 case Qt::OffsetFromUTC:
6253 out << (qint8)QDateTimePrivate::OffsetFromUTC;
6256 out << (qint8)QDateTimePrivate::TimeZone;
6259 out << (qint8)QDateTimePrivate::LocalUnknown;
6266 dateAndTime = getDateTime(dateTime.d);
6275
6276
6277
6278
6279
6280
6282QDataStream &operator>>(QDataStream &in, QDateTime &dateTime)
6287 QTimeZone zone(QTimeZone::LocalTime);
6289 if (in.version() >= QDataStream::Qt_5_2) {
6292 in >> dt >> tm >> ts;
6293 switch (
static_cast<Qt::TimeSpec>(ts)) {
6295 zone = QTimeZone::UTC;
6297 case Qt::OffsetFromUTC: {
6300 zone = QTimeZone::fromSecondsAheadOfUtc(offset);
6310 dateTime = QDateTime(dt, tm, zone);
6312 }
else if (in.version() == QDataStream::Qt_5_0) {
6315 in >> dt >> tm >> ts;
6316 dateTime = QDateTime(dt, tm, QTimeZone::UTC);
6317 if (
static_cast<Qt::TimeSpec>(ts) == Qt::LocalTime)
6318 dateTime = dateTime.toTimeZone(zone);
6320 }
else if (in.version() >= QDataStream::Qt_4_0) {
6323 in >> dt >> tm >> ts;
6324 switch (
static_cast<QDateTimePrivate::Spec>(ts)) {
6325 case QDateTimePrivate::OffsetFromUTC:
6326 case QDateTimePrivate::UTC:
6327 zone = QTimeZone::UTC;
6329 case QDateTimePrivate::TimeZone:
6330 case QDateTimePrivate::LocalUnknown:
6331 case QDateTimePrivate::LocalStandard:
6332 case QDateTimePrivate::LocalDST:
6335 dateTime = QDateTime(dt, tm, zone);
6341 dateTime = QDateTime(dt, tm);
6350
6351
6353#if !defined(QT_NO_DEBUG_STREAM) && QT_CONFIG(datestring)
6354QDebug operator<<(QDebug dbg, QDate date)
6356 QDebugStateSaver saver(dbg);
6357 dbg.nospace() <<
"QDate(";
6360 if (
int y = date.year(); y > 0 && y <= 9999)
6361 dbg.nospace() << date.toString(Qt::ISODate);
6363 dbg.nospace() << date.toString(Qt::TextDate);
6365 dbg.nospace() <<
"Invalid";
6366 dbg.nospace() <<
')';
6370QDebug operator<<(QDebug dbg, QTime time)
6372 QDebugStateSaver saver(dbg);
6373 dbg.nospace() <<
"QTime(";
6375 dbg.nospace() << time.toString(u"HH:mm:ss.zzz");
6377 dbg.nospace() <<
"Invalid";
6378 dbg.nospace() <<
')';
6382QDebug operator<<(QDebug dbg,
const QDateTime &date)
6384 QDebugStateSaver saver(dbg);
6385 dbg.nospace() <<
"QDateTime(";
6386 if (date.isValid()) {
6387 const Qt::TimeSpec ts = date.timeSpec();
6388 dbg.noquote() << date.toString(u"yyyy-MM-dd HH:mm:ss.zzz t")
6393 case Qt::OffsetFromUTC:
6394 dbg.space() << date.offsetFromUtc() <<
's';
6397#if QT_CONFIG(timezone)
6398 dbg.space() << date.timeZone().id();
6405 dbg.nospace() <<
"Invalid";
6407 return dbg.nospace() <<
')';
6412
6413
6414
6421 return key.isValid() ? qHash(key.toMSecsSinceEpoch(), seed) : seed;
6425
6426
6427
6430 return qHash(key.toJulianDay(), seed);
6434
6435
6436
6439 return qHash(key.msecsSinceStartOfDay(), seed);
size_t qHash(QTime key, size_t seed) noexcept
\qhashold{QHash}
static QTime msecsToTime(qint64 msecs)
static auto millisToWithinRange(qint64 millis)
static QDateTime toLatest(QDate day, const QTimeZone &zone)
static constexpr QDateTimePrivate::StatusFlags mergeDaylightStatus(QDateTimePrivate::StatusFlags sf, QDateTimePrivate::DaylightStatus status)
static QDate fixedDate(QCalendar::YearMonthDay parts)
static qint64 timeToMSecs(QDate date, QTime time)
static std::pair< QDate, QTime > getDateTime(const QDateTimeData &d)
static constexpr QDateTimePrivate::DaylightStatus extractDaylightStatus(QDateTimePrivate::StatusFlags status)
size_t qHash(const QDateTime &key, size_t seed)
\qhashold{QHash}
static Qt::TimeSpec getSpec(const QDateTimeData &d)
QDateTimePrivate::QDateTimeShortData ShortData
static void reviseTimeZone(QDateTimeData &d, const QTimeZone &zone, QDateTime::TransitionResolution resolve)
static QDateTimePrivate::StatusFlags getStatus(const QDateTimeData &d)
static qint64 getMSecs(const QDateTimeData &d)
static void massageAdjustedDateTime(QDateTimeData &d, QDate date, QTime time, bool forward)
static bool inDateTimeRange(qint64 jd, DaySide side)
QDateTimePrivate::QDateTimeData QDateTimeData
static bool specCanBeSmall(Qt::TimeSpec spec)
static int systemTimeYearMatching(int year)
static constexpr QDateTimePrivate::StatusFlags mergeSpec(QDateTimePrivate::StatusFlags status, Qt::TimeSpec spec)
static QDate msecsToDate(qint64 msecs)
static QString toOffsetString(Qt::DateFormat format, int offset)
size_t qHash(QDate key, size_t seed) noexcept
\qhashold{QHash}
static bool daysAndMillisOverflow(qint64 days, qint64 millisInDay, qint64 *sumMillis)
static QDate fixedDate(QCalendar::YearMonthDay parts, QCalendar cal)
static constexpr QDateTimePrivate::TransitionOptions toTransitionOptions(QDateTime::TransitionResolution res)
static void refreshSimpleDateTime(QDateTimeData &d)
bool areFarEnoughApart(qint64 leftMillis, qint64 rightMillis)
static void setDateTime(QDateTimeData &d, QDate date, QTime time)
static void refreshZonedDateTime(QDateTimeData &d, const QTimeZone &zone, QDateTimePrivate::TransitionOptions resolve)
static bool msecsCanBeSmall(qint64 msecs)
static constexpr Qt::TimeSpec extractSpec(QDateTimePrivate::StatusFlags status)
static bool usesSameOffset(const QDateTimeData &a, const QDateTimeData &b)
static void checkValidDateTime(QDateTimeData &d, QDateTime::TransitionResolution resolve)
Qt::weak_ordering compareThreeWay(const QDateTime &lhs, const QDateTime &rhs)
static QDateTime toEarliest(QDate day, const QTimeZone &zone)
static QDateTimePrivate::ZoneState stateAtMillis(const QTimeZone &zone, qint64 millis, QDateTimePrivate::TransitionOptions resolve)
static bool millisInSystemRange(qint64 millis, qint64 slack=0)
static qint64 msecsToJulianDay(qint64 msecs)