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 "qmilankoviccalendar_p.h" |
42 | #include "qcalendarmath_p.h" |
43 | #include <QtCore/qmath.h> |
44 | #include <QtCore/qlocale.h> |
45 | #include <QtCore/qdatetime.h> |
46 | |
47 | QT_BEGIN_NAMESPACE |
48 | |
49 | using namespace QRoundingDown; |
50 | |
51 | /*! |
52 | \since 5.14 |
53 | |
54 | \class QMilankovicCalendar |
55 | \inmodule QtCore |
56 | \brief The QMilankovicCalendar class provides Milanković calendar system |
57 | implementation. |
58 | |
59 | The Revised Julian calendar, also known as the Milanković calendar, or, |
60 | less formally, new calendar, is a calendar, developed and proposed by the |
61 | Serbian scientist Milutin Milanković in 1923, which effectively discontinued |
62 | the 340 years of divergence between the naming of dates sanctioned by those |
63 | Eastern Orthodox churches adopting it and the Gregorian calendar that has |
64 | come to predominate worldwide. This calendar was intended to replace the |
65 | ecclesiastical calendar based on the Julian calendar hitherto in use by all |
66 | of the Eastern Orthodox Church. The Revised Julian calendar temporarily |
67 | aligned its dates with the Gregorian calendar proclaimed in 1582 by Pope |
68 | Gregory XIII for adoption by the Christian world. The calendar has been |
69 | adopted by the Orthodox churches of Constantinople, Albania, Alexandria, |
70 | Antioch, Bulgaria, Cyprus, Greece, Poland, and Romania. |
71 | |
72 | Source: \l {https://en.wikipedia.org/wiki/Revised_Julian_calendar}{Wikipedia |
73 | page on Milanković Calendar} |
74 | */ |
75 | |
76 | QMilankovicCalendar::QMilankovicCalendar() |
77 | : QRomanCalendar(QStringLiteral("Milankovic" ), QCalendar::System::Milankovic) {} |
78 | |
79 | QString QMilankovicCalendar::name() const |
80 | { |
81 | return QStringLiteral("Milankovic" ); |
82 | } |
83 | |
84 | QCalendar::System QMilankovicCalendar::calendarSystem() const |
85 | { |
86 | return QCalendar::System::Milankovic; |
87 | } |
88 | |
89 | bool QMilankovicCalendar::isLeapYear(int year) const |
90 | { |
91 | if (year == QCalendar::Unspecified) |
92 | return false; |
93 | if (year <= 0) |
94 | ++year; |
95 | if (qMod(a: year, b: 4)) |
96 | return false; |
97 | if (qMod(a: year, b: 100) == 0) { |
98 | const qint16 century = qMod(a: qDiv(a: year, b: 100), b: 9); |
99 | if (century != 2 && century != 6) |
100 | return false; |
101 | } |
102 | return true; |
103 | } |
104 | |
105 | bool QMilankovicCalendar::dateToJulianDay(int year, int month, int day, qint64 *jd) const |
106 | { |
107 | Q_ASSERT(jd); |
108 | if (!isDateValid(year, month, day)) |
109 | return false; |
110 | if (year <= 0) |
111 | ++year; |
112 | const qint16 c0 = month < 3 ? -1 : 0; |
113 | const qint16 x1 = month - 12 * c0 - 3; |
114 | const qint16 x4 = year + c0; |
115 | const qint16 x3 = qDiv(a: x4, b: 100); |
116 | const qint16 x2 = qMod(a: x4, b: 100); |
117 | *jd = qDiv(a: 328718 * x3 + 6, b: 9) |
118 | + qDiv(a: 36525 * x2 , b: 100) |
119 | + qDiv(a: 153 * x1 + 2 , b: 5) |
120 | + day + 1721119; |
121 | return true; |
122 | } |
123 | |
124 | QCalendar::YearMonthDay QMilankovicCalendar::julianDayToDate(qint64 jd) const |
125 | { |
126 | const qint64 k3 = 9 * (jd - 1721120) + 2; |
127 | const qint64 x3 = qDiv(a: k3, b: 328718); |
128 | const qint64 k2 = 100 * qDiv(a: qMod(a: k3, b: 328718), b: 9) + 99; |
129 | const qint64 k1 = qDiv(a: qMod(a: k2, b: 36525), b: 100) * 5 + 2; |
130 | const qint64 x2 = qDiv(a: k2, b: 36525); |
131 | const qint64 x1 = qDiv(a: 5 * qDiv(a: qMod(a: k2, b: 36525), b: 100) + 2, b: 153); |
132 | const qint64 c0 = qDiv(a: x1 + 2, b: 12); |
133 | const int y = 100 * x3 + x2 + c0; |
134 | const int month = x1 - 12 * c0 + 3; |
135 | const int day = qDiv(a: qMod(a: k1, b: 153), b: 5) + 1; |
136 | return QCalendar::YearMonthDay(y > 0 ? y : y - 1, month, day); |
137 | } |
138 | |
139 | QT_END_NAMESPACE |
140 | |