summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/time/qcalendar.cpp14
-rw-r--r--src/corelib/time/qcalendar.h9
-rw-r--r--src/corelib/time/qgregoriancalendar.cpp2
-rw-r--r--src/corelib/time/qjuliancalendar.cpp128
-rw-r--r--src/corelib/time/qjuliancalendar_p.h74
-rw-r--r--src/corelib/time/qmilankoviccalendar.cpp141
-rw-r--r--src/corelib/time/qmilankoviccalendar_p.h74
-rw-r--r--src/corelib/time/qromancalendar.cpp2
-rw-r--r--src/corelib/time/time.pri4
-rw-r--r--tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp8
10 files changed, 452 insertions, 4 deletions
diff --git a/src/corelib/time/qcalendar.cpp b/src/corelib/time/qcalendar.cpp
index 06dd1c671f..4207d26e5c 100644
--- a/src/corelib/time/qcalendar.cpp
+++ b/src/corelib/time/qcalendar.cpp
@@ -29,6 +29,10 @@
#include "qcalendar.h"
#include "qcalendarbackend_p.h"
#include "qgregoriancalendar_p.h"
+#ifndef QT_BOOTSTRAPPED
+#include "qjuliancalendar_p.h"
+#include "qmilankoviccalendar_p.h"
+#endif
#include "qdatetime.h"
#include "qcalendarmath_p.h"
@@ -608,6 +612,14 @@ const QCalendarBackend *QCalendarBackend::fromEnum(QCalendar::System system)
switch (system) {
case QCalendar::System::Gregorian:
return new QGregorianCalendar;
+#ifndef QT_BOOTSTRAPPED
+ case QCalendar::System::Julian:
+ return new QJulianCalendar;
+ case QCalendar::System::Milankovic:
+ return new QMilankovicCalendar;
+#else // When highest-numbered system isn't enabled, ensure we have a case for Last:
+ case QCalendar::System::Last:
+#endif
case QCalendar::System::User:
Q_UNREACHABLE();
}
@@ -645,6 +657,8 @@ const QCalendarBackend *QCalendarBackend::fromEnum(QCalendar::System system)
This enumerated type is used to specify a choice of calendar system.
\value Gregorian The default calendar, used internationally.
+ \value Julian An ancient Roman calendar with too few leap years.
+ \value Milankovic A revised Julian calendar used by some Orthodox churches.
\sa QCalendar
*/
diff --git a/src/corelib/time/qcalendar.h b/src/corelib/time/qcalendar.h
index 1f85647cab..25740fa3d3 100644
--- a/src/corelib/time/qcalendar.h
+++ b/src/corelib/time/qcalendar.h
@@ -103,9 +103,14 @@ public:
int day = Unspecified;
};
// Feature (\w+)calendar uses CLDR type="\1" data, except as noted in type="..." comments below
- enum class System {
+ enum class System
+ {
Gregorian, // CLDR: type = "gregory", alias = "gregorian"
- Last = Gregorian,
+#ifndef QT_BOOTSTRAPPED
+ Julian = 8,
+ Milankovic = 9,
+#endif // These are Roman-based, so share Gregorian's CLDR data
+ Last = 9, // Highest number of any above
User = -1
};
// New entries must be added to the \enum doc in qcalendar.cpp and
diff --git a/src/corelib/time/qgregoriancalendar.cpp b/src/corelib/time/qgregoriancalendar.cpp
index 64501600b4..d3db572aa7 100644
--- a/src/corelib/time/qgregoriancalendar.cpp
+++ b/src/corelib/time/qgregoriancalendar.cpp
@@ -57,7 +57,7 @@ using namespace QRoundingDown;
The Gregorian calendar is a refinement of the earlier Julian calendar,
itself a late form of the Roman calendar. It is widely used.
- \sa QRomanCalendar, QCalendarBackend, QCalendar
+ \sa QRomanCalendar, QJulianCalendar, QCalendarBackend, QCalendar
*/
QGregorianCalendar::QGregorianCalendar()
diff --git a/src/corelib/time/qjuliancalendar.cpp b/src/corelib/time/qjuliancalendar.cpp
new file mode 100644
index 0000000000..8c6818eb31
--- /dev/null
+++ b/src/corelib/time/qjuliancalendar.cpp
@@ -0,0 +1,128 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 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:LGPL$
+** 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.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $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
diff --git a/src/corelib/time/qjuliancalendar_p.h b/src/corelib/time/qjuliancalendar_p.h
new file mode 100644
index 0000000000..104cf6aa30
--- /dev/null
+++ b/src/corelib/time/qjuliancalendar_p.h
@@ -0,0 +1,74 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 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:LGPL$
+** 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.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QJULIAN_CALENDAR_P_H
+#define QJULIAN_CALENDAR_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists for the convenience
+// of calendar implementations. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include "qromancalendar_p.h"
+
+QT_BEGIN_NAMESPACE
+
+class Q_CORE_EXPORT QJulianCalendar : public QRomanCalendar
+{
+public:
+ QJulianCalendar();
+ // Calendar properties:
+ QString name() const override;
+ QCalendar::System calendarSystem() const override;
+ // Date queries:
+ bool isLeapYear(int year) const override;
+ // Julian Day conversions:
+ bool dateToJulianDay(int year, int month, int day, qint64 *jd) const override;
+ QCalendar::YearMonthDay julianDayToDate(qint64 jd) const override;
+};
+
+QT_END_NAMESPACE
+
+#endif // QJULIAN_CALENDAR_P_H
diff --git a/src/corelib/time/qmilankoviccalendar.cpp b/src/corelib/time/qmilankoviccalendar.cpp
new file mode 100644
index 0000000000..68d74947b9
--- /dev/null
+++ b/src/corelib/time/qmilankoviccalendar.cpp
@@ -0,0 +1,141 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 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:LGPL$
+** 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.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qglobal.h"
+#include "qmilankoviccalendar_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 QMilankovicCalendar
+ \inmodule QtCore
+ \brief The QMilankovicCalendar class provides Milanković calendar system
+ implementation.
+
+ \section1
+
+ The Revised Julian calendar, also known as the Milanković calendar, or,
+ less formally, new calendar, is a calendar, developed and proposed by the
+ Serbian scientist Milutin Milanković in 1923, which effectively discontinued
+ the 340 years of divergence between the naming of dates sanctioned by those
+ Eastern Orthodox churches adopting it and the Gregorian calendar that has
+ come to predominate worldwide. This calendar was intended to replace the
+ ecclesiastical calendar based on the Julian calendar hitherto in use by all
+ of the Eastern Orthodox Church. The Revised Julian calendar temporarily
+ aligned its dates with the Gregorian calendar proclaimed in 1582 by Pope
+ Gregory XIII for adoption by the Christian world. The calendar has been
+ adopted by the Orthodox churches of Constantinople, Albania, Alexandria,
+ Antioch, Bulgaria, Cyprus, Greece, Poland, and Romania.
+
+ Source: \l {https://en.wikipedia.org/wiki/Revised_Julian_calendar}{Wikipedia
+ page on Milanković Calendar}
+ */
+
+QMilankovicCalendar::QMilankovicCalendar()
+ : QRomanCalendar(QStringLiteral("Milankovic"), QCalendar::System::Milankovic) {}
+
+QString QMilankovicCalendar::name() const
+{
+ return QStringLiteral("Milankovic");
+}
+
+QCalendar::System QMilankovicCalendar::calendarSystem() const
+{
+ return QCalendar::System::Milankovic;
+}
+
+bool QMilankovicCalendar::isLeapYear(int year) const
+{
+ if (year == QCalendar::Unspecified)
+ return false;
+ if (year <= 0)
+ ++year;
+ if (qMod(year, 4))
+ return false;
+ if (qMod(year, 100) == 0) {
+ const qint16 century = qMod(qDiv(year, 100), 9);
+ if (century != 2 && century != 6)
+ return false;
+ }
+ return true;
+}
+
+bool QMilankovicCalendar::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 qint16 c0 = month < 3 ? -1 : 0;
+ const qint16 x1 = month - 12 * c0 - 3;
+ const qint16 x4 = year + c0;
+ const qint16 x3 = qDiv(x4, 100);
+ const qint16 x2 = qMod(x4, 100);
+ *jd = qDiv(328718 * x3 + 6, 9)
+ + qDiv(36525 * x2 , 100)
+ + qDiv(153 * x1 + 2 , 5)
+ + day + 1721119;
+ return true;
+}
+
+QCalendar::YearMonthDay QMilankovicCalendar::julianDayToDate(qint64 jd) const
+{
+ const qint64 k3 = 9 * (jd - 1721120) + 2;
+ const qint64 x3 = qDiv(k3, 328718);
+ const qint64 k2 = 100 * qDiv(qMod(k3, 328718), 9) + 99;
+ const qint64 k1 = qDiv(qMod(k2, 36525), 100) * 5 + 2;
+ const qint64 x2 = qDiv(k2, 36525);
+ const qint64 x1 = qDiv(5 * qDiv(qMod(k2, 36525), 100) + 2, 153);
+ const qint64 c0 = qDiv(x1 + 2, 12);
+ const int y = 100 * x3 + x2 + c0;
+ const int month = 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
diff --git a/src/corelib/time/qmilankoviccalendar_p.h b/src/corelib/time/qmilankoviccalendar_p.h
new file mode 100644
index 0000000000..ebff7e7f39
--- /dev/null
+++ b/src/corelib/time/qmilankoviccalendar_p.h
@@ -0,0 +1,74 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 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:LGPL$
+** 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.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QMILANKOVICCALENDAR_CALENDAR_P_H
+#define QMILANKOVICCALENDAR_CALENDAR_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists for the convenience
+// of calendar implementations. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include "qromancalendar_p.h"
+
+QT_BEGIN_NAMESPACE
+
+class Q_CORE_EXPORT QMilankovicCalendar : public QRomanCalendar
+{
+public:
+ QMilankovicCalendar();
+ // Calendar properties:
+ QString name() const override;
+ QCalendar::System calendarSystem() const override;
+ // Date queries:
+ bool isLeapYear(int year) const override;
+ // Julian Day conversions:
+ bool dateToJulianDay(int year, int month, int day, qint64 *jd) const override;
+ QCalendar::YearMonthDay julianDayToDate(qint64 jd) const override;
+};
+
+QT_END_NAMESPACE
+
+#endif // QMILANKOVICCALENDAR_CALENDAR_P_H
diff --git a/src/corelib/time/qromancalendar.cpp b/src/corelib/time/qromancalendar.cpp
index 7b07fe8498..81b8d93d43 100644
--- a/src/corelib/time/qromancalendar.cpp
+++ b/src/corelib/time/qromancalendar.cpp
@@ -57,7 +57,7 @@ QT_BEGIN_NAMESPACE
lengths depend in a common way on whether the year is a leap year. They differ
in how they determine which years are leap years.
- \sa QGregorianCalendar
+ \sa QGregorianCalendar, QJulianCalendar, QMilankovicCalendar
*/
int QRomanCalendar::daysInMonth(int month, int year) const
diff --git a/src/corelib/time/time.pri b/src/corelib/time/time.pri
index 41896898f6..17462487f3 100644
--- a/src/corelib/time/time.pri
+++ b/src/corelib/time/time.pri
@@ -7,6 +7,8 @@ HEADERS += \
time/qdatetime.h \
time/qdatetime_p.h \
time/qgregoriancalendar_p.h \
+ time/qjuliancalendar_p.h \
+ time/qmilankoviccalendar_p.h \
time/qromancalendar_p.h \
time/qromancalendar_data_p.h
@@ -14,6 +16,8 @@ SOURCES += \
time/qdatetime.cpp \
time/qcalendar.cpp \
time/qgregoriancalendar.cpp \
+ time/qjuliancalendar.cpp \
+ time/qmilankoviccalendar.cpp \
time/qromancalendar.cpp
qtConfig(timezone) {
diff --git a/tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp b/tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp
index 3a410100f4..625567747e 100644
--- a/tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp
+++ b/tests/auto/corelib/time/qcalendar/tst_qcalendar.cpp
@@ -167,6 +167,14 @@ void tst_QCalendar::specific_data()
ADDROW(Gregorian, 1970, 1, 1, 1970, 1, 1);
+ // One known specific date, for each calendar
+#ifndef QT_BOOTSTRAPPED
+ // Julian 1582-10-4 was followed by Gregorian 1582-10-15
+ ADDROW(Julian, 1582, 10, 4, 1582, 10, 14);
+ // Milankovic matches Gregorian for a few centuries
+ ADDROW(Milankovic, 1923, 3, 20, 1923, 3, 20);
+#endif
+
#undef ADDROW
}