11#if QT_CONFIG(jalalicalendar)
12#include "qjalalicalendar_p.h"
14#if QT_CONFIG(islamiccivilcalendar)
15#include "qislamiccivilcalendar_p.h"
18#include <private/qflatmap_p.h>
23#include <qreadwritelock.h>
32 bool operator()(QAnyStringView lhs, QAnyStringView rhs)
const
34 return QAnyStringView::compare(lhs, rhs, Qt::CaseInsensitive) < 0;
41
42
43
48 static constexpr qsizetype ExpectedNumberOfBackends = qsizetype(QCalendar::System::Last) + 1;
51
52
56
57
58
59
60
61 std::vector<QCalendarBackend *> byId;
64
65
66
67
68
77
78
79
80
81
82
83 QAtomicPointer<
const QCalendarBackend> gregorianCalendar =
nullptr;
92
93
94
95
98 void ensurePopulated();
99 QCalendarBackend *registerSystemBackendLockHeld(QCalendar::System system);
100 void registerBackendLockHeld(QCalendarBackend *backend,
const QStringList &names,
101 QCalendar::System system);
106 byId.resize(ExpectedNumberOfBackends);
107 byName.reserve(ExpectedNumberOfBackends * 2);
119
120
121
122
125 const QCalendarBackend *backend = gregorianCalendar.loadAcquire();
126 if (Q_LIKELY(backend !=
nullptr))
128 return fromEnum(QCalendar::System::Gregorian);
132
133
134
135
138 return backend == gregorianCalendar.loadRelaxed();
141 const QCalendarBackend *
fromName(QAnyStringView name);
143 const QCalendarBackend *
fromEnum(QCalendar::System system);
149
150
151
152
153
156 QWriteLocker locker(&lock);
158 status.storeRelaxed(IsBeingDestroyed);
164
165
166
167
168
169
170
171
172
173
174
175
176
179 Q_ASSERT(!backend->calendarId().isValid());
183 QWriteLocker locker(&lock);
184 registerBackendLockHeld(backend, names, QCalendar::System::User);
188
189
190
191
192
193
194
197 if (Q_LIKELY(status.loadAcquire() != Unpopulated))
200 QWriteLocker locker(&lock);
201 if (status.loadAcquire() != Unpopulated)
204 for (
int i = 0; i <=
int(QCalendar::System::Last); ++i) {
205 if (byId[i] ==
nullptr)
206 registerSystemBackendLockHeld(QCalendar::System(i));
209#if defined(QT_FORCE_ASSERTS) || !defined(QT_NO_DEBUG)
210 auto oldValue = status.fetchAndStoreRelease(Populated);
211 Q_ASSERT(oldValue == Unpopulated);
213 status.storeRelease(Populated);
218
219
220
221
222
223
224QCalendarBackend *
QCalendarRegistry::registerSystemBackendLockHeld(QCalendar::System system)
226 Q_ASSERT(system != QCalendar::System::User);
228 QCalendarBackend *backend =
nullptr;
232 case QCalendar::System::Gregorian:
233 backend =
new QGregorianCalendar;
234 names = QGregorianCalendar::nameList();
236#ifndef QT_BOOTSTRAPPED
237 case QCalendar::System::Julian:
238 backend =
new QJulianCalendar;
239 names = QJulianCalendar::nameList();
241 case QCalendar::System::Milankovic:
242 backend =
new QMilankovicCalendar;
243 names = QMilankovicCalendar::nameList();
246#if QT_CONFIG(jalalicalendar)
247 case QCalendar::System::Jalali:
248 backend =
new QJalaliCalendar;
249 names = QJalaliCalendar::nameList();
252#if QT_CONFIG(islamiccivilcalendar)
253 case QCalendar::System::IslamicCivil:
254 backend =
new QIslamicCivilCalendar;
255 names = QIslamicCivilCalendar::nameList();
258 case QCalendar::System::Last:
260 case QCalendar::System::User:
266 registerBackendLockHeld(backend, names, system);
267 Q_ASSERT(backend == byId[size_t(system)]);
273
274
275
276
277
278
279void QCalendarRegistry::registerBackendLockHeld(QCalendarBackend *backend,
const QStringList &names,
280 QCalendar::System system)
282 Q_ASSERT(!backend->calendarId().isValid());
284 auto index = size_t(system);
288 if (system == QCalendar::System::User) {
289 backend->setIndex(byId.size());
290 byId.push_back(backend);
291 }
else if (byId[index] ==
nullptr) {
292 backend->setIndex(index);
293 if (system == QCalendar::System::Gregorian) {
294#if defined(QT_FORCE_ASSERTS) || !defined(QT_NO_DEBUG)
295 auto oldValue = gregorianCalendar.fetchAndStoreRelease(backend);
296 Q_ASSERT(oldValue ==
nullptr);
298 gregorianCalendar.storeRelease(backend);
302 Q_ASSERT(byId.size() > index);
303 Q_ASSERT(byId[index] ==
nullptr);
304 byId[index] = backend;
308 for (
const auto &name : names) {
309 auto [it, inserted] = byName.try_emplace(name, backend);
311 Q_ASSERT(system == QCalendar::System::User);
312 qWarning(
"Cannot register name %ls (already in use) for %ls",
313 qUtf16Printable(name), qUtf16Printable(backend->name()));
319
320
321
322
323
324
325
330 QReadLocker locker(&lock);
331 return byName.keys();
335
336
337
338
339
340
341
342
347 QReadLocker locker(&lock);
348 return byName.value(name,
nullptr);
352
353
354
355
356
357
358
362 QReadLocker locker(&lock);
364 if (index >= byId.size())
367 if (
auto backend = byId[index])
371 if (index <= size_t(QCalendar::System::Last))
372 return fromEnum(QCalendar::System(index));
378
379
380
381
382
383
384
385
386
387
388
391 Q_ASSERT(system <= QCalendar::System::Last);
392 auto index = size_t(system);
395 QReadLocker locker(&lock);
396 Q_ASSERT(byId.size() > index);
397 if (
auto backend = byId[index])
401 QWriteLocker locker(&lock);
404 if (
auto backend = byId[index])
407 return registerSystemBackendLockHeld(system);
411
412
416 l.reserve(byName.size());
422 QT_WARNING_DISABLE_CLANG(
"-Wrange-loop-analysis")
423 for (
const auto &[key, value] : byName) {
424 if (value == backend)
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
507
508
509
510
511
512
513
514
515
516
517
518QCalendarBackend::~QCalendarBackend()
520 Q_ASSERT(!m_id.isValid() || calendarRegistry.isDestroyed()
521 || calendarRegistry->isBeingDestroyed());
525
526
527
530
531
532
533
534
535
536
537
538QStringList QCalendarBackend::names()
const
540 if (Q_UNLIKELY(calendarRegistry.isDestroyed()))
543 return calendarRegistry->backendNames(
this);
547
548
549
550
551
552void QCalendarBackend::setIndex(size_t index)
554 Q_ASSERT(!m_id.isValid());
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580QCalendar::SystemId QCalendarBackend::registerCustomBackend(
const QStringList &names)
582 Q_ASSERT(!m_id.isValid());
584 if (Q_LIKELY(!calendarRegistry.isDestroyed()))
585 calendarRegistry->registerCustomBackend(
this, names);
590bool QCalendarBackend::isGregorian()
const
592 if (Q_UNLIKELY(calendarRegistry.isDestroyed()))
595 return calendarRegistry->isGregorian(
this);
599
600
601
602
603
604
605
606
607
610
611
612
613
614QCalendar::System QCalendarBackend::calendarSystem()
const
616 return m_id.isInEnum() ? QCalendar::System(m_id.index()) : QCalendar::System::User;
620
621
622
623
624
625
626#define SAFE_D() const auto d = Q_UNLIKELY(calendarRegistry.isDestroyed()) ? nullptr : d_ptr
629
630
631
632
633
634
635QString QCalendar::name()
const
638 return d ? d->name() : QString();
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
663
664
665
666
667
668
671
672
673
674
675
676
677
678
679
680
681
684
685
686
687
688
689
690
691
692
693
696
697
698
699
700
701
702
703
704
705
706
707
710
711
712
713
714
715
716
717
718int QCalendarBackend::daysInYear(
int year)
const
720 return monthsInYear(year) ? isLeapYear(year) ? 366 : 365 : 0;
724
725
726
727
728
729
730
731int QCalendarBackend::monthsInYear(
int year)
const
733 return year > 0 || (year < 0 ? isProleptic() : hasYearZero()) ? 12 : 0;
737
738
739
740
741
742
743
744
745
746
747
748
749bool QCalendarBackend::isDateValid(
int year,
int month,
int day)
const
751 return day > 0 && day <= daysInMonth(month, year);
755
756
757
758
759
760
761
762
764bool QCalendarBackend::isProleptic()
const
770
771
772
773
774
776bool QCalendarBackend::hasYearZero()
const
782
783
784
785
786
787
788
789
790
791
792
793
794int QCalendarBackend::maximumDaysInMonth()
const
800
801
802
803
804
805
806int QCalendarBackend::minimumDaysInMonth()
const
812
813
814
815
816
817
818int QCalendarBackend::maximumMonthsInYear()
const
826
827
828
829
830
831
832
833
834
837
838
839
840
841
842
843
844
845
846
847
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873int QCalendarBackend::dayOfWeek(qint64 jd)
const
875 return QRoundingDown::qMod<7>(jd) + 1;
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897qint64 QCalendarBackend::matchCenturyToWeekday(
const QCalendar::YearMonthDay &parts,
int dow)
const
899 Q_ASSERT(parts.isValid());
901 const auto checkOffset = [parts, dow,
this](
int centuries) -> std::optional<qint64> {
903 int year = parts.year + centuries * 100;
905 if (!hasYearZero() && (parts.year > 0) != (year > 0))
906 year += parts.year > 0 ? -1 : +1;
908 if (isDateValid(year, parts.month, parts.day)
909 && dateToJulianDay(year, parts.month, parts.day, &jd)
910 && dayOfWeek(jd) == dow) {
917 for (
int offset = 0; offset < 15; ++offset) {
918 if (
auto jd = checkOffset(offset))
921 if (
auto jd = checkOffset(-offset))
925 return (std::numeric_limits<qint64>::min)();
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
953
954
955
956
957
958
959
960
961
962
963
964
965
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1032
1033
1034
1035
1036
1037
1038QStringList QCalendarBackend::availableCalendars()
1040 if (Q_UNLIKELY(calendarRegistry.isDestroyed()))
1043 return calendarRegistry->availableCalendars();
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056const QCalendarBackend *QCalendarBackend::fromName(QAnyStringView name)
1058 if (Q_UNLIKELY(calendarRegistry.isDestroyed()))
1061 return calendarRegistry->fromName(name);
1065
1066
1067
1068
1069
1070
1071
1072
1073const QCalendarBackend *QCalendarBackend::fromId(QCalendar::SystemId id)
1075 if (Q_UNLIKELY(calendarRegistry.isDestroyed() || !id.isValid()))
1078 return calendarRegistry->fromIndex(id.index());
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091const QCalendarBackend *QCalendarBackend::fromEnum(QCalendar::System system)
1093 if (Q_UNLIKELY(calendarRegistry.isDestroyed() || system == QCalendar::System::User))
1096 return calendarRegistry->fromEnum(system);
1100
1101
1102
1103
1104
1105const QCalendarBackend *QCalendarBackend::gregorian()
1107 if (Q_UNLIKELY(calendarRegistry.isDestroyed()))
1110 return calendarRegistry->gregorian();
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1172
1173
1174
1175
1176
1177
1178
1181
1182
1183
1184
1185
1188
1189
1190
1191
1192
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1213QCalendar::QCalendar()
1214 : d_ptr(QCalendarBackend::gregorian())
1216 Q_ASSERT(!d_ptr || d_ptr->calendarId().isValid());
1219QCalendar::QCalendar(QCalendar::System system) : d_ptr(QCalendarBackend::fromEnum(system))
1222 Q_ASSERT(!d_ptr || (uint(system) > uint(QCalendar::System::Last))
1223 || (d_ptr->calendarId().index() == size_t(system)));
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237QCalendar::QCalendar(QCalendar::SystemId id)
1238 : d_ptr(QCalendarBackend::fromId(id))
1240 Q_ASSERT(!d_ptr || d_ptr->calendarId().index() == id.index());
1243QCalendar::QCalendar(QAnyStringView name)
1244 : d_ptr(QCalendarBackend::fromName(name))
1246 Q_ASSERT(!d_ptr || d_ptr->calendarId().isValid());
1250
1251
1252
1253
1254
1255
1256
1261
1262
1263
1264
1265
1266
1267
1268
1269int QCalendar::daysInMonth(
int month,
int year)
const
1272 return d ? d->daysInMonth(month, year) : 0;
1276
1277
1278
1279
1280int QCalendar::daysInYear(
int year)
const
1283 return d ? d->daysInYear(year) : 0;
1287
1288
1289
1290
1291
1292
1293
1294int QCalendar::monthsInYear(
int year)
const
1297 return d ? year == Unspecified ? d->maximumMonthsInYear() : d->monthsInYear(year) : 0;
1301
1302
1303
1304
1305
1306
1307
1308bool QCalendar::isDateValid(
int year,
int month,
int day)
const
1311 return d && d->isDateValid(year, month, day);
1317
1318
1319
1320bool QCalendar::isGregorian()
const
1323 return d && d->isGregorian();
1327
1328
1329
1330
1331
1332
1333
1334
1335bool QCalendar::isLeapYear(
int year)
const
1338 return d && d->isLeapYear(year);
1342
1343
1344
1345
1346bool QCalendar::isLunar()
const
1349 return d && d->isLunar();
1353
1354
1355
1356
1357
1358
1359bool QCalendar::isLuniSolar()
const
1362 return d && d->isLuniSolar();
1366
1367
1368
1369
1370
1371bool QCalendar::isSolar()
const
1374 return d && d->isSolar();
1378
1379
1380
1381
1382
1383
1384
1385
1386bool QCalendar::isProleptic()
const
1389 return d && d->isProleptic();
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417bool QCalendar::hasYearZero()
const
1420 return d && d->hasYearZero();
1424
1425
1426
1427
1428int QCalendar::maximumDaysInMonth()
const
1431 return d ? d->maximumDaysInMonth() : 0;
1435
1436
1437
1438
1439int QCalendar::minimumDaysInMonth()
const
1442 return d ? d->minimumDaysInMonth() : 0;
1446
1447
1448
1449
1450int QCalendar::maximumMonthsInYear()
const
1453 return d ? d->maximumMonthsInYear() : 0;
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474QDate QCalendar::dateFromParts(
int year,
int month,
int day)
const
1478 return d && d->dateToJulianDay(year, month, day, &jd)
1479 ? QDate::fromJulianDay(jd) : QDate();
1482QDate QCalendar::dateFromParts(
const QCalendar::YearMonthDay &parts)
const
1484 return parts.isValid() ? dateFromParts(parts.year, parts.month, parts.day) : QDate();
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506QDate QCalendar::matchCenturyToWeekday(
const QCalendar::YearMonthDay &parts,
int dow)
const
1509 return d && parts.isValid()
1510 ? QDate::fromJulianDay(d->matchCenturyToWeekday(parts, dow)) : QDate();
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523QCalendar::YearMonthDay QCalendar::partsFromDate(QDate date)
const
1526 return d && date.isValid() ? d->julianDayToDate(date.toJulianDay()) : YearMonthDay();
1530
1531
1532
1533
1534
1535
1536
1537
1538int QCalendar::dayOfWeek(QDate date)
const
1541 return d && date.isValid() ? d->dayOfWeek(date.toJulianDay()) : 0;
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565QString QCalendar::monthName(
const QLocale &locale,
int month,
int year,
1566 QLocale::FormatType format)
const
1569 const int maxMonth = year == Unspecified ? maximumMonthsInYear() : monthsInYear(year);
1570 if (!d || month < 1 || month > maxMonth)
1573 return d->monthName(locale, month, year, format);
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595QString QCalendar::standaloneMonthName(
const QLocale &locale,
int month,
int year,
1596 QLocale::FormatType format)
const
1599 const int maxMonth = year == Unspecified ? maximumMonthsInYear() : monthsInYear(year);
1600 if (!d || month < 1 || month > maxMonth)
1603 return d->standaloneMonthName(locale, month, year, format);
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620QString QCalendar::weekDayName(
const QLocale &locale,
int day,
1621 QLocale::FormatType format)
const
1624 return d ? d->weekDayName(locale, day, format) : QString();
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643QString QCalendar::standaloneWeekDayName(
const QLocale &locale,
int day,
1644 QLocale::FormatType format)
const
1647 return d ? d->standaloneWeekDayName(locale, day, format) : QString();
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669QString QCalendar::dateTimeToString(QStringView format,
const QDateTime &datetime,
1670 QDate dateOnly, QTime timeOnly,
1671 const QLocale &locale)
const
1674 return d ? d->dateTimeToString(format, datetime, dateOnly, timeOnly, locale) : QString();
1678
1679
1680
1681
1682
1683
1684QStringList QCalendar::availableCalendars()
1686 return QCalendarBackend::availableCalendars();
1691#ifndef QT_BOOTSTRAPPED
1692#include "moc_qcalendar.cpp"
\macro Q_ATOMIC_INTnn_IS_SUPPORTED
const QCalendarBackend * fromEnum(QCalendar::System system)
QStringList availableCalendars()
const QCalendarBackend * fromName(QAnyStringView name)
void registerCustomBackend(QCalendarBackend *backend, const QStringList &names)
bool isGregorian(const QCalendarBackend *backend) const
bool isBeingDestroyed() const
const QCalendarBackend * fromIndex(size_t index)
QStringList backendNames(const QCalendarBackend *backend)
const QCalendarBackend * gregorian()
Combined button and popup list for selecting options.
Q_GLOBAL_STATIC(QtPrivate::QCalendarRegistry, calendarRegistry)
bool operator()(QAnyStringView lhs, QAnyStringView rhs) const