Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qttemporalpattern_p.h
Go to the documentation of this file.
1// Copyright (C) 2026 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// Qt-Security score:significant reason:default
4
5#ifndef QTTEMPORALPATTERN_P_H
6#define QTTEMPORALPATTERN_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an implementation
13// detail. This header file may change from version to version without notice,
14// or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/qcalendar.h>
20#include <QtCore/qdatetime.h>
21#include <QtCore/qflags.h>
22#include <QtCore/qlist.h>
23#include <QtCore/qlocale.h>
24#include <QtCore/qmetaobject.h>
25#include <QtCore/qspan.h>
26#include <QtCore/qstring.h>
27#include <QtCore/qstringview.h>
28
29#include <optional>
30
32QT_BEGIN_NAMESPACE
33
34namespace QtTemporalPattern {
35
36 // Model the LDML spec's range of possible fields, see QTBUG-70516 and
37 // https://www.unicode.org/reports/tr35/tr35-dates.html#table-date-field-symbol-table
40
41 // Special:
43 // RFC 9557 annotations shall need at least one category code.
44
45 // Time:
46 // MillisecondInDay = 17,
48 Minute = 24, // MinuteFraction = 25,
49 HourMod12 = 30, Hour = 31, // HourFraction = 32,
50 PeriodInDay = 33, // am/pm; LDML also has noon, midnight, "at night" and others.
51
52 // Date:
53 DayOfWeek = 64, DayOfMonth = 65, // DayOfYear = 68, JulianDay = 69,
54 // WeekOfMonth = 72,
55 // WeekOfYear = 73, // QTBUG-57212, QTBUG-83678, QTBUG-117055
56 Month = 80,
57 // Quarter = 84,
58 // RelatedGregorianYear = 90, Century = 91,
60 // Era = 100,
61
63 };
64 constexpr inline
66
68 // This fits in 32-bits for now, but with only 6 to spare; if it is ever
69 // to be made public it may be worth expanding it to 64 bits first.
70 Numeric = 1, Verbal = 2, Standalone = 4,
71 Narrow = 0x10, Abbreviated = 0x20, Short = 0x40, Wide = 0x80,
72 ZeroPad = 0x0100, // For TimeZone, ZeroPad applies only to hour fields.
73 SpacePad = 0x0200, FlexSpace = 0x0400, StrictSpace = 0x0800,
74 // Qt used to impose case on am/pm fields:
75 LowerCase = 0x1000, UpperCase = 0x2000, // Otherwise follow locale-supplied case.
76 IgnoreCase = 0x4000, // Parsing Literal or Verbal: match case insensitively
77 // (0x8000 free)
78 // Special cases:
79 RoundFraction = 0x01'0000, // Fractional part accepts extra digits, rounding suitably
80 // (0x02'0000, 0x04'0000 free)
81 YearSignIso8601 = 0x08'0000, // Require sign when excess digits
82 // Zone-specific:
83 LocalizedZone = 0x10'0000, // Localized forms, various.
84 Iso8601 = 0x20'0000, // Non-localized standard offset forms.
85 AcceptUtcPrefix = 0x40'0000, NeedNoUtcPrefix = 0x80'0000, // On offsets
86 AllowZSuffix = 0x0100'0000, // Iso8601 zero-offset may be indicated by Z.
87 // Time-types (if none specified, infer time type from date/time fields):
88 GenericTime = 0x0200'0000, StandardTime = 0x0400'0000, DaylightSavingTime = 0x0800'0000,
89 // (0x1000'0000, 0x2000'0000, 0x4000'0000 free)
90 // RFC 9557 may need some flags.
91 // Local time from system info:
92 LocalTimeName = 0x8000'0000 // That's the last bit available to us.
93 };
96
97 namespace FieldGroup {
111 }
112
113 // Implement group-related tests:
114 constexpr inline
122
123 constexpr inline
131
132 // Classify the categories:
133 enum class DateTimePart { None, Date = 1, Time = 2, Zone = 4 };
136#ifdef QT_BUILD_INTERNAL
140#endif // For testing.
141
143 {
145 return DateTimePart::None;
146 if (quint8(category) < 16)
147 return DateTimePart::Zone;
148 if (quint8(category) < 64) // a.k.a. 0x40
149 return DateTimePart::Time;
150 return DateTimePart::Date;
151 }
152
153 // Combine with related information:
155 QString literal; // Only relevant to Literal
156 qsizetype width; // Lower bound, usually only relevant to Numeric.
159 DateTimePart part() const noexcept { return classify(category); }
160 friend bool comparesEqual(const TemporalField &lhs, const TemporalField &rhs) noexcept
161 {
162 return lhs.literal == rhs.literal
163 && lhs.width == rhs.width
164 && lhs.options == rhs.options
165 && lhs.category == rhs.category;
166 }
168 };
169
170 inline DateTimeParts hasFieldsFor(QSpan<const TemporalField> range)
171 {
172 DateTimeParts result = DateTimePart::None;
173 for (const TemporalField &it : range)
174 result |= it.part();
175 return result;
176 }
177
178 enum class SupportType { Partial = -1, None = 0, Clear = 1, HasStrays };
179 Q_CORE_EXPORT
181 bool hasBaseYear = false) noexcept;
182} // namespace QtTemporalPattern
183
184/* We may eventually want to make the following public API (along with a
185 forward-declaration of QtTemporalPattern::TemporalField), while leaving the
186 above in a private header. See qttemporalpattern.cpp's "Future Directions"
187 comment for design-related constraints on that.
188
189 This may, of course, involve splitting the types below into a public facade
190 with the actual data members hidden from view behind a shared d-pointer to a
191 private internal class, in the usual pimpl way.
192
193 Please bear that in mind when considering the design of the APIs below, and
194 any future changes thereto.
195*/
196
197namespace QtTemporalPattern {
198template <typename Payload>
200{
201 Payload payload;
203};
204} // namespace QtTemporalPattern
205
207{
209 QList<Field> m_fields;
210 QLocale m_locale;
211 QCalendar m_calendar;
212 std::optional<int> m_baseYear;
213 explicit QDateTimePattern(const QList<Field> &fs) : m_fields(fs) {}
214 explicit QDateTimePattern(const QList<Field> &fs, int centuryStart)
215 : m_fields(fs), m_baseYear(centuryStart) {}
216
217 QtTemporalPattern::SupportType dateTimeSupport() const noexcept
218 {
219 using namespace QtTemporalPattern;
220 constexpr DateTimeParts NeededParts = DateTimePart::Date | DateTimePart::Time;
221 const DateTimeParts got = hasFieldsFor(m_fields);
222 // Must have date and time (zone optional) and support what it has:
223 if (got.testFlags(NeededParts))
224 return supports(got, m_fields, m_baseYear.has_value());
226 }
227public:
228 bool isNull() const noexcept { return m_fields.isEmpty(); }
229 bool isValid() const noexcept
230 {
231 return dateTimeSupport() == QtTemporalPattern::SupportType::Clear;
232 }
233 void setLocale(const QLocale &loc) { m_locale = loc; }
234 const QLocale &locale() const noexcept { return m_locale; }
235
236 void setBaseYear(int centuryStart) { m_baseYear = centuryStart; }
237 void clearBaseYear() noexcept { m_baseYear = std::nullopt; }
238 std::optional<int> baseYear() const noexcept { return m_baseYear; }
239
240 void setCalendar(QCalendar cal) { m_calendar = cal; }
241 QCalendar calendar() const noexcept { return m_calendar; }
242
244 parse(QStringView text, const QDateTime &defaults = {}) const;
245 Q_CORE_EXPORT QString serialize(const QDateTime &datetime) const;
246
247 static Q_CORE_EXPORT QDateTimePattern fromQtFormat(QStringView format);
248 static
249 QDateTimePattern forLocale(const QLocale &locale,
250 QLocale::FormatType format = QLocale::LongFormat)
251 {
252 auto pat = fromQtFormat(locale.dateTimeFormat(format));
253 pat.setLocale(locale);
254 return pat;
255 }
256};
257
259{
261 QList<Field> m_fields;
262 QLocale m_locale;
263 explicit QTimePattern(const QList<Field> &fs) : m_fields(fs) {}
264
265 QtTemporalPattern::SupportType timeSupport() const noexcept
266 {
267 using namespace QtTemporalPattern;
268 return supports({DateTimePart::Time}, m_fields);
269 }
270public:
271 bool isNull() const noexcept { return m_fields.isEmpty(); }
272 bool isValid() const noexcept
273 {
274 return timeSupport() == QtTemporalPattern::SupportType::Clear;
275 }
276 void setLocale(const QLocale &loc) { m_locale = loc; }
277 const QLocale &locale() const noexcept { return m_locale; }
278
280 parse(QStringView text, QTime defaults = {}) const;
281 Q_CORE_EXPORT QString serialize(const QTime &time) const;
282
283 static Q_CORE_EXPORT QTimePattern fromQtFormat(QStringView format);
284 static
285 QTimePattern forLocale(const QLocale &locale, QLocale::FormatType format = QLocale::LongFormat)
286 {
287 auto pat = fromQtFormat(locale.timeFormat(format));
288 pat.setLocale(locale);
289 return pat;
290 }
291};
292
294{
296 QList<Field> m_fields;
297 QLocale m_locale;
298 QCalendar m_calendar;
299 std::optional<int> m_baseYear;
300 explicit QDatePattern(const QList<Field> &fs) : m_fields(fs) {}
301 explicit QDatePattern(const QList<Field> &fs, int centuryStart)
302 : m_fields(fs), m_baseYear(centuryStart) {}
303
304 QtTemporalPattern::SupportType dateSupport() const noexcept
305 {
306 using namespace QtTemporalPattern;
307 return supports({DateTimePart::Date}, m_fields, m_baseYear.has_value());
308 }
309public:
310 bool isNull() const noexcept { return m_fields.isEmpty(); }
311 bool isValid() const noexcept
312 {
313 return dateSupport() == QtTemporalPattern::SupportType::Clear;
314 }
315 void setLocale(const QLocale &loc) { m_locale = loc; }
316 const QLocale &locale() const noexcept { return m_locale; }
317
318 void setBaseYear(int centuryStart) { m_baseYear = centuryStart; }
319 void clearBaseYear() noexcept { m_baseYear = std::nullopt; }
320 std::optional<int> baseYear() const noexcept { return m_baseYear; }
321
322 void setCalendar(QCalendar cal) { m_calendar = cal; }
323 QCalendar calendar() const noexcept { return m_calendar; }
324
326 parse(QStringView text, QDate defaults = {}) const;
327 Q_CORE_EXPORT QString serialize(const QDate &date) const;
328
329 static Q_CORE_EXPORT QDatePattern fromQtFormat(QStringView format);
330 static
331 QDatePattern forLocale(const QLocale &locale, QLocale::FormatType format = QLocale::LongFormat)
332 {
333 auto pat = fromQtFormat(locale.dateFormat(format));
334 pat.setLocale(locale);
335 return pat;
336 }
337};
338
339QT_END_NAMESPACE
340
341#endif // QTTEMPORALPATTERN_P_H
A description of a serialization format for a date.
void setBaseYear(int centuryStart)
const QLocale & locale() const noexcept
Returns the current locale in use by this pattern.
QCalendar calendar() const noexcept
Returns the current calendar in use by this pattern.
bool isValid() const noexcept
bool isNull() const noexcept
Returns true if this pattern has no fields, otherwise false.
static QDatePattern forLocale(const QLocale &locale, QLocale::FormatType format=QLocale::LongFormat)
Construct a QDatePattern appropriate to the given locale.
void setCalendar(QCalendar cal)
void setLocale(const QLocale &loc)
void clearBaseYear() noexcept
std::optional< int > baseYear() const noexcept
A description of a serialization format for a datetime.
bool isValid() const noexcept
void clearBaseYear() noexcept
void setBaseYear(int centuryStart)
QCalendar calendar() const noexcept
Returns the current calendar in use by this pattern.
bool isNull() const noexcept
Returns true if this pattern has no fields, otherwise false.
void setCalendar(QCalendar cal)
static QDateTimePattern forLocale(const QLocale &locale, QLocale::FormatType format=QLocale::LongFormat)
Construct a QDateTimePattern appropriate to the given locale.
std::optional< int > baseYear() const noexcept
void setLocale(const QLocale &loc)
const QLocale & locale() const noexcept
Returns the current locale in use by this pattern.
A description of a serialization format for a time.
void setLocale(const QLocale &loc)
bool isValid() const noexcept
static QTimePattern forLocale(const QLocale &locale, QLocale::FormatType format=QLocale::LongFormat)
Construct a QTimePattern appropriate to the given locale.
bool isNull() const noexcept
Returns true if this pattern has no fields, otherwise false.
const QLocale & locale() const noexcept
Returns the current locale in use by this pattern.
Supporting types and functions for temporal patterns.
constexpr auto EndTemporalFieldCategories
DateTimeParts hasFieldsFor(QSpan< const TemporalField > range)
Identify the parts to which the given fields contribute data.
QT_REQUIRE_CONFIG(datestring)