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 "qjuliancalendar_p.h" |
6 | #include "qcalendarmath_p.h" |
7 | |
8 | #include <QtCore/qdatetime.h> |
9 | |
10 | QT_BEGIN_NAMESPACE |
11 | |
12 | using namespace QRoundingDown; |
13 | |
14 | /*! |
15 | \since 5.14 |
16 | |
17 | \class QJulianCalendar |
18 | \inmodule QtCore |
19 | \brief The QJulianCalendar class provides Julian calendar system |
20 | implementation. |
21 | |
22 | \section1 Julian Calendar |
23 | |
24 | The Julian calendar, proposed by Julius Caesar in 46 BC (708 AUC), was a |
25 | reform of the Roman calendar. It took effect on 1 January 45 BC (AUC 709), |
26 | by edict. It was the predominant calendar in the Roman world, most of |
27 | Europe, and in European settlements in the Americas and elsewhere, until it |
28 | was refined and gradually replaced by the Gregorian calendar, |
29 | promulgated in 1582 by Pope Gregory XIII. |
30 | |
31 | The Julian calendar gains against the mean tropical year at the rate of one |
32 | day in 128 years. For the Gregorian calendar, the figure is one day in |
33 | 3030 years. The difference in the average length of the year |
34 | between Julian (365.25 days) and Gregorian (365.2425 days) is 0.002%. |
35 | |
36 | Source: \l {https://en.wikipedia.org/wiki/Julian_calendar}{Wikipedia page on |
37 | Julian Calendar} |
38 | */ |
39 | |
40 | QString QJulianCalendar::name() const |
41 | { |
42 | return QStringLiteral("Julian" ); |
43 | } |
44 | |
45 | QStringList QJulianCalendar::nameList() |
46 | { |
47 | return { QStringLiteral("Julian" ) }; |
48 | } |
49 | |
50 | bool QJulianCalendar::isLeapYear(int year) const |
51 | { |
52 | if (year == QCalendar::Unspecified || !year) |
53 | return false; |
54 | |
55 | return qMod<4>(a: year < 0 ? year + 1 : year) == 0; |
56 | } |
57 | |
58 | // Julian Day 0 was January the first in the proleptic Julian calendar's 4713 BC. |
59 | using namespace QRomanCalendrical; |
60 | // End a Julian four-year cycle on 1 BC's leap day (Gregorian Feb 27th): |
61 | constexpr qint64 JulianBaseJd = LeapDayGregorian1Bce - 2; |
62 | |
63 | bool QJulianCalendar::dateToJulianDay(int year, int month, int day, qint64 *jd) const |
64 | { |
65 | Q_ASSERT(jd); |
66 | if (!isDateValid(year, month, day)) |
67 | return false; |
68 | |
69 | const auto yearDays = yearMonthToYearDays(year, month); |
70 | *jd = qDiv<4>(a: FourYears * yearDays.year) + yearDays.days + day + JulianBaseJd; |
71 | return true; |
72 | } |
73 | |
74 | QCalendar::YearMonthDay QJulianCalendar::julianDayToDate(qint64 jd) const |
75 | { |
76 | const auto year4Day = qDivMod<FourYears>(a: 4 * (jd - JulianBaseJd) - 1); |
77 | // Its remainder changes by 4 per day, except at roughly yearly quotient steps. |
78 | const auto ymd = dayInYearToYmd(dayInYear: qDiv<4>(a: year4Day.remainder)); |
79 | const int y = year4Day.quotient + ymd.year; |
80 | return QCalendar::YearMonthDay(y > 0 ? y : y - 1, ymd.month, ymd.day); |
81 | } |
82 | |
83 | QT_END_NAMESPACE |
84 | |