10#if QT_CONFIG(jalalicalendar)
11#include "qjalalicalendar_p.h"
13#if QT_CONFIG(islamiccivilcalendar)
14#include "qislamiccivilcalendar_p.h"
17#include <private/qflatmap_p.h>
22#include <qreadwritelock.h>
31 bool operator()(QAnyStringView lhs, QAnyStringView rhs)
const
33 return QAnyStringView::compare(lhs, rhs, Qt::CaseInsensitive) < 0;
40
41
42
50
51
55
56
57
58
59
60 std::vector<QCalendarBackend *> byId;
63
64
65
66
67
76
77
78
79
80
81
82 QAtomicPointer<
const QCalendarBackend> gregorianCalendar =
nullptr;
91
92
93
94
97 void ensurePopulated();
98 QCalendarBackend *registerSystemBackendLockHeld(QCalendar::System system);
99 void registerBackendLockHeld(QCalendarBackend *backend,
const QStringList &names,
100 QCalendar::System system);
105 byId.resize(ExpectedNumberOfBackends);
106 byName.reserve(ExpectedNumberOfBackends * 2);
118
119
120
121
124 const QCalendarBackend *backend = gregorianCalendar.loadAcquire();
125 if (Q_LIKELY(backend !=
nullptr))
127 return fromEnum(QCalendar::System::Gregorian);
131
132
133
134
137 return backend == gregorianCalendar.loadRelaxed();
140 const QCalendarBackend *
fromName(QAnyStringView name);
142 const QCalendarBackend *
fromEnum(QCalendar::System system);
148
149
150
151
152
155 QWriteLocker locker(&lock);
157 status.storeRelaxed(IsBeingDestroyed);
163
164
165
166
167
168
169
170
171
172
173
174
175
178 Q_ASSERT(!backend->calendarId().isValid());
182 QWriteLocker locker(&lock);
183 registerBackendLockHeld(backend, names, QCalendar::System::User);
187
188
189
190
191
192
193
196 if (Q_LIKELY(status.loadAcquire() != Unpopulated))
199 QWriteLocker locker(&lock);
200 if (status.loadAcquire() != Unpopulated)
203 for (
int i = 0; i <=
int(QCalendar::System::Last); ++i) {
204 if (byId[i] ==
nullptr)
205 registerSystemBackendLockHeld(QCalendar::System(i));
208#if defined(QT_FORCE_ASSERTS) || !defined(QT_NO_DEBUG)
209 auto oldValue = status.fetchAndStoreRelease(Populated);
210 Q_ASSERT(oldValue == Unpopulated);
212 status.storeRelease(Populated);
217
218
219
220
221
222
223QCalendarBackend *
QCalendarRegistry::registerSystemBackendLockHeld(QCalendar::System system)
225 Q_ASSERT(system != QCalendar::System::User);
227 QCalendarBackend *backend =
nullptr;
231 case QCalendar::System::Gregorian:
232 backend =
new QGregorianCalendar;
233 names = QGregorianCalendar::nameList();
235#ifndef QT_BOOTSTRAPPED
236 case QCalendar::System::Julian:
237 backend =
new QJulianCalendar;
238 names = QJulianCalendar::nameList();
240 case QCalendar::System::Milankovic:
241 backend =
new QMilankovicCalendar;
242 names = QMilankovicCalendar::nameList();
245#if QT_CONFIG(jalalicalendar)
246 case QCalendar::System::Jalali:
247 backend =
new QJalaliCalendar;
248 names = QJalaliCalendar::nameList();
251#if QT_CONFIG(islamiccivilcalendar)
252 case QCalendar::System::IslamicCivil:
253 backend =
new QIslamicCivilCalendar;
254 names = QIslamicCivilCalendar::nameList();
257 case QCalendar::System::Last:
259 case QCalendar::System::User:
265 registerBackendLockHeld(backend, names, system);
266 Q_ASSERT(backend == byId[size_t(system)]);
272
273
274
275
276
277
278void QCalendarRegistry::registerBackendLockHeld(QCalendarBackend *backend,
const QStringList &names,
279 QCalendar::System system)
281 Q_ASSERT(!backend->calendarId().isValid());
283 auto index = size_t(system);
287 if (system == QCalendar::System::User) {
288 backend->setIndex(byId.size());
289 byId.push_back(backend);
290 }
else if (byId[index] ==
nullptr) {
291 backend->setIndex(index);
292 if (system == QCalendar::System::Gregorian) {
293#if defined(QT_FORCE_ASSERTS) || !defined(QT_NO_DEBUG)
294 auto oldValue = gregorianCalendar.fetchAndStoreRelease(backend);
295 Q_ASSERT(oldValue ==
nullptr);
297 gregorianCalendar.storeRelease(backend);
301 Q_ASSERT(byId.size() > index);
302 Q_ASSERT(byId[index] ==
nullptr);
303 byId[index] = backend;
307 for (
const auto &name : names) {
308 auto [it, inserted] = byName.try_emplace(name, backend);
310 Q_ASSERT(system == QCalendar::System::User);
311 qWarning(
"Cannot register name %ls (already in use) for %ls",
312 qUtf16Printable(name), qUtf16Printable(backend->name()));
318
319
320
321
322
323
324
329 QReadLocker locker(&lock);
330 return byName.keys();
334
335
336
337
338
339
340
341
346 QReadLocker locker(&lock);
347 return byName.value(name,
nullptr);
351
352
353
354
355
356
357
361 QReadLocker locker(&lock);
363 if (index >= byId.size())
366 if (
auto backend = byId[index])
370 if (index <= size_t(QCalendar::System::Last))
371 return fromEnum(QCalendar::System(index));
377
378
379
380
381
382
383
384
385
386
387
390 Q_ASSERT(system <= QCalendar::System::Last);
391 auto index = size_t(system);
394 QReadLocker locker(&lock);
395 Q_ASSERT(byId.size() > index);
396 if (
auto backend = byId[index])
400 QWriteLocker locker(&lock);
403 if (
auto backend = byId[index])
406 return registerSystemBackendLockHeld(system);
410
411
415 l.reserve(byName.size());
421 QT_WARNING_DISABLE_CLANG(
"-Wrange-loop-analysis")
422 for (
const auto &[key, value] : byName) {
423 if (value == backend)
436
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
506
507
508
509
510
511
512
513
514
515
516
517QCalendarBackend::~QCalendarBackend()
519 Q_ASSERT(!m_id.isValid() || calendarRegistry.isDestroyed()
520 || calendarRegistry->isBeingDestroyed());
524
525
526
529
530
531
532
533
534
535
536
537QStringList QCalendarBackend::names()
const
539 if (Q_UNLIKELY(calendarRegistry.isDestroyed()))
542 return calendarRegistry->backendNames(
this);
546
547
548
549
550
551void QCalendarBackend::setIndex(size_t index)
553 Q_ASSERT(!m_id.isValid());
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579QCalendar::SystemId QCalendarBackend::registerCustomBackend(
const QStringList &names)
581 Q_ASSERT(!m_id.isValid());
583 if (Q_LIKELY(!calendarRegistry.isDestroyed()))
584 calendarRegistry->registerCustomBackend(
this, names);
589bool QCalendarBackend::isGregorian()
const
591 if (Q_UNLIKELY(calendarRegistry.isDestroyed()))
594 return calendarRegistry->isGregorian(
this);
598
599
600
601
602
603
604
605
606
609
610
611
612
613QCalendar::System QCalendarBackend::calendarSystem()
const
615 return m_id.isInEnum() ? QCalendar::System(m_id.index()) : QCalendar::System::User;
619
620
621
622
623
624
625#define SAFE_D() const auto d = Q_UNLIKELY(calendarRegistry.isDestroyed()) ? nullptr : d_ptr
628
629
630
631
632
633
634QString QCalendar::name()
const
637 return d ? d->name() : QString();
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
662
663
664
665
666
667
670
671
672
673
674
675
676
677
678
679
680
683
684
685
686
687
688
689
690
691
692
695
696
697
698
699
700
701
702
703
704
705
706
709
710
711
712
713
714
715
716
717int QCalendarBackend::daysInYear(
int year)
const
719 return monthsInYear(year) ? isLeapYear(year) ? 366 : 365 : 0;
723
724
725
726
727
728
729
730int QCalendarBackend::monthsInYear(
int year)
const
732 return year > 0 || (year < 0 ? isProleptic() : hasYearZero()) ? 12 : 0;
736
737
738
739
740
741
742
743
744
745
746
747
748bool QCalendarBackend::isDateValid(
int year,
int month,
int day)
const
750 return day > 0 && day <= daysInMonth(month, year);
754
755
756
757
758
759
760
761
763bool QCalendarBackend::isProleptic()
const
769
770
771
772
773
775bool QCalendarBackend::hasYearZero()
const
781
782
783
784
785
786
787
788
789
790
791
792
793int QCalendarBackend::maximumDaysInMonth()
const
799
800
801
802
803
804
805int QCalendarBackend::minimumDaysInMonth()
const
811
812
813
814
815
816
817int QCalendarBackend::maximumMonthsInYear()
const
825
826
827
828
829
830
831
832
835
836
837
838
839
840
841
842
843
844
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870int QCalendarBackend::dayOfWeek(qint64 jd)
const
872 return QRoundingDown::qMod<7>(jd) + 1;
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894qint64 QCalendarBackend::matchCenturyToWeekday(
const QCalendar::YearMonthDay &parts,
int dow)
const
896 Q_ASSERT(parts.isValid());
898 const auto checkOffset = [parts, dow,
this](
int centuries) -> std::optional<qint64> {
900 int year = parts.year + centuries * 100;
902 if (!hasYearZero() && (parts.year > 0) != (year > 0))
903 year += parts.year > 0 ? -1 : +1;
905 if (isDateValid(year, parts.month, parts.day)
906 && dateToJulianDay(year, parts.month, parts.day, &jd)
907 && dayOfWeek(jd) == dow) {
914 for (
int offset = 0; offset < 15; ++offset) {
915 if (
auto jd = checkOffset(offset))
918 if (
auto jd = checkOffset(-offset))
922 return (std::numeric_limits<qint64>::min)();
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
950
951
952
953
954
955
956
957
958
959
960
961
962
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1029
1030
1031
1032
1033
1034
1035QStringList QCalendarBackend::availableCalendars()
1037 if (Q_UNLIKELY(calendarRegistry.isDestroyed()))
1040 return calendarRegistry->availableCalendars();
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053const QCalendarBackend *QCalendarBackend::fromName(QAnyStringView name)
1055 if (Q_UNLIKELY(calendarRegistry.isDestroyed()))
1058 return calendarRegistry->fromName(name);
1062
1063
1064
1065
1066
1067
1068
1069
1070const QCalendarBackend *QCalendarBackend::fromId(QCalendar::SystemId id)
1072 if (Q_UNLIKELY(calendarRegistry.isDestroyed() || !id.isValid()))
1075 return calendarRegistry->fromIndex(id.index());
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088const QCalendarBackend *QCalendarBackend::fromEnum(QCalendar::System system)
1090 if (Q_UNLIKELY(calendarRegistry.isDestroyed() || system == QCalendar::System::User))
1093 return calendarRegistry->fromEnum(system);
1097
1098
1099
1100
1101
1102const QCalendarBackend *QCalendarBackend::gregorian()
1104 if (Q_UNLIKELY(calendarRegistry.isDestroyed()))
1107 return calendarRegistry->gregorian();
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1169
1170
1171
1172
1173
1174
1175
1178
1179
1180
1181
1182
1185
1186
1187
1188
1189
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1210QCalendar::QCalendar()
1211 : d_ptr(QCalendarBackend::gregorian())
1213 Q_ASSERT(!d_ptr || d_ptr->calendarId().isValid());
1216QCalendar::QCalendar(QCalendar::System system) : d_ptr(QCalendarBackend::fromEnum(system))
1219 Q_ASSERT(!d_ptr || (uint(system) > uint(QCalendar::System::Last))
1220 || (d_ptr->calendarId().index() == size_t(system)));
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234QCalendar::QCalendar(QCalendar::SystemId id)
1235 : d_ptr(QCalendarBackend::fromId(id))
1237 Q_ASSERT(!d_ptr || d_ptr->calendarId().index() == id.index());
1240QCalendar::QCalendar(QAnyStringView name)
1241 : d_ptr(QCalendarBackend::fromName(name))
1243 Q_ASSERT(!d_ptr || d_ptr->calendarId().isValid());
1247
1248
1249
1250
1251
1252
1253
1258
1259
1260
1261
1262
1263
1264
1265
1266int QCalendar::daysInMonth(
int month,
int year)
const
1269 return d ? d->daysInMonth(month, year) : 0;
1273
1274
1275
1276
1277int QCalendar::daysInYear(
int year)
const
1280 return d ? d->daysInYear(year) : 0;
1284
1285
1286
1287
1288
1289
1290
1291int QCalendar::monthsInYear(
int year)
const
1294 return d ? year == Unspecified ? d->maximumMonthsInYear() : d->monthsInYear(year) : 0;
1298
1299
1300
1301
1302
1303
1304
1305bool QCalendar::isDateValid(
int year,
int month,
int day)
const
1308 return d && d->isDateValid(year, month, day);
1314
1315
1316
1317bool QCalendar::isGregorian()
const
1320 return d && d->isGregorian();
1324
1325
1326
1327
1328
1329
1330
1331
1332bool QCalendar::isLeapYear(
int year)
const
1335 return d && d->isLeapYear(year);
1339
1340
1341
1342
1343bool QCalendar::isLunar()
const
1346 return d && d->isLunar();
1350
1351
1352
1353
1354
1355
1356bool QCalendar::isLuniSolar()
const
1359 return d && d->isLuniSolar();
1363
1364
1365
1366
1367
1368bool QCalendar::isSolar()
const
1371 return d && d->isSolar();
1375
1376
1377
1378
1379
1380
1381
1382
1383bool QCalendar::isProleptic()
const
1386 return d && d->isProleptic();
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414bool QCalendar::hasYearZero()
const
1417 return d && d->hasYearZero();
1421
1422
1423
1424
1425int QCalendar::maximumDaysInMonth()
const
1428 return d ? d->maximumDaysInMonth() : 0;
1432
1433
1434
1435
1436int QCalendar::minimumDaysInMonth()
const
1439 return d ? d->minimumDaysInMonth() : 0;
1443
1444
1445
1446
1447int QCalendar::maximumMonthsInYear()
const
1450 return d ? d->maximumMonthsInYear() : 0;
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469QDate QCalendar::dateFromParts(
int year,
int month,
int day)
const
1473 return d && d->dateToJulianDay(year, month, day, &jd)
1474 ? QDate::fromJulianDay(jd) : QDate();
1477QDate QCalendar::dateFromParts(
const QCalendar::YearMonthDay &parts)
const
1479 return parts.isValid() ? dateFromParts(parts.year, parts.month, parts.day) : QDate();
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501QDate QCalendar::matchCenturyToWeekday(
const QCalendar::YearMonthDay &parts,
int dow)
const
1504 return d && parts.isValid()
1505 ? QDate::fromJulianDay(d->matchCenturyToWeekday(parts, dow)) : QDate();
1509
1510
1511
1512
1513
1514
1515
1516
1517QCalendar::YearMonthDay QCalendar::partsFromDate(QDate date)
const
1520 return d && date.isValid() ? d->julianDayToDate(date.toJulianDay()) : YearMonthDay();
1524
1525
1526
1527
1528
1529
1530
1531
1532int QCalendar::dayOfWeek(QDate date)
const
1535 return d && date.isValid() ? d->dayOfWeek(date.toJulianDay()) : 0;
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559QString QCalendar::monthName(
const QLocale &locale,
int month,
int year,
1560 QLocale::FormatType format)
const
1563 const int maxMonth = year == Unspecified ? maximumMonthsInYear() : monthsInYear(year);
1564 if (!d || month < 1 || month > maxMonth)
1567 return d->monthName(locale, month, year, format);
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589QString QCalendar::standaloneMonthName(
const QLocale &locale,
int month,
int year,
1590 QLocale::FormatType format)
const
1593 const int maxMonth = year == Unspecified ? maximumMonthsInYear() : monthsInYear(year);
1594 if (!d || month < 1 || month > maxMonth)
1597 return d->standaloneMonthName(locale, month, year, format);
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614QString QCalendar::weekDayName(
const QLocale &locale,
int day,
1615 QLocale::FormatType format)
const
1618 return d ? d->weekDayName(locale, day, format) : QString();
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637QString QCalendar::standaloneWeekDayName(
const QLocale &locale,
int day,
1638 QLocale::FormatType format)
const
1641 return d ? d->standaloneWeekDayName(locale, day, format) : QString();
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663QString QCalendar::dateTimeToString(QStringView format,
const QDateTime &datetime,
1664 QDate dateOnly, QTime timeOnly,
1665 const QLocale &locale)
const
1668 return d ? d->dateTimeToString(format, datetime, dateOnly, timeOnly, locale) : QString();
1672
1673
1674
1675
1676
1677
1678QStringList QCalendar::availableCalendars()
1680 return QCalendarBackend::availableCalendars();
1685#ifndef QT_BOOTSTRAPPED
1686#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