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 const 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
224const QCalendarBackend *
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 auto index = size_t(system);
392 Q_ASSERT(index <= size_t(QCalendar::System::Last));
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()))
1095 if (size_t(system) > size_t(QCalendar::System::Last))
1098 return calendarRegistry->fromEnum(system);
1102
1103
1104
1105
1106
1107const QCalendarBackend *QCalendarBackend::gregorian()
1109 if (Q_UNLIKELY(calendarRegistry.isDestroyed()))
1112 return calendarRegistry->gregorian();
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1174
1175
1176
1177
1178
1179
1180
1183
1184
1185
1186
1187
1190
1191
1192
1193
1194
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1215QCalendar::QCalendar()
1216 : d_ptr(QCalendarBackend::gregorian())
1218 Q_ASSERT(!d_ptr || d_ptr->calendarId().isValid());
1221QCalendar::QCalendar(QCalendar::System system) : d_ptr(QCalendarBackend::fromEnum(system))
1224 Q_ASSERT(!d_ptr || (uint(system) > uint(QCalendar::System::Last))
1225 || (d_ptr->calendarId().index() == size_t(system)));
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239QCalendar::QCalendar(QCalendar::SystemId id)
1240 : d_ptr(QCalendarBackend::fromId(id))
1242 Q_ASSERT(!d_ptr || d_ptr->calendarId().index() == id.index());
1245QCalendar::QCalendar(QAnyStringView name)
1246 : d_ptr(QCalendarBackend::fromName(name))
1248 Q_ASSERT(!d_ptr || d_ptr->calendarId().isValid());
1252
1253
1254
1255
1256
1257
1258
1263
1264
1265
1266
1267
1268
1269
1270
1271int QCalendar::daysInMonth(
int month,
int year)
const
1274 return d ? d->daysInMonth(month, year) : 0;
1278
1279
1280
1281
1282int QCalendar::daysInYear(
int year)
const
1285 return d ? d->daysInYear(year) : 0;
1289
1290
1291
1292
1293
1294
1295
1296int QCalendar::monthsInYear(
int year)
const
1299 return d ? year == Unspecified ? d->maximumMonthsInYear() : d->monthsInYear(year) : 0;
1303
1304
1305
1306
1307
1308
1309
1310bool QCalendar::isDateValid(
int year,
int month,
int day)
const
1313 return d && d->isDateValid(year, month, day);
1319
1320
1321
1322bool QCalendar::isGregorian()
const
1325 return d && d->isGregorian();
1329
1330
1331
1332
1333
1334
1335
1336
1337bool QCalendar::isLeapYear(
int year)
const
1340 return d && d->isLeapYear(year);
1344
1345
1346
1347
1348bool QCalendar::isLunar()
const
1351 return d && d->isLunar();
1355
1356
1357
1358
1359
1360
1361bool QCalendar::isLuniSolar()
const
1364 return d && d->isLuniSolar();
1368
1369
1370
1371
1372
1373bool QCalendar::isSolar()
const
1376 return d && d->isSolar();
1380
1381
1382
1383
1384
1385
1386
1387
1388bool QCalendar::isProleptic()
const
1391 return d && d->isProleptic();
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419bool QCalendar::hasYearZero()
const
1422 return d && d->hasYearZero();
1426
1427
1428
1429
1430int QCalendar::maximumDaysInMonth()
const
1433 return d ? d->maximumDaysInMonth() : 0;
1437
1438
1439
1440
1441int QCalendar::minimumDaysInMonth()
const
1444 return d ? d->minimumDaysInMonth() : 0;
1448
1449
1450
1451
1452int QCalendar::maximumMonthsInYear()
const
1455 return d ? d->maximumMonthsInYear() : 0;
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476QDate QCalendar::dateFromParts(
int year,
int month,
int day)
const
1480 return d && d->dateToJulianDay(year, month, day, &jd)
1481 ? QDate::fromJulianDay(jd) : QDate();
1484QDate QCalendar::dateFromParts(
const QCalendar::YearMonthDay &parts)
const
1486 return parts.isValid() ? dateFromParts(parts.year, parts.month, parts.day) : QDate();
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508QDate QCalendar::matchCenturyToWeekday(
const QCalendar::YearMonthDay &parts,
int dow)
const
1511 return d && parts.isValid()
1512 ? QDate::fromJulianDay(d->matchCenturyToWeekday(parts, dow)) : QDate();
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525QCalendar::YearMonthDay QCalendar::partsFromDate(QDate date)
const
1528 return d && date.isValid() ? d->julianDayToDate(date.toJulianDay()) : YearMonthDay();
1532
1533
1534
1535
1536
1537
1538
1539
1540int QCalendar::dayOfWeek(QDate date)
const
1543 return d && date.isValid() ? d->dayOfWeek(date.toJulianDay()) : 0;
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567QString QCalendar::monthName(
const QLocale &locale,
int month,
int year,
1568 QLocale::FormatType format)
const
1571 const int maxMonth = year == Unspecified ? maximumMonthsInYear() : monthsInYear(year);
1572 if (!d || month < 1 || month > maxMonth)
1575 return d->monthName(locale, month, year, format);
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597QString QCalendar::standaloneMonthName(
const QLocale &locale,
int month,
int year,
1598 QLocale::FormatType format)
const
1601 const int maxMonth = year == Unspecified ? maximumMonthsInYear() : monthsInYear(year);
1602 if (!d || month < 1 || month > maxMonth)
1605 return d->standaloneMonthName(locale, month, year, format);
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622QString QCalendar::weekDayName(
const QLocale &locale,
int day,
1623 QLocale::FormatType format)
const
1626 return d ? d->weekDayName(locale, day, format) : QString();
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645QString QCalendar::standaloneWeekDayName(
const QLocale &locale,
int day,
1646 QLocale::FormatType format)
const
1649 return d ? d->standaloneWeekDayName(locale, day, format) : QString();
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671QString QCalendar::dateTimeToString(QStringView format,
const QDateTime &datetime,
1672 QDate dateOnly, QTime timeOnly,
1673 const QLocale &locale)
const
1676 return d ? d->dateTimeToString(format, datetime, dateOnly, timeOnly, locale) : QString();
1680
1681
1682
1683
1684
1685
1686QStringList QCalendar::availableCalendars()
1688 return QCalendarBackend::availableCalendars();
1693#ifndef QT_BOOTSTRAPPED
1694#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