1/****************************************************************************
2**
3** Copyright (C) 2018 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qglobal.h"
41#include "qislamiccivilcalendar_p.h"
42#include "qcalendarmath_p.h"
43#include <QtCore/qmath.h>
44
45QT_BEGIN_NAMESPACE
46
47using namespace QRoundingDown;
48
49/*!
50 \since 5.14
51 \internal
52
53 \class QIslamicCivilCalendar
54 \inmodule QtCore
55 \brief Implements a commonly-used computed version of the Islamic calendar.
56
57 \section1 Civil Islamic Calendar
58
59 QIslamicCivilCalendar implements a tabular version of the Hijri calendar
60 which is known as the Islamic Civil Calendar. It has the same numbering of
61 years and months, but the months are determined by arithmetical rules rather
62 than by observation or astronomical calculations.
63
64 \section2 Calendar Organization
65
66 The civil calendar follows the usual tabular scheme of odd-numbered months
67 and the last month of each leap year being 30 days long, the rest being 29
68 days long. Its determination of leap years follows a 30-year cycle, in each
69 of which the years 2, 5, 7, 10, 13, 16, 18, 21, 24, 26 and 29 are leap
70 years.
71
72 \sa QHijriCalendar, QCalendar
73*/
74
75QIslamicCivilCalendar::QIslamicCivilCalendar()
76 : QHijriCalendar(QStringLiteral("Islamic Civil"), QCalendar::System::IslamicCivil)
77{
78 registerAlias(QStringLiteral("islamic-civil")); // CLDR name
79 registerAlias(QStringLiteral("islamicc")); // old CLDR name, still (2018) used by Mozilla
80 // Until we have a oncrete implementation that knows all the needed ephemerides:
81 registerAlias(QStringLiteral("Islamic"));
82}
83
84QString QIslamicCivilCalendar::name() const
85{
86 return QStringLiteral("Islamic Civil");
87}
88
89QCalendar::System QIslamicCivilCalendar::calendarSystem() const
90{
91 return QCalendar::System::IslamicCivil;
92}
93
94bool QIslamicCivilCalendar::isLeapYear(int year) const
95{
96 if (year == QCalendar::Unspecified)
97 return false;
98 if (year < 0)
99 ++year;
100 return qMod(year * 11 + 14, 30) < 11;
101}
102
103bool QIslamicCivilCalendar::dateToJulianDay(int year, int month, int day, qint64 *jd) const
104{
105 Q_ASSERT(jd);
106 if (!isDateValid(year, month, day))
107 return false;
108 if (year <= 0)
109 ++year;
110 *jd = qDiv(10631 * year - 10617, 30)
111 + qDiv(325 * month - 320, 11)
112 + day + 1948439;
113 return true;
114}
115
116QCalendar::YearMonthDay QIslamicCivilCalendar::julianDayToDate(qint64 jd) const
117{
118 const qint64 epoch = 1948440;
119 const int32_t k2 = 30 * (jd - epoch) + 15;
120 const int32_t k1 = 11 * qDiv(qMod(k2, 10631), 30) + 5;
121 int y = qDiv(k2, 10631) + 1;
122 const int month = qDiv(k1, 325) + 1;
123 const int day = qDiv(qMod(k1, 325), 11) + 1;
124 return QCalendar::YearMonthDay(y > 0 ? y : y - 1, month, day);
125}
126
127QT_END_NAMESPACE
128