| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2020 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 "qjalalicalendar_p.h" |
| 42 | #include "qjalalicalendar_data_p.h" |
| 43 | #include "qcalendarmath_p.h" |
| 44 | #include <QtCore/qmath.h> |
| 45 | |
| 46 | QT_BEGIN_NAMESPACE |
| 47 | |
| 48 | using namespace QRoundingDown; |
| 49 | |
| 50 | // Constants |
| 51 | |
| 52 | static const qint64 cycleDays = 1029983; |
| 53 | static const int cycleYears = 2820; |
| 54 | static const double yearLength = 365.24219858156028368; // 365 + leapRatio; |
| 55 | static const qint64 jalaliEpoch = 2121446; // 475/01/01 AP, start of 2820 cycle |
| 56 | |
| 57 | // Calendar implementation |
| 58 | |
| 59 | static inline int cycle(qint64 jdn) |
| 60 | { |
| 61 | return qDiv(jdn - jalaliEpoch, cycleDays); |
| 62 | } |
| 63 | |
| 64 | qint64 cycleStart(int cycleNo) |
| 65 | { |
| 66 | return jalaliEpoch + cycleNo * cycleDays; |
| 67 | } |
| 68 | |
| 69 | qint64 firstDayOfYear(int year, int cycleNo) |
| 70 | { |
| 71 | qint64 firstDOYinEra = static_cast<qint64>(qFloor(year * yearLength)); |
| 72 | return jalaliEpoch + cycleNo * cycleDays + firstDOYinEra; |
| 73 | } |
| 74 | |
| 75 | /*! |
| 76 | \since 5.14 |
| 77 | |
| 78 | \class QJalaliCalendar |
| 79 | \inmodule QtCore |
| 80 | \brief The QJalaliCalendar class provides Jalali (Hijri Shamsi) calendar |
| 81 | system implementation. |
| 82 | |
| 83 | \section1 Solar Hijri Calendar System |
| 84 | |
| 85 | The Solar Hijri calendar, also called the Solar Hejri calendar, Shamsi |
| 86 | Hijri calendar or Jalali calendar, is the official calendar of Iran and |
| 87 | Afghanistan. It begins on the vernal equinox (Nowruz) as determined by |
| 88 | astronomical calculation for the Iran Standard Time meridian |
| 89 | (52.5°E or GMT+3.5h). This determination of starting moment is more accurate |
| 90 | than the Gregorian calendar for predicting the date of the vernal equinox, |
| 91 | because it uses astronomical observations rather than mathematical rules. |
| 92 | |
| 93 | \section2 Calendar Organization |
| 94 | |
| 95 | Each of the twelve months corresponds with a zodiac sign. The first six |
| 96 | months have 31 days, the next five have 30 days, and the last month has 29 |
| 97 | days in usual years but 30 days in leap years. The New Year's Day always |
| 98 | falls on the March equinox. |
| 99 | |
| 100 | \section2 Leap Year Rules |
| 101 | |
| 102 | The Solar Hijri calendar produces a five-year leap year interval after about |
| 103 | every seven four-year leap year intervals. It usually follows a 33-year |
| 104 | cycle with occasional interruptions by single 29-year or 37-year subcycles. |
| 105 | The reason for this behavior is that it tracks the observed vernal equinox. |
| 106 | By contrast, some less accurate predictive algorithms are in use based |
| 107 | on confusion between the average tropical year (365.2422 days, approximated |
| 108 | with near 128-year cycles or 2820-year great cycles) and the mean interval |
| 109 | between spring equinoxes (365.2424 days, approximated with a near 33-year |
| 110 | cycle). |
| 111 | |
| 112 | Source: \l {https://en.wikipedia.org/wiki/Solar_Hijri_calendar}{Wikipedia |
| 113 | page on Solar Hijri Calendar} |
| 114 | */ |
| 115 | |
| 116 | QJalaliCalendar::QJalaliCalendar() |
| 117 | : QCalendarBackend(QStringLiteral("Jalali" ), QCalendar::System::Jalali) |
| 118 | { |
| 119 | registerAlias(QStringLiteral("Persian" )); |
| 120 | } |
| 121 | |
| 122 | QString QJalaliCalendar::name() const |
| 123 | { |
| 124 | return QStringLiteral("Jalali" ); |
| 125 | } |
| 126 | |
| 127 | QCalendar::System QJalaliCalendar::calendarSystem() const |
| 128 | { |
| 129 | return QCalendar::System::Jalali; |
| 130 | } |
| 131 | |
| 132 | bool QJalaliCalendar::isLeapYear(int year) const |
| 133 | { |
| 134 | if (year == QCalendar::Unspecified) |
| 135 | return false; |
| 136 | if (year < 0) |
| 137 | year++; |
| 138 | return qMod((year + 2346) * 683, 2820) < 683; |
| 139 | } |
| 140 | |
| 141 | bool QJalaliCalendar::isLunar() const |
| 142 | { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | bool QJalaliCalendar::isLuniSolar() const |
| 147 | { |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | bool QJalaliCalendar::isSolar() const |
| 152 | { |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | bool QJalaliCalendar::dateToJulianDay(int year, int month, int day, qint64 *jd) const |
| 157 | { |
| 158 | Q_ASSERT(jd); |
| 159 | if (!isDateValid(year, month, day)) |
| 160 | return false; |
| 161 | |
| 162 | const int y = year - (year < 0 ? 474 : 475); |
| 163 | const int c = qDiv(y, cycleYears); |
| 164 | const int yearInCycle = y - c * cycleYears; |
| 165 | int dayInYear = day; |
| 166 | for (int i = 1; i < month; ++i) |
| 167 | dayInYear += daysInMonth(i, year); |
| 168 | *jd = firstDayOfYear(yearInCycle, c) + dayInYear - 1; |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | QCalendar::YearMonthDay QJalaliCalendar::julianDayToDate(qint64 jd) const |
| 173 | { |
| 174 | const int c = cycle(jd); |
| 175 | int yearInCycle = qFloor((jd - cycleStart(c)) / yearLength); |
| 176 | int year = yearInCycle + 475 + c * cycleYears; |
| 177 | int day = jd - firstDayOfYear(yearInCycle, c) + 1; |
| 178 | if (day > daysInYear(year <= 0 ? year - 1 : year)) { |
| 179 | year++; |
| 180 | day = 1; |
| 181 | } |
| 182 | if (year <= 0) |
| 183 | year--; |
| 184 | int month; |
| 185 | for (month = 1; month < 12; ++month) { |
| 186 | const int last = daysInMonth(month, year); |
| 187 | if (day <= last) |
| 188 | break; |
| 189 | day -= last; |
| 190 | } |
| 191 | return QCalendar::YearMonthDay(year, month, day); |
| 192 | } |
| 193 | |
| 194 | int QJalaliCalendar::daysInMonth(int month, int year) const |
| 195 | { |
| 196 | if (year && month > 0 && month <= 12) |
| 197 | return month < 7 ? 31 : month < 12 || isLeapYear(year) ? 30 : 29; |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | const QCalendarLocale *QJalaliCalendar::localeMonthIndexData() const |
| 203 | { |
| 204 | return locale_data; |
| 205 | } |
| 206 | |
| 207 | const char16_t *QJalaliCalendar::localeMonthData() const |
| 208 | { |
| 209 | return months_data; |
| 210 | } |
| 211 | |
| 212 | QT_END_NAMESPACE |
| 213 | |