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#ifndef QCALENDAR_H
41#define QCALENDAR_H
42
43#include <limits>
44
45#include <QtCore/qglobal.h>
46#include <QtCore/qlocale.h>
47#include <QtCore/qstring.h>
48#include <QtCore/qstringview.h>
49
50/* Suggested enum names for other calendars known to CLDR (v33.1)
51
52 Not yet implemented - see QCalendar::System - contributions welcome:
53
54 * Buddhist -- Thai Buddhist, to be specific
55 * Chinese
56 * Coptic
57 * Dangi -- Korean
58 * Ethiopic (Amete Mihret - epoch approx. 8 C.E.)
59 * EthiopicAmeteAlem (Amete Alem - epoch approx. 5493 B.C.E; data from
60 type="ethiopic-amete-alem", an alias for type="ethioaa")
61 * Hebrew
62 * Indian -- National
63 * Islamic -- Based on astronomical observations, not predictions, so hard to
64 implement. CLDR's data for type="islamic" apply, unless overridden, to the
65 other Islamic calendar variants, i.e. IslamicCivil, above, and the three
66 following. See QHijriCalendar, a common base to provide that data.
67 * IslamicTabular -- tabular, astronomical epoch (same as IslamicCivil, except
68 for epoch), CLDR type="islamic-tbla"
69 * Saudi -- Saudi Arabia, sighting; CLDR type="islamic-rgsa"
70 * UmmAlQura -- Umm al-Qura, Saudi Arabia, calculated; CLDR type="islamic-umalqura"
71 * Iso8601 -- as Gregorian, but treating ISO 8601 weeks as "months"
72 * Japanese -- Imperial calendar
73 * Minguo -- Republic of China, Taiwan; CLDR type="roc"
74
75 See:
76 http://www.unicode.org/repos/cldr/tags/latest/common/bcp47/calendar.xml
77
78 These can potentially be supported, as features, using CLDR's data; any
79 others shall need hand-crafted localization data; it would probably be best
80 to do that by contributing data for them to CLDR.
81*/
82
83QT_BEGIN_NAMESPACE
84
85class QCalendarBackend;
86class QDate;
87
88class Q_CORE_EXPORT QCalendar
89{
90 Q_GADGET
91public:
92 // (Extra parentheses to suppress bogus reading of min() as a macro.)
93 enum : int { Unspecified = (std::numeric_limits<int>::min)() };
94 struct YearMonthDay
95 {
96 YearMonthDay() = default;
97 YearMonthDay(int y, int m = 1, int d = 1) : year(y), month(m), day(d) {}
98
99 bool isValid() const
100 { return month != Unspecified && day != Unspecified; }
101 // (The first year supported by QDate has year == Unspecified.)
102
103 int year = Unspecified;
104 int month = Unspecified;
105 int day = Unspecified;
106 };
107 // Feature (\w+)calendar uses CLDR type="\1" data, except as noted in type="..." comments below
108 enum class System
109 {
110 Gregorian, // CLDR: type = "gregory", alias = "gregorian"
111#ifndef QT_BOOTSTRAPPED
112 Julian = 8,
113 Milankovic = 9,
114#endif // These are Roman-based, so share Gregorian's CLDR data
115
116 // Feature-controlled calendars:
117#if QT_CONFIG(jalalicalendar) // type="persian"
118 Jalali = 10,
119#endif
120#if QT_CONFIG(islamiccivilcalendar) // type="islamic-civil", uses data from type="islamic"
121 IslamicCivil = 11,
122 // tabular, civil epoch
123 // 30 year cycle, leap on 2, 5, 7, 10, 13, 16, 18, 21, 24, 26 and 29
124 // (Other variants: 2, 5, 8, (10|11), 13, 16, 19, 21, 24, 27 and 29.)
125#endif
126
127 Last = 11, // Highest number of any above
128 User = -1
129 };
130 // New entries must be added to the \enum doc in qcalendar.cpp and
131 // handled in QCalendarBackend::fromEnum()
132 Q_ENUM(System)
133
134 explicit QCalendar(); // Gregorian, optimised
135 explicit QCalendar(System system);
136 explicit QCalendar(QLatin1String name);
137 explicit QCalendar(QStringView name);
138
139 // QCalendar is a trivially copyable value type.
140 bool isValid() const { return d != nullptr; }
141
142 // Date queries:
143 int daysInMonth(int month, int year = Unspecified) const;
144 int daysInYear(int year) const;
145 int monthsInYear(int year) const;
146 bool isDateValid(int year, int month, int day) const;
147
148 // Leap years:
149 bool isLeapYear(int year) const;
150
151 // Properties of the calendar:
152 bool isGregorian() const;
153 bool isLunar() const;
154 bool isLuniSolar() const;
155 bool isSolar() const;
156 bool isProleptic() const;
157 bool hasYearZero() const;
158 int maximumDaysInMonth() const;
159 int minimumDaysInMonth() const;
160 int maximumMonthsInYear() const;
161 QString name() const;
162
163 // QDate conversions:
164 QDate dateFromParts(int year, int month, int day) const;
165 QDate dateFromParts(const YearMonthDay &parts) const;
166 YearMonthDay partsFromDate(QDate date) const;
167 int dayOfWeek(QDate date) const;
168
169 // Month and week-day names (as in QLocale):
170 QString monthName(const QLocale &locale, int month, int year = Unspecified,
171 QLocale::FormatType format=QLocale::LongFormat) const;
172 QString standaloneMonthName(const QLocale &locale, int month, int year = Unspecified,
173 QLocale::FormatType format = QLocale::LongFormat) const;
174 QString weekDayName(const QLocale &locale, int day,
175 QLocale::FormatType format = QLocale::LongFormat) const;
176 QString standaloneWeekDayName(const QLocale &locale, int day,
177 QLocale::FormatType format=QLocale::LongFormat) const;
178
179 // Formatting of date-times:
180 QString dateTimeToString(QStringView format, const QDateTime &datetime,
181 QDate dateOnly, QTime timeOnly,
182 const QLocale &locale) const;
183
184 // What's available ?
185 static QStringList availableCalendars();
186private:
187 // Always supplied by QCalendarBackend and expected to be a singleton
188 const QCalendarBackend *d;
189};
190
191QT_END_NAMESPACE
192
193#endif // QCALENDAR_H
194