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