0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
|
|
43 |
#include <QtTest/QtTest>
|
|
44 |
#ifndef Q_OS_WINCE
|
|
45 |
#include <time.h>
|
|
46 |
#endif
|
|
47 |
|
|
48 |
#include <qdatetime.h>
|
|
49 |
#include <private/qdatetime_p.h>
|
|
50 |
|
|
51 |
#ifdef Q_OS_WIN
|
|
52 |
# include <windows.h>
|
|
53 |
#endif
|
|
54 |
|
|
55 |
//TESTED_CLASS=
|
|
56 |
//TESTED_FILES=
|
|
57 |
|
|
58 |
class tst_QDateTime : public QObject
|
|
59 |
{
|
|
60 |
Q_OBJECT
|
|
61 |
|
|
62 |
public:
|
|
63 |
tst_QDateTime();
|
|
64 |
virtual ~tst_QDateTime();
|
|
65 |
|
|
66 |
static QString str( int y, int month, int d, int h, int min, int s );
|
|
67 |
static QDateTime dt( const QString& str );
|
|
68 |
public slots:
|
|
69 |
void init();
|
|
70 |
void cleanup();
|
|
71 |
private slots:
|
|
72 |
void ctor();
|
|
73 |
void operator_eq();
|
|
74 |
void isNull();
|
|
75 |
void isValid();
|
|
76 |
void date();
|
|
77 |
void time();
|
|
78 |
void timeSpec();
|
|
79 |
void toTime_t_data();
|
|
80 |
void toTime_t();
|
|
81 |
void task_137698();
|
|
82 |
void setDate();
|
|
83 |
void setTime();
|
|
84 |
void setTimeSpec();
|
|
85 |
void setTime_t();
|
|
86 |
void toString_enumformat();
|
|
87 |
void toString_strformat_data();
|
|
88 |
void toString_strformat();
|
|
89 |
void addDays();
|
|
90 |
void addMonths();
|
|
91 |
void addMonths_data();
|
|
92 |
void addYears();
|
|
93 |
void addYears_data();
|
|
94 |
void addSecs_data();
|
|
95 |
void addSecs();
|
|
96 |
void addMSecs_data();
|
|
97 |
void addMSecs();
|
|
98 |
void toTimeSpec_data();
|
|
99 |
void toTimeSpec();
|
|
100 |
void toLocalTime_data();
|
|
101 |
void toLocalTime();
|
|
102 |
void toUTC_data();
|
|
103 |
void toUTC();
|
|
104 |
void daysTo();
|
|
105 |
void secsTo_data();
|
|
106 |
void secsTo();
|
|
107 |
void operator_eqeq();
|
|
108 |
void currentDateTime();
|
|
109 |
void fromStringTextDate_data();
|
|
110 |
void fromStringTextDate();
|
|
111 |
|
|
112 |
void dateFromStringFormat_data();
|
|
113 |
void dateFromStringFormat();
|
|
114 |
void timeFromStringFormat_data();
|
|
115 |
void timeFromStringFormat();
|
|
116 |
void dateTimeFromStringFormat_data();
|
|
117 |
void dateTimeFromStringFormat();
|
|
118 |
|
|
119 |
void fromString_LOCALE_ILDATE();
|
|
120 |
|
|
121 |
void fromString();
|
|
122 |
|
|
123 |
void utcOffset();
|
|
124 |
void setUtcOffset();
|
|
125 |
|
|
126 |
void getDate();
|
|
127 |
|
|
128 |
void fewDigitsInYear() const;
|
|
129 |
void printNegativeYear() const;
|
|
130 |
void roundtripGermanLocale() const;
|
|
131 |
void utcOffsetLessThan() const;
|
|
132 |
|
|
133 |
private:
|
|
134 |
bool europeanTimeZone;
|
|
135 |
QDate defDate() const { return QDate(1900, 1, 1); }
|
|
136 |
QTime defTime() const { return QTime(0, 0, 0); }
|
|
137 |
QDateTime defDateTime() const { return QDateTime(defDate(), defTime()); }
|
|
138 |
QDateTime invalidDateTime() const { return QDateTime(invalidDate(), invalidTime()); }
|
|
139 |
QDate invalidDate() const { return QDate(); }
|
|
140 |
QTime invalidTime() const { return QTime(-1, -1, -1); }
|
|
141 |
};
|
|
142 |
|
|
143 |
Q_DECLARE_METATYPE(QDateTime)
|
|
144 |
Q_DECLARE_METATYPE(QDate)
|
|
145 |
Q_DECLARE_METATYPE(QTime)
|
|
146 |
|
|
147 |
|
|
148 |
tst_QDateTime::tst_QDateTime()
|
|
149 |
{
|
|
150 |
uint x1 = QDateTime(QDate(1990, 1, 1), QTime()).toTime_t();
|
|
151 |
uint x2 = QDateTime(QDate(1990, 6, 1), QTime()).toTime_t();
|
|
152 |
europeanTimeZone = (x1 == 631148400 && x2 == 644191200);
|
|
153 |
}
|
|
154 |
|
|
155 |
tst_QDateTime::~tst_QDateTime()
|
|
156 |
{
|
|
157 |
|
|
158 |
}
|
|
159 |
|
|
160 |
void tst_QDateTime::init()
|
|
161 |
{
|
|
162 |
#if defined(Q_OS_WINCE)
|
|
163 |
SetUserDefaultLCID(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
|
|
164 |
#elif defined(Q_OS_WIN)
|
|
165 |
SetThreadLocale(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
|
|
166 |
#endif
|
|
167 |
|
|
168 |
}
|
|
169 |
|
|
170 |
void tst_QDateTime::cleanup()
|
|
171 |
{
|
|
172 |
}
|
|
173 |
|
|
174 |
QString tst_QDateTime::str( int y, int month, int d, int h, int min, int s )
|
|
175 |
{
|
|
176 |
return QDateTime( QDate(y, month, d), QTime(h, min, s) ).toString( Qt::ISODate );
|
|
177 |
}
|
|
178 |
|
|
179 |
QDateTime tst_QDateTime::dt( const QString& str )
|
|
180 |
{
|
|
181 |
if ( str == "INVALID" ) {
|
|
182 |
return QDateTime();
|
|
183 |
} else {
|
|
184 |
return QDateTime::fromString( str, Qt::ISODate );
|
|
185 |
}
|
|
186 |
}
|
|
187 |
|
|
188 |
void tst_QDateTime::ctor()
|
|
189 |
{
|
|
190 |
QDateTime dt1(QDate(2004, 1, 2), QTime(1, 2, 3));
|
|
191 |
QDateTime dt2(QDate(2004, 1, 2), QTime(1, 2, 3), Qt::LocalTime);
|
|
192 |
QDateTime dt3(QDate(2004, 1, 2), QTime(1, 2, 3), Qt::UTC);
|
|
193 |
|
|
194 |
QVERIFY(dt1 == dt2);
|
|
195 |
QVERIFY(dt1 != dt3);
|
|
196 |
if (europeanTimeZone) {
|
|
197 |
QVERIFY(dt1 < dt3);
|
|
198 |
QVERIFY(dt1.addSecs(3600).toUTC() == dt3);
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
|
202 |
void tst_QDateTime::operator_eq()
|
|
203 |
{
|
|
204 |
QDateTime dt1(QDate(2004, 3, 24), QTime(23, 45, 57), Qt::UTC);
|
|
205 |
QDateTime dt2(QDate(2005, 3, 11), QTime(), Qt::UTC);
|
|
206 |
dt2 = dt1;
|
|
207 |
QVERIFY(dt1 == dt2);
|
|
208 |
}
|
|
209 |
|
|
210 |
void tst_QDateTime::isNull()
|
|
211 |
{
|
|
212 |
QDateTime dt1;
|
|
213 |
QVERIFY(dt1.isNull());
|
|
214 |
dt1.setDate(QDate());
|
|
215 |
QVERIFY(dt1.isNull());
|
|
216 |
dt1.setTime(QTime());
|
|
217 |
QVERIFY(dt1.isNull());
|
|
218 |
dt1.setTimeSpec(Qt::UTC);
|
|
219 |
QVERIFY(dt1.isNull()); // maybe it should return false?
|
|
220 |
|
|
221 |
dt1.setDate(QDate(2004, 1, 2));
|
|
222 |
QVERIFY(!dt1.isNull());
|
|
223 |
dt1.setTime(QTime(12, 34, 56));
|
|
224 |
QVERIFY(!dt1.isNull());
|
|
225 |
dt1.setTime(QTime());
|
|
226 |
QVERIFY(!dt1.isNull());
|
|
227 |
}
|
|
228 |
|
|
229 |
void tst_QDateTime::isValid()
|
|
230 |
{
|
|
231 |
QDateTime dt1;
|
|
232 |
QVERIFY(!dt1.isValid());
|
|
233 |
dt1.setDate(QDate());
|
|
234 |
QVERIFY(!dt1.isValid());
|
|
235 |
dt1.setTime(QTime());
|
|
236 |
QVERIFY(!dt1.isValid());
|
|
237 |
dt1.setTimeSpec(Qt::UTC);
|
|
238 |
QVERIFY(!dt1.isValid());
|
|
239 |
|
|
240 |
dt1.setDate(QDate(2004, 1, 2));
|
|
241 |
QVERIFY(dt1.isValid());
|
|
242 |
dt1.setDate(QDate());
|
|
243 |
QVERIFY(!dt1.isValid());
|
|
244 |
dt1.setTime(QTime(12, 34, 56));
|
|
245 |
QVERIFY(!dt1.isValid());
|
|
246 |
dt1.setTime(QTime());
|
|
247 |
QVERIFY(!dt1.isValid());
|
|
248 |
}
|
|
249 |
|
|
250 |
void tst_QDateTime::date()
|
|
251 |
{
|
|
252 |
QDateTime dt1(QDate(2004, 3, 24), QTime(23, 45, 57), Qt::LocalTime);
|
|
253 |
QCOMPARE(dt1.date(), QDate(2004, 3, 24));
|
|
254 |
|
|
255 |
QDateTime dt2(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::LocalTime);
|
|
256 |
QCOMPARE(dt2.date(), QDate(2004, 3, 25));
|
|
257 |
|
|
258 |
QDateTime dt3(QDate(2004, 3, 24), QTime(23, 45, 57), Qt::UTC);
|
|
259 |
QCOMPARE(dt3.date(), QDate(2004, 3, 24));
|
|
260 |
|
|
261 |
QDateTime dt4(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::UTC);
|
|
262 |
QCOMPARE(dt4.date(), QDate(2004, 3, 25));
|
|
263 |
}
|
|
264 |
|
|
265 |
void tst_QDateTime::time()
|
|
266 |
{
|
|
267 |
QDateTime dt1(QDate(2004, 3, 24), QTime(23, 45, 57), Qt::LocalTime);
|
|
268 |
QCOMPARE(dt1.time(), QTime(23, 45, 57));
|
|
269 |
|
|
270 |
QDateTime dt2(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::LocalTime);
|
|
271 |
QCOMPARE(dt2.time(), QTime(0, 45, 57));
|
|
272 |
|
|
273 |
QDateTime dt3(QDate(2004, 3, 24), QTime(23, 45, 57), Qt::UTC);
|
|
274 |
QCOMPARE(dt3.time(), QTime(23, 45, 57));
|
|
275 |
|
|
276 |
QDateTime dt4(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::UTC);
|
|
277 |
QCOMPARE(dt4.time(), QTime(0, 45, 57));
|
|
278 |
}
|
|
279 |
|
|
280 |
void tst_QDateTime::timeSpec()
|
|
281 |
{
|
|
282 |
QDateTime dt1(QDate(2004, 1, 24), QTime(23, 45, 57));
|
|
283 |
QCOMPARE(dt1.timeSpec(), Qt::LocalTime);
|
|
284 |
QCOMPARE(dt1.addDays(0).timeSpec(), Qt::LocalTime);
|
|
285 |
QCOMPARE(dt1.addMonths(0).timeSpec(), Qt::LocalTime);
|
|
286 |
QCOMPARE(dt1.addMonths(6).timeSpec(), Qt::LocalTime);
|
|
287 |
QCOMPARE(dt1.addYears(0).timeSpec(), Qt::LocalTime);
|
|
288 |
QCOMPARE(dt1.addSecs(0).timeSpec(), Qt::LocalTime);
|
|
289 |
QCOMPARE(dt1.addSecs(86400 * 185).timeSpec(), Qt::LocalTime);
|
|
290 |
QCOMPARE(dt1.toTimeSpec(Qt::LocalTime).timeSpec(), Qt::LocalTime);
|
|
291 |
QCOMPARE(dt1.toTimeSpec(Qt::UTC).timeSpec(), Qt::UTC);
|
|
292 |
|
|
293 |
QDateTime dt2(QDate(2004, 1, 24), QTime(23, 45, 57), Qt::LocalTime);
|
|
294 |
QCOMPARE(dt2.timeSpec(), Qt::LocalTime);
|
|
295 |
|
|
296 |
QDateTime dt3(QDate(2004, 1, 25), QTime(0, 45, 57), Qt::UTC);
|
|
297 |
QCOMPARE(dt3.timeSpec(), Qt::UTC);
|
|
298 |
|
|
299 |
QDateTime dt4 = QDateTime::currentDateTime();
|
|
300 |
QCOMPARE(dt4.timeSpec(), Qt::LocalTime);
|
|
301 |
}
|
|
302 |
|
|
303 |
void tst_QDateTime::setDate()
|
|
304 |
{
|
|
305 |
QDateTime dt1(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::UTC);
|
|
306 |
dt1.setDate(QDate(2004, 6, 25));
|
|
307 |
QCOMPARE(dt1.date(), QDate(2004, 6, 25));
|
|
308 |
QCOMPARE(dt1.time(), QTime(0, 45, 57));
|
|
309 |
QCOMPARE(dt1.timeSpec(), Qt::UTC);
|
|
310 |
|
|
311 |
QDateTime dt2(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::LocalTime);
|
|
312 |
dt2.setDate(QDate(2004, 6, 25));
|
|
313 |
QCOMPARE(dt2.date(), QDate(2004, 6, 25));
|
|
314 |
QCOMPARE(dt2.time(), QTime(0, 45, 57));
|
|
315 |
QCOMPARE(dt2.timeSpec(), Qt::LocalTime);
|
|
316 |
|
|
317 |
QDateTime dt3(QDate(4004, 3, 25), QTime(0, 45, 57), Qt::UTC);
|
|
318 |
dt3.setDate(QDate(4004, 6, 25));
|
|
319 |
QCOMPARE(dt3.date(), QDate(4004, 6, 25));
|
|
320 |
QCOMPARE(dt3.time(), QTime(0, 45, 57));
|
|
321 |
QCOMPARE(dt3.timeSpec(), Qt::UTC);
|
|
322 |
|
|
323 |
QDateTime dt4(QDate(4004, 3, 25), QTime(0, 45, 57), Qt::LocalTime);
|
|
324 |
dt4.setDate(QDate(4004, 6, 25));
|
|
325 |
QCOMPARE(dt4.date(), QDate(4004, 6, 25));
|
|
326 |
QCOMPARE(dt4.time(), QTime(0, 45, 57));
|
|
327 |
QCOMPARE(dt4.timeSpec(), Qt::LocalTime);
|
|
328 |
|
|
329 |
QDateTime dt5(QDate(1760, 3, 25), QTime(0, 45, 57), Qt::UTC);
|
|
330 |
dt5.setDate(QDate(1760, 6, 25));
|
|
331 |
QCOMPARE(dt5.date(), QDate(1760, 6, 25));
|
|
332 |
QCOMPARE(dt5.time(), QTime(0, 45, 57));
|
|
333 |
QCOMPARE(dt5.timeSpec(), Qt::UTC);
|
|
334 |
|
|
335 |
QDateTime dt6(QDate(1760, 3, 25), QTime(0, 45, 57), Qt::LocalTime);
|
|
336 |
dt6.setDate(QDate(1760, 6, 25));
|
|
337 |
QCOMPARE(dt6.date(), QDate(1760, 6, 25));
|
|
338 |
QCOMPARE(dt6.time(), QTime(0, 45, 57));
|
|
339 |
QCOMPARE(dt6.timeSpec(), Qt::LocalTime);
|
|
340 |
}
|
|
341 |
|
|
342 |
void tst_QDateTime::setTime()
|
|
343 |
{
|
|
344 |
QDateTime dt1(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::UTC);
|
|
345 |
dt1.setTime(QTime(23, 11, 22));
|
|
346 |
QCOMPARE(dt1.date(), QDate(2004, 3, 25));
|
|
347 |
QCOMPARE(dt1.time(), QTime(23, 11, 22));
|
|
348 |
QCOMPARE(dt1.timeSpec(), Qt::UTC);
|
|
349 |
|
|
350 |
QDateTime dt2(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::LocalTime);
|
|
351 |
dt2.setTime(QTime(23, 11, 22));
|
|
352 |
QCOMPARE(dt2.date(), QDate(2004, 3, 25));
|
|
353 |
QCOMPARE(dt2.time(), QTime(23, 11, 22));
|
|
354 |
QCOMPARE(dt2.timeSpec(), Qt::LocalTime);
|
|
355 |
|
|
356 |
QDateTime dt3(QDate(4004, 3, 25), QTime(0, 45, 57), Qt::UTC);
|
|
357 |
dt3.setTime(QTime(23, 11, 22));
|
|
358 |
QCOMPARE(dt3.date(), QDate(4004, 3, 25));
|
|
359 |
QCOMPARE(dt3.time(), QTime(23, 11, 22));
|
|
360 |
QCOMPARE(dt3.timeSpec(), Qt::UTC);
|
|
361 |
|
|
362 |
QDateTime dt4(QDate(4004, 3, 25), QTime(0, 45, 57), Qt::LocalTime);
|
|
363 |
dt4.setTime(QTime(23, 11, 22));
|
|
364 |
QCOMPARE(dt4.date(), QDate(4004, 3, 25));
|
|
365 |
QCOMPARE(dt4.time(), QTime(23, 11, 22));
|
|
366 |
QCOMPARE(dt4.timeSpec(), Qt::LocalTime);
|
|
367 |
|
|
368 |
QDateTime dt5(QDate(1760, 3, 25), QTime(0, 45, 57), Qt::UTC);
|
|
369 |
dt5.setTime(QTime(23, 11, 22));
|
|
370 |
QCOMPARE(dt5.date(), QDate(1760, 3, 25));
|
|
371 |
QCOMPARE(dt5.time(), QTime(23, 11, 22));
|
|
372 |
QCOMPARE(dt5.timeSpec(), Qt::UTC);
|
|
373 |
|
|
374 |
QDateTime dt6(QDate(1760, 3, 25), QTime(0, 45, 57), Qt::LocalTime);
|
|
375 |
dt6.setTime(QTime(23, 11, 22));
|
|
376 |
QCOMPARE(dt6.date(), QDate(1760, 3, 25));
|
|
377 |
QCOMPARE(dt6.time(), QTime(23, 11, 22));
|
|
378 |
QCOMPARE(dt6.timeSpec(), Qt::LocalTime);
|
|
379 |
}
|
|
380 |
|
|
381 |
void tst_QDateTime::setTimeSpec()
|
|
382 |
{
|
|
383 |
QDateTime dt1(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::UTC);
|
|
384 |
dt1.setTimeSpec(Qt::UTC);
|
|
385 |
QCOMPARE(dt1.date(), QDate(2004, 3, 25));
|
|
386 |
QCOMPARE(dt1.time(), QTime(0, 45, 57));
|
|
387 |
QCOMPARE(dt1.timeSpec(), Qt::UTC);
|
|
388 |
|
|
389 |
dt1.setTimeSpec(Qt::LocalTime);
|
|
390 |
QCOMPARE(dt1.date(), QDate(2004, 3, 25));
|
|
391 |
QCOMPARE(dt1.time(), QTime(0, 45, 57));
|
|
392 |
QCOMPARE(dt1.timeSpec(), Qt::LocalTime);
|
|
393 |
}
|
|
394 |
|
|
395 |
void tst_QDateTime::setTime_t()
|
|
396 |
{
|
|
397 |
QDateTime dt1;
|
|
398 |
dt1.setTime_t(0);
|
|
399 |
QCOMPARE(dt1.toUTC(), QDateTime(QDate(1970, 1, 1), QTime(), Qt::UTC));
|
|
400 |
|
|
401 |
dt1.setTimeSpec(Qt::UTC);
|
|
402 |
dt1.setTime_t(0);
|
|
403 |
QCOMPARE(dt1, QDateTime(QDate(1970, 1, 1), QTime(), Qt::UTC));
|
|
404 |
|
|
405 |
dt1.setTime_t(123456);
|
|
406 |
QCOMPARE(dt1, QDateTime(QDate(1970, 1, 2), QTime(10, 17, 36), Qt::UTC));
|
|
407 |
if (europeanTimeZone) {
|
|
408 |
QDateTime dt2;
|
|
409 |
dt2.setTime_t(123456);
|
|
410 |
QCOMPARE(dt2, QDateTime(QDate(1970, 1, 2), QTime(11, 17, 36), Qt::LocalTime));
|
|
411 |
}
|
|
412 |
|
|
413 |
dt1.setTime_t((uint)(quint32)-123456);
|
|
414 |
QCOMPARE(dt1, QDateTime(QDate(2106, 2, 5), QTime(20, 10, 40), Qt::UTC));
|
|
415 |
if (europeanTimeZone) {
|
|
416 |
QDateTime dt2;
|
|
417 |
dt2.setTime_t((uint)(quint32)-123456);
|
|
418 |
QCOMPARE(dt2, QDateTime(QDate(2106, 2, 5), QTime(21, 10, 40), Qt::LocalTime));
|
|
419 |
}
|
|
420 |
|
|
421 |
dt1.setTime_t(1214567890);
|
|
422 |
QCOMPARE(dt1, QDateTime(QDate(2008, 6, 27), QTime(11, 58, 10), Qt::UTC));
|
|
423 |
if (europeanTimeZone) {
|
|
424 |
QDateTime dt2;
|
|
425 |
dt2.setTime_t(1214567890);
|
|
426 |
QCOMPARE(dt2, QDateTime(QDate(2008, 6, 27), QTime(13, 58, 10), Qt::LocalTime));
|
|
427 |
}
|
|
428 |
|
|
429 |
dt1.setTime_t(0x7FFFFFFF);
|
|
430 |
QCOMPARE(dt1, QDateTime(QDate(2038, 1, 19), QTime(3, 14, 7), Qt::UTC));
|
|
431 |
if (europeanTimeZone) {
|
|
432 |
QDateTime dt2;
|
|
433 |
dt2.setTime_t(0x7FFFFFFF);
|
|
434 |
QCOMPARE(dt2, QDateTime(QDate(2038, 1, 19), QTime(4, 14, 7), Qt::LocalTime));
|
|
435 |
}
|
|
436 |
}
|
|
437 |
|
|
438 |
void tst_QDateTime::toString_enumformat()
|
|
439 |
{
|
|
440 |
QDateTime dt1(QDate(1995, 5, 20), QTime(12, 34, 56));
|
|
441 |
|
|
442 |
|
|
443 |
QString str1 = dt1.toString(Qt::TextDate);
|
|
444 |
QVERIFY(!str1.isEmpty()); // It's locale dependent everywhere
|
|
445 |
|
|
446 |
QString str2 = dt1.toString(Qt::ISODate);
|
|
447 |
QCOMPARE(str2, QString("1995-05-20T12:34:56"));
|
|
448 |
|
|
449 |
QString str3 = dt1.toString(Qt::LocalDate);
|
|
450 |
QVERIFY(!str3.isEmpty());
|
|
451 |
}
|
|
452 |
|
|
453 |
void tst_QDateTime::addDays()
|
|
454 |
{
|
|
455 |
for (int pass = 0; pass < 2; ++pass) {
|
|
456 |
QDateTime dt(QDate(2004, 1, 1), QTime(12, 34, 56), pass == 0 ? Qt::LocalTime : Qt::UTC);
|
|
457 |
dt = dt.addDays(185);
|
|
458 |
QVERIFY(dt.date().year() == 2004 && dt.date().month() == 7 && dt.date().day() == 4);
|
|
459 |
QVERIFY(dt.time().hour() == 12 && dt.time().minute() == 34 && dt.time().second() == 56
|
|
460 |
&& dt.time().msec() == 0);
|
|
461 |
QCOMPARE(dt.timeSpec(), (pass == 0 ? Qt::LocalTime : Qt::UTC));
|
|
462 |
|
|
463 |
dt = dt.addDays(-185);
|
|
464 |
QCOMPARE(dt.date(), QDate(2004, 1, 1));
|
|
465 |
QCOMPARE(dt.time(), QTime(12, 34, 56));
|
|
466 |
}
|
|
467 |
|
|
468 |
QDateTime dt(QDate(1752, 9, 14), QTime(12, 34, 56));
|
|
469 |
while (dt.date().year() < 8000) {
|
|
470 |
int year = dt.date().year();
|
|
471 |
if (QDate::isLeapYear(year + 1))
|
|
472 |
dt = dt.addDays(366);
|
|
473 |
else
|
|
474 |
dt = dt.addDays(365);
|
|
475 |
QCOMPARE(dt.date(), QDate(year + 1, 9, 14));
|
|
476 |
QCOMPARE(dt.time(), QTime(12, 34, 56));
|
|
477 |
}
|
|
478 |
|
|
479 |
// ### test invalid QDateTime()
|
|
480 |
}
|
|
481 |
|
|
482 |
|
|
483 |
void tst_QDateTime::addMonths_data()
|
|
484 |
{
|
|
485 |
QTest::addColumn<int>("months");
|
|
486 |
QTest::addColumn<QDate>("dt");
|
|
487 |
|
|
488 |
QTest::newRow("-15") << -15 << QDate(2002, 10, 31);
|
|
489 |
QTest::newRow("-14") << -14 << QDate(2002, 11, 30);
|
|
490 |
QTest::newRow("-13") << -13 << QDate(2002, 12, 31);
|
|
491 |
QTest::newRow("-12") << -12 << QDate(2003, 1, 31);
|
|
492 |
|
|
493 |
QTest::newRow("-11") << -11 << QDate(2003, 2, 28);
|
|
494 |
QTest::newRow("-10") << -10 << QDate(2003, 3, 31);
|
|
495 |
QTest::newRow("-9") << -9 << QDate(2003, 4, 30);
|
|
496 |
QTest::newRow("-8") << -8 << QDate(2003, 5, 31);
|
|
497 |
QTest::newRow("-7") << -7 << QDate(2003, 6, 30);
|
|
498 |
QTest::newRow("-6") << -6 << QDate(2003, 7, 31);
|
|
499 |
QTest::newRow("-5") << -5 << QDate(2003, 8, 31);
|
|
500 |
QTest::newRow("-4") << -4 << QDate(2003, 9, 30);
|
|
501 |
QTest::newRow("-3") << -3 << QDate(2003, 10, 31);
|
|
502 |
QTest::newRow("-2") << -2 << QDate(2003, 11, 30);
|
|
503 |
QTest::newRow("-1") << -1 << QDate(2003, 12, 31);
|
|
504 |
QTest::newRow("0") << 0 << QDate(2004, 1, 31);
|
|
505 |
QTest::newRow("1") << 1 << QDate(2004, 2, 29);
|
|
506 |
QTest::newRow("2") << 2 << QDate(2004, 3, 31);
|
|
507 |
QTest::newRow("3") << 3 << QDate(2004, 4, 30);
|
|
508 |
QTest::newRow("4") << 4 << QDate(2004, 5, 31);
|
|
509 |
QTest::newRow("5") << 5 << QDate(2004, 6, 30);
|
|
510 |
QTest::newRow("6") << 6 << QDate(2004, 7, 31);
|
|
511 |
QTest::newRow("7") << 7 << QDate(2004, 8, 31);
|
|
512 |
QTest::newRow("8") << 8 << QDate(2004, 9, 30);
|
|
513 |
QTest::newRow("9") << 9 << QDate(2004, 10, 31);
|
|
514 |
QTest::newRow("10") << 10 << QDate(2004, 11, 30);
|
|
515 |
QTest::newRow("11") << 11 << QDate(2004, 12, 31);
|
|
516 |
QTest::newRow("12") << 12 << QDate(2005, 1, 31);
|
|
517 |
QTest::newRow("13") << 13 << QDate(2005, 2, 28);
|
|
518 |
QTest::newRow("14") << 14 << QDate(2005, 3, 31);
|
|
519 |
QTest::newRow("15") << 15 << QDate(2005, 4, 30);
|
|
520 |
}
|
|
521 |
|
|
522 |
void tst_QDateTime::addMonths()
|
|
523 |
{
|
|
524 |
QFETCH(QDate, dt);
|
|
525 |
QFETCH(int, months);
|
|
526 |
|
|
527 |
QDateTime start(QDate(2004, 1, 31), QTime(12, 34, 56));
|
|
528 |
QCOMPARE(start.addMonths(months).date(), dt);
|
|
529 |
QCOMPARE(start.addMonths(months).time(), QTime(12, 34, 56));
|
|
530 |
}
|
|
531 |
|
|
532 |
void tst_QDateTime::addYears_data()
|
|
533 |
{
|
|
534 |
QTest::addColumn<int>("years1");
|
|
535 |
QTest::addColumn<int>("years2");
|
|
536 |
QTest::addColumn<QDate>("start");
|
|
537 |
QTest::addColumn<QDate>("dt");
|
|
538 |
|
|
539 |
QTest::newRow("0") << 0 << 0 << QDate(1752, 9, 14) << QDate(1752, 9, 14);
|
|
540 |
QTest::newRow("4000 - 4000") << 4000 << -4000 << QDate(1752, 9, 14) << QDate(1752, 9, 14);
|
|
541 |
QTest::newRow("10") << 10 << 0 << QDate(1752, 9, 14) << QDate(1762, 9, 14);
|
|
542 |
QTest::newRow("0 leap year") << 0 << 0 << QDate(1760, 2, 29) << QDate(1760, 2, 29);
|
|
543 |
QTest::newRow("1 leap year") << 1 << 0 << QDate(1760, 2, 29) << QDate(1761, 2, 28);
|
|
544 |
QTest::newRow("2 leap year") << 2 << 0 << QDate(1760, 2, 29) << QDate(1762, 2, 28);
|
|
545 |
QTest::newRow("3 leap year") << 3 << 0 << QDate(1760, 2, 29) << QDate(1763, 2, 28);
|
|
546 |
QTest::newRow("4 leap year") << 4 << 0 << QDate(1760, 2, 29) << QDate(1764, 2, 29);
|
|
547 |
|
|
548 |
QTest::newRow("toNegative1") << -2000 << 0 << QDate(1752, 9, 14) << QDate(-249, 9, 14);
|
|
549 |
QTest::newRow("toNegative2") << -1752 << 0 << QDate(1752, 9, 14) << QDate(-1, 9, 14);
|
|
550 |
QTest::newRow("toNegative3") << -1751 << 0 << QDate(1752, 9, 14) << QDate(1, 9, 14);
|
|
551 |
QTest::newRow("toPositive1") << 2000 << 0 << QDate(-1752, 9, 14) << QDate(249, 9, 14);
|
|
552 |
QTest::newRow("toPositive2") << 1752 << 0 << QDate(-1752, 9, 14) << QDate(1, 9, 14);
|
|
553 |
QTest::newRow("toPositive3") << 1751 << 0 << QDate(-1752, 9, 14) << QDate(-1, 9, 14);
|
|
554 |
}
|
|
555 |
|
|
556 |
void tst_QDateTime::addYears()
|
|
557 |
{
|
|
558 |
QFETCH(int, years1);
|
|
559 |
QFETCH(int, years2);
|
|
560 |
QFETCH(QDate, start);
|
|
561 |
QFETCH(QDate, dt);
|
|
562 |
|
|
563 |
QDateTime startdt(start, QTime(14, 25, 36));
|
|
564 |
QCOMPARE(startdt.addYears(years1).addYears(years2).date(), dt);
|
|
565 |
QCOMPARE(startdt.addYears(years1).addYears(years2).time(), QTime(14, 25, 36));
|
|
566 |
}
|
|
567 |
|
|
568 |
void tst_QDateTime::addSecs_data()
|
|
569 |
{
|
|
570 |
QTest::addColumn<QDateTime>("dt");
|
|
571 |
QTest::addColumn<int>("nsecs");
|
|
572 |
QTest::addColumn<QDateTime>("result");
|
|
573 |
|
|
574 |
QTime standardTime(12, 34, 56);
|
|
575 |
QTime daylightTime(13, 34, 56);
|
|
576 |
|
|
577 |
QTest::newRow("utc0") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::UTC) << 86400
|
|
578 |
<< QDateTime(QDate(2004, 1, 2), standardTime, Qt::UTC);
|
|
579 |
QTest::newRow("utc1") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::UTC) << (86400 * 185)
|
|
580 |
<< QDateTime(QDate(2004, 7, 4), standardTime, Qt::UTC);
|
|
581 |
QTest::newRow("utc2") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::UTC) << (86400 * 366)
|
|
582 |
<< QDateTime(QDate(2005, 1, 1), standardTime, Qt::UTC);
|
|
583 |
QTest::newRow("utc3") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::UTC) << 86400
|
|
584 |
<< QDateTime(QDate(1760, 1, 2), standardTime, Qt::UTC);
|
|
585 |
QTest::newRow("utc4") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::UTC) << (86400 * 185)
|
|
586 |
<< QDateTime(QDate(1760, 7, 4), standardTime, Qt::UTC);
|
|
587 |
QTest::newRow("utc5") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::UTC) << (86400 * 366)
|
|
588 |
<< QDateTime(QDate(1761, 1, 1), standardTime, Qt::UTC);
|
|
589 |
QTest::newRow("utc6") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::UTC) << 86400
|
|
590 |
<< QDateTime(QDate(4000, 1, 2), standardTime, Qt::UTC);
|
|
591 |
QTest::newRow("utc7") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::UTC) << (86400 * 185)
|
|
592 |
<< QDateTime(QDate(4000, 7, 4), standardTime, Qt::UTC);
|
|
593 |
QTest::newRow("utc8") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::UTC) << (86400 * 366)
|
|
594 |
<< QDateTime(QDate(4001, 1, 1), standardTime, Qt::UTC);
|
|
595 |
QTest::newRow("utc9") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::UTC) << 0
|
|
596 |
<< QDateTime(QDate(4000, 1, 1), standardTime, Qt::UTC);
|
|
597 |
|
|
598 |
if (europeanTimeZone) {
|
|
599 |
QTest::newRow("cet0") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::LocalTime) << 86400
|
|
600 |
<< QDateTime(QDate(2004, 1, 2), standardTime, Qt::LocalTime);
|
|
601 |
QTest::newRow("cet1") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::LocalTime) << (86400 * 185)
|
|
602 |
<< QDateTime(QDate(2004, 7, 4), daylightTime, Qt::LocalTime);
|
|
603 |
QTest::newRow("cet2") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::LocalTime) << (86400 * 366)
|
|
604 |
<< QDateTime(QDate(2005, 1, 1), standardTime, Qt::LocalTime);
|
|
605 |
QTest::newRow("cet3") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::LocalTime) << 86400
|
|
606 |
<< QDateTime(QDate(1760, 1, 2), standardTime, Qt::LocalTime);
|
|
607 |
#ifdef Q_OS_WIN
|
|
608 |
// QDateTime uses 1980 on Windows, which did have daylight savings in July
|
|
609 |
QTest::newRow("cet4") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::LocalTime) << (86400 * 185)
|
|
610 |
<< QDateTime(QDate(1760, 7, 4), daylightTime, Qt::LocalTime);
|
|
611 |
#else
|
|
612 |
// QDateTime uses 1970 everywhere else, which did NOT have daylight savings in July
|
|
613 |
QTest::newRow("cet4") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::LocalTime) << (86400 * 185)
|
|
614 |
<< QDateTime(QDate(1760, 7, 4), standardTime, Qt::LocalTime);
|
|
615 |
#endif
|
|
616 |
QTest::newRow("cet5") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::LocalTime) << (86400 * 366)
|
|
617 |
<< QDateTime(QDate(1761, 1, 1), standardTime, Qt::LocalTime);
|
|
618 |
QTest::newRow("cet6") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::LocalTime) << 86400
|
|
619 |
<< QDateTime(QDate(4000, 1, 2), standardTime, Qt::LocalTime);
|
|
620 |
QTest::newRow("cet7") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::LocalTime) << (86400 * 185)
|
|
621 |
<< QDateTime(QDate(4000, 7, 4), daylightTime, Qt::LocalTime);
|
|
622 |
QTest::newRow("cet8") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::LocalTime) << (86400 * 366)
|
|
623 |
<< QDateTime(QDate(4001, 1, 1), standardTime, Qt::LocalTime);
|
|
624 |
QTest::newRow("cet9") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::LocalTime) << 0
|
|
625 |
<< QDateTime(QDate(4000, 1, 1), standardTime, Qt::LocalTime);
|
|
626 |
}
|
|
627 |
|
|
628 |
// Year sign change
|
|
629 |
QTest::newRow("toNegative") << QDateTime(QDate(1, 1, 1), QTime(0, 0, 0))
|
|
630 |
<< -1
|
|
631 |
<< QDateTime(QDate(-1, 12, 31), QTime(23, 59, 59));
|
|
632 |
QTest::newRow("toPositive") << QDateTime(QDate(-1, 12, 31), QTime(23, 59, 59))
|
|
633 |
<< 1
|
|
634 |
<< QDateTime(QDate(1, 1, 1), QTime(0, 0, 0));
|
|
635 |
|
|
636 |
// Gregorian/Julian switchover
|
|
637 |
QTest::newRow("toGregorian") << QDateTime(QDate(1582, 10, 4), QTime(23, 59, 59))
|
|
638 |
<< 1
|
|
639 |
<< QDateTime(QDate(1582, 10, 15), QTime(0, 0, 0));
|
|
640 |
QTest::newRow("toJulian") << QDateTime(QDate(1582, 10, 15), QTime(0, 0, 0))
|
|
641 |
<< -1
|
|
642 |
<< QDateTime(QDate(1582, 10, 4), QTime(23, 59, 59));
|
|
643 |
}
|
|
644 |
|
|
645 |
void tst_QDateTime::addSecs()
|
|
646 |
{
|
|
647 |
QFETCH(QDateTime, dt);
|
|
648 |
QFETCH(int, nsecs);
|
|
649 |
QFETCH(QDateTime, result);
|
|
650 |
|
|
651 |
#ifdef Q_OS_IRIX
|
|
652 |
QEXPECT_FAIL("cet4", "IRIX databases say 1970 had DST", Abort);
|
|
653 |
#endif
|
|
654 |
QCOMPARE(dt.addSecs(nsecs), result);
|
|
655 |
QCOMPARE(result.addSecs(-nsecs), dt);
|
|
656 |
}
|
|
657 |
|
|
658 |
void tst_QDateTime::addMSecs_data()
|
|
659 |
{
|
|
660 |
addSecs_data();
|
|
661 |
}
|
|
662 |
|
|
663 |
void tst_QDateTime::addMSecs()
|
|
664 |
{
|
|
665 |
QFETCH(QDateTime, dt);
|
|
666 |
QFETCH(int, nsecs);
|
|
667 |
QFETCH(QDateTime, result);
|
|
668 |
|
|
669 |
#ifdef Q_OS_IRIX
|
|
670 |
QEXPECT_FAIL("cet4", "IRIX databases say 1970 had DST", Abort);
|
|
671 |
#endif
|
|
672 |
QCOMPARE(dt.addMSecs(qint64(nsecs) * 1000), result);
|
|
673 |
QCOMPARE(result.addMSecs(qint64(-nsecs) * 1000), dt);
|
|
674 |
}
|
|
675 |
|
|
676 |
void tst_QDateTime::toTimeSpec_data()
|
|
677 |
{
|
|
678 |
QTest::addColumn<QDateTime>("utc");
|
|
679 |
QTest::addColumn<QDateTime>("local");
|
|
680 |
|
|
681 |
QTime utcTime(4, 20, 30);
|
|
682 |
QTime localStandardTime(5, 20, 30);
|
|
683 |
QTime localDaylightTime(6, 20, 30);
|
|
684 |
|
|
685 |
QTest::newRow("winter1") << QDateTime(QDate(2004, 1, 1), utcTime, Qt::UTC)
|
|
686 |
<< QDateTime(QDate(2004, 1, 1), localStandardTime, Qt::LocalTime);
|
|
687 |
QTest::newRow("winter2") << QDateTime(QDate(2004, 2, 29), utcTime, Qt::UTC)
|
|
688 |
<< QDateTime(QDate(2004, 2, 29), localStandardTime, Qt::LocalTime);
|
|
689 |
QTest::newRow("winter3") << QDateTime(QDate(1760, 2, 29), utcTime, Qt::UTC)
|
|
690 |
<< QDateTime(QDate(1760, 2, 29), localStandardTime, Qt::LocalTime);
|
|
691 |
QTest::newRow("winter4") << QDateTime(QDate(6000, 2, 29), utcTime, Qt::UTC)
|
|
692 |
<< QDateTime(QDate(6000, 2, 29), localStandardTime, Qt::LocalTime);
|
|
693 |
|
|
694 |
if (europeanTimeZone) {
|
|
695 |
QTest::newRow("summer1") << QDateTime(QDate(2004, 6, 30), utcTime, Qt::UTC)
|
|
696 |
<< QDateTime(QDate(2004, 6, 30), localDaylightTime, Qt::LocalTime);
|
|
697 |
#ifdef Q_OS_WIN
|
|
698 |
// QDateTime uses 1980 on Windows, which did have daylight savings in July
|
|
699 |
QTest::newRow("summer2") << QDateTime(QDate(1760, 6, 30), utcTime, Qt::UTC)
|
|
700 |
<< QDateTime(QDate(1760, 6, 30), localDaylightTime, Qt::LocalTime);
|
|
701 |
#else
|
|
702 |
// QDateTime uses 1970 everywhere else, which did NOT have daylight savings in July
|
|
703 |
QTest::newRow("summer2") << QDateTime(QDate(1760, 6, 30), utcTime, Qt::UTC)
|
|
704 |
<< QDateTime(QDate(1760, 6, 30), localStandardTime, Qt::LocalTime);
|
|
705 |
#endif
|
|
706 |
QTest::newRow("summer3") << QDateTime(QDate(4000, 6, 30), utcTime, Qt::UTC)
|
|
707 |
<< QDateTime(QDate(4000, 6, 30), localDaylightTime, Qt::LocalTime);
|
|
708 |
}
|
|
709 |
|
|
710 |
QTest::newRow("msec") << QDateTime(QDate(4000, 6, 30), utcTime.addMSecs(1), Qt::UTC)
|
|
711 |
<< QDateTime(QDate(4000, 6, 30), localDaylightTime.addMSecs(1), Qt::LocalTime);
|
|
712 |
}
|
|
713 |
|
|
714 |
void tst_QDateTime::toTimeSpec()
|
|
715 |
{
|
|
716 |
if (europeanTimeZone) {
|
|
717 |
QFETCH(QDateTime, utc);
|
|
718 |
QFETCH(QDateTime, local);
|
|
719 |
|
|
720 |
QCOMPARE(utc.toTimeSpec(Qt::UTC), utc);
|
|
721 |
QCOMPARE(local.toTimeSpec(Qt::LocalTime), local);
|
|
722 |
#ifdef Q_OS_IRIX
|
|
723 |
QEXPECT_FAIL("summer2", "IRIX databases say 1970 had DST", Abort);
|
|
724 |
#endif
|
|
725 |
QCOMPARE(utc.toTimeSpec(Qt::LocalTime), local);
|
|
726 |
QCOMPARE(local.toTimeSpec(Qt::UTC), utc);
|
|
727 |
QCOMPARE(utc.toTimeSpec(Qt::UTC), local.toTimeSpec(Qt::UTC));
|
|
728 |
QCOMPARE(utc.toTimeSpec(Qt::LocalTime), local.toTimeSpec(Qt::LocalTime));
|
|
729 |
} else {
|
|
730 |
QSKIP("Not tested with timezone other than Central European (CET/CST)", SkipAll);
|
|
731 |
}
|
|
732 |
}
|
|
733 |
|
|
734 |
void tst_QDateTime::toLocalTime_data()
|
|
735 |
{
|
|
736 |
toTimeSpec_data();
|
|
737 |
}
|
|
738 |
|
|
739 |
void tst_QDateTime::toLocalTime()
|
|
740 |
{
|
|
741 |
if (europeanTimeZone) {
|
|
742 |
QFETCH(QDateTime, utc);
|
|
743 |
QFETCH(QDateTime, local);
|
|
744 |
|
|
745 |
QCOMPARE(local.toLocalTime(), local);
|
|
746 |
#ifdef Q_OS_IRIX
|
|
747 |
QEXPECT_FAIL("summer2", "IRIX databases say 1970 had DST", Abort);
|
|
748 |
#endif
|
|
749 |
QCOMPARE(utc.toLocalTime(), local);
|
|
750 |
QCOMPARE(utc.toLocalTime(), local.toLocalTime());
|
|
751 |
} else {
|
|
752 |
QSKIP("Not tested with timezone other than Central European (CET/CST)", SkipAll);
|
|
753 |
}
|
|
754 |
}
|
|
755 |
|
|
756 |
void tst_QDateTime::toUTC_data()
|
|
757 |
{
|
|
758 |
toTimeSpec_data();
|
|
759 |
}
|
|
760 |
|
|
761 |
void tst_QDateTime::toUTC()
|
|
762 |
{
|
|
763 |
if (europeanTimeZone) {
|
|
764 |
QFETCH(QDateTime, utc);
|
|
765 |
QFETCH(QDateTime, local);
|
|
766 |
|
|
767 |
QCOMPARE(utc.toUTC(), utc);
|
|
768 |
#ifdef Q_OS_IRIX
|
|
769 |
QEXPECT_FAIL("summer2", "IRIX databases say 1970 had DST", Abort);
|
|
770 |
#endif
|
|
771 |
QCOMPARE(local.toUTC(), utc);
|
|
772 |
QCOMPARE(utc.toUTC(), local.toUTC());
|
|
773 |
} else {
|
|
774 |
QSKIP("Not tested with timezone other than Central European (CET/CST)", SkipAll);
|
|
775 |
}
|
|
776 |
|
|
777 |
// To make sure bug 72713 never happens again
|
|
778 |
QDateTime dt = QDateTime::currentDateTime();
|
|
779 |
if(dt.time().msec() == 0){
|
|
780 |
dt.setTime(dt.time().addMSecs(1));
|
|
781 |
}
|
|
782 |
QString s = dt.toString("zzz");
|
|
783 |
QString t = dt.toUTC().toString("zzz");
|
|
784 |
QCOMPARE(s, t);
|
|
785 |
}
|
|
786 |
|
|
787 |
void tst_QDateTime::daysTo()
|
|
788 |
{
|
|
789 |
QDateTime dt1(QDate(1760, 1, 2), QTime());
|
|
790 |
QDateTime dt2(QDate(1760, 2, 2), QTime());
|
|
791 |
QDateTime dt3(QDate(1760, 3, 2), QTime());
|
|
792 |
|
|
793 |
QCOMPARE(dt1.daysTo(dt2), 31);
|
|
794 |
QCOMPARE(dt1.addDays(31), dt2);
|
|
795 |
|
|
796 |
QCOMPARE(dt2.daysTo(dt3), 29);
|
|
797 |
QCOMPARE(dt2.addDays(29), dt3);
|
|
798 |
|
|
799 |
QCOMPARE(dt1.daysTo(dt3), 60);
|
|
800 |
QCOMPARE(dt1.addDays(60), dt3);
|
|
801 |
|
|
802 |
QCOMPARE(dt2.daysTo(dt1), -31);
|
|
803 |
QCOMPARE(dt2.addDays(-31), dt1);
|
|
804 |
|
|
805 |
QCOMPARE(dt3.daysTo(dt2), -29);
|
|
806 |
QCOMPARE(dt3.addDays(-29), dt2);
|
|
807 |
|
|
808 |
QCOMPARE(dt3.daysTo(dt1), -60);
|
|
809 |
QCOMPARE(dt3.addDays(-60), dt1);
|
|
810 |
}
|
|
811 |
|
|
812 |
void tst_QDateTime::secsTo_data()
|
|
813 |
{
|
|
814 |
addSecs_data();
|
|
815 |
}
|
|
816 |
|
|
817 |
void tst_QDateTime::secsTo()
|
|
818 |
{
|
|
819 |
QFETCH(QDateTime, dt);
|
|
820 |
QFETCH(int, nsecs);
|
|
821 |
QFETCH(QDateTime, result);
|
|
822 |
|
|
823 |
#ifdef Q_OS_IRIX
|
|
824 |
QEXPECT_FAIL("cet4", "IRIX databases say 1970 had DST", Abort);
|
|
825 |
#endif
|
|
826 |
QCOMPARE(dt.secsTo(result), nsecs);
|
|
827 |
QCOMPARE(result.secsTo(dt), -nsecs);
|
|
828 |
QVERIFY((dt == result) == (0 == nsecs));
|
|
829 |
QVERIFY((dt != result) == (0 != nsecs));
|
|
830 |
QVERIFY((dt < result) == (0 < nsecs));
|
|
831 |
QVERIFY((dt <= result) == (0 <= nsecs));
|
|
832 |
QVERIFY((dt > result) == (0 > nsecs));
|
|
833 |
QVERIFY((dt >= result) == (0 >= nsecs));
|
|
834 |
}
|
|
835 |
|
|
836 |
void tst_QDateTime::currentDateTime()
|
|
837 |
{
|
|
838 |
#if defined(Q_OS_WINCE)
|
|
839 |
__time64_t buf1, buf2;
|
|
840 |
::_time64(&buf1);
|
|
841 |
#else
|
|
842 |
time_t buf1, buf2;
|
|
843 |
::time(&buf1);
|
|
844 |
#endif
|
|
845 |
QDateTime lowerBound;
|
|
846 |
lowerBound.setTime_t(buf1);
|
|
847 |
|
|
848 |
QDateTime dt1 = QDateTime::currentDateTime();
|
|
849 |
QDateTime dt2 = QDateTime::currentDateTime().toLocalTime();
|
|
850 |
QDateTime dt3 = QDateTime::currentDateTime().toUTC();
|
|
851 |
|
|
852 |
#if defined(Q_OS_WINCE)
|
|
853 |
::_time64(&buf2);
|
|
854 |
#else
|
|
855 |
::time(&buf2);
|
|
856 |
#endif
|
|
857 |
QDateTime upperBound;
|
|
858 |
upperBound.setTime_t(buf2);
|
|
859 |
upperBound = upperBound.addSecs(1);
|
|
860 |
|
|
861 |
QVERIFY(lowerBound < upperBound);
|
|
862 |
|
|
863 |
QVERIFY(lowerBound <= dt1);
|
|
864 |
QVERIFY(dt1 < upperBound);
|
|
865 |
QVERIFY(lowerBound <= dt2);
|
|
866 |
QVERIFY(dt2 < upperBound);
|
|
867 |
QVERIFY(lowerBound <= dt3);
|
|
868 |
QVERIFY(dt3 < upperBound);
|
|
869 |
|
|
870 |
QVERIFY(dt1.timeSpec() == Qt::LocalTime);
|
|
871 |
QVERIFY(dt2.timeSpec() == Qt::LocalTime);
|
|
872 |
QVERIFY(dt3.timeSpec() == Qt::UTC);
|
|
873 |
}
|
|
874 |
|
|
875 |
void tst_QDateTime::toTime_t_data()
|
|
876 |
{
|
|
877 |
QTest::addColumn<QString>("dateTimeStr");
|
|
878 |
QTest::addColumn<bool>("res");
|
|
879 |
|
|
880 |
QTest::newRow( "data1" ) << str( 1800, 1, 1, 12, 0, 0 ) << false;
|
|
881 |
QTest::newRow( "data2" ) << str( 1969, 1, 1, 12, 0, 0 ) << false;
|
|
882 |
QTest::newRow( "data3" ) << str( 2002, 1, 1, 12, 0, 0 ) << true;
|
|
883 |
QTest::newRow( "data4" ) << str( 2002, 6, 1, 12, 0, 0 ) << true;
|
|
884 |
QTest::newRow( "data5" ) << QString("INVALID") << false;
|
|
885 |
QTest::newRow( "data6" ) << str( 2038, 1, 1, 12, 0, 0 ) << true;
|
|
886 |
QTest::newRow( "data7" ) << str( 2063, 4, 5, 12, 0, 0 ) << true; // the day of First Contact
|
|
887 |
QTest::newRow( "data8" ) << str( 2107, 1, 1, 12, 0, 0 )
|
|
888 |
<< bool( sizeof(uint) > 32 && sizeof(time_t) > 32 );
|
|
889 |
}
|
|
890 |
|
|
891 |
void tst_QDateTime::toTime_t()
|
|
892 |
{
|
|
893 |
QFETCH( QString, dateTimeStr );
|
|
894 |
QDateTime datetime = dt( dateTimeStr );
|
|
895 |
|
|
896 |
uint asTime_t = datetime.toTime_t();
|
|
897 |
QFETCH( bool, res );
|
|
898 |
if (res) {
|
|
899 |
QVERIFY( asTime_t != (uint)-1 );
|
|
900 |
} else {
|
|
901 |
QVERIFY( asTime_t == (uint)-1 );
|
|
902 |
}
|
|
903 |
|
|
904 |
if ( asTime_t != (uint) -1 ) {
|
|
905 |
QDateTime datetime2 = QDateTime::fromTime_t( asTime_t );
|
|
906 |
QCOMPARE(datetime, datetime2);
|
|
907 |
}
|
|
908 |
}
|
|
909 |
|
|
910 |
void tst_QDateTime::task_137698()
|
|
911 |
{
|
|
912 |
// This bug is caused by QDateTime knowing more than it lets show
|
|
913 |
// Internally, if it knows, QDateTime stores a flag indicating if the time is
|
|
914 |
// DST or not. If it doesn't, it sets to "LocalUnknown"
|
|
915 |
//
|
|
916 |
// The problem happens if you start with a date in DST and then move to a date
|
|
917 |
// outside it (or vice-versa). Some functions did not reset the flag, which caused
|
|
918 |
// weird 1-hour jumps in time when addSecs() was called.
|
|
919 |
|
|
920 |
// WARNING: This test only works if there's a Daylight Savings Time change
|
|
921 |
// in the current locale between 2006-11-06 and 2006-10-16
|
|
922 |
// This is true for Central European Time
|
|
923 |
|
|
924 |
if (!europeanTimeZone)
|
|
925 |
QSKIP("Not tested with timezone other than Central European (CET/CEST)", SkipAll);
|
|
926 |
|
|
927 |
QDateTime dt = QDateTime(QDate(2006, 11, 6), QTime(0, 0, 0), Qt::LocalTime);
|
|
928 |
dt.setDate(QDate(2006, 10, 16));
|
|
929 |
dt = dt.addSecs(1);
|
|
930 |
QCOMPARE(dt.date(), QDate(2006, 10, 16));
|
|
931 |
QCOMPARE(dt.time(), QTime(0, 0, 1));
|
|
932 |
|
|
933 |
// now using fromTime_t
|
|
934 |
dt = QDateTime::fromTime_t(1162767600); // 2006-11-06 00:00:00 +0100
|
|
935 |
dt.setDate(QDate(2006, 10, 16));
|
|
936 |
dt = dt.addSecs (1);
|
|
937 |
QCOMPARE(dt.date(), QDate(2006, 10, 16));
|
|
938 |
QCOMPARE(dt.time(), QTime(0, 0, 1));
|
|
939 |
}
|
|
940 |
|
|
941 |
void tst_QDateTime::operator_eqeq()
|
|
942 |
{
|
|
943 |
QDateTime dt1(QDate(2004, 1, 2), QTime(2, 2, 3), Qt::LocalTime);
|
|
944 |
QDateTime dt2(QDate(2004, 1, 2), QTime(1, 2, 3), Qt::UTC);
|
|
945 |
|
|
946 |
QVERIFY(dt1 == dt1);
|
|
947 |
QVERIFY(!(dt1 != dt1));
|
|
948 |
|
|
949 |
QVERIFY(dt2 == dt2);
|
|
950 |
QVERIFY(!(dt2 != dt2));
|
|
951 |
|
|
952 |
QVERIFY(dt1 != QDateTime::currentDateTime());
|
|
953 |
QVERIFY(dt2 != QDateTime::currentDateTime());
|
|
954 |
|
|
955 |
QVERIFY(dt1.toUTC() == dt1.toUTC());
|
|
956 |
|
|
957 |
if (europeanTimeZone) {
|
|
958 |
QVERIFY(dt1.toUTC() == dt2);
|
|
959 |
QVERIFY(dt1 == dt2.toLocalTime());
|
|
960 |
}
|
|
961 |
}
|
|
962 |
|
|
963 |
void tst_QDateTime::toString_strformat_data()
|
|
964 |
{
|
|
965 |
QTest::addColumn<QDateTime>("dt");
|
|
966 |
QTest::addColumn<QString>("format");
|
|
967 |
QTest::addColumn<QString>("str");
|
|
968 |
|
|
969 |
QTest::newRow( "datetime0" ) << QDateTime() << QString("dd-MM-yyyy hh:mm:ss") << QString();
|
|
970 |
QTest::newRow( "datetime1" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999))
|
|
971 |
<< QString("dd-'mmddyy'MM-yyyy hh:mm:ss.zzz")
|
|
972 |
<< QString("31-mmddyy12-1999 23:59:59.999");
|
|
973 |
QTest::newRow( "datetime2" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999))
|
|
974 |
<< QString("dd-'apAP'MM-yyyy hh:mm:ss.zzz")
|
|
975 |
<< QString("31-apAP12-1999 23:59:59.999");
|
|
976 |
QTest::newRow( "datetime3" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999))
|
|
977 |
<< QString("Apdd-MM-yyyy hh:mm:ss.zzz")
|
|
978 |
<< QString("PMp31-12-1999 11:59:59.999");
|
|
979 |
QTest::newRow( "datetime4" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999))
|
|
980 |
<< QString("'ap'apdd-MM-yyyy 'AP'hh:mm:ss.zzz")
|
|
981 |
<< QString("appm31-12-1999 AP11:59:59.999");
|
|
982 |
QTest::newRow( "datetime5" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999))
|
|
983 |
<< QString("'''") << QString("'");
|
|
984 |
QTest::newRow( "datetime6" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999))
|
|
985 |
<< QString("'ap") << QString("ap");
|
|
986 |
QTest::newRow( "datetime7" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999))
|
|
987 |
<< QString("' ' 'hh' hh") << QString(" hh 23");
|
|
988 |
QTest::newRow( "datetime8" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999))
|
|
989 |
<< QString("d'foobar'") << QString("31foobar");
|
|
990 |
QTest::newRow( "datetime9" ) << QDateTime(QDate(1999, 12, 31), QTime(3, 59, 59, 999))
|
|
991 |
<< QString("hhhhh") << QString("03033");
|
|
992 |
QTest::newRow( "datetime10" ) << QDateTime(QDate(1999, 12, 31), QTime(3, 59, 59, 999))
|
|
993 |
<< QString("hhhhhaA") << QString("03033amAM");
|
|
994 |
QTest::newRow( "datetime11" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999))
|
|
995 |
<< QString("HHHhhhAaAPap") << QString("23231111PMpmPMpm");
|
|
996 |
QTest::newRow( "datetime12" ) << QDateTime(QDate(1999, 12, 31), QTime(3, 59, 59, 999))
|
|
997 |
<< QString("HHHhhhAaAPap") << QString("033033AMamAMam");
|
|
998 |
QTest::newRow( "datetime13" ) << QDateTime(QDate(1974, 12, 1), QTime(14, 14, 20))
|
|
999 |
<< QString("hh''mm''ss dd''MM''yyyy")
|
|
1000 |
<< QString("14'14'20 01'12'1974");
|
|
1001 |
}
|
|
1002 |
|
|
1003 |
void tst_QDateTime::toString_strformat()
|
|
1004 |
{
|
|
1005 |
QFETCH( QDateTime, dt );
|
|
1006 |
QFETCH( QString, format );
|
|
1007 |
QFETCH( QString, str );
|
|
1008 |
QCOMPARE( dt.toString( format ), str );
|
|
1009 |
}
|
|
1010 |
|
|
1011 |
void tst_QDateTime::fromStringTextDate_data()
|
|
1012 |
{
|
|
1013 |
QTest::addColumn<QString>("dateTime");
|
|
1014 |
QTest::addColumn<int>("dateFormat");
|
|
1015 |
QTest::addColumn<int>("day");
|
|
1016 |
QTest::addColumn<int>("month");
|
|
1017 |
QTest::addColumn<int>("year");
|
|
1018 |
QTest::addColumn<int>("hour");
|
|
1019 |
QTest::addColumn<int>("minute");
|
|
1020 |
QTest::addColumn<int>("second");
|
|
1021 |
QTest::addColumn<int>("msec");
|
|
1022 |
QTest::addColumn<int>("timeSpec");
|
|
1023 |
|
|
1024 |
QTest::newRow("task27910") << QString("Tue Jun 17 08:00:10 2003")
|
|
1025 |
<< int(Qt::TextDate)
|
|
1026 |
<< 17 << 6 << 2003 << 8 << 0 << 10 << 0
|
|
1027 |
<< int(Qt::LocalTime);
|
|
1028 |
QTest::newRow("task77042") << QString("2005-06-28T07:57:30.0010000000Z")
|
|
1029 |
<< int(Qt::ISODate)
|
|
1030 |
<< 28 << 6 << 2005 << 7 << 57 << 30 << 1
|
|
1031 |
<< int(Qt::UTC);
|
|
1032 |
|
|
1033 |
QTest::newRow("task77042-2") << QString("2005-06-28T07:57:30,0040000000Z")
|
|
1034 |
<< int(Qt::ISODate)
|
|
1035 |
<< 28 << 6 << 2005 << 7 << 57 << 30 << 4
|
|
1036 |
<< int(Qt::UTC);
|
|
1037 |
|
|
1038 |
QTest::newRow("task77042-3") << QString("2005-06-28T07:57:30,0015Z")
|
|
1039 |
<< int(Qt::ISODate)
|
|
1040 |
<< 28 << 6 << 2005 << 7 << 57 << 30 << 2
|
|
1041 |
<< int(Qt::UTC);
|
|
1042 |
|
|
1043 |
QTest::newRow("task77042-4") << QString("2005-06-28T07:57:30,0014Z")
|
|
1044 |
<< int(Qt::ISODate)
|
|
1045 |
<< 28 << 6 << 2005 << 7 << 57 << 30 << 1
|
|
1046 |
<< int(Qt::UTC);
|
|
1047 |
|
|
1048 |
QTest::newRow("task77042-5") << QString("2005-06-28T07:57:30,1Z")
|
|
1049 |
<< int(Qt::ISODate)
|
|
1050 |
<< 28 << 6 << 2005 << 7 << 57 << 30 << 100
|
|
1051 |
<< int(Qt::UTC);
|
|
1052 |
|
|
1053 |
QTest::newRow("task77042-6") << QString("2005-06-28T07:57:30,11")
|
|
1054 |
<< int(Qt::ISODate)
|
|
1055 |
<< 28 << 6 << 2005 << 7 << 57 << 30 << 110
|
|
1056 |
<< int(Qt::LocalTime);
|
|
1057 |
|
|
1058 |
QTest::newRow("Year 0999") << QString("Tue Jun 17 08:00:10 0999")
|
|
1059 |
<< int(Qt::TextDate)
|
|
1060 |
<< 17 << 6 << 999 << 8 << 0 << 10 << 0
|
|
1061 |
<< int(Qt::LocalTime);
|
|
1062 |
|
|
1063 |
QTest::newRow("Year 999") << QString("Tue Jun 17 08:00:10 999")
|
|
1064 |
<< int(Qt::TextDate)
|
|
1065 |
<< 17 << 6 << 999 << 8 << 0 << 10 << 0
|
|
1066 |
<< int(Qt::LocalTime);
|
|
1067 |
|
|
1068 |
QTest::newRow("Year 12345") << QString("Tue Jun 17 08:00:10 12345")
|
|
1069 |
<< int(Qt::TextDate)
|
|
1070 |
<< 17 << 6 << 12345 << 8 << 0 << 10 << 0
|
|
1071 |
<< int(Qt::LocalTime);
|
|
1072 |
|
|
1073 |
QTest::newRow("Year -4712") << QString("Tue Jan 1 00:01:02 -4712")
|
|
1074 |
<< int(Qt::TextDate)
|
|
1075 |
<< 1 << 1 << -4712 << 0 << 01 << 02 << 0
|
|
1076 |
<< int(Qt::LocalTime);
|
|
1077 |
}
|
|
1078 |
|
|
1079 |
void tst_QDateTime::fromStringTextDate()
|
|
1080 |
{
|
|
1081 |
QFETCH(QString, dateTime);
|
|
1082 |
QFETCH(int, dateFormat);
|
|
1083 |
QFETCH(int, day);
|
|
1084 |
QFETCH(int, month);
|
|
1085 |
QFETCH(int, year);
|
|
1086 |
QFETCH(int, hour);
|
|
1087 |
QFETCH(int, minute);
|
|
1088 |
QFETCH(int, second);
|
|
1089 |
QFETCH(int, msec);
|
|
1090 |
QFETCH(int, timeSpec);
|
|
1091 |
|
|
1092 |
QDateTime dt = QDateTime::fromString(dateTime, (Qt::DateFormat)dateFormat);
|
|
1093 |
QCOMPARE(dt.date().day(), day);
|
|
1094 |
QCOMPARE(dt.date().month(), month);
|
|
1095 |
QCOMPARE(dt.date().year(), year);
|
|
1096 |
QCOMPARE(dt.time().hour(), hour);
|
|
1097 |
QCOMPARE(dt.time().minute(), minute);
|
|
1098 |
QCOMPARE(dt.time().second(), second);
|
|
1099 |
QCOMPARE(dt.time().msec(), msec);
|
|
1100 |
QCOMPARE(int(dt.timeSpec()), timeSpec);
|
|
1101 |
}
|
|
1102 |
|
|
1103 |
void tst_QDateTime::dateFromStringFormat_data()
|
|
1104 |
{
|
|
1105 |
QTest::addColumn<QString>("string");
|
|
1106 |
QTest::addColumn<QString>("format");
|
|
1107 |
QTest::addColumn<QDate>("expected");
|
|
1108 |
|
|
1109 |
//get localized names
|
|
1110 |
QString january = QDate::longMonthName(1);
|
|
1111 |
QString february = QDate::longMonthName(2);
|
|
1112 |
QString march = QDate::longMonthName(3);
|
|
1113 |
QString august = QDate::longMonthName(8);
|
|
1114 |
QString mon = QDate::shortDayName(1);
|
|
1115 |
QString monday = QDate::longDayName(1);
|
|
1116 |
QString tuesday = QDate::longDayName(2);
|
|
1117 |
QString wednesday = QDate::longDayName(3);
|
|
1118 |
QString thursday = QDate::longDayName(4);
|
|
1119 |
QString friday = QDate::longDayName(5);
|
|
1120 |
QString saturday = QDate::longDayName(6);
|
|
1121 |
QString sunday = QDate::longDayName(7);
|
|
1122 |
|
|
1123 |
QTest::newRow("data0") << QString("") << QString("") << defDate();
|
|
1124 |
QTest::newRow("data1") << QString(" ") << QString("") << invalidDate();
|
|
1125 |
QTest::newRow("data2") << QString(" ") << QString(" ") << defDate();
|
|
1126 |
QTest::newRow("data3") << QString("-%$%#") << QString("$*(#@") << invalidDate();
|
|
1127 |
QTest::newRow("data4") << QString("d") << QString("'d'") << defDate();
|
|
1128 |
QTest::newRow("data5") << QString("101010") << QString("dMyy") << QDate(1910, 10, 10);
|
|
1129 |
QTest::newRow("data6") << QString("101010b") << QString("dMyy") << invalidDate();
|
|
1130 |
QTest::newRow("data7") << january << QString("MMMM") << defDate();
|
|
1131 |
QTest::newRow("data8") << QString("ball") << QString("balle") << invalidDate();
|
|
1132 |
QTest::newRow("data9") << QString("balleh") << QString("balleh") << defDate();
|
|
1133 |
QTest::newRow("data10") << QString("10.01.1") << QString("M.dd.d") << QDate(defDate().year(), 10, 1);
|
|
1134 |
QTest::newRow("data11") << QString("-1.01.1") << QString("M.dd.d") << invalidDate();
|
|
1135 |
QTest::newRow("data12") << QString("11010") << QString("dMMyy") << invalidDate();
|
|
1136 |
QTest::newRow("data13") << QString("-2") << QString("d") << invalidDate();
|
|
1137 |
QTest::newRow("data14") << QString("132") << QString("Md") << invalidDate();
|
|
1138 |
QTest::newRow("data15") << february << QString("MMMM") << QDate(defDate().year(), 2, 1);
|
|
1139 |
|
|
1140 |
QString date = mon + " " + august + " 8 2005";
|
|
1141 |
QTest::newRow("data16") << date << QString("ddd MMMM d yyyy") << QDate(2005, 8, 8);
|
|
1142 |
QTest::newRow("data17") << QString("2000:00") << QString("yyyy:yy") << QDate(2000, 1, 1);
|
|
1143 |
QTest::newRow("data18") << QString("1999:99") << QString("yyyy:yy") << QDate(1999, 1, 1);
|
|
1144 |
QTest::newRow("data19") << QString("2099:99") << QString("yyyy:yy") << QDate(2099, 1, 1);
|
|
1145 |
QTest::newRow("data20") << QString("2001:01") << QString("yyyy:yy") << QDate(2001, 1, 1);
|
|
1146 |
QTest::newRow("data21") << QString("99") << QString("yy") << QDate(1999, 1, 1);
|
|
1147 |
QTest::newRow("data22") << QString("01") << QString("yy") << QDate(1901, 1, 1);
|
|
1148 |
|
|
1149 |
QTest::newRow("data23") << monday << QString("dddd") << QDate(1900, 1, 1);
|
|
1150 |
QTest::newRow("data24") << tuesday << QString("dddd") << QDate(1900, 1, 2);
|
|
1151 |
QTest::newRow("data25") << wednesday << QString("dddd") << QDate(1900, 1, 3);
|
|
1152 |
QTest::newRow("data26") << thursday << QString("dddd") << QDate(1900, 1, 4);
|
|
1153 |
QTest::newRow("data26") << friday << QString("dddd") << QDate(1900, 1, 5);
|
|
1154 |
QTest::newRow("data27") << saturday << QString("dddd") << QDate(1900, 1, 6);
|
|
1155 |
QTest::newRow("data28") << sunday << QString("dddd") << QDate(1900, 1, 7);
|
|
1156 |
|
|
1157 |
QTest::newRow("data29") << monday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 2);
|
|
1158 |
QTest::newRow("data30") << tuesday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 3);
|
|
1159 |
QTest::newRow("data31") << wednesday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 4);
|
|
1160 |
QTest::newRow("data32") << thursday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 5);
|
|
1161 |
QTest::newRow("data33") << friday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 6);
|
|
1162 |
QTest::newRow("data34") << saturday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 7);
|
|
1163 |
QTest::newRow("data35") << sunday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 1);
|
|
1164 |
|
|
1165 |
QTest::newRow("data36 ") << tuesday + " 2007 " + march << QString("dddd yyyy MMMM") << QDate(2007, 3, 6);
|
|
1166 |
|
|
1167 |
}
|
|
1168 |
|
|
1169 |
|
|
1170 |
void tst_QDateTime::dateFromStringFormat()
|
|
1171 |
{
|
|
1172 |
QFETCH(QString, string);
|
|
1173 |
QFETCH(QString, format);
|
|
1174 |
QFETCH(QDate, expected);
|
|
1175 |
|
|
1176 |
QDate dt = QDate::fromString(string, format);
|
|
1177 |
QCOMPARE(dt, expected);
|
|
1178 |
}
|
|
1179 |
|
|
1180 |
void tst_QDateTime::timeFromStringFormat_data()
|
|
1181 |
{
|
|
1182 |
QTest::addColumn<QString>("string");
|
|
1183 |
QTest::addColumn<QString>("format");
|
|
1184 |
QTest::addColumn<QTime>("expected");
|
|
1185 |
|
|
1186 |
QTest::newRow("data0") << QString("1010") << QString("mmm") << QTime(0, 10, 0);
|
|
1187 |
QTest::newRow("data1") << QString("00") << QString("hm") << invalidTime();
|
|
1188 |
QTest::newRow("data2") << QString("10am") << QString("hap") << QTime(10, 0, 0);
|
|
1189 |
QTest::newRow("data3") << QString("10pm") << QString("hap") << QTime(22, 0, 0);
|
|
1190 |
QTest::newRow("data4") << QString("10pmam") << QString("hapap") << invalidTime();
|
|
1191 |
QTest::newRow("data5") << QString("1070") << QString("hhm") << invalidTime();
|
|
1192 |
QTest::newRow("data6") << QString("1011") << QString("hh") << invalidTime();
|
|
1193 |
QTest::newRow("data7") << QString("25") << QString("hh") << invalidTime();
|
|
1194 |
QTest::newRow("data8") << QString("22pm") << QString("Hap") << QTime(22, 0, 0);
|
|
1195 |
QTest::newRow("data9") << QString("2221") << QString("hhhh") << invalidTime();
|
|
1196 |
}
|
|
1197 |
|
|
1198 |
|
|
1199 |
void tst_QDateTime::timeFromStringFormat()
|
|
1200 |
{
|
|
1201 |
QFETCH(QString, string);
|
|
1202 |
QFETCH(QString, format);
|
|
1203 |
QFETCH(QTime, expected);
|
|
1204 |
|
|
1205 |
QTime dt = QTime::fromString(string, format);
|
|
1206 |
QCOMPARE(dt, expected);
|
|
1207 |
}
|
|
1208 |
|
|
1209 |
|
|
1210 |
void tst_QDateTime::dateTimeFromStringFormat_data()
|
|
1211 |
{
|
|
1212 |
QTest::addColumn<QString>("string");
|
|
1213 |
QTest::addColumn<QString>("format");
|
|
1214 |
QTest::addColumn<QDateTime>("expected");
|
|
1215 |
|
|
1216 |
QString january = QDate::longMonthName(1);
|
|
1217 |
QString oct = QDate::shortMonthName(10);
|
|
1218 |
QString december = QDate::longMonthName(12);
|
|
1219 |
QString thu = QDate::shortDayName(4);
|
|
1220 |
QString fri = QDate::shortDayName(5);
|
|
1221 |
QString date = "10 " + oct + " 10";
|
|
1222 |
|
|
1223 |
QTest::newRow("data0") << QString("101010") << QString("dMyy") << QDateTime(QDate(1910, 10, 10), QTime());
|
|
1224 |
QTest::newRow("data1") << QString("1020") << QString("sss") << invalidDateTime();
|
|
1225 |
QTest::newRow("data2") << QString("1010") << QString("sss") << QDateTime(defDate(), QTime(0, 0, 10));
|
|
1226 |
QTest::newRow("data3") << QString("10hello20") << QString("ss'hello'ss") << invalidDateTime();
|
|
1227 |
QTest::newRow("data4") << QString("10") << QString("''") << invalidDateTime();
|
|
1228 |
QTest::newRow("data5") << QString("10") << QString("'") << invalidDateTime();
|
|
1229 |
QTest::newRow("data6") << QString("pm") << QString("ap") << QDateTime(defDate(), QTime(12, 0, 0));
|
|
1230 |
QTest::newRow("data7") << QString("foo") << QString("ap") << invalidDateTime();
|
|
1231 |
QTest::newRow("data8") << QString("101010") << QString("dMyy") << QDateTime(QDate(1910, 10, 10), QTime());
|
|
1232 |
QTest::newRow("data9") << QString("101010") << QString("dMyy") << QDateTime(QDate(1910, 10, 10), QTime());
|
|
1233 |
QTest::newRow("data10") << QString("101010") << QString("dMyy") << QDateTime(QDate(1910, 10, 10), QTime());
|
|
1234 |
QTest::newRow("data11") << date << QString("dd MMM yy") << QDateTime(QDate(1910, 10, 10), QTime());
|
|
1235 |
date = fri + " " + december + " 3 2004";
|
|
1236 |
QTest::newRow("data12") << date << QString("ddd MMMM d yyyy") << QDateTime(QDate(2004, 12, 3), QTime());
|
|
1237 |
QTest::newRow("data13") << QString("30.02.2004") << QString("dd.MM.yyyy") << invalidDateTime();
|
|
1238 |
QTest::newRow("data14") << QString("32.01.2004") << QString("dd.MM.yyyy") << invalidDateTime();
|
|
1239 |
date = thu + " " + january + " 2004";
|
|
1240 |
QTest::newRow("data15") << date << QString("ddd MMMM yyyy") << QDateTime(QDate(2004, 1, 1), QTime());
|
|
1241 |
QTest::newRow("data16") << QString("2005-06-28T07:57:30.001Z")
|
|
1242 |
<< QString("yyyy-MM-ddThh:mm:ss.zZ")
|
|
1243 |
<< QDateTime(QDate(2005, 06, 28), QTime(07, 57, 30, 1));
|
|
1244 |
|
|
1245 |
}
|
|
1246 |
|
|
1247 |
void tst_QDateTime::dateTimeFromStringFormat()
|
|
1248 |
{
|
|
1249 |
QFETCH(QString, string);
|
|
1250 |
QFETCH(QString, format);
|
|
1251 |
QFETCH(QDateTime, expected);
|
|
1252 |
|
|
1253 |
QDateTime dt = QDateTime::fromString(string, format);
|
|
1254 |
|
|
1255 |
QCOMPARE(dt, expected);
|
|
1256 |
}
|
|
1257 |
|
|
1258 |
void tst_QDateTime::fromString()
|
|
1259 |
{
|
|
1260 |
QDateTime dt = QDateTime::fromString("Thu Jan 1 00:00:00 1970");
|
|
1261 |
QCOMPARE(dt, QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0)));
|
|
1262 |
|
|
1263 |
dt = QDateTime::fromString("Thu Jan 2 12:34 1970");
|
|
1264 |
QCOMPARE(dt, QDateTime(QDate(1970, 1, 2), QTime(12, 34, 0)));
|
|
1265 |
|
|
1266 |
dt = QDateTime::fromString("Thu Jan 1 00 1970");
|
|
1267 |
QCOMPARE(dt, QDateTime());
|
|
1268 |
|
|
1269 |
dt = QDateTime::fromString("Thu Jan 1 00:00:00:00 1970");
|
|
1270 |
QCOMPARE(dt, QDateTime());
|
|
1271 |
|
|
1272 |
dt = QDateTime::fromString(" Thu Jan 1 00:00:00 1970 ");
|
|
1273 |
QCOMPARE(dt, QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0)));
|
|
1274 |
|
|
1275 |
dt = QDateTime::fromString("Thu Jan 1 00:00:00");
|
|
1276 |
QCOMPARE(dt, QDateTime());
|
|
1277 |
|
|
1278 |
dt = QDateTime::fromString("2002-10-01", Qt::ISODate);
|
|
1279 |
QCOMPARE(dt, QDateTime(QDate(2002, 10, 1), QTime(0, 0, 0, 0)));
|
|
1280 |
|
|
1281 |
dt = QDateTime::fromString("Thu Jan 1 00:12:34 1970 GMT");
|
|
1282 |
QCOMPARE(dt.toUTC(), QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC));
|
|
1283 |
|
|
1284 |
dt = QDateTime::fromString("Thu Jan 1 00:12:34 1970 GMT-0300");
|
|
1285 |
QCOMPARE(dt.toUTC(), QDateTime(QDate(1970, 1, 1), QTime(3, 12, 34), Qt::UTC));
|
|
1286 |
|
|
1287 |
dt = QDateTime::fromString("Thu Jan 1 00:12:34 1970 GMT+0300");
|
|
1288 |
QCOMPARE(dt.toUTC(), QDateTime(QDate(1969, 12, 31), QTime(21, 12, 34), Qt::UTC));
|
|
1289 |
|
|
1290 |
dt = QDateTime::fromString("Thu Jan 1 00:12:34 1970 GMT+foo");
|
|
1291 |
QCOMPARE(dt, QDateTime());
|
|
1292 |
|
|
1293 |
dt = QDateTime::fromString("Thu Jan 1 00:12:34 1970 gmt");
|
|
1294 |
QCOMPARE(dt.toUTC(), QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC));
|
|
1295 |
|
|
1296 |
dt = QDateTime::fromString("Thu Jan 1 1970 00:00:00");
|
|
1297 |
QCOMPARE(dt, QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0)));
|
|
1298 |
|
|
1299 |
dt = QDateTime::fromString("Thu Jan 1 1970 00:12:34 GMT+0100");
|
|
1300 |
QCOMPARE(dt.toUTC(), QDateTime(QDate(1969, 12, 31), QTime(23, 12, 34), Qt::UTC));
|
|
1301 |
|
|
1302 |
QDate d = QDate::fromString("Thu Jan 1 1970");
|
|
1303 |
QCOMPARE(d, QDate(1970, 1, 1));
|
|
1304 |
|
|
1305 |
d = QDate::fromString(" Thu Jan 1 1970 ");
|
|
1306 |
QCOMPARE(d, QDate(1970, 1, 1));
|
|
1307 |
|
|
1308 |
d = QDate::fromString("Thu Jan 1");
|
|
1309 |
QCOMPARE(d, QDate());
|
|
1310 |
|
|
1311 |
QDateTime dt2(QDate(1999, 1, 18), QTime(11, 49, 00));
|
|
1312 |
|
|
1313 |
QLocale def;
|
|
1314 |
QLocale::setDefault(QLocale(QLocale::French, QLocale::France));
|
|
1315 |
|
|
1316 |
QCOMPARE(QDateTime::fromString(dt2.toString(Qt::DefaultLocaleShortDate), Qt::DefaultLocaleShortDate), dt2);
|
|
1317 |
QCOMPARE(QDateTime::fromString(dt2.toString(Qt::SystemLocaleShortDate), Qt::SystemLocaleShortDate), dt2);
|
|
1318 |
|
|
1319 |
// obsolete
|
|
1320 |
QCOMPARE(QDateTime::fromString(dt2.toString(Qt::SystemLocaleDate), Qt::SystemLocaleDate), dt2);
|
|
1321 |
QCOMPARE(QDateTime::fromString(dt2.toString(Qt::LocaleDate), Qt::LocaleDate), dt2);
|
|
1322 |
|
|
1323 |
// cannot have these because of bug in datetime parser
|
|
1324 |
// QCOMPARE(QDateTime::fromString(dt2.toString(Qt::DefaultLocaleLongDate), Qt::DefaultLocaleLongDate), dt2);
|
|
1325 |
// QCOMPARE(QDateTime::fromString(dt2.toString(Qt::SystemLocaleLongDate), Qt::SystemLocaleLongDate), dt2);
|
|
1326 |
|
|
1327 |
|
|
1328 |
// same thing for QDate and QTime
|
|
1329 |
|
|
1330 |
QCOMPARE(QDate::fromString(dt2.date().toString(Qt::DefaultLocaleShortDate), Qt::DefaultLocaleShortDate), dt2.date());
|
|
1331 |
QCOMPARE(QDate::fromString(dt2.date().toString(Qt::SystemLocaleShortDate), Qt::SystemLocaleShortDate), dt2.date());
|
|
1332 |
QCOMPARE(QDate::fromString(dt2.date().toString(Qt::LocaleDate), Qt::LocaleDate), dt2.date());
|
|
1333 |
QCOMPARE(QDate::fromString(dt2.date().toString(Qt::SystemLocaleDate), Qt::SystemLocaleDate), dt2.date());
|
|
1334 |
QCOMPARE(QTime::fromString(dt2.time().toString(Qt::DefaultLocaleShortDate), Qt::DefaultLocaleShortDate), dt2.time());
|
|
1335 |
QCOMPARE(QTime::fromString(dt2.time().toString(Qt::SystemLocaleShortDate), Qt::SystemLocaleShortDate), dt2.time());
|
|
1336 |
QCOMPARE(QTime::fromString(dt2.time().toString(Qt::LocaleDate), Qt::LocaleDate), dt2.time());
|
|
1337 |
QCOMPARE(QTime::fromString(dt2.time().toString(Qt::SystemLocaleDate), Qt::SystemLocaleDate), dt2.time());
|
|
1338 |
|
|
1339 |
QLocale::setDefault(def);
|
|
1340 |
}
|
|
1341 |
|
|
1342 |
void tst_QDateTime::fromString_LOCALE_ILDATE()
|
|
1343 |
{
|
|
1344 |
#ifdef Q_OS_WIN
|
|
1345 |
QString date1 = QLatin1String("Sun 1. Dec 13:02:00 1974");
|
|
1346 |
QString date2 = QLatin1String("Sun Dec 1 13:02:00 1974");
|
|
1347 |
|
|
1348 |
QDateTime ref(QDate(1974, 12, 1), QTime(13, 2));
|
|
1349 |
QCOMPARE(ref, QDateTime::fromString(date2, Qt::TextDate));
|
|
1350 |
QCOMPARE(ref, QDateTime::fromString(date1, Qt::TextDate));
|
|
1351 |
#else
|
|
1352 |
QSKIP("Windows only", SkipAll);
|
|
1353 |
#endif
|
|
1354 |
}
|
|
1355 |
|
|
1356 |
void tst_QDateTime::utcOffset()
|
|
1357 |
{
|
|
1358 |
/* Check default value. */
|
|
1359 |
QCOMPARE(QDateTime().utcOffset(), 0);
|
|
1360 |
}
|
|
1361 |
|
|
1362 |
void tst_QDateTime::setUtcOffset()
|
|
1363 |
{
|
|
1364 |
/* Basic tests. */
|
|
1365 |
{
|
|
1366 |
QDateTime dt(QDateTime::currentDateTime());
|
|
1367 |
dt.setTimeSpec(Qt::LocalTime);
|
|
1368 |
|
|
1369 |
dt.setUtcOffset(0);
|
|
1370 |
QCOMPARE(dt.utcOffset(), 0);
|
|
1371 |
QCOMPARE(dt.timeSpec(), Qt::UTC);
|
|
1372 |
|
|
1373 |
dt.setUtcOffset(-100);
|
|
1374 |
QCOMPARE(dt.utcOffset(), -100);
|
|
1375 |
QCOMPARE(dt.timeSpec(), Qt::OffsetFromUTC);
|
|
1376 |
}
|
|
1377 |
|
|
1378 |
/* Test detaching. */
|
|
1379 |
{
|
|
1380 |
QDateTime dt(QDateTime::currentDateTime());
|
|
1381 |
QDateTime dt2(dt);
|
|
1382 |
|
|
1383 |
dt.setUtcOffset(501);
|
|
1384 |
|
|
1385 |
QCOMPARE(dt.utcOffset(), 501);
|
|
1386 |
QCOMPARE(dt2.utcOffset(), 0);
|
|
1387 |
}
|
|
1388 |
|
|
1389 |
/* Check copying. */
|
|
1390 |
{
|
|
1391 |
QDateTime dt(QDateTime::currentDateTime());
|
|
1392 |
dt.setUtcOffset(502);
|
|
1393 |
QCOMPARE(dt.utcOffset(), 502);
|
|
1394 |
|
|
1395 |
QDateTime dt2(dt);
|
|
1396 |
QCOMPARE(dt2.utcOffset(), 502);
|
|
1397 |
}
|
|
1398 |
|
|
1399 |
/* Check assignment. */
|
|
1400 |
{
|
|
1401 |
QDateTime dt(QDateTime::currentDateTime());
|
|
1402 |
dt.setUtcOffset(502);
|
|
1403 |
QDateTime dt2;
|
|
1404 |
dt2 = dt;
|
|
1405 |
|
|
1406 |
QCOMPARE(dt2.utcOffset(), 502);
|
|
1407 |
}
|
|
1408 |
}
|
|
1409 |
|
|
1410 |
void tst_QDateTime::getDate()
|
|
1411 |
{
|
|
1412 |
{
|
|
1413 |
int y = -33, m = -44, d = -55;
|
|
1414 |
QDate date;
|
|
1415 |
date.getDate(&y, &m, &d);
|
|
1416 |
QVERIFY(date.year() == y);
|
|
1417 |
QVERIFY(date.month() == m);
|
|
1418 |
QVERIFY(date.day() == d);
|
|
1419 |
|
|
1420 |
date.getDate(0, 0, 0);
|
|
1421 |
}
|
|
1422 |
|
|
1423 |
{
|
|
1424 |
int y = -33, m = -44, d = -55;
|
|
1425 |
QDate date(1998, 5, 24);
|
|
1426 |
date.getDate(0, &m, 0);
|
|
1427 |
date.getDate(&y, 0, 0);
|
|
1428 |
date.getDate(0, 0, &d);
|
|
1429 |
|
|
1430 |
QVERIFY(date.year() == y);
|
|
1431 |
QVERIFY(date.month() == m);
|
|
1432 |
QVERIFY(date.day() == d);
|
|
1433 |
}
|
|
1434 |
}
|
|
1435 |
|
|
1436 |
void tst_QDateTime::fewDigitsInYear() const
|
|
1437 |
{
|
|
1438 |
const QDateTime three(QDate(300, 10, 11), QTime());
|
|
1439 |
QCOMPARE(three.toString(QLatin1String("yyyy-MM-dd")), QString::fromLatin1("0300-10-11"));
|
|
1440 |
|
|
1441 |
const QDateTime two(QDate(20, 10, 11), QTime());
|
|
1442 |
QCOMPARE(two.toString(QLatin1String("yyyy-MM-dd")), QString::fromLatin1("0020-10-11"));
|
|
1443 |
|
|
1444 |
const QDateTime yyTwo(QDate(30, 10, 11), QTime());
|
|
1445 |
QCOMPARE(yyTwo.toString(QLatin1String("yy-MM-dd")), QString::fromLatin1("30-10-11"));
|
|
1446 |
|
|
1447 |
const QDateTime yyOne(QDate(4, 10, 11), QTime());
|
|
1448 |
QCOMPARE(yyOne.toString(QLatin1String("yy-MM-dd")), QString::fromLatin1("04-10-11"));
|
|
1449 |
}
|
|
1450 |
|
|
1451 |
void tst_QDateTime::printNegativeYear() const
|
|
1452 |
{
|
|
1453 |
{
|
|
1454 |
QDateTime date(QDate(-20, 10, 11));
|
|
1455 |
QVERIFY(date.isValid());
|
|
1456 |
QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0020"));
|
|
1457 |
}
|
|
1458 |
|
|
1459 |
{
|
|
1460 |
QDateTime date(QDate(-3, 10, 11));
|
|
1461 |
QVERIFY(date.isValid());
|
|
1462 |
QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0003"));
|
|
1463 |
}
|
|
1464 |
|
|
1465 |
{
|
|
1466 |
QDateTime date(QDate(-400, 10, 11));
|
|
1467 |
QVERIFY(date.isValid());
|
|
1468 |
QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0400"));
|
|
1469 |
}
|
|
1470 |
}
|
|
1471 |
|
|
1472 |
void tst_QDateTime::roundtripGermanLocale() const
|
|
1473 |
{
|
|
1474 |
/* This code path should not result in warnings. */
|
|
1475 |
const QDateTime theDateTime(QDateTime::currentDateTime());
|
|
1476 |
theDateTime.fromString(theDateTime.toString(Qt::TextDate), Qt::TextDate);
|
|
1477 |
}
|
|
1478 |
|
|
1479 |
void tst_QDateTime::utcOffsetLessThan() const
|
|
1480 |
{
|
|
1481 |
QDateTime dt1(QDate(2002, 10, 10), QTime(0, 0, 0));
|
|
1482 |
QDateTime dt2(dt1);
|
|
1483 |
|
|
1484 |
dt1.setUtcOffset(-(2 * 60 * 60)); // Minus two hours.
|
|
1485 |
dt2.setUtcOffset(-(3 * 60 * 60)); // Minus three hours.
|
|
1486 |
|
|
1487 |
QVERIFY(dt1 != dt2);
|
|
1488 |
QVERIFY(!(dt1 == dt2));
|
|
1489 |
QVERIFY(dt1 < dt2);
|
|
1490 |
QVERIFY(!(dt2 < dt1));
|
|
1491 |
}
|
|
1492 |
|
|
1493 |
Q_DECLARE_METATYPE(Qt::DateFormat)
|
|
1494 |
|
|
1495 |
QTEST_APPLESS_MAIN(tst_QDateTime)
|
|
1496 |
#include "tst_qdatetime.moc"
|