1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtXmlPatterns 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//
41// W A R N I N G
42// -------------
43//
44// This file is not part of the Qt API. It exists purely as an
45// implementation detail. This header file may change from version to
46// version without notice, or even be removed.
47//
48// We mean it.
49
50#ifndef Patternist_AbstractDateTime_H
51#define Patternist_AbstractDateTime_H
52
53#include <QDateTime>
54#include <QRegExp>
55
56#include <private/qitem_p.h>
57
58QT_BEGIN_NAMESPACE
59
60namespace QPatternist
61{
62 /**
63 * @short Base class for classes implementing values related to time, date or both.
64 *
65 * @see <a href="http://www.w3.org/TR/xmlschema-2/#dateTime">XML Schema
66 * Part 2: Datatypes Second Edition, 3.2.7 dateTime</a>
67 * @see <a href="http://www.w3.org/TR/xpath-datamodel/#dates-and-times">XQuery
68 * 1.0 and XPath 2.0 Data Model (XDM), 3.3.2 Dates and Times</a>
69 * @see <a href="http://www.cl.cam.ac.uk/~mgk25/iso-time.html">A summary of
70 * the international standard date and time notation, Markus Kuhn</a>
71 * @see <a href="http://en.wikipedia.org/wiki/Iso_date">ISO 8601,
72 * From Wikipedia, the free encyclopedia</a>
73 * @author Frans Englich <frans.englich@nokia.com>
74 * @ingroup Patternist_xdm
75 */
76 class AbstractDateTime : public AtomicValue
77 {
78 public:
79 typedef QExplicitlySharedDataPointer<AbstractDateTime> Ptr;
80
81 AbstractDateTime(const QDateTime &dateTime);
82
83 enum
84 {
85 DefaultYear = 2000,
86 DefaultMonth = 1,
87 DefaultDay = 1
88 };
89
90 /**
91 * @returns the date time this class represents, as a QDateTime.
92 */
93 inline const QDateTime &toDateTime() const
94 {
95 return m_dateTime;
96 }
97
98
99 /**
100 * @short Acts as a mapping table for AbstractDateTime::create()
101 * and describes where certain fields in a QRegExp pattern can be found
102 * for a particular W3C XML Schema date/time type.
103 *
104 * @author Frans Englich <frans.englich@nokia.com>
105 * @ingroup Patternist_xdm
106 */
107 class CaptureTable
108 {
109 public:
110 CaptureTable(const QRegExp &exp,
111 const qint8 zoneOffsetSignP,
112 const qint8 zoneOffsetHourP,
113 const qint8 zoneOffsetMinuteP,
114 const qint8 zoneOffsetUTCSymbolP,
115 const qint8 yearP,
116 const qint8 monthP = -1,
117 const qint8 dayP = -1,
118 const qint8 hourP = -1,
119 const qint8 minutesP = -1,
120 const qint8 secondsP = -1,
121 const qint8 msecondsP = -1,
122 const qint8 yearSignP = -1) : regExp(exp)
123 , zoneOffsetSign(zoneOffsetSignP)
124 , zoneOffsetHour(zoneOffsetHourP)
125 , zoneOffsetMinute(zoneOffsetMinuteP)
126 , zoneOffsetUTCSymbol(zoneOffsetUTCSymbolP)
127 , year(yearP)
128 , month(monthP)
129 , day(dayP)
130 , hour(hourP)
131 , minutes(minutesP)
132 , seconds(secondsP)
133 , mseconds(msecondsP)
134 , yearSign(yearSignP)
135 {
136 Q_ASSERT(exp.isValid());
137 }
138
139 QRegExp regExp;
140 const qint8 zoneOffsetSign;
141 const qint8 zoneOffsetHour;
142 const qint8 zoneOffsetMinute;
143 const qint8 zoneOffsetUTCSymbol;
144 const qint8 year;
145 const qint8 month;
146 const qint8 day;
147 const qint8 hour;
148 const qint8 minutes;
149 const qint8 seconds;
150 const qint8 mseconds;
151 const qint8 yearSign;
152
153 private:
154 Q_DISABLE_COPY(CaptureTable)
155 };
156
157 /**
158 * @returns m_dateTime's time part converted to string. This is for
159 * example "12" or "01.023".
160 */
161 QString timeToString() const;
162
163 /**
164 * @returns m_dateTime's date part converted to string. This is for
165 * example "2004-05-12" or "-2004-05-12".
166 */
167 QString dateToString() const;
168
169 /**
170 * Serializes the milli seconds @p msecs into a string representation. For
171 * example, if @p msecs is 1, ".001" is returned; if @p msecs is 100 then
172 * is ".1" returned.
173 */
174 static QString serializeMSeconds(const MSecondProperty msecs);
175
176 /**
177 * A factory function for creating instances that are of the dynamic
178 * type of this class, that represents @p dt.
179 *
180 * The default implementation performs an assert() call. This function
181 * is not pure virtual because all sub-classes do not use it.
182 */
183 virtual Item fromValue(const QDateTime &dt) const;
184
185 /**
186 * Determines whether @p dt is a date-time that can be represented,
187 * and isn't too early or too late. If it is valid, @c true is returned. Otherwise,
188 * @c false is returned and @p message is set to contain a translated message for
189 * human consumption, describing the error.
190 */
191 static bool isRangeValid(const QDate &date,
192 QString &message);
193
194 protected:
195
196 /**
197 * @returns m_dateTime' zone offset converted to string, as per the
198 * the W3C XML Schema types. This is for
199 * example "Z" or "+12.00"(depending on m_dateTime).
200 */
201 QString zoneOffsetToString() const;
202
203 static QDateTime create(AtomicValue::Ptr &errorMessage,
204 const QString &lexicalSource,
205 const CaptureTable &captTable);
206
207 /**
208 * @short Makes the QDateTime::timeSpec() and QDateTime::zoneOffset()
209 * of @p ot * consistent to @p from.
210 */
211 static void copyTimeSpec(const QDateTime &from,
212 QDateTime &to);
213
214 const QDateTime m_dateTime;
215
216 private:
217 enum ZoneOffsetParseResult
218 {
219 /**
220 * syntax or logical error was encountered.
221 */
222 Error,
223 /**
224 * It's a valid offset from UTC.
225 */
226 Offset,
227
228 /**
229 * No zone offset was specified, it's an implementation defined zone offset.
230 */
231 LocalTime,
232 UTC
233 };
234
235 /**
236 * @short Parses the zone offset. All types use zone offsets.
237 *
238 * If result is set to Offset, the offset is returned, otherwise
239 * the return value is undefined.
240 *
241 * The offset is in seconds.
242 */
243 static ZOTotal parseZoneOffset(ZoneOffsetParseResult &result,
244 const QStringList &capts,
245 const CaptureTable &captTable);
246
247 static inline void setUtcOffset(QDateTime &result,
248 const ZoneOffsetParseResult zoResult,
249 const int zoOffset);
250 };
251}
252
253QT_END_NAMESPACE
254
255#endif
256

source code of qtxmlpatterns/src/xmlpatterns/data/qabstractdatetime_p.h