1/****************************************************************************
2**
3** Copyright (C) 2020 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtTest/QtTest>
30#include "qdatetime.h"
31#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
32# include <locale.h>
33#endif
34
35class tst_QTime : public QObject
36{
37 Q_OBJECT
38
39#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
40public:
41 tst_QTime()
42 {
43 // Some tests depend on C locale - BF&I it with belt *and* braces:
44 qputenv(varName: "LC_ALL", value: "C");
45 setlocale(LC_ALL, locale: "C");
46 // Need to instantiate as early as possible, before anything accesses
47 // the QSystemLocale singleton; once it exists, there's no changing it.
48 }
49#endif // remove for ### Qt 6
50
51private slots:
52 void msecsTo_data();
53 void msecsTo();
54 void secsTo_data();
55 void secsTo();
56 void setHMS_data();
57 void setHMS();
58 void hour_data();
59 void hour();
60 void isValid();
61 void isNull();
62 void addMSecs_data();
63 void addMSecs();
64 void addSecs_data();
65 void addSecs();
66 void operator_eq_eq_data();
67 void operator_eq_eq();
68 void operator_lt();
69 void operator_gt();
70 void operator_lt_eq();
71 void operator_gt_eq();
72 void fromStringFormat_data();
73 void fromStringFormat();
74 void fromStringDateFormat_data();
75 void fromStringDateFormat();
76 void toStringDateFormat_data();
77 void toStringDateFormat();
78 void toStringFormat_data();
79 void toStringFormat();
80#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
81 void toStringLocale();
82#endif // ### Qt 6: remove
83 void msecsSinceStartOfDay_data();
84 void msecsSinceStartOfDay();
85
86private:
87 QTime invalidTime() { return QTime(-1, -1, -1); }
88};
89
90Q_DECLARE_METATYPE(Qt::DateFormat)
91
92void tst_QTime::addSecs_data()
93{
94 QTest::addColumn<QTime>(name: "t1");
95 QTest::addColumn<int>(name: "i");
96 QTest::addColumn<QTime>(name: "exp");
97
98 QTest::newRow(dataTag: "Data0") << QTime(0,0,0) << 200 << QTime(0,3,20);
99 QTest::newRow(dataTag: "Data1") << QTime(0,0,0) << 20 << QTime(0,0,20);
100 QTest::newRow(dataTag: "overflow")
101 << QTime(0,0,0) << (INT_MAX / 1000 + 1)
102 << QTime::fromMSecsSinceStartOfDay(msecs: ((INT_MAX / 1000 + 1) % 86400) * 1000);
103}
104
105void tst_QTime::addSecs()
106{
107 QFETCH( QTime, t1 );
108 QFETCH( int, i );
109 QTime t2;
110 t2 = t1.addSecs( secs: i );
111 QFETCH( QTime, exp );
112 QCOMPARE( t2, exp );
113}
114
115void tst_QTime::addMSecs_data()
116{
117 QTest::addColumn<QTime>(name: "t1");
118 QTest::addColumn<int>(name: "i");
119 QTest::addColumn<QTime>(name: "exp");
120
121 // start with testing positive values
122 QTest::newRow( dataTag: "Data1_0") << QTime(0,0,0,0) << 2000 << QTime(0,0,2,0);
123 QTest::newRow( dataTag: "Data1_1") << QTime(0,0,0,0) << 200 << QTime(0,0,0,200);
124 QTest::newRow( dataTag: "Data1_2") << QTime(0,0,0,0) << 20 << QTime(0,0,0,20);
125 QTest::newRow( dataTag: "Data1_3") << QTime(0,0,0,1) << 1 << QTime(0,0,0,2);
126 QTest::newRow( dataTag: "Data1_4") << QTime(0,0,0,0) << 0 << QTime(0,0,0,0);
127
128 QTest::newRow( dataTag: "Data2_0") << QTime(0,0,0,98) << 0 << QTime(0,0,0,98);
129 QTest::newRow( dataTag: "Data2_1") << QTime(0,0,0,98) << 1 << QTime(0,0,0,99);
130 QTest::newRow( dataTag: "Data2_2") << QTime(0,0,0,98) << 2 << QTime(0,0,0,100);
131 QTest::newRow( dataTag: "Data2_3") << QTime(0,0,0,98) << 3 << QTime(0,0,0,101);
132
133 QTest::newRow( dataTag: "Data3_0") << QTime(0,0,0,998) << 0 << QTime(0,0,0,998);
134 QTest::newRow( dataTag: "Data3_1") << QTime(0,0,0,998) << 1 << QTime(0,0,0,999);
135 QTest::newRow( dataTag: "Data3_2") << QTime(0,0,0,998) << 2 << QTime(0,0,1,0);
136 QTest::newRow( dataTag: "Data3_3") << QTime(0,0,0,998) << 3 << QTime(0,0,1,1);
137
138 QTest::newRow( dataTag: "Data4_0") << QTime(0,0,1,995) << 4 << QTime(0,0,1,999);
139 QTest::newRow( dataTag: "Data4_1") << QTime(0,0,1,995) << 5 << QTime(0,0,2,0);
140 QTest::newRow( dataTag: "Data4_2") << QTime(0,0,1,995) << 6 << QTime(0,0,2,1);
141 QTest::newRow( dataTag: "Data4_3") << QTime(0,0,1,995) << 100 << QTime(0,0,2,95);
142 QTest::newRow( dataTag: "Data4_4") << QTime(0,0,1,995) << 105 << QTime(0,0,2,100);
143
144 QTest::newRow( dataTag: "Data5_0") << QTime(0,0,59,995) << 4 << QTime(0,0,59,999);
145 QTest::newRow( dataTag: "Data5_1") << QTime(0,0,59,995) << 5 << QTime(0,1,0,0);
146 QTest::newRow( dataTag: "Data5_2") << QTime(0,0,59,995) << 6 << QTime(0,1,0,1);
147 QTest::newRow( dataTag: "Data5_3") << QTime(0,0,59,995) << 1006 << QTime(0,1,1,1);
148
149 QTest::newRow( dataTag: "Data6_0") << QTime(0,59,59,995) << 4 << QTime(0,59,59,999);
150 QTest::newRow( dataTag: "Data6_1") << QTime(0,59,59,995) << 5 << QTime(1,0,0,0);
151 QTest::newRow( dataTag: "Data6_2") << QTime(0,59,59,995) << 6 << QTime(1,0,0,1);
152 QTest::newRow( dataTag: "Data6_3") << QTime(0,59,59,995) << 106 << QTime(1,0,0,101);
153 QTest::newRow( dataTag: "Data6_4") << QTime(0,59,59,995) << 1004 << QTime(1,0,0,999);
154 QTest::newRow( dataTag: "Data6_5") << QTime(0,59,59,995) << 1005 << QTime(1,0,1,0);
155 QTest::newRow( dataTag: "Data6_6") << QTime(0,59,59,995) << 61006 << QTime(1,1,1,1);
156
157 QTest::newRow( dataTag: "Data7_0") << QTime(23,59,59,995) << 0 << QTime(23,59,59,995);
158 QTest::newRow( dataTag: "Data7_1") << QTime(23,59,59,995) << 4 << QTime(23,59,59,999);
159 QTest::newRow( dataTag: "Data7_2") << QTime(23,59,59,995) << 5 << QTime(0,0,0,0);
160 QTest::newRow( dataTag: "Data7_3") << QTime(23,59,59,995) << 6 << QTime(0,0,0,1);
161 QTest::newRow( dataTag: "Data7_4") << QTime(23,59,59,995) << 7 << QTime(0,0,0,2);
162
163 // must test negative values too...
164 QTest::newRow( dataTag: "Data11_0") << QTime(0,0,2,0) << -2000 << QTime(0,0,0,0);
165 QTest::newRow( dataTag: "Data11_1") << QTime(0,0,0,200) << -200 << QTime(0,0,0,0);
166 QTest::newRow( dataTag: "Data11_2") << QTime(0,0,0,20) << -20 << QTime(0,0,0,0);
167 QTest::newRow( dataTag: "Data11_3") << QTime(0,0,0,2) << -1 << QTime(0,0,0,1);
168 QTest::newRow( dataTag: "Data11_4") << QTime(0,0,0,0) << -0 << QTime(0,0,0,0);
169
170 QTest::newRow( dataTag: "Data12_0") << QTime(0,0,0,98) << -0 << QTime(0,0,0,98);
171 QTest::newRow( dataTag: "Data12_1") << QTime(0,0,0,99) << -1 << QTime(0,0,0,98);
172 QTest::newRow( dataTag: "Data12_2") << QTime(0,0,0,100) << -2 << QTime(0,0,0,98);
173 QTest::newRow( dataTag: "Data12_3") << QTime(0,0,0,101) << -3 << QTime(0,0,0,98);
174
175 QTest::newRow( dataTag: "Data13_0") << QTime(0,0,0,998) << -0 << QTime(0,0,0,998);
176 QTest::newRow( dataTag: "Data13_1") << QTime(0,0,0,999) << -1 << QTime(0,0,0,998);
177 QTest::newRow( dataTag: "Data13_2") << QTime(0,0,1,0) << -2 << QTime(0,0,0,998);
178 QTest::newRow( dataTag: "Data13_3") << QTime(0,0,1,1) << -3 << QTime(0,0,0,998);
179
180 QTest::newRow( dataTag: "Data14_0") << QTime(0,0,1,999) << -4 << QTime(0,0,1,995);
181 QTest::newRow( dataTag: "Data14_1") << QTime(0,0,2,0) << -5 << QTime(0,0,1,995);
182 QTest::newRow( dataTag: "Data14_2") << QTime(0,0,2,1) << -6 << QTime(0,0,1,995);
183 QTest::newRow( dataTag: "Data14_3") << QTime(0,0,2,95) << -100 << QTime(0,0,1,995);
184 QTest::newRow( dataTag: "Data14_4") << QTime(0,0,2,100) << -105 << QTime(0,0,1,995);
185
186 QTest::newRow( dataTag: "Data15_0") << QTime(0,0,59,999) << -4 << QTime(0,0,59,995);
187 QTest::newRow( dataTag: "Data15_1") << QTime(0,1,0,0) << -5 << QTime(0,0,59,995);
188 QTest::newRow( dataTag: "Data15_2") << QTime(0,1,0,1) << -6 << QTime(0,0,59,995);
189 QTest::newRow( dataTag: "Data15_3") << QTime(0,1,1,1) << -1006 << QTime(0,0,59,995);
190
191 QTest::newRow( dataTag: "Data16_0") << QTime(0,59,59,999) << -4 << QTime(0,59,59,995);
192 QTest::newRow( dataTag: "Data16_1") << QTime(1,0,0,0) << -5 << QTime(0,59,59,995);
193 QTest::newRow( dataTag: "Data16_2") << QTime(1,0,0,1) << -6 << QTime(0,59,59,995);
194 QTest::newRow( dataTag: "Data16_3") << QTime(1,0,0,101) << -106 << QTime(0,59,59,995);
195 QTest::newRow( dataTag: "Data16_4") << QTime(1,0,0,999) << -1004 << QTime(0,59,59,995);
196 QTest::newRow( dataTag: "Data16_5") << QTime(1,0,1,0) << -1005 << QTime(0,59,59,995);
197 QTest::newRow( dataTag: "Data16_6") << QTime(1,1,1,1) << -61006 << QTime(0,59,59,995);
198
199 QTest::newRow( dataTag: "Data17_0") << QTime(23,59,59,995) << -0 << QTime(23,59,59,995);
200 QTest::newRow( dataTag: "Data17_1") << QTime(23,59,59,999) << -4 << QTime(23,59,59,995);
201 QTest::newRow( dataTag: "Data17_2") << QTime(0,0,0,0) << -5 << QTime(23,59,59,995);
202 QTest::newRow( dataTag: "Data17_3") << QTime(0,0,0,1) << -6 << QTime(23,59,59,995);
203 QTest::newRow( dataTag: "Data17_4") << QTime(0,0,0,2) << -7 << QTime(23,59,59,995);
204
205 QTest::newRow( dataTag: "Data18_0" ) << invalidTime() << 1 << invalidTime();
206}
207
208void tst_QTime::addMSecs()
209{
210 QFETCH( QTime, t1 );
211 QFETCH( int, i );
212 QTime t2;
213 t2 = t1.addMSecs( ms: i );
214 QFETCH( QTime, exp );
215 QCOMPARE( t2, exp );
216}
217
218void tst_QTime::isNull()
219{
220 QTime t1;
221 QVERIFY( t1.isNull() );
222 QTime t2(0,0,0);
223 QVERIFY( !t2.isNull() );
224 QTime t3(0,0,1);
225 QVERIFY( !t3.isNull() );
226 QTime t4(0,0,0,1);
227 QVERIFY( !t4.isNull() );
228 QTime t5(23,59,59);
229 QVERIFY( !t5.isNull() );
230}
231
232void tst_QTime::isValid()
233{
234 QTime t1;
235 QVERIFY( !t1.isValid() );
236 QTime t2(24,0,0,0);
237 QVERIFY( !t2.isValid() );
238 QTime t3(23,60,0,0);
239 QVERIFY( !t3.isValid() );
240 QTime t4(23,0,-1,0);
241 QVERIFY( !t4.isValid() );
242 QTime t5(23,0,60,0);
243 QVERIFY( !t5.isValid() );
244 QTime t6(23,0,0,1000);
245 QVERIFY( !t6.isValid() );
246}
247
248void tst_QTime::hour_data()
249{
250 QTest::addColumn<int>(name: "hour");
251 QTest::addColumn<int>(name: "minute");
252 QTest::addColumn<int>(name: "sec");
253 QTest::addColumn<int>(name: "msec");
254
255 QTest::newRow( dataTag: "data0" ) << 0 << 0 << 0 << 0;
256 QTest::newRow( dataTag: "data1" ) << 0 << 0 << 0 << 1;
257 QTest::newRow( dataTag: "data2" ) << 1 << 2 << 3 << 4;
258 QTest::newRow( dataTag: "data3" ) << 2 << 12 << 13 << 65;
259 QTest::newRow( dataTag: "data4" ) << 23 << 59 << 59 << 999;
260 QTest::newRow( dataTag: "data5" ) << -1 << -1 << -1 << -1;
261}
262
263void tst_QTime::hour()
264{
265 QFETCH( int, hour );
266 QFETCH( int, minute );
267 QFETCH( int, sec );
268 QFETCH( int, msec );
269
270 QTime t1( hour, minute, sec, msec );
271 QCOMPARE( t1.hour(), hour );
272 QCOMPARE( t1.minute(), minute );
273 QCOMPARE( t1.second(), sec );
274 QCOMPARE( t1.msec(), msec );
275}
276
277void tst_QTime::setHMS_data()
278{
279 QTest::addColumn<int>(name: "hour");
280 QTest::addColumn<int>(name: "minute");
281 QTest::addColumn<int>(name: "sec");
282
283 QTest::newRow( dataTag: "data0" ) << 0 << 0 << 0;
284 QTest::newRow( dataTag: "data1" ) << 1 << 2 << 3;
285 QTest::newRow( dataTag: "data2" ) << 0 << 59 << 0;
286 QTest::newRow( dataTag: "data3" ) << 0 << 59 << 59;
287 QTest::newRow( dataTag: "data4" ) << 23 << 0 << 0;
288 QTest::newRow( dataTag: "data5" ) << 23 << 59 << 0;
289 QTest::newRow( dataTag: "data6" ) << 23 << 59 << 59;
290 QTest::newRow( dataTag: "data7" ) << -1 << -1 << -1;
291}
292
293void tst_QTime::setHMS()
294{
295 QFETCH( int, hour );
296 QFETCH( int, minute );
297 QFETCH( int, sec );
298
299 QTime t(3,4,5);
300 t.setHMS( h: hour, m: minute, s: sec );
301 QCOMPARE( t.hour(), hour );
302 QCOMPARE( t.minute(), minute );
303 QCOMPARE( t.second(), sec );
304}
305
306void tst_QTime::secsTo_data()
307{
308 QTest::addColumn<QTime>(name: "t1");
309 QTest::addColumn<QTime>(name: "t2");
310 QTest::addColumn<int>(name: "delta");
311
312 QTest::newRow( dataTag: "data0" ) << QTime(0,0,0) << QTime(0,0,59) << 59;
313 QTest::newRow( dataTag: "data1" ) << QTime(0,0,0) << QTime(0,1,0) << 60;
314 QTest::newRow( dataTag: "data2" ) << QTime(0,0,0) << QTime(0,10,0) << 600;
315 QTest::newRow( dataTag: "data3" ) << QTime(0,0,0) << QTime(23,59,59) << 86399;
316 QTest::newRow( dataTag: "data4" ) << QTime(-1, -1, -1) << QTime(0, 0, 0) << 0;
317 QTest::newRow( dataTag: "data5" ) << QTime(0, 0, 0) << QTime(-1, -1, -1) << 0;
318 QTest::newRow( dataTag: "data6" ) << QTime(-1, -1, -1) << QTime(-1, -1, -1) << 0;
319 QTest::newRow(dataTag: "disregard msec (1s)") << QTime(12, 30, 1, 500) << QTime(12, 30, 2, 400) << 1;
320 QTest::newRow(dataTag: "disregard msec (0s)") << QTime(12, 30, 1, 500) << QTime(12, 30, 1, 900) << 0;
321 QTest::newRow(dataTag: "disregard msec (-1s)") << QTime(12, 30, 2, 400) << QTime(12, 30, 1, 500) << -1;
322 QTest::newRow(dataTag: "disregard msec (0s)") << QTime(12, 30, 1, 900) << QTime(12, 30, 1, 500) << 0;
323}
324
325void tst_QTime::secsTo()
326{
327 QFETCH( QTime, t1 );
328 QFETCH( QTime, t2 );
329 QFETCH( int, delta );
330
331 QCOMPARE( t1.secsTo( t2 ), delta );
332}
333
334void tst_QTime::msecsTo_data()
335{
336 QTest::addColumn<QTime>(name: "t1");
337 QTest::addColumn<QTime>(name: "t2");
338 QTest::addColumn<int>(name: "delta");
339
340 QTest::newRow( dataTag: "data0" ) << QTime(0,0,0,0) << QTime(0,0,0,0) << 0;
341 QTest::newRow( dataTag: "data1" ) << QTime(0,0,0,0) << QTime(0,0,1,0) << 1000;
342 QTest::newRow( dataTag: "data2" ) << QTime(0,0,0,0) << QTime(0,0,10,0) << 10000;
343 QTest::newRow( dataTag: "data3" ) << QTime(0,0,0,0) << QTime(23,59,59,0) << 86399000;
344 QTest::newRow( dataTag: "data4" ) << QTime(-1, -1, -1, -1) << QTime(0, 0, 0, 0) << 0;
345 QTest::newRow( dataTag: "data5" ) << QTime(0, 0, 0, 0) << QTime(-1, -1, -1, -1) << 0;
346 QTest::newRow( dataTag: "data6" ) << QTime(-1, -1, -1, -1) << QTime(-1, -1, -1, -1) << 0;
347}
348
349void tst_QTime::msecsTo()
350{
351 QFETCH( QTime, t1 );
352 QFETCH( QTime, t2 );
353 QFETCH( int, delta );
354
355 QCOMPARE( t1.msecsTo( t2 ), delta );
356}
357
358void tst_QTime::operator_eq_eq_data()
359{
360 QTest::addColumn<QTime>(name: "t1");
361 QTest::addColumn<QTime>(name: "t2");
362 QTest::addColumn<bool>(name: "expectEqual");
363
364 QTime time1(0, 0, 0, 0);
365 QTime time2 = time1.addMSecs(ms: 1);
366 QTime time3 = time1.addMSecs(ms: -1);
367 QTime time4(23, 59, 59, 999);
368
369 QTest::newRow(dataTag: "data0") << time1 << time2 << false;
370 QTest::newRow(dataTag: "data1") << time2 << time3 << false;
371 QTest::newRow(dataTag: "data2") << time4 << time1 << false;
372 QTest::newRow(dataTag: "data3") << time1 << time1 << true;
373 QTest::newRow(dataTag: "data4") << QTime(12,34,56,20) << QTime(12,34,56,20) << true;
374 QTest::newRow(dataTag: "data5") << QTime(01,34,56,20) << QTime(13,34,56,20) << false;
375}
376
377void tst_QTime::operator_eq_eq()
378{
379 QFETCH(QTime, t1);
380 QFETCH(QTime, t2);
381 QFETCH(bool, expectEqual);
382
383 bool equal = t1 == t2;
384 QCOMPARE(equal, expectEqual);
385 bool notEqual = t1 != t2;
386 QCOMPARE(notEqual, !expectEqual);
387
388 if (equal)
389 QVERIFY(qHash(t1) == qHash(t2));
390}
391
392void tst_QTime::operator_lt()
393{
394 QTime t1(0,0,0,0);
395 QTime t2(0,0,0,0);
396 QVERIFY( !(t1 < t2) );
397
398 t1 = QTime(12,34,56,20);
399 t2 = QTime(12,34,56,30);
400 QVERIFY( t1 < t2 );
401
402 t1 = QTime(13,34,46,20);
403 t2 = QTime(13,34,56,20);
404 QVERIFY( t1 < t2 );
405
406 t1 = QTime(13,24,56,20);
407 t2 = QTime(13,34,56,20);
408 QVERIFY( t1 < t2 );
409
410 t1 = QTime(12,34,56,20);
411 t2 = QTime(13,34,56,20);
412 QVERIFY( t1 < t2 );
413
414 t1 = QTime(14,34,56,20);
415 t2 = QTime(13,34,56,20);
416 QVERIFY( !(t1 < t2) );
417
418 t1 = QTime(13,44,56,20);
419 t2 = QTime(13,34,56,20);
420 QVERIFY( !(t1 < t2) );
421
422 t1 = QTime(13,34,56,20);
423 t2 = QTime(13,34,46,20);
424 QVERIFY( !(t1 < t2) );
425
426 t1 = QTime(13,44,56,30);
427 t2 = QTime(13,44,56,20);
428 QVERIFY( !(t1 < t2) );
429}
430
431void tst_QTime::operator_gt()
432{
433 QTime t1(0,0,0,0);
434 QTime t2(0,0,0,0);
435 QVERIFY( !(t1 > t2) );
436
437 t1 = QTime(12,34,56,20);
438 t2 = QTime(12,34,56,30);
439 QVERIFY( !(t1 > t2) );
440
441 t1 = QTime(13,34,46,20);
442 t2 = QTime(13,34,56,20);
443 QVERIFY( !(t1 > t2) );
444
445 t1 = QTime(13,24,56,20);
446 t2 = QTime(13,34,56,20);
447 QVERIFY( !(t1 > t2) );
448
449 t1 = QTime(12,34,56,20);
450 t2 = QTime(13,34,56,20);
451 QVERIFY( !(t1 > t2) );
452
453 t1 = QTime(14,34,56,20);
454 t2 = QTime(13,34,56,20);
455 QVERIFY( t1 > t2 );
456
457 t1 = QTime(13,44,56,20);
458 t2 = QTime(13,34,56,20);
459 QVERIFY( t1 > t2 );
460
461 t1 = QTime(13,34,56,20);
462 t2 = QTime(13,34,46,20);
463 QVERIFY( t1 > t2 );
464
465 t1 = QTime(13,44,56,30);
466 t2 = QTime(13,44,56,20);
467 QVERIFY( t1 > t2 );
468}
469
470void tst_QTime::operator_lt_eq()
471{
472 QTime t1(0,0,0,0);
473 QTime t2(0,0,0,0);
474 QVERIFY( t1 <= t2 );
475
476 t1 = QTime(12,34,56,20);
477 t2 = QTime(12,34,56,30);
478 QVERIFY( t1 <= t2 );
479
480 t1 = QTime(13,34,46,20);
481 t2 = QTime(13,34,56,20);
482 QVERIFY( t1 <= t2 );
483
484 t1 = QTime(13,24,56,20);
485 t2 = QTime(13,34,56,20);
486 QVERIFY( t1 <= t2 );
487
488 t1 = QTime(12,34,56,20);
489 t2 = QTime(13,34,56,20);
490 QVERIFY( t1 <= t2 );
491
492 t1 = QTime(14,34,56,20);
493 t2 = QTime(13,34,56,20);
494 QVERIFY( !(t1 <= t2) );
495
496 t1 = QTime(13,44,56,20);
497 t2 = QTime(13,34,56,20);
498 QVERIFY( !(t1 <= t2) );
499
500 t1 = QTime(13,34,56,20);
501 t2 = QTime(13,34,46,20);
502 QVERIFY( !(t1 <= t2) );
503
504 t1 = QTime(13,44,56,30);
505 t2 = QTime(13,44,56,20);
506 QVERIFY( !(t1 <= t2) );
507}
508
509void tst_QTime::operator_gt_eq()
510{
511 QTime t1(0,0,0,0);
512 QTime t2(0,0,0,0);
513 QVERIFY( t1 >= t2 );
514
515 t1 = QTime(12,34,56,20);
516 t2 = QTime(12,34,56,30);
517 QVERIFY( !(t1 >= t2) );
518
519 t1 = QTime(13,34,46,20);
520 t2 = QTime(13,34,56,20);
521 QVERIFY( !(t1 >= t2) );
522
523 t1 = QTime(13,24,56,20);
524 t2 = QTime(13,34,56,20);
525 QVERIFY( !(t1 >= t2) );
526
527 t1 = QTime(12,34,56,20);
528 t2 = QTime(13,34,56,20);
529 QVERIFY( !(t1 >= t2) );
530
531 t1 = QTime(14,34,56,20);
532 t2 = QTime(13,34,56,20);
533 QVERIFY( t1 >= t2 );
534
535 t1 = QTime(13,44,56,20);
536 t2 = QTime(13,34,56,20);
537 QVERIFY( t1 >= t2 );
538
539 t1 = QTime(13,34,56,20);
540 t2 = QTime(13,34,46,20);
541 QVERIFY( t1 >= t2 );
542
543 t1 = QTime(13,44,56,30);
544 t2 = QTime(13,44,56,20);
545 QVERIFY( t1 >= t2 );
546}
547
548void tst_QTime::fromStringFormat_data()
549{
550 QTest::addColumn<QString>(name: "string");
551 QTest::addColumn<QString>(name: "format");
552 QTest::addColumn<QTime>(name: "expected");
553
554 QTest::newRow(dataTag: "data0") << QString("1010") << QString("mmm") << QTime(0, 10, 0);
555 QTest::newRow(dataTag: "data1") << QString("00") << QString("hm") << invalidTime();
556 QTest::newRow(dataTag: "data2") << QString("10am") << QString("hap") << QTime(10, 0, 0);
557 QTest::newRow(dataTag: "data3") << QString("10pm") << QString("hap") << QTime(22, 0, 0);
558 QTest::newRow(dataTag: "data4") << QString("10pmam") << QString("hapap") << invalidTime();
559 QTest::newRow(dataTag: "data5") << QString("1070") << QString("hhm") << invalidTime();
560 QTest::newRow(dataTag: "data6") << QString("1011") << QString("hh") << invalidTime();
561 QTest::newRow(dataTag: "data7") << QString("25") << QString("hh") << invalidTime();
562 QTest::newRow(dataTag: "data8") << QString("22pm") << QString("Hap") << QTime(22, 0, 0);
563 QTest::newRow(dataTag: "data9") << QString("2221") << QString("hhhh") << invalidTime();
564 QTest::newRow(dataTag: "data10") << QString("02:23PM") << QString("hh:mmAP") << QTime(14,23,0,0);
565 QTest::newRow(dataTag: "data11") << QString("02:23pm") << QString("hh:mmap") << QTime(14,23,0,0);
566 QTest::newRow(dataTag: "short-msecs-lt100") << QString("10:12:34:045") << QString("hh:m:ss:z") << QTime(10,12,34,45);
567 QTest::newRow(dataTag: "short-msecs-gt100") << QString("10:12:34:45") << QString("hh:m:ss:z") << QTime(10,12,34,450);
568 QTest::newRow(dataTag: "late") << QString("23:59:59.999") << QString("hh:mm:ss.z") << QTime(23, 59, 59, 999);
569}
570
571void tst_QTime::fromStringFormat()
572{
573 QFETCH(QString, string);
574 QFETCH(QString, format);
575 QFETCH(QTime, expected);
576
577 QTime dt = QTime::fromString(s: string, format);
578 QCOMPARE(dt, expected);
579}
580
581void tst_QTime::fromStringDateFormat_data()
582{
583 QTest::addColumn<QString>(name: "string");
584 QTest::addColumn<Qt::DateFormat>(name: "format");
585 QTest::addColumn<QTime>(name: "expected");
586
587 QTest::newRow(dataTag: "TextDate - data0") << QString("00:00:00") << Qt::TextDate << QTime(0,0,0,0);
588 QTest::newRow(dataTag: "TextDate - data1") << QString("10:12:34") << Qt::TextDate << QTime(10,12,34,0);
589 QTest::newRow(dataTag: "TextDate - data2") << QString("19:03:54.998601") << Qt::TextDate << QTime(19, 3, 54, 999);
590 QTest::newRow(dataTag: "TextDate - data3") << QString("19:03:54.999601") << Qt::TextDate << QTime(19, 3, 54, 999);
591 QTest::newRow(dataTag: "TextDate - data4") << QString("10:12") << Qt::TextDate << QTime(10, 12, 0, 0);
592 QTest::newRow(dataTag: "TextDate - invalid, minutes") << QString::fromLatin1(str: "23:XX:00") << Qt::TextDate << invalidTime();
593 QTest::newRow(dataTag: "TextDate - invalid, minute fraction") << QString::fromLatin1(str: "23:00.123456") << Qt::TextDate << invalidTime();
594 QTest::newRow(dataTag: "TextDate - invalid, seconds") << QString::fromLatin1(str: "23:00:XX") << Qt::TextDate << invalidTime();
595 QTest::newRow(dataTag: "TextDate - invalid, milliseconds") << QString::fromLatin1(str: "23:01:01:XXXX") << Qt::TextDate
596#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
597 << invalidTime();
598#else
599 << QTime(23, 1, 1, 0);
600#endif
601 QTest::newRow(dataTag: "TextDate - midnight 24") << QString("24:00:00") << Qt::TextDate << QTime();
602
603 QTest::newRow(dataTag: "IsoDate - valid, start of day, omit seconds") << QString::fromLatin1(str: "00:00") << Qt::ISODate << QTime(0, 0, 0);
604 QTest::newRow(dataTag: "IsoDate - valid, omit seconds") << QString::fromLatin1(str: "22:21") << Qt::ISODate << QTime(22, 21, 0);
605 QTest::newRow(dataTag: "IsoDate - valid, omit seconds (2)") << QString::fromLatin1(str: "23:59") << Qt::ISODate << QTime(23, 59, 0);
606 QTest::newRow(dataTag: "IsoDate - valid, end of day") << QString::fromLatin1(str: "23:59:59") << Qt::ISODate << QTime(23, 59, 59);
607
608 QTest::newRow(dataTag: "IsoDate - invalid, empty string") << QString::fromLatin1(str: "") << Qt::ISODate << invalidTime();
609 QTest::newRow(dataTag: "IsoDate - invalid, too many hours") << QString::fromLatin1(str: "25:00") << Qt::ISODate << invalidTime();
610 QTest::newRow(dataTag: "IsoDate - invalid, too many minutes") << QString::fromLatin1(str: "10:70") << Qt::ISODate << invalidTime();
611 // This is a valid time if it happens on June 30 or December 31 (leap seconds).
612 QTest::newRow(dataTag: "IsoDate - invalid, too many seconds") << QString::fromLatin1(str: "23:59:60") << Qt::ISODate << invalidTime();
613 QTest::newRow(dataTag: "IsoDate - invalid, minutes") << QString::fromLatin1(str: "23:XX:00") << Qt::ISODate << invalidTime();
614 QTest::newRow(dataTag: "IsoDate - invalid, not enough minutes") << QString::fromLatin1(str: "23:0") << Qt::ISODate << invalidTime();
615 QTest::newRow(dataTag: "IsoDate - invalid, minute fraction") << QString::fromLatin1(str: "23:00,XX") << Qt::ISODate << invalidTime();
616 QTest::newRow(dataTag: "IsoDate - invalid, seconds") << QString::fromLatin1(str: "23:00:XX") << Qt::ISODate << invalidTime();
617 QTest::newRow(dataTag: "IsoDate - invalid, milliseconds") << QString::fromLatin1(str: "23:01:01:XXXX") << Qt::ISODate
618#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
619 << invalidTime();
620#else
621 << QTime(23, 1, 1, 0);
622#endif
623 QTest::newRow(dataTag: "IsoDate - data0") << QString("00:00:00") << Qt::ISODate << QTime(0,0,0,0);
624 QTest::newRow(dataTag: "IsoDate - data1") << QString("10:12:34") << Qt::ISODate << QTime(10,12,34,0);
625 QTest::newRow(dataTag: "IsoDate - data2") << QString("19:03:54.998601") << Qt::ISODate << QTime(19, 3, 54, 999);
626 QTest::newRow(dataTag: "IsoDate - data3") << QString("19:03:54.999601") << Qt::ISODate << QTime(19, 3, 54, 999);
627 QTest::newRow(dataTag: "IsoDate - midnight 24") << QString("24:00:00") << Qt::ISODate << QTime(0, 0, 0, 0);
628 QTest::newRow(dataTag: "IsoDate - minute fraction midnight") << QString("24:00,0") << Qt::ISODate << QTime(0, 0, 0, 0);
629
630 // Test Qt::RFC2822Date format (RFC 2822).
631 QTest::newRow(dataTag: "RFC 2822") << QString::fromLatin1(str: "13 Feb 1987 13:24:51 +0100")
632 << Qt::RFC2822Date << QTime(13, 24, 51);
633 QTest::newRow(dataTag: "RFC 2822 after space")
634 << QString::fromLatin1(str: " 13 Feb 1987 13:24:51 +0100")
635 << Qt::RFC2822Date << QTime(13, 24, 51);
636 QTest::newRow(dataTag: "RFC 2822 with day") << QString::fromLatin1(str: "Thu, 01 Jan 1970 00:12:34 +0000")
637 << Qt::RFC2822Date << QTime(0, 12, 34);
638 QTest::newRow(dataTag: "RFC 2822 with day after space")
639 << QString::fromLatin1(str: " Thu, 01 Jan 1970 00:12:34 +0000")
640 << Qt::RFC2822Date << QTime(0, 12, 34);
641 // No timezone
642 QTest::newRow(dataTag: "RFC 2822 no timezone") << QString::fromLatin1(str: "01 Jan 1970 00:12:34")
643 << Qt::RFC2822Date << QTime(0, 12, 34);
644 // No time specified
645 QTest::newRow(dataTag: "RFC 2822 date only") << QString::fromLatin1(str: "01 Nov 2002")
646 << Qt::RFC2822Date << invalidTime();
647 QTest::newRow(dataTag: "RFC 2822 with day date only") << QString::fromLatin1(str: "Fri, 01 Nov 2002")
648 << Qt::RFC2822Date << invalidTime();
649 // Test invalid month, day, year
650 QTest::newRow(dataTag: "RFC 2822 invalid month name") << QString::fromLatin1(str: "13 Fev 1987 13:24:51 +0100")
651 << Qt::RFC2822Date << QTime(13, 24, 51);
652 QTest::newRow(dataTag: "RFC 2822 invalid day") << QString::fromLatin1(str: "36 Fev 1987 13:24:51 +0100")
653 << Qt::RFC2822Date << QTime(13, 24, 51);
654 QTest::newRow(dataTag: "RFC 2822 invalid year") << QString::fromLatin1(str: "13 Fev 0000 13:24:51 +0100")
655 << Qt::RFC2822Date << QTime(13, 24, 51);
656 // Test invalid characters (currently ignoring trailing junk, but see QTBUG-80038).
657 QTest::newRow(dataTag: "RFC 2822 invalid character at end")
658 << QString::fromLatin1(str: "01 Jan 2012 08:00:00 +0100!")
659 << Qt::RFC2822Date << QTime(8, 0, 0);
660 QTest::newRow(dataTag: "RFC 2822 invalid character at front")
661 << QString::fromLatin1(str: "!01 Jan 2012 08:00:00 +0100")
662 << Qt::RFC2822Date << invalidTime();
663 QTest::newRow(dataTag: "RFC 2822 invalid character both ends")
664 << QString::fromLatin1(str: "!01 Jan 2012 08:00:00 +0100!")
665 << Qt::RFC2822Date << invalidTime();
666 QTest::newRow(dataTag: "RFC 2822 invalid character at front, 2 at back")
667 << QString::fromLatin1(str: "!01 Jan 2012 08:00:00 +0100..")
668 << Qt::RFC2822Date << invalidTime();
669 QTest::newRow(dataTag: "RFC 2822 invalid character 2 at front")
670 << QString::fromLatin1(str: "!!01 Jan 2012 08:00:00 +0100")
671 << Qt::RFC2822Date << invalidTime();
672 // The common date text used by the "invalid character" tests, just to be
673 // sure *it's* not what's invalid:
674 QTest::newRow(dataTag: "RFC 2822 invalid character at end")
675 << QString::fromLatin1(str: "01 Jan 2012 08:00:00 +0100")
676 << Qt::RFC2822Date << QTime(8, 0, 0);
677
678 // Test Qt::RFC2822Date format (RFC 850 and 1036, permissive).
679 QTest::newRow(dataTag: "RFC 850 and 1036") << QString::fromLatin1(str: "Fri Feb 13 13:24:51 1987 +0100")
680 << Qt::RFC2822Date << QTime(13, 24, 51);
681 QTest::newRow(dataTag: "RFC 850 and 1036 after space")
682 << QString::fromLatin1(str: " Fri Feb 13 13:24:51 1987 +0100")
683 << Qt::RFC2822Date << QTime(13, 24, 51);
684 // No timezone
685 QTest::newRow(dataTag: "RFC 850 and 1036 no timezone") << QString::fromLatin1(str: "Thu Jan 01 00:12:34 1970")
686 << Qt::RFC2822Date << QTime(0, 12, 34);
687 // No time specified
688 QTest::newRow(dataTag: "RFC 850 and 1036 date only") << QString::fromLatin1(str: "Fri Nov 01 2002")
689 << Qt::RFC2822Date << invalidTime();
690 // Test invalid characters (currently ignoring trailing junk, but see QTBUG-80038).
691 QTest::newRow(dataTag: "RFC 850 and 1036 invalid character at end")
692 << QString::fromLatin1(str: "Sun Jan 01 08:00:00 2012 +0100!")
693 << Qt::RFC2822Date << QTime(8, 0, 0);
694 QTest::newRow(dataTag: "RFC 850 and 1036 invalid character at front")
695 << QString::fromLatin1(str: "!Sun Jan 01 08:00:00 2012 +0100")
696 << Qt::RFC2822Date << invalidTime();
697 QTest::newRow(dataTag: "RFC 850 and 1036 invalid character both ends")
698 << QString::fromLatin1(str: "!Sun Jan 01 08:00:00 2012 +0100!")
699 << Qt::RFC2822Date << invalidTime();
700 QTest::newRow(dataTag: "RFC 850 and 1036 invalid character at front, 2 at back")
701 << QString::fromLatin1(str: "!Sun Jan 01 08:00:00 2012 +0100..")
702 << Qt::RFC2822Date << invalidTime();
703 // The common date text used by the "invalid character" tests, just to be
704 // sure *it's* not what's invalid:
705 QTest::newRow(dataTag: "RFC 850 and 1036 invalid character at end")
706 << QString::fromLatin1(str: "Sun Jan 01 08:00:00 2012 +0100")
707 << Qt::RFC2822Date << QTime(8, 0, 0);
708
709 QTest::newRow(dataTag: "RFC empty") << QString::fromLatin1(str: "") << Qt::RFC2822Date << invalidTime();
710}
711
712void tst_QTime::fromStringDateFormat()
713{
714 QFETCH(QString, string);
715 QFETCH(Qt::DateFormat, format);
716 QFETCH(QTime, expected);
717
718 QTime dt = QTime::fromString(s: string, f: format);
719 QCOMPARE(dt, expected);
720}
721
722void tst_QTime::toStringDateFormat_data()
723{
724 QTest::addColumn<QTime>(name: "time");
725 QTest::addColumn<Qt::DateFormat>(name: "format");
726 QTest::addColumn<QString>(name: "expected");
727
728 QTest::newRow(dataTag: "00:00:00.000") << QTime(0, 0, 0, 0) << Qt::TextDate << QString("00:00:00");
729 QTest::newRow(dataTag: "ISO 00:00:00.000") << QTime(0, 0, 0, 0) << Qt::ISODate << QString("00:00:00");
730 QTest::newRow(dataTag: "Text 10:12:34.000") << QTime(10, 12, 34, 0) << Qt::TextDate << QString("10:12:34");
731 QTest::newRow(dataTag: "ISO 10:12:34.000") << QTime(10, 12, 34, 0) << Qt::ISODate << QString("10:12:34");
732 QTest::newRow(dataTag: "Text 10:12:34.001") << QTime(10, 12, 34, 001) << Qt::TextDate << QString("10:12:34");
733 QTest::newRow(dataTag: "ISO 10:12:34.001") << QTime(10, 12, 34, 001) << Qt::ISODate << QString("10:12:34");
734 QTest::newRow(dataTag: "Text 10:12:34.999") << QTime(10, 12, 34, 999) << Qt::TextDate << QString("10:12:34");
735 QTest::newRow(dataTag: "ISO 10:12:34.999") << QTime(10, 12, 34, 999) << Qt::ISODate << QString("10:12:34");
736 QTest::newRow(dataTag: "RFC2822Date") << QTime(10, 12, 34, 999) << Qt::RFC2822Date << QString("10:12:34");
737 QTest::newRow(dataTag: "ISOWithMs 10:12:34.000") << QTime(10, 12, 34, 0) << Qt::ISODateWithMs << QString("10:12:34.000");
738 QTest::newRow(dataTag: "ISOWithMs 10:12:34.020") << QTime(10, 12, 34, 20) << Qt::ISODateWithMs << QString("10:12:34.020");
739 QTest::newRow(dataTag: "ISOWithMs 10:12:34.999") << QTime(10, 12, 34, 999) << Qt::ISODateWithMs << QString("10:12:34.999");
740}
741
742void tst_QTime::toStringDateFormat()
743{
744 QFETCH(QTime, time);
745 QFETCH(Qt::DateFormat, format);
746 QFETCH(QString, expected);
747
748 QCOMPARE(time.toString(format), expected);
749}
750
751void tst_QTime::toStringFormat_data()
752{
753 QTest::addColumn<QTime>(name: "t");
754 QTest::addColumn<QString>(name: "format");
755 QTest::addColumn<QString>(name: "str");
756
757 QTest::newRow( dataTag: "midnight" ) << QTime(0,0,0,0) << QString("h:m:s:z") << QString("0:0:0:0");
758 QTest::newRow( dataTag: "full" ) << QTime(10,12,34,53) << QString("hh:mm:ss:zzz") << QString("10:12:34:053");
759 QTest::newRow( dataTag: "short-msecs-lt100" ) << QTime(10,12,34,45) << QString("hh:m:ss:z") << QString("10:12:34:045");
760 QTest::newRow( dataTag: "short-msecs-gt100" ) << QTime(10,12,34,450) << QString("hh:m:ss:z") << QString("10:12:34:45");
761 QTest::newRow( dataTag: "am-pm" ) << QTime(10,12,34,45) << QString("hh:ss ap") << QString("10:34 am");
762 QTest::newRow( dataTag: "AM-PM" ) << QTime(22,12,34,45) << QString("hh:zzz AP") << QString("10:045 PM");
763 QTest::newRow( dataTag: "invalid" ) << QTime(230,230,230,230) << QString("hh:mm:ss") << QString();
764}
765
766void tst_QTime::toStringFormat()
767{
768 QFETCH( QTime, t );
769 QFETCH( QString, format );
770 QFETCH( QString, str );
771
772 QCOMPARE( t.toString( format ), str );
773}
774
775#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
776void tst_QTime::toStringLocale()
777{
778 QTime time(18, 30);
779 QCOMPARE(time.toString(Qt::SystemLocaleShortDate),
780 QLocale::system().toString(time, QLocale::ShortFormat));
781 QCOMPARE(time.toString(Qt::DefaultLocaleShortDate),
782 QLocale().toString(time, QLocale::ShortFormat));
783 QCOMPARE(time.toString(Qt::SystemLocaleLongDate),
784 QLocale::system().toString(time, QLocale::LongFormat));
785 QCOMPARE(time.toString(Qt::DefaultLocaleLongDate),
786 QLocale().toString(time, QLocale::LongFormat));
787 QLocale::setDefault(QLocale::German);
788 QCOMPARE(time.toString(Qt::SystemLocaleShortDate),
789 QLocale::system().toString(time, QLocale::ShortFormat));
790 QCOMPARE(time.toString(Qt::DefaultLocaleShortDate),
791 QLocale().toString(time, QLocale::ShortFormat));
792 QCOMPARE(time.toString(Qt::SystemLocaleLongDate),
793 QLocale::system().toString(time, QLocale::LongFormat));
794 QCOMPARE(time.toString(Qt::DefaultLocaleLongDate),
795 QLocale().toString(time, QLocale::LongFormat));
796}
797#endif // ### Qt 6: remove
798
799void tst_QTime::msecsSinceStartOfDay_data()
800{
801 QTest::addColumn<int>(name: "msecs");
802 QTest::addColumn<bool>(name: "isValid");
803 QTest::addColumn<int>(name: "hour");
804 QTest::addColumn<int>(name: "minute");
805 QTest::addColumn<int>(name: "second");
806 QTest::addColumn<int>(name: "msec");
807
808 QTest::newRow(dataTag: "00:00:00.000") << 0 << true
809 << 0 << 0 << 0 << 0;
810 QTest::newRow(dataTag: "01:00:00.001") << ((1 * 3600 * 1000) + 1) << true
811 << 1 << 0 << 0 << 1;
812 QTest::newRow(dataTag: "03:04:05.678") << ((3 * 3600 + 4 * 60 + 5) * 1000 + 678) << true
813 << 3 << 4 << 5 << 678;
814 QTest::newRow(dataTag: "23:59:59.999") << ((23 * 3600 + 59 * 60 + 59) * 1000 + 999) << true
815 << 23 << 59 << 59 << 999;
816 QTest::newRow(dataTag: "24:00:00.000") << ((24 * 3600) * 1000) << false
817 << -1 << -1 << -1 << -1;
818 QTest::newRow(dataTag: "-1 invalid") << -1 << false
819 << -1 << -1 << -1 << -1;
820}
821
822void tst_QTime::msecsSinceStartOfDay()
823{
824 QFETCH(int, msecs);
825 QFETCH(bool, isValid);
826 QFETCH(int, hour);
827 QFETCH(int, minute);
828 QFETCH(int, second);
829 QFETCH(int, msec);
830
831 QTime time = QTime::fromMSecsSinceStartOfDay(msecs);
832 QCOMPARE(time.isValid(), isValid);
833 if (msecs >= 0)
834 QCOMPARE(time.msecsSinceStartOfDay(), msecs);
835 else
836 QCOMPARE(time.msecsSinceStartOfDay(), 0);
837 QCOMPARE(time.hour(), hour);
838 QCOMPARE(time.minute(), minute);
839 QCOMPARE(time.second(), second);
840 QCOMPARE(time.msec(), msec);
841}
842
843QTEST_APPLESS_MAIN(tst_QTime)
844#include "tst_qtime.moc"
845

source code of qtbase/tests/auto/corelib/time/qtime/tst_qtime.cpp