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 "qjuliancalendar_p.h"
42#include "qromancalendar_data_p.h"
43#include "qcalendarmath_p.h"
44#include <QtCore/qmath.h>
45#include <QtCore/qlocale.h>
46#include <QtCore/qdatetime.h>
47
48QT_BEGIN_NAMESPACE
49
50using namespace QRoundingDown;
51
52/*!
53 \since 5.14
54
55 \class QJulianCalendar
56 \inmodule QtCore
57 \brief The QJulianCalendar class provides Julian calendar system
58 implementation.
59
60 \section1 Julian Calendar
61
62 The Julian calendar, proposed by Julius Caesar in 46 BC (708 AUC), was a
63 reform of the Roman calendar. It took effect on 1 January 45 BC (AUC 709),
64 by edict. It was the predominant calendar in the Roman world, most of
65 Europe, and in European settlements in the Americas and elsewhere, until it
66 was refined and gradually replaced by the Gregorian calendar,
67 promulgated in 1582 by Pope Gregory XIII.
68
69 The Julian calendar gains against the mean tropical year at the rate of one
70 day in 128 years. For the Gregorian calendar, the figure is one day in
71 3030 years. The difference in the average length of the year
72 between Julian (365.25 days) and Gregorian (365.2425 days) is 0.002%.
73
74 Source: \l {https://en.wikipedia.org/wiki/Julian_calendar}{Wikipedia page on
75 Julian Calendar}
76 */
77
78QJulianCalendar::QJulianCalendar()
79 : QRomanCalendar(QStringLiteral("Julian"), QCalendar::System::Julian) {}
80
81QString QJulianCalendar::name() const
82{
83 return QStringLiteral("Julian");
84}
85
86QCalendar::System QJulianCalendar::calendarSystem() const
87{
88 return QCalendar::System::Julian;
89}
90
91bool QJulianCalendar::isLeapYear(int year) const
92{
93 if (year == QCalendar::Unspecified || !year)
94 return false;
95
96 return qMod(year < 0 ? year + 1 : year, 4) == 0;
97}
98
99// Julian Day 0 was January the first in the proleptic Julian calendar's 4713 BC
100
101bool QJulianCalendar::dateToJulianDay(int year, int month, int day, qint64 *jd) const
102{
103 Q_ASSERT(jd);
104 if (!isDateValid(year, month, day))
105 return false;
106 if (year < 0)
107 ++year;
108 const qint64 c0 = month < 3 ? -1 : 0;
109 const qint64 j1 = qDiv(1461 * (year + c0), 4);
110 const qint64 j2 = qDiv(153 * month - 1836 * c0 - 457, 5);
111 *jd = j1 + j2 + day + 1721117;
112 return true;
113}
114
115QCalendar::YearMonthDay QJulianCalendar::julianDayToDate(qint64 jd) const
116{
117 const qint64 y2 = jd - 1721118;
118 const qint64 k2 = 4 * y2 + 3;
119 const qint64 k1 = 5 * qDiv(qMod(k2, 1461), 4) + 2;
120 const qint64 x1 = qDiv(k1, 153);
121 const qint64 c0 = qDiv(x1 + 2, 12);
122 const int y = qint16(qDiv(k2, 1461) + c0);
123 const int month = quint8(x1 - 12 * c0 + 3);
124 const int day = qDiv(qMod(k1, 153), 5) + 1;
125 return QCalendar::YearMonthDay(y > 0 ? y : y - 1, month, day);
126}
127
128QT_END_NAMESPACE
129