source: trunk/src/xmlpatterns/data/qabstractdatetime.cpp@ 284

Last change on this file since 284 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 12.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QStringList>
43
44#include "qbuiltintypes_p.h"
45#include "qitem_p.h"
46#include "qpatternistlocale_p.h"
47#include "qvalidationerror_p.h"
48
49#include "qabstractdatetime_p.h"
50
51QT_BEGIN_NAMESPACE
52
53using namespace QPatternist;
54
55AbstractDateTime::AbstractDateTime(const QDateTime &dateTime) : m_dateTime(dateTime)
56{
57 Q_ASSERT(dateTime.isValid());
58}
59
60#define badData(msg) errorMessage = ValidationError::createError(msg); return QDateTime()
61#define getCapt(sym) ((captTable.sym == -1) ? QString() : capts.at(captTable.sym))
62#define getSafeCapt(sym) ((captTable.sym == -1) ? QString() : capts.value(captTable.sym))
63
64QDateTime AbstractDateTime::create(AtomicValue::Ptr &errorMessage,
65 const QString &lexicalSource,
66 const CaptureTable &captTable)
67{
68 QRegExp myExp(captTable.regExp);
69
70 if(!myExp.exactMatch(lexicalSource))
71 {
72 badData(QString());
73 }
74
75 const QStringList capts(myExp.capturedTexts());
76 const QString yearStr(getCapt(year));
77
78 if(yearStr.size() > 4 && yearStr.at(0) == QLatin1Char('0'))
79 {
80 badData(QtXmlPatterns::tr("Year %1 is invalid because it begins with %2.")
81 .arg(formatData(yearStr)).arg(formatData("0")));
82 }
83
84 /* If the strings are empty, load default values which are
85 * guranteed to pass the validness tests. */
86 const QString monthStr(getCapt(month));
87 const QString dayStr(getCapt(day));
88 YearProperty year = yearStr.isEmpty() ? DefaultYear : yearStr.toInt();
89 if(getCapt(yearSign) == QChar::fromLatin1('-'))
90 year = -year;
91 const MonthProperty month = monthStr.isEmpty() ? DefaultMonth : monthStr.toInt();
92 const MonthProperty day = dayStr.isEmpty() ? DefaultDay : dayStr.toInt();
93
94 if(!QDate::isValid(year, month, day))
95 {
96 /* Try to give an intelligent message. */
97 if(day > 31 || day < 1)
98 {
99 badData(QtXmlPatterns::tr("Day %1 is outside the range %2..%3.")
100 .arg(formatData(QString::number(day)))
101 .arg(formatData("01"))
102 .arg(formatData("31")));
103 }
104 else if(month > 12 || month < -12 || month == 0)
105 {
106 badData(QtXmlPatterns::tr("Month %1 is outside the range %2..%3.")
107 .arg(month)
108 .arg(formatData("01"))
109 .arg(formatData("12")));
110
111 }
112 else if(QDate::isValid(DefaultYear, month, day))
113 {
114 /* We can't use the badData() macro here because we need a different
115 * error code: FODT0001 instead of FORG0001. */
116 errorMessage = ValidationError::createError(QtXmlPatterns::tr(
117 "Overflow: Can't represent date %1.")
118 .arg(formatData(QLatin1String("%1-%2-%3"))
119 .arg(year).arg(month).arg(day)),
120 ReportContext::FODT0001);
121 return QDateTime();
122 }
123 else
124 {
125 badData(QtXmlPatterns::tr("Day %1 is invalid for month %2.")
126 .arg(formatData(QString::number(day)))
127 .arg(formatData(QString::number(month))));
128 }
129 }
130
131 /* Parse the zone offset. */
132 ZoneOffsetParseResult zoResult;
133 const ZOTotal offset = parseZoneOffset(zoResult, capts, captTable);
134
135 if(zoResult == Error)
136 {
137 errorMessage = ValidationError::createError();
138 /* We encountered an error, so stop processing. */
139 return QDateTime();
140 }
141
142 QDate date(year, month, day);
143
144 /* Only deal with time if time is needed. */
145 if(captTable.hour == -1)
146 {
147 QDateTime result(date);
148 setUtcOffset(result, zoResult, offset);
149 return result;
150 }
151 else
152 {
153 /* Now, it's time for the time-part.
154 *
155 * If the strings are empty, toInt() will return 0, which
156 * in all cases is valid properties. */
157 const QString hourStr(getCapt(hour));
158 const QString minutesStr(getCapt(minutes));
159 const QString secondsStr(getCapt(seconds));
160 HourProperty hour = hourStr.toInt();
161 const MinuteProperty mins = minutesStr.toInt();
162 const SecondProperty secs = secondsStr.toInt();
163
164 QString msecondsStr(getSafeCapt(mseconds));
165 if(!msecondsStr.isEmpty())
166 msecondsStr = msecondsStr.leftJustified(3, QLatin1Char('0'));
167 const MSecondProperty msecs = msecondsStr.toInt();
168
169 if(hour == 24)
170 {
171 /* 24:00:00.00 is an invalid time for QTime, so handle it here. */
172 if(mins != 0 || secs != 0 || msecs != 0)
173 {
174 badData(QtXmlPatterns::tr("Time 24:%1:%2.%3 is invalid. "
175 "Hour is 24, but minutes, seconds, "
176 "and milliseconds are not all 0; ")
177 .arg(mins).arg(secs).arg(msecs));
178 }
179 else
180 {
181 hour = 0;
182 date = date.addDays(1);
183 }
184 }
185 else if(!QTime::isValid(hour, mins, secs, msecs))
186 {
187 badData(QtXmlPatterns::tr("Time %1:%2:%3.%4 is invalid.")
188 .arg(hour).arg(mins).arg(secs).arg(msecs));
189 }
190
191 const QTime time(hour, mins, secs, msecs);
192 Q_ASSERT(time.isValid());
193
194 QDateTime result(date, time);
195 setUtcOffset(result, zoResult, offset);
196 return result;
197 }
198}
199
200ZOTotal AbstractDateTime::parseZoneOffset(ZoneOffsetParseResult &result,
201 const QStringList &capts,
202 const CaptureTable &captTable)
203{
204 const QString zoneOffsetSignStr(getCapt(zoneOffsetSign));
205
206 if(zoneOffsetSignStr.isEmpty())
207 {
208 const QString zoneOffsetUTCStr(getCapt(zoneOffsetUTCSymbol));
209 Q_ASSERT(zoneOffsetUTCStr.isEmpty() || zoneOffsetUTCStr == QLatin1String("Z"));
210
211 if(zoneOffsetUTCStr.isEmpty())
212 result = LocalTime;
213 else
214 result = UTC;
215
216 return 0;
217 }
218
219 Q_ASSERT(zoneOffsetSignStr == QLatin1String("-") || zoneOffsetSignStr == QLatin1String("+"));
220
221 const QString zoneOffsetHourStr(getCapt(zoneOffsetHour));
222 Q_ASSERT(!zoneOffsetHourStr.isEmpty());
223 const ZOHourProperty zoHour = zoneOffsetHourStr.toInt();
224
225 if(zoHour > 14 || zoHour < -14)
226 {
227 result = Error;
228 return 0;
229 /*
230 badZOData(QtXmlPatterns::tr("%1 it is not a valid hour property in a zone offset. "
231 "It must be less than or equal to 14.").arg(zoHour));
232 */
233 }
234
235 const QString zoneOffsetMinuteStr(getCapt(zoneOffsetMinute));
236 Q_ASSERT(!zoneOffsetMinuteStr.isEmpty());
237 const ZOHourProperty zoMins = zoneOffsetMinuteStr.toInt();
238
239 if(zoHour == 14 && zoMins != 0)
240 {
241 /*
242 badZOData(QtXmlPatterns::tr("When the hour property in a zone offset is 14, the minute property "
243 "must be 0, not %1.").arg(zoMins));
244 */
245 result = Error;
246 return 0;
247 }
248 else if(zoMins > 59 || zoMins < -59)
249 {
250 /*
251 badZOData(QtXmlPatterns::tr("The minute property in a zone offset cannot be larger than 59. "
252 "%1 is therefore invalid.").arg(zoMins));
253 */
254 result = Error;
255 return 0;
256 }
257
258 if(zoHour == 0 && zoMins == 0) /* "-00:00" and "+00:00" is equal to 'Z'. */
259 {
260 result = UTC;
261 return 0;
262 }
263 else
264 {
265 ZOTotal zoneOffset = (zoHour * 60 + zoMins) * 60;
266
267 if(zoneOffsetSignStr == QChar::fromLatin1('-'))
268 zoneOffset = -zoneOffset;
269
270 result = Offset;
271 return zoneOffset;
272 }