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