1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QDATETIME_P_H
43 #define QDATETIME_P_H
44 
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55 
56 #include "qplatformdefs.h"
57 #include "QtCore/qatomic.h"
58 #include "QtCore/qdatetime.h"
59 #include "QtCore/qstringlist.h"
60 #include "QtCore/qlocale.h"
61 #ifndef QT_BOOTSTRAPPED
62 # include "QtCore/qvariant.h"
63 #endif
64 #include "QtCore/qvector.h"
65 
66 
67 #define QDATETIMEEDIT_TIME_MIN QTime(0, 0, 0, 0)
68 #define QDATETIMEEDIT_TIME_MAX QTime(23, 59, 59, 999)
69 #define QDATETIMEEDIT_DATE_MIN QDate(100, 1, 1)
70 #define QDATETIMEEDIT_COMPAT_DATE_MIN QDate(1752, 9, 14)
71 #define QDATETIMEEDIT_DATE_MAX QDate(7999, 12, 31)
72 #define QDATETIMEEDIT_DATETIME_MIN QDateTime(QDATETIMEEDIT_DATE_MIN, QDATETIMEEDIT_TIME_MIN)
73 #define QDATETIMEEDIT_COMPAT_DATETIME_MIN QDateTime(QDATETIMEEDIT_COMPAT_DATE_MIN, QDATETIMEEDIT_TIME_MIN)
74 #define QDATETIMEEDIT_DATETIME_MAX QDateTime(QDATETIMEEDIT_DATE_MAX, QDATETIMEEDIT_TIME_MAX)
75 #define QDATETIMEEDIT_DATE_INITIAL QDate(2000, 1, 1)
76 
77 QT_BEGIN_NAMESPACE
78 
79 class QDateTimePrivate
80 {
81 public:
82     enum Spec { LocalUnknown = -1, LocalStandard = 0, LocalDST = 1, UTC = 2, OffsetFromUTC = 3};
83 
QDateTimePrivate()84     QDateTimePrivate() : spec(LocalUnknown), utcOffset(0) {}
QDateTimePrivate(const QDateTimePrivate & other)85     QDateTimePrivate(const QDateTimePrivate &other)
86         : date(other.date), time(other.time), spec(other.spec), utcOffset(other.utcOffset)
87     {}
88 
89     QAtomicInt ref;
90     QDate date;
91     QTime time;
92     Spec spec;
93     /*!
94       \internal
95       \since 4.4
96 
97       The offset in seconds. Applies only when timeSpec() is OffsetFromUTC.
98      */
99     int utcOffset;
100 
101     Spec getLocal(QDate &outDate, QTime &outTime) const;
102     void getUTC(QDate &outDate, QTime &outTime) const;
103     static QDateTime addMSecs(const QDateTime &dt, qint64 msecs);
104     static void addMSecs(QDate &utcDate, QTime &utcTime, qint64 msecs);
105 };
106 
107 #ifndef QT_BOOTSTRAPPED
108 
109 class Q_CORE_EXPORT QDateTimeParser
110 {
111 public:
112     enum Context {
113         FromString,
114         DateTimeEdit
115     };
QDateTimeParser(QVariant::Type t,Context ctx)116     QDateTimeParser(QVariant::Type t, Context ctx)
117         : currentSectionIndex(-1), display(0), cachedDay(-1), parserType(t),
118         fixday(false), spec(Qt::LocalTime), context(ctx)
119     {
120         defaultLocale = QLocale::system();
121         first.type = FirstSection;
122         first.pos = -1;
123         first.count = -1;
124         last.type = FirstSection;
125         last.pos = -1;
126         last.count = -1;
127         none.type = NoSection;
128         none.pos = -1;
129         none.count = -1;
130     }
~QDateTimeParser()131     virtual ~QDateTimeParser() {}
132     enum {
133         Neither = -1,
134         AM = 0,
135         PM = 1,
136         PossibleAM = 2,
137         PossiblePM = 3,
138         PossibleBoth = 4
139     };
140 
141     enum Section {
142         NoSection = 0x00000,
143         AmPmSection = 0x00001,
144         MSecSection = 0x00002,
145         SecondSection = 0x00004,
146         MinuteSection = 0x00008,
147         Hour12Section   = 0x00010,
148         Hour24Section   = 0x00020,
149         TimeSectionMask = (AmPmSection|MSecSection|SecondSection|MinuteSection|Hour12Section|Hour24Section),
150         Internal = 0x10000,
151         DaySection = 0x00100,
152         MonthSection = 0x00200,
153         YearSection = 0x00400,
154         YearSection2Digits = 0x00800,
155         DayOfWeekSection = 0x01000,
156         DateSectionMask = (DaySection|MonthSection|YearSection|YearSection2Digits|DayOfWeekSection),
157         FirstSection = 0x02000|Internal,
158         LastSection = 0x04000|Internal,
159         CalendarPopupSection = 0x08000|Internal,
160 
161         NoSectionIndex = -1,
162         FirstSectionIndex = -2,
163         LastSectionIndex = -3,
164         CalendarPopupIndex = -4
165     }; // duplicated from qdatetimeedit.h
166     Q_DECLARE_FLAGS(Sections, Section)
167 
168     struct SectionNode {
169         Section type;
170         mutable int pos;
171         int count;
172     };
173 
174     enum State { // duplicated from QValidator
175         Invalid,
176         Intermediate,
177         Acceptable
178     };
179 
180     struct StateNode {
StateNodeStateNode181         StateNode() : state(Invalid), conflicts(false) {}
182         QString input;
183         State state;
184         bool conflicts;
185         QDateTime value;
186     };
187 
188     enum AmPm {
189         AmText,
190         PmText
191     };
192 
193     enum Case {
194         UpperCase,
195         LowerCase
196     };
197 
198 #ifndef QT_NO_DATESTRING
199     StateNode parse(QString &input, int &cursorPosition, const QDateTime &currentValue, bool fixup) const;
200 #endif
201     int sectionMaxSize(int index) const;
202     int sectionSize(int index) const;
203     int sectionMaxSize(Section s, int count) const;
204     int sectionPos(int index) const;
205     int sectionPos(const SectionNode &sn) const;
206 
207     const SectionNode &sectionNode(int index) const;
208     Section sectionType(int index) const;
209     QString sectionText(int sectionIndex) const;
210     QString sectionText(const QString &text, int sectionIndex, int index) const;
211     int getDigit(const QDateTime &dt, int index) const;
212     bool setDigit(QDateTime &t, int index, int newval) const;
213     int parseSection(const QDateTime &currentValue, int sectionIndex, QString &txt, int &cursorPosition,
214                      int index, QDateTimeParser::State &state, int *used = 0) const;
215     int absoluteMax(int index, const QDateTime &value = QDateTime()) const;
216     int absoluteMin(int index) const;
217     bool parseFormat(const QString &format);
218 #ifndef QT_NO_DATESTRING
219     bool fromString(const QString &text, QDate *date, QTime *time) const;
220 #endif
221 
222 #ifndef QT_NO_TEXTDATE
223     int findMonth(const QString &str1, int monthstart, int sectionIndex,
224                   QString *monthName = 0, int *used = 0) const;
225     int findDay(const QString &str1, int intDaystart, int sectionIndex,
226                 QString *dayName = 0, int *used = 0) const;
227 #endif
228     int findAmPm(QString &str1, int index, int *used = 0) const;
229     int maxChange(int s) const;
230     bool potentialValue(const QString &str, int min, int max, int index,
231                         const QDateTime &currentValue, int insert) const;
232     bool skipToNextSection(int section, const QDateTime &current, const QString &sectionText) const;
233     QString sectionName(int s) const;
234     QString stateName(int s) const;
235 
236     QString sectionFormat(int index) const;
237     QString sectionFormat(Section s, int count) const;
238 
239     enum FieldInfoFlag {
240         Numeric = 0x01,
241         FixedWidth = 0x02,
242         AllowPartial = 0x04,
243         Fraction = 0x08
244     };
245     Q_DECLARE_FLAGS(FieldInfo, FieldInfoFlag)
246 
247     FieldInfo fieldInfo(int index) const;
248 
249     virtual QDateTime getMinimum() const;
250     virtual QDateTime getMaximum() const;
cursorPosition()251     virtual int cursorPosition() const { return -1; }
displayText()252     virtual QString displayText() const { return text; }
253     virtual QString getAmPmText(AmPm ap, Case cs) const;
locale()254     virtual QLocale locale() const { return defaultLocale; }
255 
256     mutable int currentSectionIndex;
257     Sections display;
258     mutable int cachedDay;
259     mutable QString text;
260     QVector<SectionNode> sectionNodes;
261     SectionNode first, last, none, popup;
262     QStringList separators;
263     QString displayFormat;
264     QLocale defaultLocale;
265     QVariant::Type parserType;
266 
267     bool fixday;
268 
269     Qt::TimeSpec spec; // spec if used by QDateTimeEdit
270     Context context;
271 };
272 
273 Q_CORE_EXPORT bool operator==(const QDateTimeParser::SectionNode &s1, const QDateTimeParser::SectionNode &s2);
274 
275 Q_DECLARE_OPERATORS_FOR_FLAGS(QDateTimeParser::Sections)
276 Q_DECLARE_OPERATORS_FOR_FLAGS(QDateTimeParser::FieldInfo)
277 
278 #ifdef Q_OS_SYMBIAN
279 QDateTime qt_symbian_TTime_To_QDateTime(const TTime& time);
280 TTime qt_symbian_QDateTime_To_TTime(const QDateTime& datetime);
281 time_t qt_symbian_TTime_To_time_t(const TTime& time);
282 TTime qt_symbian_time_t_To_TTime(time_t time);
283 #endif //Q_OS_SYMBIAN
284 
285 #endif // QT_BOOTSTRAPPED
286 
287 QT_END_NAMESPACE
288 
289 #endif // QDATETIME_P_H
290