| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2019 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 "qgregoriancalendar_p.h" |
| 41 | #include "qcalendarmath_p.h" |
| 42 | #include <QtCore/qdatetime.h> |
| 43 | |
| 44 | QT_BEGIN_NAMESPACE |
| 45 | |
| 46 | using namespace QRoundingDown; |
| 47 | |
| 48 | /*! |
| 49 | \since 5.14 |
| 50 | |
| 51 | \class QGregorianCalendar |
| 52 | \inmodule QtCore |
| 53 | \brief The QGregorianCalendar class implements the Gregorian calendar. |
| 54 | |
| 55 | \section1 The Gregorian Calendar |
| 56 | |
| 57 | The Gregorian calendar is a refinement of the earlier Julian calendar, |
| 58 | itself a late form of the Roman calendar. It is widely used. |
| 59 | |
| 60 | \sa QRomanCalendar, QJulianCalendar, QCalendar |
| 61 | */ |
| 62 | |
| 63 | QGregorianCalendar::QGregorianCalendar() |
| 64 | : QRomanCalendar(QStringLiteral("Gregorian" ), QCalendar::System::Gregorian) |
| 65 | { |
| 66 | registerAlias(QStringLiteral("gregory" )); |
| 67 | } |
| 68 | |
| 69 | QString QGregorianCalendar::name() const |
| 70 | { |
| 71 | return QStringLiteral("Gregorian" ); |
| 72 | } |
| 73 | |
| 74 | QCalendar::System QGregorianCalendar::calendarSystem() const |
| 75 | { |
| 76 | return QCalendar::System::Gregorian; |
| 77 | } |
| 78 | |
| 79 | bool QGregorianCalendar::isLeapYear(int year) const |
| 80 | { |
| 81 | return leapTest(year); |
| 82 | } |
| 83 | |
| 84 | bool QGregorianCalendar::leapTest(int year) |
| 85 | { |
| 86 | if (year == QCalendar::Unspecified) |
| 87 | return false; |
| 88 | |
| 89 | // No year 0 in Gregorian calendar, so -1, -5, -9 etc are leap years |
| 90 | if (year < 1) |
| 91 | ++year; |
| 92 | |
| 93 | return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); |
| 94 | } |
| 95 | |
| 96 | // Duplicating code from QRomanCalendar, but inlining isLeapYear() as leapTest(): |
| 97 | int QGregorianCalendar::monthLength(int month, int year) |
| 98 | { |
| 99 | if (month < 1 || month > 12) |
| 100 | return 0; |
| 101 | |
| 102 | if (month == 2) |
| 103 | return leapTest(year) ? 29 : 28; |
| 104 | |
| 105 | return 30 | ((month & 1) ^ (month >> 3)); |
| 106 | } |
| 107 | |
| 108 | bool QGregorianCalendar::validParts(int year, int month, int day) |
| 109 | { |
| 110 | return year && 0 < day && day <= monthLength(month, year); |
| 111 | } |
| 112 | |
| 113 | int QGregorianCalendar::weekDayOfJulian(qint64 jd) |
| 114 | { |
| 115 | return qMod(a: jd, b: 7) + 1; |
| 116 | } |
| 117 | |
| 118 | bool QGregorianCalendar::dateToJulianDay(int year, int month, int day, qint64 *jd) const |
| 119 | { |
| 120 | return julianFromParts(year, month, day, jd); |
| 121 | } |
| 122 | |
| 123 | bool QGregorianCalendar::julianFromParts(int year, int month, int day, qint64 *jd) |
| 124 | { |
| 125 | Q_ASSERT(jd); |
| 126 | if (!validParts(year, month, day)) |
| 127 | return false; |
| 128 | |
| 129 | if (year < 0) |
| 130 | ++year; |
| 131 | |
| 132 | /* |
| 133 | * Math from The Calendar FAQ at http://www.tondering.dk/claus/cal/julperiod.php |
| 134 | * This formula is correct for all julian days, when using mathematical integer |
| 135 | * division (round to negative infinity), not c++11 integer division (round to zero) |
| 136 | */ |
| 137 | int a = month < 3 ? 1 : 0; |
| 138 | qint64 y = qint64(year) + 4800 - a; |
| 139 | int m = month + 12 * a - 3; |
| 140 | *jd = day + qDiv(a: 153 * m + 2, b: 5) - 32045 |
| 141 | + 365 * y + qDiv(a: y, b: 4) - qDiv(a: y, b: 100) + qDiv(a: y, b: 400); |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | QCalendar::YearMonthDay QGregorianCalendar::julianDayToDate(qint64 jd) const |
| 146 | { |
| 147 | return partsFromJulian(jd); |
| 148 | } |
| 149 | |
| 150 | QCalendar::YearMonthDay QGregorianCalendar::partsFromJulian(qint64 jd) |
| 151 | { |
| 152 | /* |
| 153 | * Math from The Calendar FAQ at http://www.tondering.dk/claus/cal/julperiod.php |
| 154 | * This formula is correct for all julian days, when using mathematical integer |
| 155 | * division (round to negative infinity), not c++11 integer division (round to zero) |
| 156 | */ |
| 157 | qint64 a = jd + 32044; |
| 158 | qint64 b = qDiv(a: 4 * a + 3, b: 146097); |
| 159 | int c = a - qDiv(a: 146097 * b, b: 4); |
| 160 | |
| 161 | int d = qDiv(a: 4 * c + 3, b: 1461); |
| 162 | int e = c - qDiv(a: 1461 * d, b: 4); |
| 163 | int m = qDiv(a: 5 * e + 2, b: 153); |
| 164 | |
| 165 | int y = 100 * b + d - 4800 + qDiv(a: m, b: 10); |
| 166 | |
| 167 | // Adjust for no year 0 |
| 168 | int year = y > 0 ? y : y - 1; |
| 169 | int month = m + 3 - 12 * qDiv(a: m, b: 10); |
| 170 | int day = e - qDiv(a: 153 * m + 2, b: 5) + 1; |
| 171 | |
| 172 | return QCalendar::YearMonthDay(year, month, day); |
| 173 | } |
| 174 | |
| 175 | QT_END_NAMESPACE |
| 176 | |