14#include "private/qcalendarmath_p.h"
15#include "private/qdatetime_p.h"
16#if QT_CONFIG(datetimeparser)
17#include "private/qdatetimeparser_p.h"
20#include "private/qcore_mac_p.h"
22#include "private/qgregoriancalendar_p.h"
23#include "private/qlocale_tools_p.h"
24#include "private/qlocaltime_p.h"
25#include "private/qnumeric_p.h"
26#include "private/qstringconverter_p.h"
27#include "private/qstringiterator_p.h"
28#if QT_CONFIG(timezone)
29#include "private/qtimezoneprivate_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
1327QString QDate::toString(QStringView format, QCalendar cal)
const
1329 return QLocale::c().toString(*
this, format, cal);
1334
1335
1336
1337QString QDate::toString(QStringView format)
const
1339 return QLocale::c().toString(*
this, format, QCalendar());
1343
1344
1345
1346QString QDate::toString(
const QString &format)
const
1348 return QLocale::c().toString(*
this, qToStringViewIgnoringNull(format), QCalendar());
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362bool QDate::setDate(
int year,
int month,
int day)
1364 const auto maybe = QGregorianCalendar::julianFromParts(year, month, day);
1365 jd = maybe.value_or(nullJd());
1370
1371
1372
1373
1374
1375
1376
1377
1378
1380bool QDate::setDate(
int year,
int month,
int day, QCalendar cal)
1382 *
this = QDate(year, month, day, cal);
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398void QDate::getDate(
int *year,
int *month,
int *day)
const
1400 QCalendar::YearMonthDay parts;
1402 parts = QGregorianCalendar::partsFromJulian(jd);
1404 const bool ok = parts.isValid();
1406 *year = ok ? parts.year : 0;
1408 *month = ok ? parts.month : 0;
1410 *day = ok ? parts.day : 0;
1414
1415
1416
1417
1418
1419
1420
1421
1423QDate QDate::addDays(qint64 ndays)
const
1428 if (qint64 r; Q_UNLIKELY(qAddOverflow(jd, ndays, &r)))
1431 return fromJulianDay(r);
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1470QDate QDate::addMonths(
int nmonths, QCalendar cal)
const
1478 auto parts = cal.partsFromDate(*
this);
1480 if (!parts.isValid())
1482 Q_ASSERT(parts.year || cal.hasYearZero());
1484 parts.month += nmonths;
1485 while (parts.month <= 0) {
1486 if (--parts.year || cal.hasYearZero())
1487 parts.month += cal.monthsInYear(parts.year);
1489 int count = cal.monthsInYear(parts.year);
1490 while (parts.month > count) {
1491 parts.month -= count;
1492 count = (++parts.year || cal.hasYearZero()) ? cal.monthsInYear(parts.year) : 0;
1495 return fixedDate(parts, cal);
1499
1500
1502QDate QDate::addMonths(
int nmonths)
const
1510 auto parts = QGregorianCalendar::partsFromJulian(jd);
1512 if (!parts.isValid())
1514 Q_ASSERT(parts.year);
1516 parts.month += nmonths;
1517 while (parts.month <= 0) {
1521 while (parts.month > 12) {
1527 return fixedDate(parts);
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1546QDate QDate::addYears(
int nyears, QCalendar cal)
const
1551 auto parts = cal.partsFromDate(*
this);
1552 if (!parts.isValid())
1555 int old_y = parts.year;
1556 parts.year += nyears;
1559 if (!cal.hasYearZero() && ((old_y > 0) != (parts.year > 0) || !parts.year))
1560 parts.year += nyears > 0 ? +1 : -1;
1562 return fixedDate(parts, cal);
1566
1567
1569QDate QDate::addYears(
int nyears)
const
1574 auto parts = QGregorianCalendar::partsFromJulian(jd);
1575 if (!parts.isValid())
1578 int old_y = parts.year;
1579 parts.year += nyears;
1582 if ((old_y > 0) != (parts.year > 0) || !parts.year)
1583 parts.year += nyears > 0 ? +1 : -1;
1585 return fixedDate(parts);
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1600qint64 QDate::daysTo(QDate d)
const
1602 if (isNull() || d.isNull())
1611
1612
1613
1614
1615
1618
1619
1620
1621
1622
1623
1624
1627
1628
1629
1630
1633
1634
1635
1636
1637
1640
1641
1642
1643
1646
1647
1648
1649
1650
1653
1654
1655
1656
1657
1659#if QT_CONFIG(datestring)
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1676
1677
1678
1679QDate QDate::fromString(QStringView string, Qt::DateFormat format)
1681 if (string.isEmpty())
1685 case Qt::RFC2822Date:
1686 return rfcDateImpl(string).date;
1688 case Qt::TextDate: {
1690 QVarLengthArray<QStringView, 4> parts;
1691 auto tokens = string.tokenize(u' ', Qt::SkipEmptyParts);
1692 auto it = tokens.begin();
1693 for (
int i = 0; i < 4 && it != tokens.end(); ++i, ++it)
1694 parts.emplace_back(*it);
1696 if (parts.size() != 4 || it != tokens.end())
1700 int year = parts.at(3).toInt(&ok);
1701 int day = ok ? parts.at(2).toInt(&ok) : 0;
1705 const int month = fromShortMonthName(parts.at(1));
1709 return QDate(year, month, day);
1713 if (string.size() >= 10 && string[4].isPunct() && string[7].isPunct()
1714 && (string.size() == 10 || !string[10].isDigit())) {
1715 const ParsedInt year = readInt(string.first(4));
1716 const ParsedInt month = readInt(string.sliced(5, 2));
1717 const ParsedInt day = readInt(string.sliced(8, 2));
1718 if (year.ok() && year.result > 0 && year.result <= 9999 && month.ok() && day.ok())
1719 return QDate(year.result, month.result, day.result);
1727
1728
1729
1730
1731
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
1859
1860
1861
1862
1865
1866
1867
1868QDate QDate::fromString(
const QString &string, QStringView format,
int baseYear, QCalendar cal)
1871#if QT_CONFIG(datetimeparser)
1872 QDateTimeParser dt(QMetaType::QDate, QDateTimeParser::FromString, cal);
1873 dt.setDefaultLocale(QLocale::c());
1874 if (dt.parseFormat(format))
1875 dt.fromString(string, &date,
nullptr, baseYear);
1886
1887
1888
1889
1892
1893
1894
1895
1898
1899
1900
1901
1904
1905
1906
1907
1908
1909
1912
1913
1914
1915
1916
1917QDate QDate::fromString(
const QString &string, QStringView format,
int baseYear)
1919 return fromString(string, format, baseYear, QCalendar());
1923
1924
1925
1926
1927
1928
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1943bool QDate::isValid(
int year,
int month,
int day)
1945 return QGregorianCalendar::validParts(year, month, day);
1949
1950
1951
1952
1953
1954
1955
1957bool QDate::isLeapYear(
int y)
1959 return QGregorianCalendar::leapTest(y);
1963
1964
1965
1966
1967
1970
1971
1972
1973
1974
1977
1978
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2027
2028
2029
2030
2031
2032
2033
2034
2037
2038
2039
2040
2041
2042
2043
2044
2046QTime::QTime(
int h,
int m,
int s,
int ms)
2048 setHMS(h, m, s, ms);
2053
2054
2055
2056
2057
2058
2059
2060
2063
2064
2065
2066
2067
2068
2069
2071bool QTime::isValid()
const
2073 return mds > NullTime && mds < MSECS_PER_DAY;
2078
2079
2080
2081
2082
2083
2085int QTime::hour()
const
2090 return ds() / MSECS_PER_HOUR;
2094
2095
2096
2097
2098
2099
2101int QTime::minute()
const
2106 return (ds() % MSECS_PER_HOUR) / MSECS_PER_MIN;
2110
2111
2112
2113
2114
2115
2117int QTime::second()
const
2122 return (ds() / MSECS_PER_SEC) % SECS_PER_MIN;
2126
2127
2128
2129
2130
2131
2133int QTime::msec()
const
2138 return ds() % MSECS_PER_SEC;
2141#if QT_CONFIG(datestring)
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2166QString QTime::toString(Qt::DateFormat format)
const
2172 case Qt::ISODateWithMs:
2173 return QString::asprintf(
"%02d:%02d:%02d.%03d", hour(), minute(), second(), msec());
2174 case Qt::RFC2822Date:
2178 return QString::asprintf(
"%02d:%02d:%02d", hour(), minute(), second());
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
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
2289QString QTime::toString(QStringView format)
const
2291 return QLocale::c().toString(*
this, format);
2297
2298
2299
2300
2301
2302
2303
2304
2305
2307bool QTime::setHMS(
int h,
int m,
int s,
int ms)
2309 if (!isValid(h,m,s,ms)) {
2313 mds = ((h * MINS_PER_HOUR + m) * SECS_PER_MIN + s) * MSECS_PER_SEC + ms;
2314 Q_ASSERT(mds >= 0 && mds < MSECS_PER_DAY);
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2333QTime QTime::addSecs(
int s)
const
2336 return addMSecs(s * MSECS_PER_SEC);
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2354int QTime::secsTo(QTime t)
const
2356 if (!isValid() || !t.isValid())
2360 int ourSeconds = ds() / MSECS_PER_SEC;
2361 int theirSeconds = t.ds() / MSECS_PER_SEC;
2362 return theirSeconds - ourSeconds;
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2377QTime QTime::addMSecs(
int ms)
const
2381 t.mds = QRoundingDown::qMod<MSECS_PER_DAY>(ds() + ms);
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2399int QTime::msecsTo(QTime t)
const
2401 if (!isValid() || !t.isValid())
2403 return t.ds() - ds();
2408
2409
2410
2411
2414
2415
2416
2417
2420
2421
2422
2423
2426
2427
2428
2429
2430
2433
2434
2435
2436
2439
2440
2441
2442
2443
2446
2447
2448
2449
2450
2451
2452
2453
2454
2457
2458
2459
2460
2461
2462
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2479#if QT_CONFIG(datestring)
2481static QTime fromIsoTimeString(QStringView string, Qt::DateFormat format,
bool *isMidnight24)
2483 Q_ASSERT(format == Qt::TextDate || format == Qt::ISODate || format == Qt::ISODateWithMs);
2485 *isMidnight24 =
false;
2491 const qsizetype dot = string.indexOf(u'.'), comma = string.indexOf(u',');
2493 tail = string.sliced(dot + 1);
2494 if (tail.indexOf(u'.') != -1)
2496 string = string.first(dot);
2497 }
else if (comma != -1) {
2498 tail = string.sliced(comma + 1);
2499 string = string.first(comma);
2501 if (tail.indexOf(u',') != -1)
2504 const ParsedInt frac = readInt(tail);
2506 if (tail.isEmpty() ? dot != -1 || comma != -1 : !frac.ok())
2508 Q_ASSERT(frac.ok() ^ tail.isEmpty());
2509 double fraction = frac.ok() ? frac.result * std::pow(0.1, tail.size()) : 0.0;
2511 const qsizetype size = string.size();
2512 if (size < 2 || size > 8)
2515 ParsedInt hour = readInt(string.first(2));
2516 if (!hour.ok() || hour.result > (format == Qt::TextDate ? 23 : 24))
2520 if (string.size() > 2) {
2521 if (string[2] == u':' && string.size() > 4)
2522 minute = readInt(string.sliced(3, 2));
2523 if (!minute.ok() || minute.result >= MINS_PER_HOUR)
2525 }
else if (format == Qt::TextDate) {
2527 }
else if (frac.ok()) {
2528 Q_ASSERT(!(fraction < 0.0) && fraction < 1.0);
2529 fraction *= MINS_PER_HOUR;
2530 minute.result = qulonglong(fraction);
2531 fraction -= minute.result;
2535 if (string.size() > 5) {
2536 if (string[5] == u':' && string.size() == 8)
2537 second = readInt(string.sliced(6, 2));
2538 if (!second.ok() || second.result >= SECS_PER_MIN)
2540 }
else if (frac.ok()) {
2541 if (format == Qt::TextDate)
2543 Q_ASSERT(!(fraction < 0.0) && fraction < 1.0);
2544 fraction *= SECS_PER_MIN;
2545 second.result = qulonglong(fraction);
2546 fraction -= second.result;
2549 Q_ASSERT(!(fraction < 0.0) && fraction < 1.0);
2551 int msec = frac.ok() ? qRound(MSECS_PER_SEC * fraction) : 0;
2553 if (msec == MSECS_PER_SEC) {
2556 if (isMidnight24 || hour.result < 23 || minute.result < 59 || second.result < 59) {
2558 if (++second.result == SECS_PER_MIN) {
2560 if (++minute.result == MINS_PER_HOUR) {
2569 msec = MSECS_PER_SEC - 1;
2574 if (hour.result == 24 && minute.result == 0 && second.result == 0 && msec == 0) {
2575 Q_ASSERT(format != Qt::TextDate);
2577 *isMidnight24 =
true;
2581 return QTime(hour.result, minute.result, second.result, msec);
2585
2586
2587
2588
2589
2590
2591
2592
2595
2596
2597
2598QTime QTime::fromString(QStringView string, Qt::DateFormat format)
2600 if (string.isEmpty())
2604 case Qt::RFC2822Date:
2605 return rfcDateImpl(string).time;
2607 case Qt::ISODateWithMs:
2610 return fromIsoTimeString(string, format,
nullptr);
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
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
2694
2695
2696
2697
2700
2701
2702
2703QTime QTime::fromString(
const QString &string, QStringView format)
2706#if QT_CONFIG(datetimeparser)
2707 QDateTimeParser dt(QMetaType::QTime, QDateTimeParser::FromString, QCalendar());
2708 dt.setDefaultLocale(QLocale::c());
2709 if (dt.parseFormat(format))
2710 dt.fromString(string,
nullptr, &time);
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2734bool QTime::isValid(
int h,
int m,
int s,
int ms)
2736 return (uint(h) < 24 && uint(m) < MINS_PER_HOUR && uint(s) < SECS_PER_MIN
2737 && uint(ms) < MSECS_PER_SEC);
2741
2742
2751 return JULIAN_DAY_FOR_EPOCH + QRoundingDown::qDiv<MSECS_PER_DAY>(msecs);
2756 return QDate::fromJulianDay(msecsToJulianDay(msecs));
2761 return QTime::fromMSecsSinceStartOfDay(QRoundingDown::qMod<MSECS_PER_DAY>(msecs));
2768 return qMulOverflow(days, std::integral_constant<qint64, MSECS_PER_DAY>(), sumMillis)
2769 || qAddOverflow(*sumMillis, millisInDay, sumMillis);
2775 qint64 days = date.toJulianDay() - JULIAN_DAY_FOR_EPOCH;
2776 qint64 msecs, dayms = time.msecsSinceStartOfDay();
2777 if (days < 0 && dayms > 0) {
2779 dayms -= MSECS_PER_DAY;
2781 if (daysAndMillisOverflow(days, dayms, &msecs)) {
2782 using Bound = std::numeric_limits<qint64>;
2783 return days < 0 ? Bound::min() : Bound::max();
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2807 static const auto bounds = QLocalTime::computeSystemMillisRange();
2808 return (bounds.minClip || millis >= bounds.min - slack)
2809 && (bounds.maxClip || millis <= bounds.max + slack);
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2830#if defined(Q_OS_WIN) || defined(Q_OS_WASM)
2831 static constexpr int forLeapEarly[] = { 1984, 1996, 1980, 1992, 1976, 1988, 1972 };
2832 static constexpr int regularEarly[] = { 1978, 1973, 1974, 1975, 1970, 1971, 1977 };
2834 static constexpr int forLeapEarly[] = { 1928, 1912, 1924, 1908, 1920, 1904, 1916 };
2835 static constexpr int regularEarly[] = { 1905, 1906, 1907, 1902, 1903, 1909, 1910 };
2837 static constexpr int forLeapLate[] = { 2012, 2024, 2036, 2020, 2032, 2016, 2028 };
2838 static constexpr int regularLate[] = { 2034, 2035, 2030, 2031, 2037, 2027, 2033 };
2839 const int dow = QGregorianCalendar::yearStartWeekDay(year);
2840 Q_ASSERT(dow == QDate(year, 1, 1).dayOfWeek());
2841 const int res = (QGregorianCalendar::leapTest(year)
2842 ? (year < 1970 ? forLeapEarly : forLeapLate)
2843 : (year < 1970 ? regularEarly : regularLate))[dow == 7 ? 0 : dow];
2844 Q_ASSERT(QDate(res, 1, 1).dayOfWeek() == dow);
2845 Q_ASSERT(QDate(res, 12, 31).dayOfWeek() == QDate(year, 12, 31).dayOfWeek());
2850QDateTimePrivate::ZoneState QDateTimePrivate::expressUtcAsLocal(qint64 utcMSecs)
2852 ZoneState result{utcMSecs};
2854 if (millisInSystemRange(utcMSecs)) {
2855 result = QLocalTime::utcToLocal(utcMSecs);
2862#if QT_CONFIG(timezone)
2863 if (
const auto sys = QTimeZone::systemTimeZone(); sys.isValid()) {
2864 result.offset = sys.d->offsetFromUtc(utcMSecs);
2865 if (result.offset != QTimeZonePrivate::invalidSeconds()) {
2866 if (qAddOverflow(utcMSecs, result.offset * MSECS_PER_SEC, &result.when))
2868 result.dst = sys.d->isDaylightTime(utcMSecs) ? DaylightTime : StandardTime;
2869 result.valid =
true;
2878 const qint64 jd = msecsToJulianDay(utcMSecs);
2879 const auto ymd = QGregorianCalendar::partsFromJulian(jd);
2880 qint64 diffMillis, fakeUtc;
2881 const auto fakeJd = QGregorianCalendar::julianFromParts(systemTimeYearMatching(ymd.year),
2882 ymd.month, ymd.day);
2883 if (Q_UNLIKELY(!fakeJd
2884 || qMulOverflow(jd - *fakeJd, std::integral_constant<qint64, MSECS_PER_DAY>(),
2886 || qSubOverflow(utcMSecs, diffMillis, &fakeUtc))) {
2890 result = QLocalTime::utcToLocal(fakeUtc);
2892 if (!result.valid || qAddOverflow(result.when, diffMillis, &result.when)) {
2895 result.when = utcMSecs;
2896 result.valid =
false;
2907 qint64 jd = msecsToJulianDay(millis);
2908 auto ymd = QGregorianCalendar::partsFromJulian(jd);
2909 const auto fakeJd = QGregorianCalendar::julianFromParts(systemTimeYearMatching(ymd.year),
2910 ymd.month, ymd.day);
2911 result.good = fakeJd && !daysAndMillisOverflow(*fakeJd - jd, millis, &result.shifted);
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2973 case QDateTime::TransitionResolution::RelativeToBefore:
2974 return QDateTimePrivate::GapUseAfter | QDateTimePrivate::FoldUseBefore;
2975 case QDateTime::TransitionResolution::RelativeToAfter:
2976 return QDateTimePrivate::GapUseBefore | QDateTimePrivate::FoldUseAfter;
2977 case QDateTime::TransitionResolution::PreferBefore:
2978 return QDateTimePrivate::GapUseBefore | QDateTimePrivate::FoldUseBefore;
2979 case QDateTime::TransitionResolution::PreferAfter:
2980 return QDateTimePrivate::GapUseAfter | QDateTimePrivate::FoldUseAfter;
2981 case QDateTime::TransitionResolution::PreferStandard:
2982 return QDateTimePrivate::GapUseBefore
2983 | QDateTimePrivate::FoldUseAfter
2984 | QDateTimePrivate::FlipForReverseDst;
2985 case QDateTime::TransitionResolution::PreferDaylightSaving:
2986 return QDateTimePrivate::GapUseAfter
2987 | QDateTimePrivate::FoldUseBefore
2988 | QDateTimePrivate::FlipForReverseDst;
2989 case QDateTime::TransitionResolution::Reject:
break;
2997 return toTransitionOptions(dst == QDateTimePrivate::DaylightTime
2998 ? QDateTime::TransitionResolution::PreferDaylightSaving
2999 : QDateTime::TransitionResolution::PreferStandard);
3002QString QDateTimePrivate::localNameAtMillis(qint64 millis, DaylightStatus dst)
3004 const QDateTimePrivate::TransitionOptions resolve = toTransitionOptions(dst);
3005 QString abbreviation;
3006 if (millisInSystemRange(millis, MSECS_PER_DAY)) {
3007 abbreviation = QLocalTime::localTimeAbbbreviationAt(millis, resolve);
3008 if (!abbreviation.isEmpty())
3009 return abbreviation;
3013#if QT_CONFIG(timezone)
3015 const auto sys = QTimeZone::systemTimeZone();
3016 if (sys.isValid()) {
3017 ZoneState state = zoneStateAtMillis(sys, millis, resolve);
3019 return sys.d->abbreviation(state.when - state.offset * MSECS_PER_SEC);
3025 auto fake = millisToWithinRange(millis);
3026 if (Q_LIKELY(fake.good))
3027 return QLocalTime::localTimeAbbbreviationAt(fake.shifted, resolve);
3034QDateTimePrivate::ZoneState QDateTimePrivate::localStateAtMillis(
3035 qint64 millis, QDateTimePrivate::TransitionOptions resolve)
3039 if (millisInSystemRange(millis, MSECS_PER_DAY)) {
3040 auto result = QLocalTime::mapLocalTime(millis, resolve);
3046#if QT_CONFIG(timezone)
3048 const auto sys = QTimeZone::systemTimeZone();
3050 return zoneStateAtMillis(sys, millis, resolve);
3055 auto fake = millisToWithinRange(millis);
3056 if (Q_LIKELY(fake.good)) {
3057 auto result = QLocalTime::mapLocalTime(fake.shifted, resolve);
3060 if (Q_UNLIKELY(qAddOverflow(result.when, millis - fake.shifted, &adjusted))) {
3061 using Bound = std::numeric_limits<qint64>;
3062 adjusted = millis < fake.shifted ? Bound::min() : Bound::max();
3064 result.when = adjusted;
3066 result.when = millis;
3074#if QT_CONFIG(timezone)
3078QDateTimePrivate::ZoneState QDateTimePrivate::zoneStateAtMillis(
3079 const QTimeZone &zone, qint64 millis, QDateTimePrivate::TransitionOptions resolve)
3081 Q_ASSERT(zone.isValid());
3082 Q_ASSERT(zone.timeSpec() == Qt::TimeZone);
3083 return zone.d->stateAtZoneTime(millis, resolve);
3088 QDateTimePrivate::TransitionOptions resolve)
3090 if (zone.timeSpec() == Qt::LocalTime)
3091 return QDateTimePrivate::localStateAtMillis(millis, resolve);
3092#if QT_CONFIG(timezone)
3093 if (zone.timeSpec() == Qt::TimeZone && zone.isValid())
3094 return QDateTimePrivate::zoneStateAtMillis(zone, millis, resolve);
3101 return spec == Qt::LocalTime || spec == Qt::UTC;
3106 if constexpr (!QDateTimeData::CanBeSmall)
3110 sd.msecs = qintptr(msecs);
3111 return sd.msecs == msecs;
3114static constexpr inline
3117 status &= ~QDateTimePrivate::TimeSpecMask;
3118 status |= QDateTimePrivate::StatusFlags::fromInt(
int(spec) << QDateTimePrivate::TimeSpecShift);
3124 return Qt::TimeSpec((status & QDateTimePrivate::TimeSpecMask).toInt() >> QDateTimePrivate::TimeSpecShift);
3131 sf &= ~QDateTimePrivate::DaylightMask;
3132 if (status == QDateTimePrivate::DaylightTime) {
3133 sf |= QDateTimePrivate::SetToDaylightTime;
3134 }
else if (status == QDateTimePrivate::StandardTime) {
3135 sf |= QDateTimePrivate::SetToStandardTime;
3141static constexpr inline
3144 if (status.testFlag(QDateTimePrivate::SetToDaylightTime))
3145 return QDateTimePrivate::DaylightTime;
3146 if (status.testFlag(QDateTimePrivate::SetToStandardTime))
3147 return QDateTimePrivate::StandardTime;
3148 return QDateTimePrivate::UnknownDaylightTime;
3156 return qintptr(d.d) >> 8;
3166 return QDateTimePrivate::StatusFlag(qintptr(d.d) & 0xFF);
3173 return extractSpec(getStatus(d));
3177
3178
3179
3180
3183 const auto status = getStatus(a);
3184 if (status != getStatus(b))
3188 switch (extractSpec(status)) {
3195
3196
3197
3198
3199 case Qt::OffsetFromUTC:
3200 Q_ASSERT(!a.isShort() && !b.isShort());
3201 return a->m_offsetFromUtc == b->m_offsetFromUtc;
3203 Q_UNREACHABLE_RETURN(
false);
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3220 constexpr quint64 UtcOffsetMillisRange
3221 = quint64(QTimeZone::MaxUtcOffsetSecs - QTimeZone::MinUtcOffsetSecs) * MSECS_PER_SEC;
3223 return qSubOverflow(leftMillis, rightMillis, &gap) || QtPrivate::qUnsignedAbs(gap) > UtcOffsetMillisRange;
3228 QDateTimePrivate::TransitionOptions resolve)
3230 Q_ASSERT(zone.timeSpec() == Qt::TimeZone || zone.timeSpec() == Qt::LocalTime);
3231 auto status = getStatus(d);
3232 Q_ASSERT(extractSpec(status) == zone.timeSpec());
3233 int offsetFromUtc = 0;
3235
3236
3237
3238
3239
3240
3241
3242
3245 if (!status.testFlags(QDateTimePrivate::ValidDate | QDateTimePrivate::ValidTime)) {
3246 status.setFlag(QDateTimePrivate::ValidDateTime,
false);
3250 qint64 msecs = getMSecs(d);
3251 QDateTimePrivate::ZoneState state = stateAtMillis(zone, msecs, resolve);
3252 Q_ASSERT(!state.valid || (state.offset >= -SECS_PER_DAY && state.offset <= SECS_PER_DAY));
3253 if (state.dst == QDateTimePrivate::UnknownDaylightTime) {
3254 status.setFlag(QDateTimePrivate::ValidDateTime,
false);
3255 }
else if (state.valid) {
3256 status = mergeDaylightStatus(status, state.dst);
3257 offsetFromUtc = state.offset;
3258 status.setFlag(QDateTimePrivate::ValidDateTime,
true);
3259 if (Q_UNLIKELY(msecs != state.when)) {
3261 if (status.testFlag(QDateTimePrivate::ShortData)) {
3262 if (msecsCanBeSmall(state.when)) {
3263 d.data.msecs = qintptr(state.when);
3266 status.setFlag(QDateTimePrivate::ShortData,
false);
3270 if (!status.testFlag(QDateTimePrivate::ShortData))
3271 d->m_msecs = state.when;
3274 status.setFlag(QDateTimePrivate::ValidDateTime,
false);
3278 if (status.testFlag(QDateTimePrivate::ShortData)) {
3279 d.data.status = status.toInt();
3281 d->m_status = status;
3282 d->m_offsetFromUtc = offsetFromUtc;
3289 auto status = getStatus(d);
3290 Q_ASSERT(QTimeZone::isUtcOrFixedOffset(extractSpec(status)));
3291 status.setFlag(QDateTimePrivate::ValidDateTime,
3292 status.testFlags(QDateTimePrivate::ValidDate | QDateTimePrivate::ValidTime));
3294 if (status.testFlag(QDateTimePrivate::ShortData))
3295 d.data.status = status.toInt();
3297 d->m_status = status;
3303 auto spec = extractSpec(getStatus(d));
3305 case Qt::OffsetFromUTC:
3314 refreshZonedDateTime(d, d.timeZone(), toTransitionOptions(resolve));
3320 QDateTime::TransitionResolution resolve)
3322 Qt::TimeSpec spec = zone.timeSpec();
3323 auto status = mergeSpec(getStatus(d), spec);
3324 bool reuse = d.isShort();
3329 Q_ASSERT(zone.fixedSecondsAheadOfUtc() == 0);
3331 case Qt::OffsetFromUTC:
3333 offset = zone.fixedSecondsAheadOfUtc();
3343 status &= ~(QDateTimePrivate::ValidDateTime | QDateTimePrivate::DaylightMask);
3345 d.data.status = status.toInt();
3348 d->m_status = status & ~QDateTimePrivate::ShortData;
3349 d->m_offsetFromUtc = offset;
3350#if QT_CONFIG(timezone)
3351 if (spec == Qt::TimeZone)
3352 d->m_timeZone = zone;
3356 if (QTimeZone::isUtcOrFixedOffset(spec))
3359 refreshZonedDateTime(d, zone, toTransitionOptions(resolve));
3365 if (!time.isValid() && date.isValid())
3366 time = QTime::fromMSecsSinceStartOfDay(0);
3368 QDateTimePrivate::StatusFlags newStatus = { };
3372 if (date.isValid()) {
3373 days = date.toJulianDay() - JULIAN_DAY_FOR_EPOCH;
3374 newStatus = QDateTimePrivate::ValidDate;
3379 if (time.isValid()) {
3380 ds = time.msecsSinceStartOfDay();
3381 newStatus |= QDateTimePrivate::ValidTime;
3383 Q_ASSERT(ds < MSECS_PER_DAY);
3386 if (days < 0 && ds > 0) {
3388 ds -= MSECS_PER_DAY;
3393 if (daysAndMillisOverflow(days, qint64(ds), &msecs)) {
3394 newStatus = QDateTimePrivate::StatusFlags{};
3399 if (msecsCanBeSmall(msecs)) {
3401 d.data.msecs = qintptr(msecs);
3402 d.data.status &= ~(QDateTimePrivate::ValidityMask | QDateTimePrivate::DaylightMask).toInt();
3403 d.data.status |= newStatus.toInt();
3412 d->m_status &= ~(QDateTimePrivate::ValidityMask | QDateTimePrivate::DaylightMask);
3413 d->m_status |= newStatus;
3419 auto status = getStatus(d);
3420 const qint64 msecs = getMSecs(d);
3421 const auto dayMilli = QRoundingDown::qDivMod<MSECS_PER_DAY>(msecs);
3422 return { status.testFlag(QDateTimePrivate::ValidDate)
3423 ? QDate::fromJulianDay(JULIAN_DAY_FOR_EPOCH + dayMilli.quotient)
3425 status.testFlag(QDateTimePrivate::ValidTime)
3426 ? QTime::fromMSecsSinceStartOfDay(dayMilli.remainder)
3431
3432
3434inline QDateTime::Data::Data()
noexcept
3439 quintptr value = mergeSpec(QDateTimePrivate::ShortData, Qt::LocalTime).toInt();
3440 d =
reinterpret_cast<QDateTimePrivate *>(value);
3443inline QDateTime::Data::Data(
const QTimeZone &zone)
3445 Qt::TimeSpec spec = zone.timeSpec();
3446 if (CanBeSmall && Q_LIKELY(specCanBeSmall(spec))) {
3447 quintptr value = mergeSpec(QDateTimePrivate::ShortData, spec).toInt();
3448 d =
reinterpret_cast<QDateTimePrivate *>(value);
3449 Q_ASSERT(isShort());
3452 d =
new QDateTimePrivate;
3454 d->m_status = mergeSpec({}, spec);
3455 if (spec == Qt::OffsetFromUTC)
3456 d->m_offsetFromUtc = zone.fixedSecondsAheadOfUtc();
3457 else if (spec == Qt::TimeZone)
3458 d->m_timeZone = zone;
3459 Q_ASSERT(!isShort());
3463inline QDateTime::Data::Data(
const Data &other)
noexcept
3468 if (specCanBeSmall(extractSpec(d->m_status)) && msecsCanBeSmall(d->m_msecs)) {
3470 sd.msecs = qintptr(d->m_msecs);
3471 sd.status = (d->m_status | QDateTimePrivate::ShortData).toInt();
3480inline QDateTime::Data::Data(Data &&other)
noexcept
3485 Q_ASSERT(dummy.isShort());
3486 other.data = dummy.data;
3489inline QDateTime::Data &QDateTime::Data::operator=(
const Data &other)
noexcept
3491 if (isShort() ? data == other.data : d == other.d)
3496 if (!other.isShort()) {
3498 if (specCanBeSmall(extractSpec(other.d->m_status)) && msecsCanBeSmall(other.d->m_msecs)) {
3500 sd.msecs = qintptr(other.d->m_msecs);
3501 sd.status = (other.d->m_status | QDateTimePrivate::ShortData).toInt();
3509 if (!(quintptr(x) & QDateTimePrivate::ShortData) && !x->ref.deref())
3514inline QDateTime::Data::~Data()
3516 if (!isShort() && !d->ref.deref())
3520inline bool QDateTime::Data::isShort()
const
3522 bool b = quintptr(d) & QDateTimePrivate::ShortData;
3525 Q_ASSERT(b || !d->m_status.testFlag(QDateTimePrivate::ShortData));
3529 if constexpr (CanBeSmall)
3531 return Q_UNLIKELY(b);
3534inline void QDateTime::Data::detach()
3536 QDateTimePrivate *x;
3537 bool wasShort = isShort();
3540 x =
new QDateTimePrivate;
3541 x->m_status = QDateTimePrivate::StatusFlags::fromInt(data.status) & ~QDateTimePrivate::ShortData;
3542 x->m_msecs = data.msecs;
3544 if (d->ref.loadRelaxed() == 1)
3547 x =
new QDateTimePrivate(*d);
3550 x->ref.storeRelaxed(1);
3551 if (!wasShort && !d->ref.deref())
3556void QDateTime::Data::invalidate()
3559 data.status &= ~
int(QDateTimePrivate::ValidityMask);
3562 d->m_status &= ~QDateTimePrivate::ValidityMask;
3566QTimeZone QDateTime::Data::timeZone()
const
3568 switch (getSpec(*
this)) {
3570 return QTimeZone::UTC;
3571 case Qt::OffsetFromUTC:
3572 return QTimeZone::fromSecondsAheadOfUtc(d->m_offsetFromUtc);
3574#if QT_CONFIG(timezone)
3575 if (d->m_timeZone.isValid())
3576 return d->m_timeZone;
3580 return QTimeZone::LocalTime;
3585inline const QDateTimePrivate *QDateTime::Data::operator->()
const
3587 Q_ASSERT(!isShort());
3591inline QDateTimePrivate *QDateTime::Data::operator->()
3594 Q_ASSERT(!isShort());
3595 Q_ASSERT(d->ref.loadRelaxed() == 1);
3600
3601
3604QDateTime::Data QDateTimePrivate::create(QDate toDate, QTime toTime,
const QTimeZone &zone,
3605 QDateTime::TransitionResolution resolve)
3607 QDateTime::Data result(zone);
3608 setDateTime(result, toDate, toTime);
3609 if (zone.isUtcOrFixedOffset())
3610 refreshSimpleDateTime(result);
3612 refreshZonedDateTime(result, zone, toTransitionOptions(resolve));
3617
3618
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
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
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
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
3999
4000
4001
4002
4003
4004
4005QDateTime::QDateTime()
noexcept
4007#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
) || defined(QT_BOOTSTRAPPED) || QT_POINTER_SIZE == 8
4008 static_assert(
sizeof(ShortData) ==
sizeof(qint64));
4009 static_assert(
sizeof(Data) ==
sizeof(qint64));
4011 static_assert(
sizeof(ShortData) >=
sizeof(
void*),
"oops, Data::swap() is broken!");
4014#if QT_DEPRECATED_SINCE(6
, 9
)
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035QDateTime::QDateTime(QDate date, QTime time, Qt::TimeSpec spec,
int offsetSeconds)
4036 : d(QDateTimePrivate::create(date, time, asTimeZone(spec, offsetSeconds,
"QDateTime"),
4037 TransitionResolution::LegacyBehavior))
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4060QDateTime::QDateTime(QDate date, QTime time,
const QTimeZone &timeZone, TransitionResolution resolve)
4061 : d(QDateTimePrivate::create(date, time, timeZone, resolve))
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4078QDateTime::QDateTime(QDate date, QTime time, TransitionResolution resolve)
4079 : d(QDateTimePrivate::create(date, time, QTimeZone::LocalTime, resolve))
4084
4085
4086QDateTime::QDateTime(
const QDateTime &other)
noexcept
4092
4093
4094
4095
4096QDateTime::QDateTime(QDateTime &&other)
noexcept
4097 : d(std::move(other.d))
4102
4103
4104QDateTime::~QDateTime()
4109
4110
4112QDateTime &QDateTime::operator=(
const QDateTime &other)
noexcept
4118
4119
4120
4121
4124
4125
4126
4127
4128
4130bool QDateTime::isNull()
const
4133 return !getStatus(d).testAnyFlag(QDateTimePrivate::ValidityMask);
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4150bool QDateTime::isValid()
const
4152 return getStatus(d).testFlag(QDateTimePrivate::ValidDateTime);
4156
4157
4158
4159
4161QDate QDateTime::date()
const
4163 return getStatus(d).testFlag(QDateTimePrivate::ValidDate) ? msecsToDate(getMSecs(d)) : QDate();
4167
4168
4169
4170
4172QTime QDateTime::time()
const
4174 return getStatus(d).testFlag(QDateTimePrivate::ValidTime) ? msecsToTime(getMSecs(d)) : QTime();
4178
4179
4180
4181
4182
4183
4184
4185
4186
4188Qt::TimeSpec QDateTime::timeSpec()
const
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4207QTimeZone QDateTime::timeRepresentation()
const
4209 return d.timeZone();
4212#if QT_CONFIG(timezone)
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4228QTimeZone QDateTime::timeZone()
const
4230 return d.timeZone().asBackendZone();
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4256int QDateTime::offsetFromUtc()
const
4258 const auto status = getStatus(d);
4259 if (!status.testFlags(QDateTimePrivate::ValidDate | QDateTimePrivate::ValidTime))
4263 return d->m_offsetFromUtc;
4265 auto spec = extractSpec(status);
4266 if (spec == Qt::LocalTime) {
4268 const auto resolve = toTransitionOptions(extractDaylightStatus(status));
4269 return QDateTimePrivate::localStateAtMillis(getMSecs(d), resolve).offset;
4272 Q_ASSERT(spec == Qt::UTC);
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4297QString QDateTime::timeZoneAbbreviation()
const
4302 switch (getSpec(d)) {
4305 case Qt::OffsetFromUTC:
4306 return "UTC"_L1 + toOffsetString(Qt::ISODate, d->m_offsetFromUtc);
4308#if !QT_CONFIG(timezone)
4311 Q_ASSERT(d->m_timeZone.isValid());
4312 return d->m_timeZone.abbreviation(*
this);
4315#if defined(Q_OS_WIN) && QT_CONFIG(timezone)
4317 if (QString sys = QTimeZone::systemTimeZone().abbreviation(*
this); !sys.isEmpty())
4321 return QDateTimePrivate::localNameAtMillis(getMSecs(d),
4322 extractDaylightStatus(getStatus(d)));
4328
4329
4330
4331
4332
4333
4334
4335
4336
4338bool QDateTime::isDaylightTime()
const
4343 switch (getSpec(d)) {
4345 case Qt::OffsetFromUTC:
4348#if !QT_CONFIG(timezone)
4351 Q_ASSERT(d->m_timeZone.isValid());
4352 if (
auto dst = extractDaylightStatus(getStatus(d));
4353 dst != QDateTimePrivate::UnknownDaylightTime) {
4354 return dst == QDateTimePrivate::DaylightTime;
4356 return d->m_timeZone.d->isDaylightTime(toMSecsSinceEpoch());
4358 case Qt::LocalTime: {
4359 auto dst = extractDaylightStatus(getStatus(d));
4360 if (dst == QDateTimePrivate::UnknownDaylightTime) {
4361 dst = QDateTimePrivate::localStateAtMillis(
4362 getMSecs(d), toTransitionOptions(TransitionResolution::LegacyBehavior)).dst;
4364 return dst == QDateTimePrivate::DaylightTime;
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4385void QDateTime::setDate(QDate date, TransitionResolution resolve)
4387 setDateTime(d, date, time());
4388 checkValidDateTime(d, resolve);
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4410void QDateTime::setTime(QTime time, TransitionResolution resolve)
4412 setDateTime(d, date(), time);
4413 checkValidDateTime(d, resolve);
4416#if QT_DEPRECATED_SINCE(6
, 9
)
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4435void QDateTime::setTimeSpec(Qt::TimeSpec spec)
4437 reviseTimeZone(d, asTimeZone(spec, 0,
"QDateTime::setTimeSpec"),
4438 TransitionResolution::LegacyBehavior);
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4457void QDateTime::setOffsetFromUtc(
int offsetSeconds)
4459 reviseTimeZone(d, QTimeZone::fromSecondsAheadOfUtc(offsetSeconds),
4460 TransitionResolution::Reject);
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4484void QDateTime::setTimeZone(
const QTimeZone &toZone, TransitionResolution resolve)
4486 reviseTimeZone(d, toZone, resolve);
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504qint64 QDateTime::toMSecsSinceEpoch()
const
4511 const auto status = getStatus(d);
4512 if (!status.testFlags(QDateTimePrivate::ValidDate | QDateTimePrivate::ValidTime))
4515 switch (extractSpec(status)) {
4519 case Qt::OffsetFromUTC:
4520 Q_ASSERT(!d.isShort());
4521 return d->m_msecs - d->m_offsetFromUtc * MSECS_PER_SEC;
4524 if (status.testFlag(QDateTimePrivate::ShortData)) {
4526 const auto resolve = toTransitionOptions(extractDaylightStatus(getStatus(d)));
4527 const auto state = QDateTimePrivate::localStateAtMillis(getMSecs(d), resolve);
4528 return state.when - state.offset * MSECS_PER_SEC;
4531 return d->m_msecs - d->m_offsetFromUtc * MSECS_PER_SEC;
4534 Q_ASSERT(!d.isShort());
4535#if QT_CONFIG(timezone)
4537 if (d->m_timeZone.isValid())
4538 return d->m_msecs - d->m_offsetFromUtc * MSECS_PER_SEC;
4542 Q_UNREACHABLE_RETURN(0);
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560qint64 QDateTime::toSecsSinceEpoch()
const
4562 return toMSecsSinceEpoch() / MSECS_PER_SEC;
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580void QDateTime::setMSecsSinceEpoch(qint64 msecs)
4582 auto status = getStatus(d);
4583 const auto spec = extractSpec(status);
4584 Q_ASSERT(specCanBeSmall(spec) || !d.isShort());
4585 QDateTimePrivate::ZoneState state(msecs);
4587 status &= ~QDateTimePrivate::ValidityMask;
4588 if (QTimeZone::isUtcOrFixedOffset(spec)) {
4589 if (spec == Qt::OffsetFromUTC)
4590 state.offset = d->m_offsetFromUtc;
4591 if (!state.offset || !qAddOverflow(msecs, state.offset * MSECS_PER_SEC, &state.when))
4592 status |= QDateTimePrivate::ValidityMask;
4593 }
else if (spec == Qt::LocalTime) {
4594 state = QDateTimePrivate::expressUtcAsLocal(msecs);
4596 status = mergeDaylightStatus(status | QDateTimePrivate::ValidityMask, state.dst);
4597#if QT_CONFIG(timezone)
4598 }
else if (spec == Qt::TimeZone && (d.detach(), d->m_timeZone.isValid())) {
4599 const auto data = d->m_timeZone.d->data(msecs);
4600 if (Q_LIKELY(data.offsetFromUtc != QTimeZonePrivate::invalidSeconds())) {
4601 state.offset = data.offsetFromUtc;
4602 Q_ASSERT(state.offset >= -SECS_PER_DAY && state.offset <= SECS_PER_DAY);
4604 || !Q_UNLIKELY(qAddOverflow(msecs, state.offset * MSECS_PER_SEC, &state.when))) {
4605 d->m_status = mergeDaylightStatus(status | QDateTimePrivate::ValidityMask,
4606 data.daylightTimeOffset
4607 ? QDateTimePrivate::DaylightTime
4608 : QDateTimePrivate::StandardTime);
4609 d->m_msecs = state.when;
4610 d->m_offsetFromUtc = state.offset;
4616 Q_ASSERT(!status.testFlag(QDateTimePrivate::ValidDateTime)
4617 || (state.offset >= -SECS_PER_DAY && state.offset <= SECS_PER_DAY));
4619 if (msecsCanBeSmall(state.when) && d.isShort()) {
4621 d.data.msecs = qintptr(state.when);
4622 d.data.status = status.toInt();
4625 d->m_status = status & ~QDateTimePrivate::ShortData;
4626 d->m_msecs = state.when;
4627 d->m_offsetFromUtc = state.offset;
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642void QDateTime::setSecsSinceEpoch(qint64 secs)
4645 if (!qMulOverflow(secs, std::integral_constant<qint64, MSECS_PER_SEC>(), &msecs))
4646 setMSecsSinceEpoch(msecs);
4651#if QT_CONFIG(datestring)
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682QString QDateTime::toString(Qt::DateFormat format)
const
4689 case Qt::RFC2822Date:
4690 buf = QLocale::c().toString(*
this, u"dd MMM yyyy hh:mm:ss ");
4691 buf += toOffsetString(Qt::TextDate, offsetFromUtc());
4694 case Qt::TextDate: {
4695 const std::pair<QDate, QTime> p = getDateTime(d);
4696 buf = toStringTextDate(p.first);
4698 buf.insert(buf.lastIndexOf(u' '),
4699 u' ' + p.second.toString(Qt::TextDate));
4701 switch (timeSpec()) {
4704#if QT_CONFIG(timezone)
4706 buf += u' ' + d->m_timeZone.displayName(
4707 *
this, QTimeZone::OffsetName, QLocale::c());
4716 if (getSpec(d) == Qt::OffsetFromUTC)
4717 buf += toOffsetString(Qt::TextDate, offsetFromUtc());
4722 case Qt::ISODateWithMs: {
4723 const std::pair<QDate, QTime> p = getDateTime(d);
4724 buf = toStringIsoDate(p.first);
4727 buf += u'T' + p.second.toString(format);
4728 switch (getSpec(d)) {
4732 case Qt::OffsetFromUTC:
4734 buf += toOffsetString(Qt::ISODate, offsetFromUtc());
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788QString QDateTime::toString(QStringView format, QCalendar cal)
const
4790 return QLocale::c().toString(*
this, format, cal);
4795
4796
4797
4798QString QDateTime::toString(QStringView format)
const
4800 return QLocale::c().toString(*
this, format, QCalendar());
4804
4805
4806
4807QString QDateTime::toString(
const QString &format)
const
4809 return QLocale::c().toString(*
this, qToStringViewIgnoringNull(format), QCalendar());
4815 const QDateTimePrivate::TransitionOptions resolve = toTransitionOptions(
4816 forward ? QDateTime::TransitionResolution::RelativeToBefore
4817 : QDateTime::TransitionResolution::RelativeToAfter);
4818 auto status = getStatus(d);
4819 Q_ASSERT(status.testFlags(QDateTimePrivate::ValidDate | QDateTimePrivate::ValidTime
4820 | QDateTimePrivate::ValidDateTime));
4821 auto spec = extractSpec(status);
4822 if (QTimeZone::isUtcOrFixedOffset(spec)) {
4823 setDateTime(d, date, time);
4827 qint64 local = timeToMSecs(date, time);
4828 const QDateTimePrivate::ZoneState state = stateAtMillis(d.timeZone(), local, resolve);
4829 Q_ASSERT(state.valid || state.dst == QDateTimePrivate::UnknownDaylightTime);
4830 if (state.dst == QDateTimePrivate::UnknownDaylightTime)
4831 status.setFlag(QDateTimePrivate::ValidDateTime,
false);
4833 status = mergeDaylightStatus(status | QDateTimePrivate::ValidDateTime, state.dst);
4835 if (status & QDateTimePrivate::ShortData) {
4836 d.data.msecs = state.when;
4837 d.data.status = status.toInt();
4840 d->m_status = status;
4842 d->m_msecs = state.when;
4843 d->m_offsetFromUtc = state.offset;
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4863QDateTime QDateTime::addDays(qint64 ndays)
const
4868 QDateTime dt(*
this);
4869 std::pair<QDate, QTime> p = getDateTime(d);
4870 massageAdjustedDateTime(dt.d, p.first.addDays(ndays), p.second, ndays >= 0);
4875
4876
4877
4878
4879
4880
4881
4882
4885
4886
4887
4888
4889
4890
4891
4892
4895
4896
4897
4898
4899
4900
4901
4902
4905
4906
4907
4908
4909
4910
4911
4912
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4929QDateTime QDateTime::addMonths(
int nmonths)
const
4934 QDateTime dt(*
this);
4935 std::pair<QDate, QTime> p = getDateTime(d);
4936 massageAdjustedDateTime(dt.d, p.first.addMonths(nmonths), p.second, nmonths >= 0);
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4955QDateTime QDateTime::addYears(
int nyears)
const
4960 QDateTime dt(*
this);
4961 std::pair<QDate, QTime> p = getDateTime(d);
4962 massageAdjustedDateTime(dt.d, p.first.addYears(nyears), p.second, nyears >= 0);
4967
4968
4969
4970
4971
4972
4973
4974
4976QDateTime QDateTime::addSecs(qint64 s)
const
4979 if (qMulOverflow(s, std::integral_constant<qint64, MSECS_PER_SEC>(), &msecs))
4981 return addMSecs(msecs);
4985
4986
4987
4988
4989
4990
4991
4992
4993QDateTime QDateTime::addMSecs(qint64 msecs)
const
4998 QDateTime dt(*
this);
4999 switch (getSpec(d)) {
5003 if (!qAddOverflow(toMSecsSinceEpoch(), msecs, &msecs))
5004 dt.setMSecsSinceEpoch(msecs);
5009 case Qt::OffsetFromUTC:
5011 if (qAddOverflow(getMSecs(d), msecs, &msecs)) {
5013 }
else if (d.isShort() && msecsCanBeSmall(msecs)) {
5014 dt.d.data.msecs = qintptr(msecs);
5017 dt.d->m_msecs = msecs;
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5060qint64 QDateTime::daysTo(
const QDateTime &other)
const
5062 return date().daysTo(other.date());
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5082qint64 QDateTime::secsTo(
const QDateTime &other)
const
5084 return msecsTo(other) / MSECS_PER_SEC;
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5101qint64 QDateTime::msecsTo(
const QDateTime &other)
const
5103 if (!isValid() || !other.isValid())
5106 return other.toMSecsSinceEpoch() - toMSecsSinceEpoch();
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5178#if QT_DEPRECATED_SINCE(6
, 9
)
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5198QDateTime QDateTime::toTimeSpec(Qt::TimeSpec spec)
const
5200 return toTimeZone(asTimeZone(spec, 0,
"toTimeSpec"));
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5218QDateTime QDateTime::toOffsetFromUtc(
int offsetSeconds)
const
5220 return toTimeZone(QTimeZone::fromSecondsAheadOfUtc(offsetSeconds));
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234QDateTime QDateTime::toLocalTime()
const
5236 return toTimeZone(QTimeZone::LocalTime);
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250QDateTime QDateTime::toUTC()
const
5252 return toTimeZone(QTimeZone::UTC);
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5273QDateTime QDateTime::toTimeZone(
const QTimeZone &timeZone)
const
5275 if (timeRepresentation() == timeZone)
5279 QDateTime ret = *
this;
5280 ret.setTimeZone(timeZone);
5284 return fromMSecsSinceEpoch(toMSecsSinceEpoch(), timeZone);
5288
5289
5290
5291
5292
5293
5295bool QDateTime::equals(
const QDateTime &other)
const
5298 return !other.isValid();
5299 if (!other.isValid())
5302 const qint64 thisMs = getMSecs(d);
5303 const qint64 yourMs = getMSecs(other.d);
5304 if (usesSameOffset(d, other.d) || areFarEnoughApart(thisMs, yourMs))
5305 return thisMs == yourMs;
5308 return toMSecsSinceEpoch() == other.toMSecsSinceEpoch();
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5333
5334
5335
5336
5337
5338
5339
5340
5341
5346 return rhs.isValid() ? Qt::weak_ordering::less : Qt::weak_ordering::equivalent;
5349 return Qt::weak_ordering::greater;
5351 const qint64 lhms = getMSecs(lhs.d), rhms = getMSecs(rhs.d);
5352 if (usesSameOffset(lhs.d, rhs.d) || areFarEnoughApart(lhms, rhms))
5353 return Qt::compareThreeWay(lhms, rhms);
5356 return Qt::compareThreeWay(lhs.toMSecsSinceEpoch(), rhs.toMSecsSinceEpoch());
5360
5361
5362
5363
5364
5365
5366
5367
5368
5371
5372
5373
5374
5375
5376
5377
5378
5379
5382
5383
5384
5385
5386
5387
5388
5389
5392
5393
5394
5395
5396
5397
5398
5399
5400
5403
5404
5405
5406
5407
5408
5409
5410
5411
5414
5415
5417QDateTime QDateTime::currentDateTime()
5419 return currentDateTime(QTimeZone::LocalTime);
5423
5424
5425
5426
5427
5428
5429
5430
5432QDateTime QDateTime::currentDateTimeUtc()
5434 return currentDateTime(QTimeZone::UTC);
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5450
5451
5452
5453
5454
5455
5456
5457
5458
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5486
5487
5489
5490
5491
5492QDateTime QDateTime::fromStdTimePoint(
5493 std::chrono::time_point<
5494 std::chrono::system_clock,
5495 std::chrono::milliseconds
5498 return fromMSecsSinceEpoch(time.time_since_epoch().count(), QTimeZone::UTC);
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5565#if defined(Q_OS_WIN)
5566static inline uint msecsFromDecomposed(
int hour,
int minute,
int sec,
int msec = 0)
5568 return MSECS_PER_HOUR * hour + MSECS_PER_MIN * minute + MSECS_PER_SEC * sec + msec;
5571QDate QDate::currentDate()
5575 return QDate(st.wYear, st.wMonth, st.wDay);
5578QTime QTime::currentTime()
5583 ct.setHMS(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
5587QDateTime QDateTime::currentDateTime(
const QTimeZone &zone)
5591 const Qt::TimeSpec spec = zone.timeSpec();
5599 QDate d(st.wYear, st.wMonth, st.wDay);
5600 QTime t(msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds));
5601 QDateTime utc(d, t, QTimeZone::UTC);
5602 return spec == Qt::UTC ? utc : utc.toTimeZone(zone);
5605qint64 QDateTime::currentMSecsSinceEpoch()
noexcept
5609 const qint64 daysAfterEpoch = QDate(1970, 1, 1).daysTo(QDate(st.wYear, st.wMonth, st.wDay));
5611 return msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds) +
5612 daysAfterEpoch * MSECS_PER_DAY;
5615qint64 QDateTime::currentSecsSinceEpoch()
noexcept
5619 const qint64 daysAfterEpoch = QDate(1970, 1, 1).daysTo(QDate(st.wYear, st.wMonth, st.wDay));
5621 return st.wHour * SECS_PER_HOUR + st.wMinute * SECS_PER_MIN + st.wSecond +
5622 daysAfterEpoch * SECS_PER_DAY;
5625#elif defined(Q_OS_UNIX)
5626QDate QDate::currentDate()
5628 return QDateTime::currentDateTime().date();
5631QTime QTime::currentTime()
5633 return QDateTime::currentDateTime().time();
5636QDateTime QDateTime::currentDateTime(
const QTimeZone &zone)
5638 return fromMSecsSinceEpoch(currentMSecsSinceEpoch(), zone);
5641qint64 QDateTime::currentMSecsSinceEpoch()
noexcept
5643 struct timespec when;
5644 if (clock_gettime(CLOCK_REALTIME, &when) == 0)
5645 return when.tv_sec * MSECS_PER_SEC + (when.tv_nsec + 500'000) / 1'000'000;
5646 Q_UNREACHABLE_RETURN(0);
5649qint64 QDateTime::currentSecsSinceEpoch()
noexcept
5651 struct timespec when;
5652 if (clock_gettime(CLOCK_REALTIME, &when) == 0)
5654 Q_UNREACHABLE_RETURN(0);
5657#error "What system is this?"
5660#if QT_DEPRECATED_SINCE(6
, 9
)
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec,
int offsetSeconds)
5686 return fromMSecsSinceEpoch(msecs,
5687 asTimeZone(spec, offsetSeconds,
"QDateTime::fromMSecsSinceEpoch"));
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713QDateTime QDateTime::fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec,
int offsetSeconds)
5715 return fromSecsSinceEpoch(secs,
5716 asTimeZone(spec, offsetSeconds,
"QDateTime::fromSecsSinceEpoch"));
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs,
const QTimeZone &timeZone)
5737 reviseTimeZone(dt.d, timeZone, TransitionResolution::Reject);
5738 if (timeZone.isValid())
5739 dt.setMSecsSinceEpoch(msecs);
5744
5746QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs)
5748 return fromMSecsSinceEpoch(msecs, QTimeZone::LocalTime);
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765QDateTime QDateTime::fromSecsSinceEpoch(qint64 secs,
const QTimeZone &timeZone)
5768 reviseTimeZone(dt.d, timeZone, TransitionResolution::Reject);
5769 if (timeZone.isValid())
5770 dt.setSecsSinceEpoch(secs);
5775
5777QDateTime QDateTime::fromSecsSinceEpoch(qint64 secs)
5779 return fromSecsSinceEpoch(secs, QTimeZone::LocalTime);
5782#if QT_CONFIG(datestring)
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5798
5799
5800
5801QDateTime QDateTime::fromString(QStringView string, Qt::DateFormat format)
5803 if (string.isEmpty())
5807 case Qt::RFC2822Date: {
5808 const ParsedRfcDateTime rfc = rfcDateImpl(string);
5810 if (!rfc.date.isValid() || !rfc.time.isValid())
5813 QDateTime dateTime(rfc.date, rfc.time, QTimeZone::UTC);
5814 dateTime.setTimeZone(QTimeZone::fromSecondsAheadOfUtc(rfc.utcOffset));
5818 case Qt::ISODateWithMs: {
5819 const int size = string.size();
5823 QDate date = QDate::fromString(string.first(10), Qt::ISODate);
5824 if (!date.isValid())
5827 return date.startOfDay();
5829 QTimeZone zone = QTimeZone::LocalTime;
5830 QStringView isoString = string.sliced(10);
5833 if (isoString.size() < 2
5834 || !(isoString.startsWith(u'T', Qt::CaseInsensitive)
5838 || isoString.startsWith(u' '))) {
5841 isoString = isoString.sliced(1);
5844 if (isoString.endsWith(u'Z', Qt::CaseInsensitive)) {
5845 zone = QTimeZone::UTC;
5850 int signIndex = isoString.size() - 1;
5851 Q_ASSERT(signIndex >= 0);
5854 QChar character(isoString[signIndex]);
5855 found = character == u'+' || character == u'-';
5856 }
while (!found && --signIndex >= 0);
5860 int offset = fromOffsetString(isoString.sliced(signIndex), &ok);
5863 isoString = isoString.first(signIndex);
5864 zone = QTimeZone::fromSecondsAheadOfUtc(offset);
5870 bool isMidnight24 =
false;
5871 QTime time = fromIsoTimeString(isoString, format, &isMidnight24);
5872 if (!time.isValid())
5875 return date.addDays(1).startOfDay(zone);
5876 return QDateTime(date, time, zone);
5878 case Qt::TextDate: {
5879 QVarLengthArray<QStringView, 6> parts;
5881 auto tokens = string.tokenize(u' ', Qt::SkipEmptyParts);
5882 auto it = tokens.begin();
5883 for (
int i = 0; i < 6 && it != tokens.end(); ++i, ++it)
5884 parts.emplace_back(*it);
5888 if (parts.size() < 5 || it != tokens.end())
5895 if (parts.at(3).contains(u':'))
5897 else if (parts.at(4).contains(u':'))
5903 int day = parts.at(2).toInt(&ok);
5904 int year = ok ? parts.at(yearPart).toInt(&ok) : 0;
5905 int month = fromShortMonthName(parts.at(1));
5906 if (!ok || year == 0 || day == 0 || month < 1)
5909 const QDate date(year, month, day);
5910 if (!date.isValid())
5913 const QTime time = fromIsoTimeString(parts.at(timePart), format,
nullptr);
5914 if (!time.isValid())
5917 if (parts.size() == 5)
5918 return QDateTime(date, time);
5920 QStringView tz = parts.at(5);
5921 if (tz.startsWith(
"UTC"_L1)
5923 || tz.startsWith(
"GMT"_L1, Qt::CaseInsensitive)) {
5926 return QDateTime(date, time, QTimeZone::UTC);
5928 int offset = fromOffsetString(tz, &ok);
5929 return ok ? QDateTime(date, time, QTimeZone::fromSecondsAheadOfUtc(offset))
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
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
6024
6025
6026
6027
6030
6031
6032
6033QDateTime QDateTime::fromString(
const QString &string, QStringView format,
int baseYear,
6036#if QT_CONFIG(datetimeparser)
6039 QDateTimeParser dt(QMetaType::QDateTime, QDateTimeParser::FromString, cal);
6040 dt.setDefaultLocale(QLocale::c());
6041 if (dt.parseFormat(format) && (dt.fromString(string, &datetime, baseYear)
6042 || !datetime.isValid())) {
6055
6056
6057
6058
6061
6062
6063
6064
6067
6068
6069
6070
6073
6074
6075
6076
6077
6078
6081
6082
6083
6084
6085
6086QDateTime QDateTime::fromString(
const QString &string, QStringView format,
int baseYear)
6088 return fromString(string, format, baseYear, QCalendar());
6092
6093
6094
6095
6096
6097
6101
6102
6104#ifndef QT_NO_DATASTREAM
6106
6107
6108
6109
6110
6111
6113QDataStream &operator<<(QDataStream &out, QDate date)
6115 if (out.version() < QDataStream::Qt_5_0)
6116 return out << quint32(date.jd);
6118 return out << date.jd;
6122
6123
6124
6125
6126
6127
6129QDataStream &operator>>(QDataStream &in, QDate &date)
6131 if (in.version() < QDataStream::Qt_5_0) {
6135 date.jd = (jd != 0 ? jd : QDate::nullJd());
6144
6145
6146
6147
6148
6149
6151QDataStream &operator<<(QDataStream &out, QTime time)
6153 if (out.version() >= QDataStream::Qt_4_0) {
6154 return out << quint32(time.mds);
6157 return out << quint32(time.isNull() ? 0 : time.mds);
6162
6163
6164
6165
6166
6167
6169QDataStream &operator>>(QDataStream &in, QTime &time)
6173 if (in.version() >= QDataStream::Qt_4_0) {
6177 time.mds = (ds == 0) ? QTime::NullTime :
int(ds);
6183
6184
6185
6186
6187
6188
6189QDataStream &operator<<(QDataStream &out,
const QDateTime &dateTime)
6191 std::pair<QDate, QTime> dateAndTime;
6194 if (out.version() >= QDataStream::Qt_5_2) {
6197 dateAndTime = getDateTime(dateTime.d);
6198 out << dateAndTime << qint8(dateTime.timeSpec());
6199 if (dateTime.timeSpec() == Qt::OffsetFromUTC)
6200 out << qint32(dateTime.offsetFromUtc());
6201#if QT_CONFIG(timezone)
6202 else if (dateTime.timeSpec() == Qt::TimeZone)
6203 out << dateTime.timeZone();
6206 }
else if (out.version() == QDataStream::Qt_5_0) {
6212 dateAndTime = getDateTime((dateTime.isValid() ? dateTime.toUTC() : dateTime).d);
6213 out << dateAndTime << qint8(dateTime.timeSpec());
6215 }
else if (out.version() >= QDataStream::Qt_4_0) {
6218 dateAndTime = getDateTime(dateTime.d);
6220 switch (dateTime.timeSpec()) {
6222 out << (qint8)QDateTimePrivate::UTC;
6224 case Qt::OffsetFromUTC:
6225 out << (qint8)QDateTimePrivate::OffsetFromUTC;
6228 out << (qint8)QDateTimePrivate::TimeZone;
6231 out << (qint8)QDateTimePrivate::LocalUnknown;
6238 dateAndTime = getDateTime(dateTime.d);
6247
6248
6249
6250
6251
6252
6254QDataStream &operator>>(QDataStream &in, QDateTime &dateTime)
6259 QTimeZone zone(QTimeZone::LocalTime);
6261 if (in.version() >= QDataStream::Qt_5_2) {
6264 in >> dt >> tm >> ts;
6265 switch (
static_cast<Qt::TimeSpec>(ts)) {
6267 zone = QTimeZone::UTC;
6269 case Qt::OffsetFromUTC: {
6272 zone = QTimeZone::fromSecondsAheadOfUtc(offset);
6282 dateTime = QDateTime(dt, tm, zone);
6284 }
else if (in.version() == QDataStream::Qt_5_0) {
6287 in >> dt >> tm >> ts;
6288 dateTime = QDateTime(dt, tm, QTimeZone::UTC);
6289 if (
static_cast<Qt::TimeSpec>(ts) == Qt::LocalTime)
6290 dateTime = dateTime.toTimeZone(zone);
6292 }
else if (in.version() >= QDataStream::Qt_4_0) {
6295 in >> dt >> tm >> ts;
6296 switch (
static_cast<QDateTimePrivate::Spec>(ts)) {
6297 case QDateTimePrivate::OffsetFromUTC:
6298 case QDateTimePrivate::UTC:
6299 zone = QTimeZone::UTC;
6301 case QDateTimePrivate::TimeZone:
6302 case QDateTimePrivate::LocalUnknown:
6303 case QDateTimePrivate::LocalStandard:
6304 case QDateTimePrivate::LocalDST:
6307 dateTime = QDateTime(dt, tm, zone);
6313 dateTime = QDateTime(dt, tm);
6322
6323
6325#if !defined(QT_NO_DEBUG_STREAM) && QT_CONFIG(datestring)
6326QDebug operator<<(QDebug dbg, QDate date)
6328 QDebugStateSaver saver(dbg);
6329 dbg.nospace() <<
"QDate(";
6332 if (
int y = date.year(); y > 0 && y <= 9999)
6333 dbg.nospace() << date.toString(Qt::ISODate);
6335 dbg.nospace() << date.toString(Qt::TextDate);
6337 dbg.nospace() <<
"Invalid";
6338 dbg.nospace() <<
')';
6342QDebug operator<<(QDebug dbg, QTime time)
6344 QDebugStateSaver saver(dbg);
6345 dbg.nospace() <<
"QTime(";
6347 dbg.nospace() << time.toString(u"HH:mm:ss.zzz");
6349 dbg.nospace() <<
"Invalid";
6350 dbg.nospace() <<
')';
6354QDebug operator<<(QDebug dbg,
const QDateTime &date)
6356 QDebugStateSaver saver(dbg);
6357 dbg.nospace() <<
"QDateTime(";
6358 if (date.isValid()) {
6359 const Qt::TimeSpec ts = date.timeSpec();
6360 dbg.noquote() << date.toString(u"yyyy-MM-dd HH:mm:ss.zzz t")
6365 case Qt::OffsetFromUTC:
6366 dbg.space() << date.offsetFromUtc() <<
's';
6369#if QT_CONFIG(timezone)
6370 dbg.space() << date.timeZone().id();
6377 dbg.nospace() <<
"Invalid";
6379 return dbg.nospace() <<
')';
6384
6385
6386
6393 return key.isValid() ? qHash(key.toMSecsSinceEpoch(), seed) : seed;
6397
6398
6399
6402 return qHash(key.toJulianDay(), seed);
6406
6407
6408
6411 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)