Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qislamiccivilcalendar.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qglobal.h"
8
9#include <limits>
10
12
13using namespace QRoundingDown;
14
15/*!
16 \since 5.14
17 \internal
18
19 \class QIslamicCivilCalendar
20 \inmodule QtCore
21 \brief Implements a commonly-used computed version of the Islamic calendar.
22
23 \section1 Civil Islamic Calendar
24
25 QIslamicCivilCalendar implements a tabular version of the Hijri calendar
26 which is known as the Islamic Civil Calendar. It has the same numbering of
27 years and months, but the months are determined by arithmetical rules rather
28 than by observation or astronomical calculations.
29
30 \section2 Calendar Organization
31
32 The civil calendar follows the usual tabular scheme of odd-numbered months
33 and the last month of each leap year being 30 days long, the rest being 29
34 days long. Its determination of leap years follows a 30-year cycle, in each
35 of which the years 2, 5, 7, 10, 13, 16, 18, 21, 24, 26 and 29 are leap
36 years.
37
38 \sa QHijriCalendar, QCalendar
39*/
40
41QString QIslamicCivilCalendar::name() const
42{
43 return QStringLiteral("Islamic Civil");
44}
45
46QStringList QIslamicCivilCalendar::nameList()
47{
48 return {
49 QStringLiteral("Islamic Civil"),
50 QStringLiteral("islamic-civil"), // CLDR name
51 QStringLiteral("islamicc"), // old CLDR name, still (2018) used by Mozilla
52 // Until we have a concrete implementation that knows all the needed ephemerides:
53 QStringLiteral("Islamic"),
54 };
55}
56
57bool QIslamicCivilCalendar::isLeapYear(int year) const
58{
59 if (year == QCalendar::Unspecified)
60 return false;
61 if (year < 0)
62 ++year;
63
64 year = qMod<30>(year); // Avoids overflow in:
65 return qMod<30>(year * 11 + 14) < 11;
66}
67
68// First day of first year (Gregorian 622 CE July 19th) is the base date here:
69constexpr qint64 EpochJd = 1948440;
70// Each 30 years has 11 leap years of 355 days and 19 ordinary years of 354:
71constexpr unsigned ThirtyYears = 11 * 355 + 19 * 354;
72// The first eleven months of the year alternate 30, 29, ..., 29, 30 days in length.
73constexpr unsigned ElevenMonths = 6 * 30 + 5 * 29;
74
75bool QIslamicCivilCalendar::dateToJulianDay(int year, int month, int day, qint64 *jd) const
76{
77 Q_ASSERT(jd);
78 if (!isDateValid(year, month, day))
79 return false;
80
81 *jd = qDiv<30>(qint64(ThirtyYears) * (year > 0 ? year - 1 : year) + 14)
82 + qDiv<11>(ElevenMonths * (month - 1) + 5)
83 + day + EpochJd - 1;
84 return true;
85}
86
87QCalendar::YearMonthDay QIslamicCivilCalendar::julianDayToDate(qint64 jd) const
88{
89 using Bound = std::numeric_limits<int>;
90 constexpr qint64 maxInt = Bound::max();
91 const auto year30Day = qDivMod<ThirtyYears>(30 * (jd - EpochJd) + 15);
92 // Its remainder changes by 30 per day, except roughly yearly.
93 const auto month11Day = qDivMod<ElevenMonths>(11 * qDiv<30>(year30Day.remainder) + 5);
94 // Its remainder changes by 11 per day except roughly monthly.
95 const int month = month11Day.quotient + 1;
96 const int day = qDiv<11>(month11Day.remainder) + 1;
97 const qint64 year = year30Day.quotient + 1;
98 // We'll be subtracting 1 if -ve, so the lower bound is -maxInt, not Bound::min():
99 if (year < -maxInt || year > maxInt)
100 return {};
101
102 int y = static_cast<int>(year);
103 return QCalendar::YearMonthDay(y > 0 ? y : y - 1, month, day);
104}
105
106QT_END_NAMESPACE
Combined button and popup list for selecting options.
constexpr unsigned ElevenMonths
constexpr qint64 EpochJd
constexpr unsigned ThirtyYears