Merge remote-tracking branch 'origin/master' into api_changes
[profile/ivi/qtbase.git] / src / corelib / tools / qdatetime_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
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 : public QSharedData
80 {
81 public:
82     enum Spec { LocalUnknown = -1, LocalStandard = 0, LocalDST = 1, UTC = 2, OffsetFromUTC = 3};
83
84     QDateTimePrivate() : spec(LocalUnknown), utcOffset(0) {}
85     QDateTimePrivate(const QDateTimePrivate &other)
86         : QSharedData(other), date(other.date), time(other.time), spec(other.spec), utcOffset(other.utcOffset)
87     {}
88
89     QDate date;
90     QTime time;
91     Spec spec;
92     /*!
93       \internal
94       \since 4.4
95
96       The offset in seconds. Applies only when timeSpec() is OffsetFromUTC.
97      */
98     int utcOffset;
99
100     Spec getLocal(QDate &outDate, QTime &outTime) const;
101     void getUTC(QDate &outDate, QTime &outTime) const;
102     static QDateTime addMSecs(const QDateTime &dt, qint64 msecs);
103     static void addMSecs(QDate &utcDate, QTime &utcTime, qint64 msecs);
104 };
105
106 #ifndef QT_BOOTSTRAPPED
107
108 class Q_CORE_EXPORT QDateTimeParser
109 {
110 public:
111     enum Context {
112         FromString,
113         DateTimeEdit
114     };
115     QDateTimeParser(QVariant::Type t, Context ctx)
116         : currentSectionIndex(-1), display(0), cachedDay(-1), parserType(t),
117         fixday(false), spec(Qt::LocalTime), context(ctx)
118     {
119         defaultLocale = QLocale::system();
120         first.type = FirstSection;
121         first.pos = -1;
122         first.count = -1;
123         last.type = FirstSection;
124         last.pos = -1;
125         last.count = -1;
126         none.type = NoSection;
127         none.pos = -1;
128         none.count = -1;
129     }
130     virtual ~QDateTimeParser() {}
131     enum {
132         Neither = -1,
133         AM = 0,
134         PM = 1,
135         PossibleAM = 2,
136         PossiblePM = 3,
137         PossibleBoth = 4
138     };
139
140     enum Section {
141         NoSection = 0x00000,
142         AmPmSection = 0x00001,
143         MSecSection = 0x00002,
144         SecondSection = 0x00004,
145         MinuteSection = 0x00008,
146         Hour12Section   = 0x00010,
147         Hour24Section   = 0x00020,
148         TimeSectionMask = (AmPmSection|MSecSection|SecondSection|MinuteSection|Hour12Section|Hour24Section),
149         Internal = 0x10000,
150         DaySection = 0x00100,
151         MonthSection = 0x00200,
152         YearSection = 0x00400,
153         YearSection2Digits = 0x00800,
154         DayOfWeekSection = 0x01000,
155         DateSectionMask = (DaySection|MonthSection|YearSection|YearSection2Digits|DayOfWeekSection),
156         FirstSection = 0x02000|Internal,
157         LastSection = 0x04000|Internal,
158         CalendarPopupSection = 0x08000|Internal,
159
160         NoSectionIndex = -1,
161         FirstSectionIndex = -2,
162         LastSectionIndex = -3,
163         CalendarPopupIndex = -4
164     }; // duplicated from qdatetimeedit.h
165     Q_DECLARE_FLAGS(Sections, Section)
166
167     struct SectionNode {
168         Section type;
169         mutable int pos;
170         int count;
171     };
172
173     enum State { // duplicated from QValidator
174         Invalid,
175         Intermediate,
176         Acceptable
177     };
178
179     struct StateNode {
180         StateNode() : state(Invalid), conflicts(false) {}
181         QString input;
182         State state;
183         bool conflicts;
184         QDateTime value;
185     };
186
187     enum AmPm {
188         AmText,
189         PmText
190     };
191
192     enum Case {
193         UpperCase,
194         LowerCase
195     };
196
197 #ifndef QT_NO_DATESTRING
198     StateNode parse(QString &input, int &cursorPosition, const QDateTime &currentValue, bool fixup) const;
199 #endif
200     int sectionMaxSize(int index) const;
201     int sectionSize(int index) const;
202     int sectionMaxSize(Section s, int count) const;
203     int sectionPos(int index) const;
204     int sectionPos(const SectionNode &sn) const;
205
206     const SectionNode &sectionNode(int index) const;
207     Section sectionType(int index) const;
208     QString sectionText(int sectionIndex) const;
209     QString sectionText(const QString &text, int sectionIndex, int index) const;
210     int getDigit(const QDateTime &dt, int index) const;
211     bool setDigit(QDateTime &t, int index, int newval) const;
212     int parseSection(const QDateTime &currentValue, int sectionIndex, QString &txt, int &cursorPosition,
213                      int index, QDateTimeParser::State &state, int *used = 0) const;
214     int absoluteMax(int index, const QDateTime &value = QDateTime()) const;
215     int absoluteMin(int index) const;
216     bool parseFormat(const QString &format);
217 #ifndef QT_NO_DATESTRING
218     bool fromString(const QString &text, QDate *date, QTime *time) const;
219 #endif
220
221 #ifndef QT_NO_TEXTDATE
222     int findMonth(const QString &str1, int monthstart, int sectionIndex,
223                   QString *monthName = 0, int *used = 0) const;
224     int findDay(const QString &str1, int intDaystart, int sectionIndex,
225                 QString *dayName = 0, int *used = 0) const;
226 #endif
227     int findAmPm(QString &str1, int index, int *used = 0) const;
228     int maxChange(int s) const;
229     bool potentialValue(const QString &str, int min, int max, int index,
230                         const QDateTime &currentValue, int insert) const;
231     bool skipToNextSection(int section, const QDateTime &current, const QString &sectionText) const;
232     QString sectionName(int s) const;
233     QString stateName(int s) const;
234
235     QString sectionFormat(int index) const;
236     QString sectionFormat(Section s, int count) const;
237
238     enum FieldInfoFlag {
239         Numeric = 0x01,
240         FixedWidth = 0x02,
241         AllowPartial = 0x04,
242         Fraction = 0x08
243     };
244     Q_DECLARE_FLAGS(FieldInfo, FieldInfoFlag)
245
246     FieldInfo fieldInfo(int index) const;
247
248     virtual QDateTime getMinimum() const;
249     virtual QDateTime getMaximum() const;
250     virtual int cursorPosition() const { return -1; }
251     virtual QString displayText() const { return text; }
252     virtual QString getAmPmText(AmPm ap, Case cs) const;
253     virtual QLocale locale() const { return defaultLocale; }
254
255     mutable int currentSectionIndex;
256     Sections display;
257     mutable int cachedDay;
258     mutable QString text;
259     QVector<SectionNode> sectionNodes;
260     SectionNode first, last, none, popup;
261     QStringList separators;
262     QString displayFormat;
263     QLocale defaultLocale;
264     QVariant::Type parserType;
265
266     bool fixday;
267
268     Qt::TimeSpec spec; // spec if used by QDateTimeEdit
269     Context context;
270 };
271
272 Q_CORE_EXPORT bool operator==(const QDateTimeParser::SectionNode &s1, const QDateTimeParser::SectionNode &s2);
273
274 Q_DECLARE_OPERATORS_FOR_FLAGS(QDateTimeParser::Sections)
275 Q_DECLARE_OPERATORS_FOR_FLAGS(QDateTimeParser::FieldInfo)
276
277 #endif // QT_BOOTSTRAPPED
278
279 QT_END_NAMESPACE
280
281 #endif // QDATETIME_P_H