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
4#include "qglobal.h"
5#include "qmilankoviccalendar_p.h"
6#include "qcalendarmath_p.h"
7
8#include <QtCore/qdatetime.h>
9
10QT_BEGIN_NAMESPACE
11
12using namespace QRoundingDown;
13
14/*!
15 \since 5.14
16
17 \class QMilankovicCalendar
18 \inmodule QtCore
19 \brief The QMilankovicCalendar class provides Milanković calendar system
20 implementation.
21
22 The Revised Julian calendar, also known as the Milanković calendar, or,
23 less formally, new calendar, is a calendar, developed and proposed by the
24 Serbian scientist Milutin Milanković in 1923, which effectively discontinued
25 the 340 years of divergence between the naming of dates sanctioned by those
26 Eastern Orthodox churches adopting it and the Gregorian calendar that has
27 come to predominate worldwide. This calendar was intended to replace the
28 ecclesiastical calendar based on the Julian calendar hitherto in use by all
29 of the Eastern Orthodox Church. The Revised Julian calendar temporarily
30 aligned its dates with the Gregorian calendar proclaimed in 1582 by Pope
31 Gregory XIII for adoption by the Christian world. The calendar has been
32 adopted by the Orthodox churches of Constantinople, Albania, Alexandria,
33 Antioch, Bulgaria, Cyprus, Greece, Poland, and Romania.
34
35 Source: \l {https://en.wikipedia.org/wiki/Revised_Julian_calendar}{Wikipedia
36 page on Milanković Calendar}
37 */
38
39QString QMilankovicCalendar::name() const
40{
41 return QStringLiteral("Milankovic");
42}
43
44QStringList QMilankovicCalendar::nameList()
45{
46 return { QStringLiteral("Milankovic") };
47}
48
49bool QMilankovicCalendar::isLeapYear(int year) const
50{
51 if (year == QCalendar::Unspecified)
52 return false;
53 if (year <= 0)
54 ++year;
55 if (qMod<4>(a: year))
56 return false;
57 const auto yeardm = qDivMod<100>(a: year);
58 if (yeardm.remainder == 0) {
59 const qint16 century = qMod<9>(a: yeardm.quotient);
60 if (century != 2 && century != 6)
61 return false;
62 }
63 return true;
64}
65
66using namespace QRomanCalendrical;
67// End a Milankovic nine-century cycle on 1 BC, Feb 28 (Gregorian Feb 29):
68constexpr qint64 MilankovicBaseJd = LeapDayGregorian1Bce;
69// Leap years every 4 years, except for 7 turn-of-century years per nine centuries:
70constexpr unsigned NineCenturies = 365 * 900 + 900 / 4 - 7;
71// When the turn-of-century is a leap year, the century has 25 leap years in it:
72constexpr unsigned LeapCentury = 365 * 100 + 25;
73
74bool QMilankovicCalendar::dateToJulianDay(int year, int month, int day, qint64 *jd) const
75{
76 Q_ASSERT(jd);
77 if (!isDateValid(year, month, day))
78 return false;
79
80 const auto yearDays = yearMonthToYearDays(year, month);
81 const auto centuryYear = qDivMod<100>(a: yearDays.year);
82 const qint64 fromYear = qDiv<9>(a: NineCenturies * centuryYear.quotient + 6)
83 + qDiv<100>(a: LeapCentury * centuryYear.remainder);
84 *jd = fromYear + yearDays.days + day + MilankovicBaseJd;
85 return true;
86}
87
88QCalendar::YearMonthDay QMilankovicCalendar::julianDayToDate(qint64 jd) const
89{
90 const auto century9Day = qDivMod<NineCenturies>(a: 9 * (jd - MilankovicBaseJd) - 7);
91 // Its remainder changes by 9 per day, except roughly once per century.
92 const auto year100Day = qDivMod<LeapCentury>(a: 100 * qDiv<9>(a: century9Day.remainder) + 99);
93 // Its remainder changes by 100 per day, except roughly once per year.
94 const auto ymd = dayInYearToYmd(dayInYear: qDiv<100>(a: year100Day.remainder));
95 const int y = 100 * century9Day.quotient + year100Day.quotient + ymd.year;
96 return QCalendar::YearMonthDay(y > 0 ? y : y - 1, ymd.month, ymd.day);
97}
98
99QT_END_NAMESPACE
100

source code of qtbase/src/corelib/time/qmilankoviccalendar.cpp