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
836
837
838
839
840
841
842
843
844
845
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871int QCalendarBackend::dayOfWeek(qint64 jd)
const
873 return QRoundingDown::qMod<7>(jd) + 1;
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895qint64 QCalendarBackend::matchCenturyToWeekday(
const QCalendar::YearMonthDay &parts,
int dow)
const
897 Q_ASSERT(parts.isValid());
899 const auto checkOffset = [parts, dow,
this](
int centuries) -> std::optional<qint64> {
901 int year = parts.year + centuries * 100;
903 if (!hasYearZero() && (parts.year > 0) != (year > 0))
904 year += parts.year > 0 ? -1 : +1;
906 if (isDateValid(year, parts.month, parts.day)
907 && dateToJulianDay(year, parts.month, parts.day, &jd)
908 && dayOfWeek(jd) == dow) {
915 for (
int offset = 0; offset < 15; ++offset) {
916 if (
auto jd = checkOffset(offset))
919 if (
auto jd = checkOffset(-offset))
923 return (std::numeric_limits<qint64>::min)();
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
951
952
953
954
955
956
957
958
959
960
961
962
963
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1030
1031
1032
1033
1034
1035
1036QStringList QCalendarBackend::availableCalendars()
1038 if (Q_UNLIKELY(calendarRegistry.isDestroyed()))
1041 return calendarRegistry->availableCalendars();
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054const QCalendarBackend *QCalendarBackend::fromName(QAnyStringView name)
1056 if (Q_UNLIKELY(calendarRegistry.isDestroyed()))
1059 return calendarRegistry->fromName(name);
1063
1064
1065
1066
1067
1068
1069
1070
1071const QCalendarBackend *QCalendarBackend::fromId(QCalendar::SystemId id)
1073 if (Q_UNLIKELY(calendarRegistry.isDestroyed() || !id.isValid()))
1076 return calendarRegistry->fromIndex(id.index());
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089const QCalendarBackend *QCalendarBackend::fromEnum(QCalendar::System system)
1091 if (Q_UNLIKELY(calendarRegistry.isDestroyed() || system == QCalendar::System::User))
1094 return calendarRegistry->fromEnum(system);
1098
1099
1100
1101
1102
1103const QCalendarBackend *QCalendarBackend::gregorian()
1105 if (Q_UNLIKELY(calendarRegistry.isDestroyed()))
1108 return calendarRegistry->gregorian();
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1170
1171
1172
1173
1174
1175
1176
1179
1180
1181
1182
1183
1186
1187
1188
1189
1190
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1211QCalendar::QCalendar()
1212 : d_ptr(QCalendarBackend::gregorian())
1214 Q_ASSERT(!d_ptr || d_ptr->calendarId().isValid());
1217QCalendar::QCalendar(QCalendar::System system) : d_ptr(QCalendarBackend::fromEnum(system))
1220 Q_ASSERT(!d_ptr || (uint(system) > uint(QCalendar::System::Last))
1221 || (d_ptr->calendarId().index() == size_t(system)));
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235QCalendar::QCalendar(QCalendar::SystemId id)
1236 : d_ptr(QCalendarBackend::fromId(id))
1238 Q_ASSERT(!d_ptr || d_ptr->calendarId().index() == id.index());
1241QCalendar::QCalendar(QAnyStringView name)
1242 : d_ptr(QCalendarBackend::fromName(name))
1244 Q_ASSERT(!d_ptr || d_ptr->calendarId().isValid());
1248
1249
1250
1251
1252
1253
1254
1259
1260
1261
1262
1263
1264
1265
1266
1267int QCalendar::daysInMonth(
int month,
int year)
const
1270 return d ? d->daysInMonth(month, year) : 0;
1274
1275
1276
1277
1278int QCalendar::daysInYear(
int year)
const
1281 return d ? d->daysInYear(year) : 0;
1285
1286
1287
1288
1289
1290
1291
1292int QCalendar::monthsInYear(
int year)
const
1295 return d ? year == Unspecified ? d->maximumMonthsInYear() : d->monthsInYear(year) : 0;
1299
1300
1301
1302
1303
1304
1305
1306bool QCalendar::isDateValid(
int year,
int month,
int day)
const
1309 return d && d->isDateValid(year, month, day);
1315
1316
1317
1318bool QCalendar::isGregorian()
const
1321 return d && d->isGregorian();
1325
1326
1327
1328
1329
1330
1331
1332
1333bool QCalendar::isLeapYear(
int year)
const
1336 return d && d->isLeapYear(year);
1340
1341
1342
1343
1344bool QCalendar::isLunar()
const
1347 return d && d->isLunar();
1351
1352
1353
1354
1355
1356
1357bool QCalendar::isLuniSolar()
const
1360 return d && d->isLuniSolar();
1364
1365
1366
1367
1368
1369bool QCalendar::isSolar()
const
1372 return d && d->isSolar();
1376
1377
1378
1379
1380
1381
1382
1383
1384bool QCalendar::isProleptic()
const
1387 return d && d->isProleptic();
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415bool QCalendar::hasYearZero()
const
1418 return d && d->hasYearZero();
1422
1423
1424
1425
1426int QCalendar::maximumDaysInMonth()
const
1429 return d ? d->maximumDaysInMonth() : 0;
1433
1434
1435
1436
1437int QCalendar::minimumDaysInMonth()
const
1440 return d ? d->minimumDaysInMonth() : 0;
1444
1445
1446
1447
1448int QCalendar::maximumMonthsInYear()
const
1451 return d ? d->maximumMonthsInYear() : 0;
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470QDate QCalendar::dateFromParts(
int year,
int month,
int day)
const
1474 return d && d->dateToJulianDay(year, month, day, &jd)
1475 ? QDate::fromJulianDay(jd) : QDate();
1478QDate QCalendar::dateFromParts(
const QCalendar::YearMonthDay &parts)
const
1480 return parts.isValid() ? dateFromParts(parts.year, parts.month, parts.day) : QDate();
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502QDate QCalendar::matchCenturyToWeekday(
const QCalendar::YearMonthDay &parts,
int dow)
const
1505 return d && parts.isValid()
1506 ? QDate::fromJulianDay(d->matchCenturyToWeekday(parts, dow)) : QDate();
1510
1511
1512
1513
1514
1515
1516
1517
1518QCalendar::YearMonthDay QCalendar::partsFromDate(QDate date)
const
1521 return d && date.isValid() ? d->julianDayToDate(date.toJulianDay()) : YearMonthDay();
1525
1526
1527
1528
1529
1530
1531
1532
1533int QCalendar::dayOfWeek(QDate date)
const
1536 return d && date.isValid() ? d->dayOfWeek(date.toJulianDay()) : 0;
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560QString QCalendar::monthName(
const QLocale &locale,
int month,
int year,
1561 QLocale::FormatType format)
const
1564 const int maxMonth = year == Unspecified ? maximumMonthsInYear() : monthsInYear(year);
1565 if (!d || month < 1 || month > maxMonth)
1568 return d->monthName(locale, month, year, format);
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590QString QCalendar::standaloneMonthName(
const QLocale &locale,
int month,
int year,
1591 QLocale::FormatType format)
const
1594 const int maxMonth = year == Unspecified ? maximumMonthsInYear() : monthsInYear(year);
1595 if (!d || month < 1 || month > maxMonth)
1598 return d->standaloneMonthName(locale, month, year, format);
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615QString QCalendar::weekDayName(
const QLocale &locale,
int day,
1616 QLocale::FormatType format)
const
1619 return d ? d->weekDayName(locale, day, format) : QString();
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638QString QCalendar::standaloneWeekDayName(
const QLocale &locale,
int day,
1639 QLocale::FormatType format)
const
1642 return d ? d->standaloneWeekDayName(locale, day, format) : QString();
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664QString QCalendar::dateTimeToString(QStringView format,
const QDateTime &datetime,
1665 QDate dateOnly, QTime timeOnly,
1666 const QLocale &locale)
const
1669 return d ? d->dateTimeToString(format, datetime, dateOnly, timeOnly, locale) : QString();
1673
1674
1675
1676
1677
1678
1679QStringList QCalendar::availableCalendars()
1681 return QCalendarBackend::availableCalendars();
1686#ifndef QT_BOOTSTRAPPED
1687#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