Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qjalalicalendar.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qglobal.h"
5#include "qjalalicalendar_p.h"
7#include "qcalendarmath_p.h"
8#include <QtCore/qmath.h>
9
11
12using namespace QRoundingDown;
13
14// Constants
15
16constexpr qint64 cycleDays = 1029983;
17constexpr int cycleYears = 2820;
18constexpr double yearLength = 365.24219858156028368; // 365 + 683 / 2820.
19constexpr qint64 jalaliEpoch = 2121446; // 475/01/01 AP, start of 2820 cycle
20// This appears to be based on Ahmad Birashk's algorithm.
21
22// Calendar implementation
23
24static inline int cycle(qint64 jdn)
25{
26 return qDiv<cycleDays>(jdn - jalaliEpoch);
27}
28
29qint64 cycleStart(int cycleNo)
30{
31 return jalaliEpoch + cycleNo * cycleDays;
32}
33
34qint64 firstDayOfYear(int year, int cycleNo)
35{
36 qint64 firstDOYinEra = static_cast<qint64>(qFloor(year * yearLength));
37 return jalaliEpoch + cycleNo * cycleDays + firstDOYinEra;
38}
39
82{
83 return QStringLiteral("Jalali");
84}
85
87{
88 return {
89 QStringLiteral("Jalali"),
90 QStringLiteral("Persian"),
91 };
92}
93
94bool QJalaliCalendar::isLeapYear(int year) const
95{
96 if (year == QCalendar::Unspecified)
97 return false;
98 if (year < 0)
99 year++;
100 return qMod<2820>((year + 2346) * 683) < 683;
101}
102
104{
105 return false;
106}
107
109{
110 return false;
111}
112
114{
115 return true;
116}
117
118bool QJalaliCalendar::dateToJulianDay(int year, int month, int day, qint64 *jd) const
119{
120 Q_ASSERT(jd);
121 if (!isDateValid(year, month, day))
122 return false;
123
124 const int y = year - (year < 0 ? 474 : 475);
125 const int c = qDiv<cycleYears>(y);
126 const int yearInCycle = y - c * cycleYears;
127 int dayInYear = day;
128 for (int i = 1; i < month; ++i)
129 dayInYear += daysInMonth(i, year);
130 *jd = firstDayOfYear(yearInCycle, c) + dayInYear - 1;
131 return true;
132}
133
135{
136 const int c = cycle(jd);
137 int yearInCycle = qFloor((jd - cycleStart(c)) / yearLength);
138 int year = yearInCycle + 475 + c * cycleYears;
139 int day = jd - firstDayOfYear(yearInCycle, c) + 1;
140 if (day > daysInYear(year <= 0 ? year - 1 : year)) {
141 year++;
142 day = 1;
143 }
144 if (year <= 0)
145 year--;
146 int month;
147 for (month = 1; month < 12; ++month) {
148 const int last = daysInMonth(month, year);
149 if (day <= last)
150 break;
151 day -= last;
152 }
153 return QCalendar::YearMonthDay(year, month, day);
154}
155
156int QJalaliCalendar::daysInMonth(int month, int year) const
157{
158 if (year && month > 0 && month <= 12)
159 return month < 7 ? 31 : month < 12 || isLeapYear(year) ? 30 : 29;
160
161 return 0;
162}
163
168
169const char16_t *QJalaliCalendar::localeMonthData() const
170{
172}
173
virtual int daysInYear(int year) const
Returns the total number of days in the year number year.
virtual bool isDateValid(int year, int month, int day) const
Returns true if the date specified by year, month, and day is valid for this calendar; otherwise retu...
@ Unspecified
Definition qcalendar.h:57
QCalendar::YearMonthDay julianDayToDate(qint64 jd) const override
Computes the year, month, and day in this calendar for the given Julian day number jd.
const QCalendarLocale * localeMonthIndexData() const override
const char16_t * localeMonthData() const override
static QStringList nameList()
bool isLunar() const override
Returns true if this calendar is a lunar calendar.
int daysInMonth(int month, int year=QCalendar::Unspecified) const override
Returns number of days in the month number month, in year year.
bool isLeapYear(int year) const override
Returns true if the specified year is a leap year for this calendar.
bool isSolar() const override
Returns true if this calendar is a solar calendar.
QString name() const override
Returns the primary name of the calendar.
bool isLuniSolar() const override
Returns true if this calendar is a lunisolar calendar.
bool dateToJulianDay(int year, int month, int day, qint64 *jd) const override
Computes the Julian day number corresponding to the specified year, month, and day.
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
Combined button and popup list for selecting options.
static constexpr char16_t months_data[]
static constexpr QCalendarLocale locale_data[]
constexpr int cycleYears
constexpr qint64 jalaliEpoch
qint64 cycleStart(int cycleNo)
static int cycle(qint64 jdn)
qint64 firstDayOfYear(int year, int cycleNo)
constexpr double yearLength
constexpr qint64 cycleDays
int qFloor(T v)
Definition qmath.h:42
GLint y
const GLubyte * c
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define QStringLiteral(str)
long long qint64
Definition qtypes.h:60