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
qdatetimeparser_p.h
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#ifndef QDATETIMEPARSER_P_H
5#define QDATETIMEPARSER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/private/qglobal_p.h>
19#include "qplatformdefs.h"
20#include "QtCore/qatomic.h"
21#include "QtCore/qcalendar.h"
22#include "QtCore/qcoreapplication.h"
23#include "QtCore/qdatetime.h"
24#include "QtCore/qlist.h"
25#include "QtCore/qlocale.h"
26#include "QtCore/qstringlist.h"
27#ifndef QT_BOOTSTRAPPED
28# include "QtCore/qvariant.h"
29#endif
30
31QT_REQUIRE_CONFIG(datetimeparser);
32
33#define QDATETIMEEDIT_TIME_MIN QTime(0, 0) // Prefer QDate::startOfDay()
34#define QDATETIMEEDIT_TIME_MAX QTime(23, 59, 59, 999) // Prefer QDate::endOfDay()
35#define QDATETIMEEDIT_DATE_MIN QDate(100, 1, 1)
36#define QDATETIMEEDIT_COMPAT_DATE_MIN QDate(1752, 9, 14)
37#define QDATETIMEEDIT_DATE_MAX QDate(9999, 12, 31)
38#define QDATETIMEEDIT_DATE_INITIAL QDate(2000, 1, 1)
39
41
42class Q_CORE_EXPORT QDateTimeParser
43{
44public:
45 enum Context {
47 DateTimeEdit
48 };
50 : parserType(t), context(ctx), calendar(cal)
51 {
52 defaultLocale = QLocale::system();
53 first.type = FirstSection;
54 first.pos = -1;
55 first.count = -1;
56 first.zeroesAdded = 0;
57 last.type = LastSection;
58 last.pos = -1;
59 last.count = -1;
60 last.zeroesAdded = 0;
61 none.type = NoSection;
62 none.pos = -1;
63 none.count = -1;
64 none.zeroesAdded = 0;
65 }
66 virtual ~QDateTimeParser();
67
68 enum Section {
69 NoSection = 0x00000,
70 AmPmSection = 0x00001,
71 MSecSection = 0x00002,
72 SecondSection = 0x00004,
73 MinuteSection = 0x00008,
74 Hour12Section = 0x00010,
75 Hour24Section = 0x00020,
76 TimeZoneSection = 0x00040,
77 HourSectionMask = (Hour12Section | Hour24Section),
78 TimeSectionMask = (MSecSection | SecondSection | MinuteSection |
79 HourSectionMask | AmPmSection | TimeZoneSection),
80
81 DaySection = 0x00100,
82 MonthSection = 0x00200,
83 YearSection = 0x00400,
84 YearSection2Digits = 0x00800,
85 YearSectionMask = YearSection | YearSection2Digits,
86 DayOfWeekSectionShort = 0x01000,
87 DayOfWeekSectionLong = 0x02000,
88 DayOfWeekSectionMask = DayOfWeekSectionShort | DayOfWeekSectionLong,
89 DaySectionMask = DaySection | DayOfWeekSectionMask,
90 DateSectionMask = DaySectionMask | MonthSection | YearSectionMask,
91
92 Internal = 0x10000,
93 FirstSection = 0x20000 | Internal,
94 LastSection = 0x40000 | Internal,
95 CalendarPopupSection = 0x80000 | Internal,
96
97 NoSectionIndex = -1,
98 FirstSectionIndex = -2,
99 LastSectionIndex = -3,
100 CalendarPopupIndex = -4
101 }; // extending qdatetimeedit.h's equivalent
102 Q_DECLARE_FLAGS(Sections, Section)
103
104 struct Q_CORE_EXPORT SectionNode {
106 mutable int pos;
107 int count; // (used as Case(count) indicator for AmPmSection)
109
110 static QString name(Section s);
111 QString name() const { return name(type); }
112 QString format() const;
113 int maxChange() const;
114 };
115
116 enum State { // duplicated from QValidator
119 Acceptable
120 };
121
122 struct StateNode {
123 StateNode() : state(Invalid), padded(0), conflicts(false) {}
124 StateNode(const QDateTime &val, State ok=Acceptable, int pad=0, bool bad=false)
125 : value(val), state(ok), padded(pad), conflicts(bad) {}
130 };
131
132 enum AmPm {
134 PmText
135 };
136
137 StateNode parse(const QString &input, int position,
138 const QDateTime &defaultValue, bool fixup) const;
139 bool fromString(const QString &text, QDate *date, QTime *time,
140 int baseYear = QLocale::DefaultTwoDigitBaseYear) const;
141 bool fromString(const QString &text, QDateTime *datetime, int baseYear) const;
142 bool parseFormat(QStringView format);
143
145 Numeric = 0x01,
146 FixedWidth = 0x02,
147 AllowPartial = 0x04,
148 Fraction = 0x08
149 };
150 Q_DECLARE_FLAGS(FieldInfo, FieldInfoFlag)
151
152 FieldInfo fieldInfo(int index) const;
153
154 void setDefaultLocale(const QLocale &loc) { defaultLocale = loc; }
155 virtual QString displayText() const { return m_text; }
156 void setCalendar(QCalendar calendar);
157
158private:
159 int sectionMaxSize(Section s, int count) const;
160 QString sectionText(const QString &text, int sectionIndex, int index) const;
161 StateNode scanString(const QDateTime &defaultValue, bool fixup) const;
162 struct ParsedSection {
163 int value;
164 int used;
165 int zeroes;
166 State state;
167 constexpr ParsedSection(State ok = Invalid,
168 int val = 0, int read = 0, int zs = 0)
169 : value(ok == Invalid ? -1 : val), used(read), zeroes(zs), state(ok)
170 {}
171 };
172 ParsedSection parseSection(const QDateTime &currentValue, int sectionIndex, int offset) const;
173 int findMonth(QStringView str, int monthstart, int sectionIndex,
174 int year, QString *monthName = nullptr, int *used = nullptr) const;
175 int findDay(QStringView str, int intDaystart, int sectionIndex,
176 QString *dayName = nullptr, int *used = nullptr) const;
177 ParsedSection findUtcOffset(QStringView str, int mode) const;
178 ParsedSection findTimeZoneName(QStringView str, const QDateTime &when) const;
179 ParsedSection findTimeZone(QStringView str, const QDateTime &when,
180 int maxVal, int minVal, int mode) const;
181
182 enum AmPmFinder {
183 Neither = -1,
184 AM = 0,
185 PM = 1,
186 PossibleAM = 2,
187 PossiblePM = 3,
188 PossibleBoth = 4
189 };
190 AmPmFinder findAmPm(QString &str, int index, int *used = nullptr) const;
191
192 bool potentialValue(QStringView str, int min, int max, int index,
193 const QDateTime &currentValue, int insert) const;
194 bool potentialValue(const QString &str, int min, int max, int index,
195 const QDateTime &currentValue, int insert) const
196 {
197 return potentialValue(QStringView(str), min, max, index, currentValue, insert);
198 }
199
200 enum Case {
201 NativeCase,
202 LowerCase,
204 };
205
206 QString getAmPmText(AmPm ap, Case cs) const;
207 QDateTime baseDate(const QTimeZone &zone) const;
208
209 friend class QDTPUnitTestParser;
210
211protected: // for the benefit of QDateTimeEditPrivate
212 int sectionSize(int index) const;
213 int sectionMaxSize(int index) const;
214 int sectionPos(int index) const;
215 int sectionPos(SectionNode sn) const;
216
217 const SectionNode &sectionNode(int index) const;
218 Section sectionType(int index) const;
219 QString sectionText(int sectionIndex) const;
220 int getDigit(const QDateTime &dt, int index) const;
221 bool setDigit(QDateTime &t, int index, int newval) const;
222
223 int absoluteMax(int index, const QDateTime &value = QDateTime()) const;
224 int absoluteMin(int index) const;
225
226 bool skipToNextSection(int section, const QDateTime &current, QStringView sectionText) const;
227 bool skipToNextSection(int section, const QDateTime &current, const QString &sectionText) const
228 {
229 return skipToNextSection(section, current, QStringView(sectionText));
230 }
231 QString stateName(State s) const;
232
233 virtual QDateTime getMinimum() const;
234 virtual QDateTime getMaximum() const;
235 virtual int cursorPosition() const { return -1; }
236 virtual QLocale locale() const { return defaultLocale; }
237
238 mutable int currentSectionIndex = int(NoSectionIndex);
239 mutable int defaultCenturyStart = QLocale::DefaultTwoDigitBaseYear;
240 Sections display;
241 /*
242 This stores the most recently selected day.
243 It is useful when considering the following scenario:
244
245 1. Date is: 31/01/2000
246 2. User increments month: 29/02/2000
247 3. User increments month: 31/03/2000
248
249 At step 1, cachedDay stores 31. At step 2, the 31 is invalid for February, so the cachedDay is not updated.
250 At step 3, the month is changed to March, for which 31 is a valid day. Since 29 < 31, the day is set to cachedDay.
251 This is good for when users have selected their desired day and are scrolling up or down in the month or year section
252 and do not want smaller months (or non-leap years) to alter the day that they chose.
253 */
254 mutable int cachedDay = -1;
256 QList<SectionNode> sectionNodes;
257 SectionNode first, last, none, popup;
262 bool fixday = false;
265};
267
269
270Q_DECLARE_OPERATORS_FOR_FLAGS(QDateTimeParser::Sections)
271Q_DECLARE_OPERATORS_FOR_FLAGS(QDateTimeParser::FieldInfo)
272
274
275#endif // QDATETIME_P_H
The QCalendar class describes calendar systems.
Definition qcalendar.h:53
QDateTimeParser(QMetaType::Type t, Context ctx, QCalendar cal=QCalendar())
virtual QLocale locale() const
virtual QString displayText() const
virtual int cursorPosition() const
QList< SectionNode > sectionNodes
void setDefaultLocale(const QLocale &loc)
QMetaType::Type parserType
bool skipToNextSection(int section, const QDateTime &current, const QString &sectionText) const
\inmodule QtCore\reentrant
Definition qdatetime.h:283
\inmodule QtCore \reentrant
Definition qdatetime.h:29
static constexpr int DefaultTwoDigitBaseYear
Definition qlocale.h:39
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition qlocale.cpp:2862
Type
\macro Q_DECLARE_OPAQUE_POINTER(PointerType)
Definition qmetatype.h:345
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qtimezone.h:26
\inmodule QtCore \reentrant
Definition qdatetime.h:215
EGLContext ctx
QString str
[2]
QString text
QDate date
[1]
cache insert(employee->id(), employee)
else opt state
[0]
Combined button and popup list for selecting options.
static void * context
Q_CORE_EXPORT bool operator==(QDateTimeParser::SectionNode s1, QDateTimeParser::SectionNode s2)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define Q_DECLARE_FLAGS(Flags, Enum)
Definition qflags.h:174
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
Definition qflags.h:194
@ Invalid
GLenum mode
GLuint index
[2]
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
GLenum GLenum GLsizei count
GLenum type
GLenum GLuint GLintptr offset
GLuint name
GLint first
GLint GLsizei GLsizei GLenum format
GLdouble s
[6]
Definition qopenglext.h:235
GLuint GLfloat * val
GLdouble GLdouble t
Definition qopenglext.h:243
GLenum GLenum GLenum input
static bool fromString(const QMetaObject *mo, QString s, Allocate &&allocate)
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
#define s2
#define QT_REQUIRE_CONFIG(feature)
@ Q_PRIMITIVE_TYPE
Definition qtypeinfo.h:157
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:180
ReturnedValue read(const char *data)
QCalendarWidget * calendar
[0]
StateNode(const QDateTime &val, State ok=Acceptable, int pad=0, bool bad=false)