author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 08 Apr 2010 14:19:33 +0300 | |
branch | RCL_3 |
changeset 7 | 3f74d0d4af4c |
parent 5 | d3bac044e0f0 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
450 |
qDebug() << str3; |
0 | 451 |
QVERIFY(!str3.isEmpty()); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
452 |
//check for date/time components in any order |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
453 |
//year may be 2 or 4 digits |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
454 |
QVERIFY(str3.contains("95")); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
455 |
//day and month may be in numeric or word form |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
456 |
QVERIFY(str3.contains("12")); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
457 |
QVERIFY(str3.contains("34")); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
458 |
//seconds may be absent |
0 | 459 |
} |
460 |
||
461 |
void tst_QDateTime::addDays() |
|
462 |
{ |
|
463 |
for (int pass = 0; pass < 2; ++pass) { |
|
464 |
QDateTime dt(QDate(2004, 1, 1), QTime(12, 34, 56), pass == 0 ? Qt::LocalTime : Qt::UTC); |
|
465 |
dt = dt.addDays(185); |
|
466 |
QVERIFY(dt.date().year() == 2004 && dt.date().month() == 7 && dt.date().day() == 4); |
|
467 |
QVERIFY(dt.time().hour() == 12 && dt.time().minute() == 34 && dt.time().second() == 56 |
|
468 |
&& dt.time().msec() == 0); |
|
469 |
QCOMPARE(dt.timeSpec(), (pass == 0 ? Qt::LocalTime : Qt::UTC)); |
|
470 |
||
471 |
dt = dt.addDays(-185); |
|
472 |
QCOMPARE(dt.date(), QDate(2004, 1, 1)); |
|
473 |
QCOMPARE(dt.time(), QTime(12, 34, 56)); |
|
474 |
} |
|
475 |
||
476 |
QDateTime dt(QDate(1752, 9, 14), QTime(12, 34, 56)); |
|
477 |
while (dt.date().year() < 8000) { |
|
478 |
int year = dt.date().year(); |
|
479 |
if (QDate::isLeapYear(year + 1)) |
|
480 |
dt = dt.addDays(366); |
|
481 |
else |
|
482 |
dt = dt.addDays(365); |
|
483 |
QCOMPARE(dt.date(), QDate(year + 1, 9, 14)); |
|
484 |
QCOMPARE(dt.time(), QTime(12, 34, 56)); |
|
485 |
} |
|
486 |
||
487 |
// ### test invalid QDateTime() |
|
488 |
} |
|
489 |
||
490 |
||
491 |
void tst_QDateTime::addMonths_data() |
|
492 |
{ |
|
493 |
QTest::addColumn<int>("months"); |
|
494 |
QTest::addColumn<QDate>("dt"); |
|
495 |
||
496 |
QTest::newRow("-15") << -15 << QDate(2002, 10, 31); |
|
497 |
QTest::newRow("-14") << -14 << QDate(2002, 11, 30); |
|
498 |
QTest::newRow("-13") << -13 << QDate(2002, 12, 31); |
|
499 |
QTest::newRow("-12") << -12 << QDate(2003, 1, 31); |
|
500 |
||
501 |
QTest::newRow("-11") << -11 << QDate(2003, 2, 28); |
|
502 |
QTest::newRow("-10") << -10 << QDate(2003, 3, 31); |
|
503 |
QTest::newRow("-9") << -9 << QDate(2003, 4, 30); |
|
504 |
QTest::newRow("-8") << -8 << QDate(2003, 5, 31); |
|
505 |
QTest::newRow("-7") << -7 << QDate(2003, 6, 30); |
|
506 |
QTest::newRow("-6") << -6 << QDate(2003, 7, 31); |
|
507 |
QTest::newRow("-5") << -5 << QDate(2003, 8, 31); |
|
508 |
QTest::newRow("-4") << -4 << QDate(2003, 9, 30); |
|
509 |
QTest::newRow("-3") << -3 << QDate(2003, 10, 31); |
|
510 |
QTest::newRow("-2") << -2 << QDate(2003, 11, 30); |
|
511 |
QTest::newRow("-1") << -1 << QDate(2003, 12, 31); |
|
512 |
QTest::newRow("0") << 0 << QDate(2004, 1, 31); |
|
513 |
QTest::newRow("1") << 1 << QDate(2004, 2, 29); |
|
514 |
QTest::newRow("2") << 2 << QDate(2004, 3, 31); |
|
515 |
QTest::newRow("3") << 3 << QDate(2004, 4, 30); |
|
516 |
QTest::newRow("4") << 4 << QDate(2004, 5, 31); |
|
517 |
QTest::newRow("5") << 5 << QDate(2004, 6, 30); |
|
518 |
QTest::newRow("6") << 6 << QDate(2004, 7, 31); |
|
519 |
QTest::newRow("7") << 7 << QDate(2004, 8, 31); |
|
520 |
QTest::newRow("8") << 8 << QDate(2004, 9, 30); |
|
521 |
QTest::newRow("9") << 9 << QDate(2004, 10, 31); |
|
522 |
QTest::newRow("10") << 10 << QDate(2004, 11, 30); |
|
523 |
QTest::newRow("11") << 11 << QDate(2004, 12, 31); |
|
524 |
QTest::newRow("12") << 12 << QDate(2005, 1, 31); |
|
525 |
QTest::newRow("13") << 13 << QDate(2005, 2, 28); |
|
526 |
QTest::newRow("14") << 14 << QDate(2005, 3, 31); |
|
527 |
QTest::newRow("15") << 15 << QDate(2005, 4, 30); |
|
528 |
} |
|
529 |
||
530 |
void tst_QDateTime::addMonths() |
|
531 |
{ |
|
532 |
QFETCH(QDate, dt); |
|
533 |
QFETCH(int, months); |
|
534 |
||
535 |
QDateTime start(QDate(2004, 1, 31), QTime(12, 34, 56)); |
|
536 |
QCOMPARE(start.addMonths(months).date(), dt); |
|
537 |
QCOMPARE(start.addMonths(months).time(), QTime(12, 34, 56)); |
|
538 |
} |
|
539 |
||
540 |
void tst_QDateTime::addYears_data() |
|
541 |
{ |
|
542 |
QTest::addColumn<int>("years1"); |
|
543 |
QTest::addColumn<int>("years2"); |
|
544 |
QTest::addColumn<QDate>("start"); |
|
545 |
QTest::addColumn<QDate>("dt"); |
|
546 |
||
547 |
QTest::newRow("0") << 0 << 0 << QDate(1752, 9, 14) << QDate(1752, 9, 14); |
|
548 |
QTest::newRow("4000 - 4000") << 4000 << -4000 << QDate(1752, 9, 14) << QDate(1752, 9, 14); |
|
549 |
QTest::newRow("10") << 10 << 0 << QDate(1752, 9, 14) << QDate(1762, 9, 14); |
|
550 |
QTest::newRow("0 leap year") << 0 << 0 << QDate(1760, 2, 29) << QDate(1760, 2, 29); |
|
551 |
QTest::newRow("1 leap year") << 1 << 0 << QDate(1760, 2, 29) << QDate(1761, 2, 28); |
|
552 |
QTest::newRow("2 leap year") << 2 << 0 << QDate(1760, 2, 29) << QDate(1762, 2, 28); |
|
553 |
QTest::newRow("3 leap year") << 3 << 0 << QDate(1760, 2, 29) << QDate(1763, 2, 28); |
|
554 |
QTest::newRow("4 leap year") << 4 << 0 << QDate(1760, 2, 29) << QDate(1764, 2, 29); |
|
555 |
||
556 |
QTest::newRow("toNegative1") << -2000 << 0 << QDate(1752, 9, 14) << QDate(-249, 9, 14); |
|
557 |
QTest::newRow("toNegative2") << -1752 << 0 << QDate(1752, 9, 14) << QDate(-1, 9, 14); |
|
558 |
QTest::newRow("toNegative3") << -1751 << 0 << QDate(1752, 9, 14) << QDate(1, 9, 14); |
|
559 |
QTest::newRow("toPositive1") << 2000 << 0 << QDate(-1752, 9, 14) << QDate(249, 9, 14); |
|
560 |
QTest::newRow("toPositive2") << 1752 << 0 << QDate(-1752, 9, 14) << QDate(1, 9, 14); |
|
561 |
QTest::newRow("toPositive3") << 1751 << 0 << QDate(-1752, 9, 14) << QDate(-1, 9, 14); |
|
562 |
} |
|
563 |
||
564 |
void tst_QDateTime::addYears() |
|
565 |
{ |
|
566 |
QFETCH(int, years1); |
|
567 |
QFETCH(int, years2); |
|
568 |
QFETCH(QDate, start); |
|
569 |
QFETCH(QDate, dt); |
|
570 |
||
571 |
QDateTime startdt(start, QTime(14, 25, 36)); |
|
572 |
QCOMPARE(startdt.addYears(years1).addYears(years2).date(), dt); |
|
573 |
QCOMPARE(startdt.addYears(years1).addYears(years2).time(), QTime(14, 25, 36)); |
|
574 |
} |
|
575 |
||
576 |
void tst_QDateTime::addSecs_data() |
|
577 |
{ |
|
578 |
QTest::addColumn<QDateTime>("dt"); |
|
579 |
QTest::addColumn<int>("nsecs"); |
|
580 |
QTest::addColumn<QDateTime>("result"); |
|
581 |
||
582 |
QTime standardTime(12, 34, 56); |
|
583 |
QTime daylightTime(13, 34, 56); |
|
584 |
||
585 |
QTest::newRow("utc0") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::UTC) << 86400 |
|
586 |
<< QDateTime(QDate(2004, 1, 2), standardTime, Qt::UTC); |
|
587 |
QTest::newRow("utc1") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::UTC) << (86400 * 185) |
|
588 |
<< QDateTime(QDate(2004, 7, 4), standardTime, Qt::UTC); |
|
589 |
QTest::newRow("utc2") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::UTC) << (86400 * 366) |
|
590 |
<< QDateTime(QDate(2005, 1, 1), standardTime, Qt::UTC); |
|
591 |
QTest::newRow("utc3") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::UTC) << 86400 |
|
592 |
<< QDateTime(QDate(1760, 1, 2), standardTime, Qt::UTC); |
|
593 |
QTest::newRow("utc4") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::UTC) << (86400 * 185) |
|
594 |
<< QDateTime(QDate(1760, 7, 4), standardTime, Qt::UTC); |
|
595 |
QTest::newRow("utc5") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::UTC) << (86400 * 366) |
|
596 |
<< QDateTime(QDate(1761, 1, 1), standardTime, Qt::UTC); |
|
597 |
QTest::newRow("utc6") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::UTC) << 86400 |
|
598 |
<< QDateTime(QDate(4000, 1, 2), standardTime, Qt::UTC); |
|
599 |
QTest::newRow("utc7") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::UTC) << (86400 * 185) |
|
600 |
<< QDateTime(QDate(4000, 7, 4), standardTime, Qt::UTC); |
|
601 |
QTest::newRow("utc8") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::UTC) << (86400 * 366) |
|
602 |
<< QDateTime(QDate(4001, 1, 1), standardTime, Qt::UTC); |
|
603 |
QTest::newRow("utc9") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::UTC) << 0 |
|
604 |
<< QDateTime(QDate(4000, 1, 1), standardTime, Qt::UTC); |
|
605 |
||
606 |
if (europeanTimeZone) { |
|
607 |
QTest::newRow("cet0") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::LocalTime) << 86400 |
|
608 |
<< QDateTime(QDate(2004, 1, 2), standardTime, Qt::LocalTime); |
|
609 |
QTest::newRow("cet1") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::LocalTime) << (86400 * 185) |
|
610 |
<< QDateTime(QDate(2004, 7, 4), daylightTime, Qt::LocalTime); |
|
611 |
QTest::newRow("cet2") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::LocalTime) << (86400 * 366) |
|
612 |
<< QDateTime(QDate(2005, 1, 1), standardTime, Qt::LocalTime); |
|
613 |
QTest::newRow("cet3") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::LocalTime) << 86400 |
|
614 |
<< QDateTime(QDate(1760, 1, 2), standardTime, Qt::LocalTime); |
|
615 |
#ifdef Q_OS_WIN |
|
616 |
// QDateTime uses 1980 on Windows, which did have daylight savings in July |
|
617 |
QTest::newRow("cet4") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::LocalTime) << (86400 * 185) |
|
618 |
<< QDateTime(QDate(1760, 7, 4), daylightTime, Qt::LocalTime); |
|
619 |
#else |
|
620 |
// QDateTime uses 1970 everywhere else, which did NOT have daylight savings in July |
|
621 |
QTest::newRow("cet4") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::LocalTime) << (86400 * 185) |
|
622 |
<< QDateTime(QDate(1760, 7, 4), standardTime, Qt::LocalTime); |
|
623 |
#endif |
|
624 |
QTest::newRow("cet5") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::LocalTime) << (86400 * 366) |
|
625 |
<< QDateTime(QDate(1761, 1, 1), standardTime, Qt::LocalTime); |
|
626 |
QTest::newRow("cet6") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::LocalTime) << 86400 |
|
627 |
<< QDateTime(QDate(4000, 1, 2), standardTime, Qt::LocalTime); |
|
628 |
QTest::newRow("cet7") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::LocalTime) << (86400 * 185) |
|
629 |
<< QDateTime(QDate(4000, 7, 4), daylightTime, Qt::LocalTime); |
|
630 |
QTest::newRow("cet8") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::LocalTime) << (86400 * 366) |
|
631 |
<< QDateTime(QDate(4001, 1, 1), standardTime, Qt::LocalTime); |
|
632 |
QTest::newRow("cet9") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::LocalTime) << 0 |
|
633 |
<< QDateTime(QDate(4000, 1, 1), standardTime, Qt::LocalTime); |
|
634 |
} |
|
635 |
||
636 |
// Year sign change |
|
637 |
QTest::newRow("toNegative") << QDateTime(QDate(1, 1, 1), QTime(0, 0, 0)) |
|
638 |
<< -1 |
|
639 |
<< QDateTime(QDate(-1, 12, 31), QTime(23, 59, 59)); |
|
640 |
QTest::newRow("toPositive") << QDateTime(QDate(-1, 12, 31), QTime(23, 59, 59)) |
|
641 |
<< 1 |
|
642 |
<< QDateTime(QDate(1, 1, 1), QTime(0, 0, 0)); |
|
643 |
||
644 |
// Gregorian/Julian switchover |
|
645 |
QTest::newRow("toGregorian") << QDateTime(QDate(1582, 10, 4), QTime(23, 59, 59)) |
|
646 |
<< 1 |
|
647 |
<< QDateTime(QDate(1582, 10, 15), QTime(0, 0, 0)); |
|
648 |
QTest::newRow("toJulian") << QDateTime(QDate(1582, 10, 15), QTime(0, 0, 0)) |
|
649 |
<< -1 |
|
650 |
<< QDateTime(QDate(1582, 10, 4), QTime(23, 59, 59)); |
|
651 |
} |
|
652 |
||
653 |
void tst_QDateTime::addSecs() |
|
654 |
{ |
|
655 |
QFETCH(QDateTime, dt); |
|
656 |
QFETCH(int, nsecs); |
|
657 |
QFETCH(QDateTime, result); |
|
658 |
||
659 |
#ifdef Q_OS_IRIX |
|
660 |
QEXPECT_FAIL("cet4", "IRIX databases say 1970 had DST", Abort); |
|
661 |
#endif |
|
662 |
QCOMPARE(dt.addSecs(nsecs), result); |
|
663 |
QCOMPARE(result.addSecs(-nsecs), dt); |
|
664 |
} |
|
665 |
||
666 |
void tst_QDateTime::addMSecs_data() |
|
667 |
{ |
|
668 |
addSecs_data(); |
|
669 |
} |
|
670 |
||
671 |
void tst_QDateTime::addMSecs() |
|
672 |
{ |
|
673 |
QFETCH(QDateTime, dt); |
|
674 |
QFETCH(int, nsecs); |
|
675 |
QFETCH(QDateTime, result); |
|
676 |
||
677 |
#ifdef Q_OS_IRIX |
|
678 |
QEXPECT_FAIL("cet4", "IRIX databases say 1970 had DST", Abort); |
|
679 |
#endif |
|
680 |
QCOMPARE(dt.addMSecs(qint64(nsecs) * 1000), result); |
|
681 |
QCOMPARE(result.addMSecs(qint64(-nsecs) * 1000), dt); |
|
682 |
} |
|
683 |
||
684 |
void tst_QDateTime::toTimeSpec_data() |
|
685 |
{ |
|
686 |
QTest::addColumn<QDateTime>("utc"); |
|
687 |
QTest::addColumn<QDateTime>("local"); |
|
688 |
||
689 |
QTime utcTime(4, 20, 30); |
|
690 |
QTime localStandardTime(5, 20, 30); |
|
691 |
QTime localDaylightTime(6, 20, 30); |
|
692 |
||
693 |
QTest::newRow("winter1") << QDateTime(QDate(2004, 1, 1), utcTime, Qt::UTC) |
|
694 |
<< QDateTime(QDate(2004, 1, 1), localStandardTime, Qt::LocalTime); |
|
695 |
QTest::newRow("winter2") << QDateTime(QDate(2004, 2, 29), utcTime, Qt::UTC) |
|
696 |
<< QDateTime(QDate(2004, 2, 29), localStandardTime, Qt::LocalTime); |
|
697 |
QTest::newRow("winter3") << QDateTime(QDate(1760, 2, 29), utcTime, Qt::UTC) |
|
698 |
<< QDateTime(QDate(1760, 2, 29), localStandardTime, Qt::LocalTime); |
|
699 |
QTest::newRow("winter4") << QDateTime(QDate(6000, 2, 29), utcTime, Qt::UTC) |
|
700 |
<< QDateTime(QDate(6000, 2, 29), localStandardTime, Qt::LocalTime); |
|
701 |
||
702 |
if (europeanTimeZone) { |
|
703 |
QTest::newRow("summer1") << QDateTime(QDate(2004, 6, 30), utcTime, Qt::UTC) |
|
704 |
<< QDateTime(QDate(2004, 6, 30), localDaylightTime, Qt::LocalTime); |
|
705 |
#ifdef Q_OS_WIN |
|
706 |
// QDateTime uses 1980 on Windows, which did have daylight savings in July |
|
707 |
QTest::newRow("summer2") << QDateTime(QDate(1760, 6, 30), utcTime, Qt::UTC) |
|
708 |
<< QDateTime(QDate(1760, 6, 30), localDaylightTime, Qt::LocalTime); |
|
709 |
#else |
|
710 |
// QDateTime uses 1970 everywhere else, which did NOT have daylight savings in July |
|
711 |
QTest::newRow("summer2") << QDateTime(QDate(1760, 6, 30), utcTime, Qt::UTC) |
|
712 |
<< QDateTime(QDate(1760, 6, 30), localStandardTime, Qt::LocalTime); |
|
713 |
#endif |
|
714 |
QTest::newRow("summer3") << QDateTime(QDate(4000, 6, 30), utcTime, Qt::UTC) |
|
715 |
<< QDateTime(QDate(4000, 6, 30), localDaylightTime, Qt::LocalTime); |
|
716 |
} |
|
717 |
||
718 |
QTest::newRow("msec") << QDateTime(QDate(4000, 6, 30), utcTime.addMSecs(1), Qt::UTC) |
|
719 |
<< QDateTime(QDate(4000, 6, 30), localDaylightTime.addMSecs(1), Qt::LocalTime); |
|
720 |
} |
|
721 |
||
722 |
void tst_QDateTime::toTimeSpec() |
|
723 |
{ |
|
724 |
if (europeanTimeZone) { |
|
725 |
QFETCH(QDateTime, utc); |
|
726 |
QFETCH(QDateTime, local); |
|
727 |
||
728 |
QCOMPARE(utc.toTimeSpec(Qt::UTC), utc); |
|
729 |
QCOMPARE(local.toTimeSpec(Qt::LocalTime), local); |
|
730 |
#ifdef Q_OS_IRIX |
|
731 |
QEXPECT_FAIL("summer2", "IRIX databases say 1970 had DST", Abort); |
|
732 |
#endif |
|
733 |
QCOMPARE(utc.toTimeSpec(Qt::LocalTime), local); |
|
734 |
QCOMPARE(local.toTimeSpec(Qt::UTC), utc); |
|
735 |
QCOMPARE(utc.toTimeSpec(Qt::UTC), local.toTimeSpec(Qt::UTC)); |
|
736 |
QCOMPARE(utc.toTimeSpec(Qt::LocalTime), local.toTimeSpec(Qt::LocalTime)); |
|
737 |
} else { |
|
738 |
QSKIP("Not tested with timezone other than Central European (CET/CST)", SkipAll); |
|
739 |
} |
|
740 |
} |
|
741 |
||
742 |
void tst_QDateTime::toLocalTime_data() |
|
743 |
{ |
|
744 |
toTimeSpec_data(); |
|
745 |
} |
|
746 |
||
747 |
void tst_QDateTime::toLocalTime() |
|
748 |
{ |
|
749 |
if (europeanTimeZone) { |
|
750 |
QFETCH(QDateTime, utc); |
|
751 |
QFETCH(QDateTime, local); |
|
752 |
||
753 |
QCOMPARE(local.toLocalTime(), local); |
|
754 |
#ifdef Q_OS_IRIX |
|
755 |
QEXPECT_FAIL("summer2", "IRIX databases say 1970 had DST", Abort); |
|
756 |
#endif |
|
757 |
QCOMPARE(utc.toLocalTime(), local); |
|
758 |
QCOMPARE(utc.toLocalTime(), local.toLocalTime()); |
|
759 |
} else { |
|
760 |
QSKIP("Not tested with timezone other than Central European (CET/CST)", SkipAll); |
|
761 |
} |
|
762 |
} |
|
763 |
||
764 |
void tst_QDateTime::toUTC_data() |
|
765 |
{ |
|
766 |
toTimeSpec_data(); |
|
767 |
} |
|
768 |
||
769 |
void tst_QDateTime::toUTC() |
|
770 |
{ |
|
771 |
if (europeanTimeZone) { |
|
772 |
QFETCH(QDateTime, utc); |
|
773 |
QFETCH(QDateTime, local); |
|
774 |
||
775 |
QCOMPARE(utc.toUTC(), utc); |
|
776 |
#ifdef Q_OS_IRIX |
|
777 |
QEXPECT_FAIL("summer2", "IRIX databases say 1970 had DST", Abort); |
|
778 |
#endif |
|
779 |
QCOMPARE(local.toUTC(), utc); |
|
780 |
QCOMPARE(utc.toUTC(), local.toUTC()); |
|
781 |
} else { |
|
782 |
QSKIP("Not tested with timezone other than Central European (CET/CST)", SkipAll); |
|
783 |
} |
|
784 |
||
785 |
// To make sure bug 72713 never happens again |
|
786 |
QDateTime dt = QDateTime::currentDateTime(); |
|
787 |
if(dt.time().msec() == 0){ |
|
788 |
dt.setTime(dt.time().addMSecs(1)); |
|
789 |
} |
|
790 |
QString s = dt.toString("zzz"); |
|
791 |
QString t = dt.toUTC().toString("zzz"); |
|
792 |
QCOMPARE(s, t); |
|
793 |
} |
|
794 |
||
795 |
void tst_QDateTime::daysTo() |
|
796 |
{ |
|
797 |
QDateTime dt1(QDate(1760, 1, 2), QTime()); |
|
798 |
QDateTime dt2(QDate(1760, 2, 2), QTime()); |
|
799 |
QDateTime dt3(QDate(1760, 3, 2), QTime()); |
|
800 |
||
801 |
QCOMPARE(dt1.daysTo(dt2), 31); |
|
802 |
QCOMPARE(dt1.addDays(31), dt2); |
|
803 |
||
804 |
QCOMPARE(dt2.daysTo(dt3), 29); |
|
805 |
QCOMPARE(dt2.addDays(29), dt3); |
|
806 |
||
807 |
QCOMPARE(dt1.daysTo(dt3), 60); |
|
808 |
QCOMPARE(dt1.addDays(60), dt3); |
|
809 |
||
810 |
QCOMPARE(dt2.daysTo(dt1), -31); |
|
811 |
QCOMPARE(dt2.addDays(-31), dt1); |
|
812 |
||
813 |
QCOMPARE(dt3.daysTo(dt2), -29); |
|
814 |
QCOMPARE(dt3.addDays(-29), dt2); |
|
815 |
||
816 |
QCOMPARE(dt3.daysTo(dt1), -60); |
|
817 |
QCOMPARE(dt3.addDays(-60), dt1); |
|
818 |
} |
|
819 |
||
820 |
void tst_QDateTime::secsTo_data() |
|
821 |
{ |
|
822 |
addSecs_data(); |
|
823 |
} |
|
824 |
||
825 |
void tst_QDateTime::secsTo() |
|
826 |
{ |
|
827 |
QFETCH(QDateTime, dt); |
|
828 |
QFETCH(int, nsecs); |
|
829 |
QFETCH(QDateTime, result); |
|
830 |
||
831 |
#ifdef Q_OS_IRIX |
|
832 |
QEXPECT_FAIL("cet4", "IRIX databases say 1970 had DST", Abort); |
|
833 |
#endif |
|
834 |
QCOMPARE(dt.secsTo(result), nsecs); |
|
835 |
QCOMPARE(result.secsTo(dt), -nsecs); |
|
836 |
QVERIFY((dt == result) == (0 == nsecs)); |
|
837 |
QVERIFY((dt != result) == (0 != nsecs)); |
|
838 |
QVERIFY((dt < result) == (0 < nsecs)); |
|
839 |
QVERIFY((dt <= result) == (0 <= nsecs)); |
|
840 |
QVERIFY((dt > result) == (0 > nsecs)); |
|
841 |
QVERIFY((dt >= result) == (0 >= nsecs)); |
|
842 |
} |
|
843 |
||
844 |
void tst_QDateTime::currentDateTime() |
|
845 |
{ |
|
846 |
#if defined(Q_OS_WINCE) |
|
847 |
__time64_t buf1, buf2; |
|
848 |
::_time64(&buf1); |
|
849 |
#else |
|
850 |
time_t buf1, buf2; |
|
851 |
::time(&buf1); |
|
852 |
#endif |
|
853 |
QDateTime lowerBound; |
|
854 |
lowerBound.setTime_t(buf1); |
|
855 |
||
856 |
QDateTime dt1 = QDateTime::currentDateTime(); |
|
857 |
QDateTime dt2 = QDateTime::currentDateTime().toLocalTime(); |
|
858 |
QDateTime dt3 = QDateTime::currentDateTime().toUTC(); |
|
859 |
||
860 |
#if defined(Q_OS_WINCE) |
|
861 |
::_time64(&buf2); |
|
862 |
#else |
|
863 |
::time(&buf2); |
|
864 |
#endif |
|
865 |
QDateTime upperBound; |
|
866 |
upperBound.setTime_t(buf2); |
|
867 |
upperBound = upperBound.addSecs(1); |
|
868 |
||
869 |
QVERIFY(lowerBound < upperBound); |
|
870 |
||
871 |
QVERIFY(lowerBound <= dt1); |
|
872 |
QVERIFY(dt1 < upperBound); |
|
873 |
QVERIFY(lowerBound <= dt2); |
|
874 |
QVERIFY(dt2 < upperBound); |
|
875 |
QVERIFY(lowerBound <= dt3); |
|
876 |
QVERIFY(dt3 < upperBound); |
|
877 |
||
878 |
QVERIFY(dt1.timeSpec() == Qt::LocalTime); |
|
879 |
QVERIFY(dt2.timeSpec() == Qt::LocalTime); |
|
880 |
QVERIFY(dt3.timeSpec() == Qt::UTC); |
|
881 |
} |
|
882 |
||
883 |
void tst_QDateTime::toTime_t_data() |
|
884 |
{ |
|
885 |
QTest::addColumn<QString>("dateTimeStr"); |
|
886 |
QTest::addColumn<bool>("res"); |
|
887 |
||
888 |
QTest::newRow( "data1" ) << str( 1800, 1, 1, 12, 0, 0 ) << false; |
|
889 |
QTest::newRow( "data2" ) << str( 1969, 1, 1, 12, 0, 0 ) << false; |
|
890 |
QTest::newRow( "data3" ) << str( 2002, 1, 1, 12, 0, 0 ) << true; |
|
891 |
QTest::newRow( "data4" ) << str( 2002, 6, 1, 12, 0, 0 ) << true; |
|
892 |
QTest::newRow( "data5" ) << QString("INVALID") << false; |
|
893 |
QTest::newRow( "data6" ) << str( 2038, 1, 1, 12, 0, 0 ) << true; |
|
894 |
QTest::newRow( "data7" ) << str( 2063, 4, 5, 12, 0, 0 ) << true; // the day of First Contact |
|
895 |
QTest::newRow( "data8" ) << str( 2107, 1, 1, 12, 0, 0 ) |
|
896 |
<< bool( sizeof(uint) > 32 && sizeof(time_t) > 32 ); |
|
897 |
} |
|
898 |
||
899 |
void tst_QDateTime::toTime_t() |
|
900 |
{ |
|
901 |
QFETCH( QString, dateTimeStr ); |
|
902 |
QDateTime datetime = dt( dateTimeStr ); |
|
903 |
||
904 |
uint asTime_t = datetime.toTime_t(); |
|
905 |
QFETCH( bool, res ); |
|
906 |
if (res) { |
|
907 |
QVERIFY( asTime_t != (uint)-1 ); |
|
908 |
} else { |
|
909 |
QVERIFY( asTime_t == (uint)-1 ); |
|
910 |
} |
|
911 |
||
912 |
if ( asTime_t != (uint) -1 ) { |
|
913 |
QDateTime datetime2 = QDateTime::fromTime_t( asTime_t ); |
|
914 |
QCOMPARE(datetime, datetime2); |
|
915 |
} |
|
916 |
} |
|
917 |
||
918 |
void tst_QDateTime::task_137698() |
|
919 |
{ |
|
920 |
// This bug is caused by QDateTime knowing more than it lets show |
|
921 |
// Internally, if it knows, QDateTime stores a flag indicating if the time is |
|
922 |
// DST or not. If it doesn't, it sets to "LocalUnknown" |
|
923 |
// |
|
924 |
// The problem happens if you start with a date in DST and then move to a date |
|
925 |
// outside it (or vice-versa). Some functions did not reset the flag, which caused |
|
926 |
// weird 1-hour jumps in time when addSecs() was called. |
|
927 |
||
928 |
// WARNING: This test only works if there's a Daylight Savings Time change |
|
929 |
// in the current locale between 2006-11-06 and 2006-10-16 |
|
930 |
// This is true for Central European Time |
|
931 |
||
932 |
if (!europeanTimeZone) |
|
933 |
QSKIP("Not tested with timezone other than Central European (CET/CEST)", SkipAll); |
|
934 |
||
935 |
QDateTime dt = QDateTime(QDate(2006, 11, 6), QTime(0, 0, 0), Qt::LocalTime); |
|
936 |
dt.setDate(QDate(2006, 10, 16)); |
|
937 |
dt = dt.addSecs(1); |
|
938 |
QCOMPARE(dt.date(), QDate(2006, 10, 16)); |
|
939 |
QCOMPARE(dt.time(), QTime(0, 0, 1)); |
|
940 |
||
941 |
// now using fromTime_t |
|
942 |
dt = QDateTime::fromTime_t(1162767600); // 2006-11-06 00:00:00 +0100 |
|
943 |
dt.setDate(QDate(2006, 10, 16)); |
|
944 |
dt = dt.addSecs (1); |
|
945 |
QCOMPARE(dt.date(), QDate(2006, 10, 16)); |
|
946 |
QCOMPARE(dt.time(), QTime(0, 0, 1)); |
|
947 |
} |
|
948 |
||
949 |
void tst_QDateTime::operator_eqeq() |
|
950 |
{ |
|
951 |
QDateTime dt1(QDate(2004, 1, 2), QTime(2, 2, 3), Qt::LocalTime); |
|
952 |
QDateTime dt2(QDate(2004, 1, 2), QTime(1, 2, 3), Qt::UTC); |
|
953 |
||
954 |
QVERIFY(dt1 == dt1); |
|
955 |
QVERIFY(!(dt1 != dt1)); |
|
956 |
||
957 |
QVERIFY(dt2 == dt2); |
|
958 |
QVERIFY(!(dt2 != dt2)); |
|
959 |
||
960 |
QVERIFY(dt1 != QDateTime::currentDateTime()); |
|
961 |
QVERIFY(dt2 != QDateTime::currentDateTime()); |
|
962 |
||
963 |
QVERIFY(dt1.toUTC() == dt1.toUTC()); |
|
964 |
||
965 |
if (europeanTimeZone) { |
|
966 |
QVERIFY(dt1.toUTC() == dt2); |
|
967 |
QVERIFY(dt1 == dt2.toLocalTime()); |
|
968 |
} |
|
969 |
} |
|
970 |
||
971 |
void tst_QDateTime::toString_strformat_data() |
|
972 |
{ |
|
973 |
QTest::addColumn<QDateTime>("dt"); |
|
974 |
QTest::addColumn<QString>("format"); |
|
975 |
QTest::addColumn<QString>("str"); |
|
976 |
||
977 |
QTest::newRow( "datetime0" ) << QDateTime() << QString("dd-MM-yyyy hh:mm:ss") << QString(); |
|
978 |
QTest::newRow( "datetime1" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999)) |
|
979 |
<< QString("dd-'mmddyy'MM-yyyy hh:mm:ss.zzz") |
|
980 |
<< QString("31-mmddyy12-1999 23:59:59.999"); |
|
981 |
QTest::newRow( "datetime2" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999)) |
|
982 |
<< QString("dd-'apAP'MM-yyyy hh:mm:ss.zzz") |
|
983 |
<< QString("31-apAP12-1999 23:59:59.999"); |
|
984 |
QTest::newRow( "datetime3" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999)) |
|
985 |
<< QString("Apdd-MM-yyyy hh:mm:ss.zzz") |
|
986 |
<< QString("PMp31-12-1999 11:59:59.999"); |
|
987 |
QTest::newRow( "datetime4" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999)) |
|
988 |
<< QString("'ap'apdd-MM-yyyy 'AP'hh:mm:ss.zzz") |
|
989 |
<< QString("appm31-12-1999 AP11:59:59.999"); |
|
990 |
QTest::newRow( "datetime5" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999)) |
|
991 |
<< QString("'''") << QString("'"); |
|
992 |
QTest::newRow( "datetime6" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999)) |
|
993 |
<< QString("'ap") << QString("ap"); |
|
994 |
QTest::newRow( "datetime7" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999)) |
|
995 |
<< QString("' ' 'hh' hh") << QString(" hh 23"); |
|
996 |
QTest::newRow( "datetime8" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999)) |
|
997 |
<< QString("d'foobar'") << QString("31foobar"); |
|
998 |
QTest::newRow( "datetime9" ) << QDateTime(QDate(1999, 12, 31), QTime(3, 59, 59, 999)) |
|
999 |
<< QString("hhhhh") << QString("03033"); |
|
1000 |
QTest::newRow( "datetime10" ) << QDateTime(QDate(1999, 12, 31), QTime(3, 59, 59, 999)) |
|
1001 |
<< QString("hhhhhaA") << QString("03033amAM"); |
|
1002 |
QTest::newRow( "datetime11" ) << QDateTime(QDate(1999, 12, 31), QTime(23, 59, 59, 999)) |
|
1003 |
<< QString("HHHhhhAaAPap") << QString("23231111PMpmPMpm"); |
|
1004 |
QTest::newRow( "datetime12" ) << QDateTime(QDate(1999, 12, 31), QTime(3, 59, 59, 999)) |
|
1005 |
<< QString("HHHhhhAaAPap") << QString("033033AMamAMam"); |
|
1006 |
QTest::newRow( "datetime13" ) << QDateTime(QDate(1974, 12, 1), QTime(14, 14, 20)) |
|
1007 |
<< QString("hh''mm''ss dd''MM''yyyy") |
|
1008 |
<< QString("14'14'20 01'12'1974"); |
|
1009 |
} |
|
1010 |
||
1011 |
void tst_QDateTime::toString_strformat() |
|
1012 |
{ |
|
1013 |
QFETCH( QDateTime, dt ); |
|
1014 |
QFETCH( QString, format ); |
|
1015 |
QFETCH( QString, str ); |
|
1016 |
QCOMPARE( dt.toString( format ), str ); |
|
1017 |
} |
|
1018 |
||
1019 |
void tst_QDateTime::fromStringTextDate_data() |
|
1020 |
{ |
|
1021 |
QTest::addColumn<QString>("dateTime"); |
|
1022 |
QTest::addColumn<int>("dateFormat"); |
|
1023 |
QTest::addColumn<int>("day"); |
|
1024 |
QTest::addColumn<int>("month"); |
|
1025 |
QTest::addColumn<int>("year"); |
|
1026 |
QTest::addColumn<int>("hour"); |
|
1027 |
QTest::addColumn<int>("minute"); |
|
1028 |
QTest::addColumn<int>("second"); |
|
1029 |
QTest::addColumn<int>("msec"); |
|
1030 |
QTest::addColumn<int>("timeSpec"); |
|
1031 |
||
1032 |
QTest::newRow("task27910") << QString("Tue Jun 17 08:00:10 2003") |
|
1033 |
<< int(Qt::TextDate) |
|
1034 |
<< 17 << 6 << 2003 << 8 << 0 << 10 << 0 |
|
1035 |
<< int(Qt::LocalTime); |
|
1036 |
QTest::newRow("task77042") << QString("2005-06-28T07:57:30.0010000000Z") |
|
1037 |
<< int(Qt::ISODate) |
|
1038 |
<< 28 << 6 << 2005 << 7 << 57 << 30 << 1 |
|
1039 |
<< int(Qt::UTC); |
|
1040 |
||
1041 |
QTest::newRow("task77042-2") << QString("2005-06-28T07:57:30,0040000000Z") |
|
1042 |
<< int(Qt::ISODate) |
|
1043 |
<< 28 << 6 << 2005 << 7 << 57 << 30 << 4 |
|
1044 |
<< int(Qt::UTC); |
|
1045 |
||
1046 |
QTest::newRow("task77042-3") << QString("2005-06-28T07:57:30,0015Z") |
|
1047 |
<< int(Qt::ISODate) |
|
1048 |
<< 28 << 6 << 2005 << 7 << 57 << 30 << 2 |
|
1049 |
<< int(Qt::UTC); |
|
1050 |
||
1051 |
QTest::newRow("task77042-4") << QString("2005-06-28T07:57:30,0014Z") |
|
1052 |
<< int(Qt::ISODate) |
|
1053 |
<< 28 << 6 << 2005 << 7 << 57 << 30 << 1 |
|
1054 |
<< int(Qt::UTC); |
|
1055 |
||
1056 |
QTest::newRow("task77042-5") << QString("2005-06-28T07:57:30,1Z") |
|
1057 |
<< int(Qt::ISODate) |
|
1058 |
<< 28 << 6 << 2005 << 7 << 57 << 30 << 100 |
|
1059 |
<< int(Qt::UTC); |
|
1060 |
||
1061 |
QTest::newRow("task77042-6") << QString("2005-06-28T07:57:30,11") |
|
1062 |
<< int(Qt::ISODate) |
|
1063 |
<< 28 << 6 << 2005 << 7 << 57 << 30 << 110 |
|
1064 |
<< int(Qt::LocalTime); |
|
1065 |
||
1066 |
QTest::newRow("Year 0999") << QString("Tue Jun 17 08:00:10 0999") |
|
1067 |
<< int(Qt::TextDate) |
|
1068 |
<< 17 << 6 << 999 << 8 << 0 << 10 << 0 |
|
1069 |
<< int(Qt::LocalTime); |
|
1070 |
||
1071 |
QTest::newRow("Year 999") << QString("Tue Jun 17 08:00:10 999") |
|
1072 |
<< int(Qt::TextDate) |
|
1073 |
<< 17 << 6 << 999 << 8 << 0 << 10 << 0 |
|
1074 |
<< int(Qt::LocalTime); |
|
1075 |
||
1076 |
QTest::newRow("Year 12345") << QString("Tue Jun 17 08:00:10 12345") |
|
1077 |
<< int(Qt::TextDate) |
|
1078 |
<< 17 << 6 << 12345 << 8 << 0 << 10 << 0 |
|
1079 |
<< int(Qt::LocalTime); |
|
1080 |
||
1081 |
QTest::newRow("Year -4712") << QString("Tue Jan 1 00:01:02 -4712") |
|
1082 |
<< int(Qt::TextDate) |
|
1083 |
<< 1 << 1 << -4712 << 0 << 01 << 02 << 0 |
|
1084 |
<< int(Qt::LocalTime); |
|
1085 |
} |
|
1086 |
||
1087 |
void tst_QDateTime::fromStringTextDate() |
|
1088 |
{ |
|
1089 |
QFETCH(QString, dateTime); |
|
1090 |
QFETCH(int, dateFormat); |
|
1091 |
QFETCH(int, day); |
|
1092 |
QFETCH(int, month); |
|
1093 |
QFETCH(int, year); |
|
1094 |
QFETCH(int, hour); |
|
1095 |
QFETCH(int, minute); |
|
1096 |
QFETCH(int, second); |
|
1097 |
QFETCH(int, msec); |
|
1098 |
QFETCH(int, timeSpec); |
|
1099 |
||
1100 |
QDateTime dt = QDateTime::fromString(dateTime, (Qt::DateFormat)dateFormat); |
|
1101 |
QCOMPARE(dt.date().day(), day); |
|
1102 |
QCOMPARE(dt.date().month(), month); |
|
1103 |
QCOMPARE(dt.date().year(), year); |
|
1104 |
QCOMPARE(dt.time().hour(), hour); |
|
1105 |
QCOMPARE(dt.time().minute(), minute); |
|
1106 |
QCOMPARE(dt.time().second(), second); |
|
1107 |
QCOMPARE(dt.time().msec(), msec); |
|
1108 |
QCOMPARE(int(dt.timeSpec()), timeSpec); |
|
1109 |
} |
|
1110 |
||
1111 |
void tst_QDateTime::dateFromStringFormat_data() |
|
1112 |
{ |
|
1113 |
QTest::addColumn<QString>("string"); |
|
1114 |
QTest::addColumn<QString>("format"); |
|
1115 |
QTest::addColumn<QDate>("expected"); |
|
1116 |
||
1117 |
//get localized names |
|
1118 |
QString january = QDate::longMonthName(1); |
|
1119 |
QString february = QDate::longMonthName(2); |
|
1120 |
QString march = QDate::longMonthName(3); |
|
1121 |
QString august = QDate::longMonthName(8); |
|
1122 |
QString mon = QDate::shortDayName(1); |
|
1123 |
QString monday = QDate::longDayName(1); |
|
1124 |
QString tuesday = QDate::longDayName(2); |
|
1125 |
QString wednesday = QDate::longDayName(3); |
|
1126 |
QString thursday = QDate::longDayName(4); |
|
1127 |
QString friday = QDate::longDayName(5); |
|
1128 |
QString saturday = QDate::longDayName(6); |
|
1129 |
QString sunday = QDate::longDayName(7); |
|
1130 |
||
1131 |
QTest::newRow("data0") << QString("") << QString("") << defDate(); |
|
1132 |
QTest::newRow("data1") << QString(" ") << QString("") << invalidDate(); |
|
1133 |
QTest::newRow("data2") << QString(" ") << QString(" ") << defDate(); |
|
1134 |
QTest::newRow("data3") << QString("-%$%#") << QString("$*(#@") << invalidDate(); |
|
1135 |
QTest::newRow("data4") << QString("d") << QString("'d'") << defDate(); |
|
1136 |
QTest::newRow("data5") << QString("101010") << QString("dMyy") << QDate(1910, 10, 10); |
|
1137 |
QTest::newRow("data6") << QString("101010b") << QString("dMyy") << invalidDate(); |
|
1138 |
QTest::newRow("data7") << january << QString("MMMM") << defDate(); |
|
1139 |
QTest::newRow("data8") << QString("ball") << QString("balle") << invalidDate(); |
|
1140 |
QTest::newRow("data9") << QString("balleh") << QString("balleh") << defDate(); |
|
1141 |
QTest::newRow("data10") << QString("10.01.1") << QString("M.dd.d") << QDate(defDate().year(), 10, 1); |
|
1142 |
QTest::newRow("data11") << QString("-1.01.1") << QString("M.dd.d") << invalidDate(); |
|
1143 |
QTest::newRow("data12") << QString("11010") << QString("dMMyy") << invalidDate(); |
|
1144 |
QTest::newRow("data13") << QString("-2") << QString("d") << invalidDate(); |
|
1145 |
QTest::newRow("data14") << QString("132") << QString("Md") << invalidDate(); |
|
1146 |
QTest::newRow("data15") << february << QString("MMMM") << QDate(defDate().year(), 2, 1); |
|
1147 |
||
1148 |
QString date = mon + " " + august + " 8 2005"; |
|
1149 |
QTest::newRow("data16") << date << QString("ddd MMMM d yyyy") << QDate(2005, 8, 8); |
|
1150 |
QTest::newRow("data17") << QString("2000:00") << QString("yyyy:yy") << QDate(2000, 1, 1); |
|
1151 |
QTest::newRow("data18") << QString("1999:99") << QString("yyyy:yy") << QDate(1999, 1, 1); |
|
1152 |
QTest::newRow("data19") << QString("2099:99") << QString("yyyy:yy") << QDate(2099, 1, 1); |
|
1153 |
QTest::newRow("data20") << QString("2001:01") << QString("yyyy:yy") << QDate(2001, 1, 1); |
|
1154 |
QTest::newRow("data21") << QString("99") << QString("yy") << QDate(1999, 1, 1); |
|
1155 |
QTest::newRow("data22") << QString("01") << QString("yy") << QDate(1901, 1, 1); |
|
1156 |
||
1157 |
QTest::newRow("data23") << monday << QString("dddd") << QDate(1900, 1, 1); |
|
1158 |
QTest::newRow("data24") << tuesday << QString("dddd") << QDate(1900, 1, 2); |
|
1159 |
QTest::newRow("data25") << wednesday << QString("dddd") << QDate(1900, 1, 3); |
|
1160 |
QTest::newRow("data26") << thursday << QString("dddd") << QDate(1900, 1, 4); |
|
1161 |
QTest::newRow("data26") << friday << QString("dddd") << QDate(1900, 1, 5); |
|
1162 |
QTest::newRow("data27") << saturday << QString("dddd") << QDate(1900, 1, 6); |
|
1163 |
QTest::newRow("data28") << sunday << QString("dddd") << QDate(1900, 1, 7); |
|
1164 |
||
1165 |
QTest::newRow("data29") << monday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 2); |
|
1166 |
QTest::newRow("data30") << tuesday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 3); |
|
1167 |
QTest::newRow("data31") << wednesday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 4); |
|
1168 |
QTest::newRow("data32") << thursday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 5); |
|
1169 |
QTest::newRow("data33") << friday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 6); |
|
1170 |
QTest::newRow("data34") << saturday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 7); |
|
1171 |
QTest::newRow("data35") << sunday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 1); |
|
1172 |
||
1173 |
QTest::newRow("data36 ") << tuesday + " 2007 " + march << QString("dddd yyyy MMMM") << QDate(2007, 3, 6); |
|
1174 |
||
1175 |
} |
|
1176 |
||
1177 |
||
1178 |
void tst_QDateTime::dateFromStringFormat() |
|
1179 |
{ |
|
1180 |
QFETCH(QString, string); |
|
1181 |
QFETCH(QString, format); |
|
1182 |
QFETCH(QDate, expected); |
|
1183 |
||
1184 |
QDate dt = QDate::fromString(string, format); |
|
1185 |
QCOMPARE(dt, expected); |
|
1186 |
} |
|
1187 |
||
1188 |
void tst_QDateTime::timeFromStringFormat_data() |
|
1189 |
{ |
|
1190 |
QTest::addColumn<QString>("string"); |
|
1191 |
QTest::addColumn<QString>("format"); |
|
1192 |
QTest::addColumn<QTime>("expected"); |
|
1193 |
||
1194 |
QTest::newRow("data0") << QString("1010") << QString("mmm") << QTime(0, 10, 0); |
|
1195 |
QTest::newRow("data1") << QString("00") << QString("hm") << invalidTime(); |
|
1196 |
QTest::newRow("data2") << QString("10am") << QString("hap") << QTime(10, 0, 0); |
|
1197 |
QTest::newRow("data3") << QString("10pm") << QString("hap") << QTime(22, 0, 0); |
|
1198 |
QTest::newRow("data4") << QString("10pmam") << QString("hapap") << invalidTime(); |
|
1199 |
QTest::newRow("data5") << QString("1070") << QString("hhm") << invalidTime(); |
|
1200 |
QTest::newRow("data6") << QString("1011") << QString("hh") << invalidTime(); |
|
1201 |
QTest::newRow("data7") << QString("25") << QString("hh") << invalidTime(); |
|
1202 |
QTest::newRow("data8") << QString("22pm") << QString("Hap") << QTime(22, 0, 0); |
|
1203 |
QTest::newRow("data9") << QString("2221") << QString("hhhh") << invalidTime(); |
|
1204 |
} |
|
1205 |
||
1206 |
||
1207 |
void tst_QDateTime::timeFromStringFormat() |
|
1208 |
{ |
|
1209 |
QFETCH(QString, string); |
|
1210 |
QFETCH(QString, format); |
|
1211 |
QFETCH(QTime, expected); |
|
1212 |
||
1213 |
QTime dt = QTime::fromString(string, format); |
|
1214 |
QCOMPARE(dt, expected); |
|
1215 |
} |
|
1216 |
||
1217 |
||
1218 |
void tst_QDateTime::dateTimeFromStringFormat_data() |
|
1219 |
{ |
|
1220 |
QTest::addColumn<QString>("string"); |
|
1221 |
QTest::addColumn<QString>("format"); |
|
1222 |
QTest::addColumn<QDateTime>("expected"); |
|
1223 |
||
1224 |
QString january = QDate::longMonthName(1); |
|
1225 |
QString oct = QDate::shortMonthName(10); |
|
1226 |
QString december = QDate::longMonthName(12); |
|
1227 |
QString thu = QDate::shortDayName(4); |
|
1228 |
QString fri = QDate::shortDayName(5); |
|
1229 |
QString date = "10 " + oct + " 10"; |
|
1230 |
||
1231 |
QTest::newRow("data0") << QString("101010") << QString("dMyy") << QDateTime(QDate(1910, 10, 10), QTime()); |
|
1232 |
QTest::newRow("data1") << QString("1020") << QString("sss") << invalidDateTime(); |
|
1233 |
QTest::newRow("data2") << QString("1010") << QString("sss") << QDateTime(defDate(), QTime(0, 0, 10)); |
|
1234 |
QTest::newRow("data3") << QString("10hello20") << QString("ss'hello'ss") << invalidDateTime(); |
|
1235 |
QTest::newRow("data4") << QString("10") << QString("''") << invalidDateTime(); |
|
1236 |
QTest::newRow("data5") << QString("10") << QString("'") << invalidDateTime(); |
|
1237 |
QTest::newRow("data6") << QString("pm") << QString("ap") << QDateTime(defDate(), QTime(12, 0, 0)); |
|
1238 |
QTest::newRow("data7") << QString("foo") << QString("ap") << invalidDateTime(); |
|
1239 |
QTest::newRow("data8") << QString("101010") << QString("dMyy") << QDateTime(QDate(1910, 10, 10), QTime()); |
|
1240 |
QTest::newRow("data9") << QString("101010") << QString("dMyy") << QDateTime(QDate(1910, 10, 10), QTime()); |
|
1241 |
QTest::newRow("data10") << QString("101010") << QString("dMyy") << QDateTime(QDate(1910, 10, 10), QTime()); |
|
1242 |
QTest::newRow("data11") << date << QString("dd MMM yy") << QDateTime(QDate(1910, 10, 10), QTime()); |
|
1243 |
date = fri + " " + december + " 3 2004"; |
|
1244 |
QTest::newRow("data12") << date << QString("ddd MMMM d yyyy") << QDateTime(QDate(2004, 12, 3), QTime()); |
|
1245 |
QTest::newRow("data13") << QString("30.02.2004") << QString("dd.MM.yyyy") << invalidDateTime(); |
|
1246 |
QTest::newRow("data14") << QString("32.01.2004") << QString("dd.MM.yyyy") << invalidDateTime(); |
|
1247 |
date = thu + " " + january + " 2004"; |
|
1248 |
QTest::newRow("data15") << date << QString("ddd MMMM yyyy") << QDateTime(QDate(2004, 1, 1), QTime()); |
|
1249 |
QTest::newRow("data16") << QString("2005-06-28T07:57:30.001Z") |
|
1250 |
<< QString("yyyy-MM-ddThh:mm:ss.zZ") |
|
1251 |
<< QDateTime(QDate(2005, 06, 28), QTime(07, 57, 30, 1)); |
|
1252 |
||
1253 |
} |
|
1254 |
||
1255 |
void tst_QDateTime::dateTimeFromStringFormat() |
|
1256 |
{ |
|
1257 |
QFETCH(QString, string); |
|
1258 |
QFETCH(QString, format); |
|
1259 |
QFETCH(QDateTime, expected); |
|
1260 |
||
1261 |
QDateTime dt = QDateTime::fromString(string, format); |
|
1262 |
||
1263 |
QCOMPARE(dt, expected); |
|
1264 |
} |
|
1265 |
||
1266 |
void tst_QDateTime::fromString() |
|
1267 |
{ |
|
1268 |
QDateTime dt = QDateTime::fromString("Thu Jan 1 00:00:00 1970"); |
|
1269 |
QCOMPARE(dt, QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0))); |
|
1270 |
||
1271 |
dt = QDateTime::fromString("Thu Jan 2 12:34 1970"); |
|
1272 |
QCOMPARE(dt, QDateTime(QDate(1970, 1, 2), QTime(12, 34, 0))); |
|
1273 |
||
1274 |
dt = QDateTime::fromString("Thu Jan 1 00 1970"); |
|
1275 |
QCOMPARE(dt, QDateTime()); |
|
1276 |
||
1277 |
dt = QDateTime::fromString("Thu Jan 1 00:00:00:00 1970"); |
|
1278 |
QCOMPARE(dt, QDateTime()); |
|
1279 |
||
1280 |
dt = QDateTime::fromString(" Thu Jan 1 00:00:00 1970 "); |
|
1281 |
QCOMPARE(dt, QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0))); |
|
1282 |
||
1283 |
dt = QDateTime::fromString("Thu Jan 1 00:00:00"); |
|
1284 |
QCOMPARE(dt, QDateTime()); |
|
1285 |
||
1286 |
dt = QDateTime::fromString("2002-10-01", Qt::ISODate); |
|
1287 |
QCOMPARE(dt, QDateTime(QDate(2002, 10, 1), QTime(0, 0, 0, 0))); |
|
1288 |
||
1289 |
dt = QDateTime::fromString("Thu Jan 1 00:12:34 1970 GMT"); |
|
1290 |
QCOMPARE(dt.toUTC(), QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC)); |
|
1291 |
||
1292 |
dt = QDateTime::fromString("Thu Jan 1 00:12:34 1970 GMT-0300"); |
|
1293 |
QCOMPARE(dt.toUTC(), QDateTime(QDate(1970, 1, 1), QTime(3, 12, 34), Qt::UTC)); |
|
1294 |
||
1295 |
dt = QDateTime::fromString("Thu Jan 1 00:12:34 1970 GMT+0300"); |
|
1296 |
QCOMPARE(dt.toUTC(), QDateTime(QDate(1969, 12, 31), QTime(21, 12, 34), Qt::UTC)); |
|
1297 |
||
1298 |
dt = QDateTime::fromString("Thu Jan 1 00:12:34 1970 GMT+foo"); |
|
1299 |
QCOMPARE(dt, QDateTime()); |
|
1300 |
||
1301 |
dt = QDateTime::fromString("Thu Jan 1 00:12:34 1970 gmt"); |
|
1302 |
QCOMPARE(dt.toUTC(), QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC)); |
|
1303 |
||
1304 |
dt = QDateTime::fromString("Thu Jan 1 1970 00:00:00"); |
|
1305 |
QCOMPARE(dt, QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0))); |
|
1306 |
||
1307 |
dt = QDateTime::fromString("Thu Jan 1 1970 00:12:34 GMT+0100"); |
|
1308 |
QCOMPARE(dt.toUTC(), QDateTime(QDate(1969, 12, 31), QTime(23, 12, 34), Qt::UTC)); |
|
1309 |
||
1310 |
QDate d = QDate::fromString("Thu Jan 1 1970"); |
|
1311 |
QCOMPARE(d, QDate(1970, 1, 1)); |
|
1312 |
||
1313 |
d = QDate::fromString(" Thu Jan 1 1970 "); |
|
1314 |
QCOMPARE(d, QDate(1970, 1, 1)); |
|
1315 |
||
1316 |
d = QDate::fromString("Thu Jan 1"); |
|
1317 |
QCOMPARE(d, QDate()); |
|
1318 |
||
1319 |
QDateTime dt2(QDate(1999, 1, 18), QTime(11, 49, 00)); |
|
1320 |
||
1321 |
QLocale def; |
|
1322 |
QLocale::setDefault(QLocale(QLocale::French, QLocale::France)); |
|
1323 |
||
1324 |
QCOMPARE(QDateTime::fromString(dt2.toString(Qt::DefaultLocaleShortDate), Qt::DefaultLocaleShortDate), dt2); |
|
1325 |
QCOMPARE(QDateTime::fromString(dt2.toString(Qt::SystemLocaleShortDate), Qt::SystemLocaleShortDate), dt2); |
|
1326 |
||
1327 |
// obsolete |
|
1328 |
QCOMPARE(QDateTime::fromString(dt2.toString(Qt::SystemLocaleDate), Qt::SystemLocaleDate), dt2); |
|
1329 |
QCOMPARE(QDateTime::fromString(dt2.toString(Qt::LocaleDate), Qt::LocaleDate), dt2); |
|
1330 |
||
1331 |
// cannot have these because of bug in datetime parser |
|
1332 |
// QCOMPARE(QDateTime::fromString(dt2.toString(Qt::DefaultLocaleLongDate), Qt::DefaultLocaleLongDate), dt2); |
|
1333 |
// QCOMPARE(QDateTime::fromString(dt2.toString(Qt::SystemLocaleLongDate), Qt::SystemLocaleLongDate), dt2); |
|
1334 |
||
1335 |
||
1336 |
// same thing for QDate and QTime |
|
1337 |
||
1338 |
QCOMPARE(QDate::fromString(dt2.date().toString(Qt::DefaultLocaleShortDate), Qt::DefaultLocaleShortDate), dt2.date()); |
|
1339 |
QCOMPARE(QDate::fromString(dt2.date().toString(Qt::SystemLocaleShortDate), Qt::SystemLocaleShortDate), dt2.date()); |
|
1340 |
QCOMPARE(QDate::fromString(dt2.date().toString(Qt::LocaleDate), Qt::LocaleDate), dt2.date()); |
|
1341 |
QCOMPARE(QDate::fromString(dt2.date().toString(Qt::SystemLocaleDate), Qt::SystemLocaleDate), dt2.date()); |
|
1342 |
QCOMPARE(QTime::fromString(dt2.time().toString(Qt::DefaultLocaleShortDate), Qt::DefaultLocaleShortDate), dt2.time()); |
|
1343 |
QCOMPARE(QTime::fromString(dt2.time().toString(Qt::SystemLocaleShortDate), Qt::SystemLocaleShortDate), dt2.time()); |
|
1344 |
QCOMPARE(QTime::fromString(dt2.time().toString(Qt::LocaleDate), Qt::LocaleDate), dt2.time()); |
|
1345 |
QCOMPARE(QTime::fromString(dt2.time().toString(Qt::SystemLocaleDate), Qt::SystemLocaleDate), dt2.time()); |
|
1346 |
||
1347 |
QLocale::setDefault(def); |
|
1348 |
} |
|
1349 |
||
1350 |
void tst_QDateTime::fromString_LOCALE_ILDATE() |
|
1351 |
{ |
|
1352 |
#ifdef Q_OS_WIN |
|
1353 |
QString date1 = QLatin1String("Sun 1. Dec 13:02:00 1974"); |
|
1354 |
QString date2 = QLatin1String("Sun Dec 1 13:02:00 1974"); |
|
1355 |
||
1356 |
QDateTime ref(QDate(1974, 12, 1), QTime(13, 2)); |
|
1357 |
QCOMPARE(ref, QDateTime::fromString(date2, Qt::TextDate)); |
|
1358 |
QCOMPARE(ref, QDateTime::fromString(date1, Qt::TextDate)); |
|
1359 |
#else |
|
1360 |
QSKIP("Windows only", SkipAll); |
|
1361 |
#endif |
|
1362 |
} |
|
1363 |
||
1364 |
void tst_QDateTime::utcOffset() |
|
1365 |
{ |
|
1366 |
/* Check default value. */ |
|
1367 |
QCOMPARE(QDateTime().utcOffset(), 0); |
|
1368 |
} |
|
1369 |
||
1370 |
void tst_QDateTime::setUtcOffset() |
|
1371 |
{ |
|
1372 |
/* Basic tests. */ |
|
1373 |
{ |
|
1374 |
QDateTime dt(QDateTime::currentDateTime()); |
|
1375 |
dt.setTimeSpec(Qt::LocalTime); |
|
1376 |
||
1377 |
dt.setUtcOffset(0); |
|
1378 |
QCOMPARE(dt.utcOffset(), 0); |
|
1379 |
QCOMPARE(dt.timeSpec(), Qt::UTC); |
|
1380 |
||
1381 |
dt.setUtcOffset(-100); |
|
1382 |
QCOMPARE(dt.utcOffset(), -100); |
|
1383 |
QCOMPARE(dt.timeSpec(), Qt::OffsetFromUTC); |
|
1384 |
} |
|
1385 |
||
1386 |
/* Test detaching. */ |
|
1387 |
{ |
|
1388 |
QDateTime dt(QDateTime::currentDateTime()); |
|
1389 |
QDateTime dt2(dt); |
|
1390 |
||
1391 |
dt.setUtcOffset(501); |
|
1392 |
||
1393 |
QCOMPARE(dt.utcOffset(), 501); |
|
1394 |
QCOMPARE(dt2.utcOffset(), 0); |
|
1395 |
} |
|
1396 |
||
1397 |
/* Check copying. */ |
|
1398 |
{ |
|
1399 |
QDateTime dt(QDateTime::currentDateTime()); |
|
1400 |
dt.setUtcOffset(502); |
|
1401 |
QCOMPARE(dt.utcOffset(), 502); |
|
1402 |
||
1403 |
QDateTime dt2(dt); |
|
1404 |
QCOMPARE(dt2.utcOffset(), 502); |
|
1405 |
} |
|
1406 |
||
1407 |
/* Check assignment. */ |
|
1408 |
{ |
|
1409 |
QDateTime dt(QDateTime::currentDateTime()); |
|
1410 |
dt.setUtcOffset(502); |
|
1411 |
QDateTime dt2; |
|
1412 |
dt2 = dt; |
|
1413 |
||
1414 |
QCOMPARE(dt2.utcOffset(), 502); |
|
1415 |
} |
|
1416 |
} |
|
1417 |
||
1418 |
void tst_QDateTime::getDate() |
|
1419 |
{ |
|
1420 |
{ |
|
1421 |
int y = -33, m = -44, d = -55; |
|
1422 |
QDate date; |
|
1423 |
date.getDate(&y, &m, &d); |
|
1424 |
QVERIFY(date.year() == y); |
|
1425 |
QVERIFY(date.month() == m); |
|
1426 |
QVERIFY(date.day() == d); |
|
1427 |
||
1428 |
date.getDate(0, 0, 0); |
|
1429 |
} |
|
1430 |
||
1431 |
{ |
|
1432 |
int y = -33, m = -44, d = -55; |
|
1433 |
QDate date(1998, 5, 24); |
|
1434 |
date.getDate(0, &m, 0); |
|
1435 |
date.getDate(&y, 0, 0); |
|
1436 |
date.getDate(0, 0, &d); |
|
1437 |
||
1438 |
QVERIFY(date.year() == y); |
|
1439 |
QVERIFY(date.month() == m); |
|
1440 |
QVERIFY(date.day() == d); |
|
1441 |
} |
|
1442 |
} |
|
1443 |
||
1444 |
void tst_QDateTime::fewDigitsInYear() const |
|
1445 |
{ |
|
1446 |
const QDateTime three(QDate(300, 10, 11), QTime()); |
|
1447 |
QCOMPARE(three.toString(QLatin1String("yyyy-MM-dd")), QString::fromLatin1("0300-10-11")); |
|
1448 |
||
1449 |
const QDateTime two(QDate(20, 10, 11), QTime()); |
|
1450 |
QCOMPARE(two.toString(QLatin1String("yyyy-MM-dd")), QString::fromLatin1("0020-10-11")); |
|
1451 |
||
1452 |
const QDateTime yyTwo(QDate(30, 10, 11), QTime()); |
|
1453 |
QCOMPARE(yyTwo.toString(QLatin1String("yy-MM-dd")), QString::fromLatin1("30-10-11")); |
|
1454 |
||
1455 |
const QDateTime yyOne(QDate(4, 10, 11), QTime()); |
|
1456 |
QCOMPARE(yyOne.toString(QLatin1String("yy-MM-dd")), QString::fromLatin1("04-10-11")); |
|
1457 |
} |
|
1458 |
||
1459 |
void tst_QDateTime::printNegativeYear() const |
|
1460 |
{ |
|
1461 |
{ |
|
1462 |
QDateTime date(QDate(-20, 10, 11)); |
|
1463 |
QVERIFY(date.isValid()); |
|
1464 |
QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0020")); |
|
1465 |
} |
|
1466 |
||
1467 |
{ |
|
1468 |
QDateTime date(QDate(-3, 10, 11)); |
|
1469 |
QVERIFY(date.isValid()); |
|
1470 |
QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0003")); |
|
1471 |
} |
|
1472 |
||
1473 |
{ |
|
1474 |
QDateTime date(QDate(-400, 10, 11)); |
|
1475 |
QVERIFY(date.isValid()); |
|
1476 |
QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0400")); |
|
1477 |
} |
|
1478 |
} |
|
1479 |
||
1480 |
void tst_QDateTime::roundtripGermanLocale() const |
|
1481 |
{ |
|
1482 |
/* This code path should not result in warnings. */ |
|
1483 |
const QDateTime theDateTime(QDateTime::currentDateTime()); |
|
1484 |
theDateTime.fromString(theDateTime.toString(Qt::TextDate), Qt::TextDate); |
|
1485 |
} |
|
1486 |
||
1487 |
void tst_QDateTime::utcOffsetLessThan() const |
|
1488 |
{ |
|
1489 |
QDateTime dt1(QDate(2002, 10, 10), QTime(0, 0, 0)); |
|
1490 |
QDateTime dt2(dt1); |
|
1491 |
||
1492 |
dt1.setUtcOffset(-(2 * 60 * 60)); // Minus two hours. |
|
1493 |
dt2.setUtcOffset(-(3 * 60 * 60)); // Minus three hours. |
|
1494 |
||
1495 |
QVERIFY(dt1 != dt2); |
|
1496 |
QVERIFY(!(dt1 == dt2)); |
|
1497 |
QVERIFY(dt1 < dt2); |
|
1498 |
QVERIFY(!(dt2 < dt1)); |
|
1499 |
} |
|
1500 |
||
1501 |
Q_DECLARE_METATYPE(Qt::DateFormat) |
|
1502 |
||
1503 |
QTEST_APPLESS_MAIN(tst_QDateTime) |
|
1504 |
#include "tst_qdatetime.moc" |