summaryrefslogtreecommitdiffstats
path: root/src/corelib/time/qjuliancalendar.cpp
blob: b899c6a09ebf14dc1d43b188c66caad68189bf4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "qglobal.h"
#include "qjuliancalendar_p.h"
#include "qromancalendar_data_p.h"
#include "qcalendarmath_p.h"
#include <QtCore/qmath.h>
#include <QtCore/qlocale.h>
#include <QtCore/qdatetime.h>

QT_BEGIN_NAMESPACE

using namespace QRoundingDown;

/*!
    \since 5.14

    \class QJulianCalendar
    \inmodule QtCore
    \brief The QJulianCalendar class provides Julian calendar system
    implementation.

    \section1 Julian Calendar

    The Julian calendar, proposed by Julius Caesar in 46 BC (708 AUC), was a
    reform of the Roman calendar. It took effect on 1 January 45 BC (AUC 709),
    by edict. It was the predominant calendar in the Roman world, most of
    Europe, and in European settlements in the Americas and elsewhere, until it
    was refined and gradually replaced by the Gregorian calendar,
    promulgated in 1582 by Pope Gregory XIII.

    The Julian calendar gains against the mean tropical year at the rate of one
    day in 128 years. For the Gregorian calendar, the figure is one day in
    3030 years. The difference in the average length of the year
    between Julian (365.25 days) and Gregorian (365.2425 days) is 0.002%.

    Source: \l {https://en.wikipedia.org/wiki/Julian_calendar}{Wikipedia page on
    Julian Calendar}
 */

QJulianCalendar::QJulianCalendar()
    : QRomanCalendar(QStringLiteral("Julian"), QCalendar::System::Julian) {}

QString QJulianCalendar::name() const
{
    return QStringLiteral("Julian");
}

QCalendar::System QJulianCalendar::calendarSystem() const
{
    return QCalendar::System::Julian;
}

bool QJulianCalendar::isLeapYear(int year) const
{
    if (year == QCalendar::Unspecified || !year)
        return false;

    return qMod(year < 0 ? year + 1 : year, 4) == 0;
}

// Julian Day 0 was January the first in the proleptic Julian calendar's 4713 BC

bool QJulianCalendar::dateToJulianDay(int year, int month, int day, qint64 *jd) const
{
    Q_ASSERT(jd);
    if (!isDateValid(year, month, day))
        return false;
    if (year < 0)
        ++year;
    const qint64 c0 = month < 3 ? -1 : 0;
    const qint64 j1 = qDiv(1461 * (year + c0), 4);
    const qint64 j2 = qDiv(153 * month - 1836 * c0 - 457, 5);
    *jd = j1 + j2 + day + 1721117;
    return true;
}

QCalendar::YearMonthDay QJulianCalendar::julianDayToDate(qint64 jd) const
{
    const qint64 y2 = jd - 1721118;
    const qint64 k2 = 4 * y2 + 3;
    const qint64 k1 = 5 * qDiv(qMod(k2, 1461), 4) + 2;
    const qint64 x1 = qDiv(k1, 153);
    const qint64 c0 = qDiv(x1 + 2, 12);
    const int y = qint16(qDiv(k2, 1461) + c0);
    const int month = quint8(x1 - 12 * c0 + 3);
    const int day = qDiv(qMod(k1, 153), 5) + 1;
    return QCalendar::YearMonthDay(y > 0 ? y : y - 1, month, day);
}

QT_END_NAMESPACE