1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
43 #include <qdatetime.h>
46 class tst_QDate : public QObject
54 void julianDay_data();
56 void dayOfWeek_data();
58 void dayOfYear_data();
60 void daysInMonth_data();
64 void weekNumber_invalid_data();
65 void weekNumber_invalid();
66 void weekNumber_data();
68 void julianDaysLimits();
71 void addMonths_data();
76 void operator_eq_eq();
77 void operator_not_eq();
80 void operator_lt_eq();
81 void operator_gt_eq();
82 void fromStringDateFormat_data();
83 void fromStringDateFormat();
84 void fromStringFormat_data();
85 void fromStringFormat();
86 void toStringFormat_data();
87 void toStringFormat();
88 void toStringDateFormat_data();
89 void toStringDateFormat();
91 void yearsZeroToNinetyNine();
92 void negativeYear() const;
93 void printNegativeYear() const;
94 void roundtripGermanLocale() const;
95 void shortDayName() const;
96 void standaloneShortDayName() const;
97 void longDayName() const;
98 void standaloneLongDayName() const;
99 void shortMonthName() const;
100 void standaloneShortMonthName() const;
101 void longMonthName() const;
102 void standaloneLongMonthName() const;
103 void roundtrip() const;
105 QDate defDate() const { return QDate(1900, 1, 1); }
106 QDate invalidDate() const { return QDate(); }
109 Q_DECLARE_METATYPE(QDate)
110 Q_DECLARE_METATYPE(Qt::DateFormat)
112 void tst_QDate::isNull_data()
114 QTest::addColumn<qint64>("jd");
115 QTest::addColumn<bool>("null");
117 qint64 minJd = std::numeric_limits<qint64>::min() / 2;
118 qint64 maxJd = std::numeric_limits<qint64>::max() / 2;
120 QTest::newRow("qint64 min") << std::numeric_limits<qint64>::min() << true;
121 QTest::newRow("minJd - 1") << minJd - 1 << true;
122 QTest::newRow("minJd") << minJd << false;
123 QTest::newRow("minJd + 1") << minJd + 1 << false;
124 QTest::newRow("maxJd - 1") << maxJd - 1 << false;
125 QTest::newRow("maxJd") << maxJd << false;
126 QTest::newRow("maxJd + 1") << maxJd + 1 << true;
127 QTest::newRow("qint64 max") << std::numeric_limits<qint64>::max() << true;
130 void tst_QDate::isNull()
134 QDate d = QDate::fromJulianDay(jd);
135 QTEST(d.isNull(), "null");
138 void tst_QDate::isValid_data()
140 qint64 nullJd = std::numeric_limits<qint64>::min();
142 QTest::addColumn<int>("year");
143 QTest::addColumn<int>("month");
144 QTest::addColumn<int>("day");
145 QTest::addColumn<qint64>("jd");
146 QTest::addColumn<bool>("valid");
148 QTest::newRow("0-0-0") << 0 << 0 << 0 << nullJd << false;
149 QTest::newRow("month 0") << 2000 << 0 << 1 << nullJd << false;
150 QTest::newRow("day 0") << 2000 << 1 << 0 << nullJd << false;
152 QTest::newRow("month 13") << 2000 << 13 << 1 << nullJd << false;
155 QTest::newRow("non-leap") << 2006 << 2 << 29 << nullJd << false;
156 QTest::newRow("normal leap") << 2004 << 2 << 29 << qint64(2453065) << true;
157 QTest::newRow("century leap 1900") << 1900 << 2 << 29 << nullJd << false;
158 QTest::newRow("century leap 2100") << 2100 << 2 << 29 << nullJd << false;
159 QTest::newRow("400-years leap 2000") << 2000 << 2 << 29 << qint64(2451604) << true;
160 QTest::newRow("400-years leap 2400") << 2400 << 2 << 29 << qint64(2597701) << true;
161 QTest::newRow("400-years leap 1600") << 1600 << 2 << 29 << qint64(2305507) << true;
162 QTest::newRow("year 0") << 0 << 2 << 27 << nullJd << false;
164 // test the number of days in months:
165 QTest::newRow("jan") << 2000 << 1 << 31 << qint64(2451575) << true;
166 QTest::newRow("feb") << 2000 << 2 << 29 << qint64(2451604) << true; // same data as 400-years leap
167 QTest::newRow("mar") << 2000 << 3 << 31 << qint64(2451635) << true;
168 QTest::newRow("apr") << 2000 << 4 << 30 << qint64(2451665) << true;
169 QTest::newRow("may") << 2000 << 5 << 31 << qint64(2451696) << true;
170 QTest::newRow("jun") << 2000 << 6 << 30 << qint64(2451726) << true;
171 QTest::newRow("jul") << 2000 << 7 << 31 << qint64(2451757) << true;
172 QTest::newRow("aug") << 2000 << 8 << 31 << qint64(2451788) << true;
173 QTest::newRow("sep") << 2000 << 9 << 30 << qint64(2451818) << true;
174 QTest::newRow("oct") << 2000 << 10 << 31 << qint64(2451849) << true;
175 QTest::newRow("nov") << 2000 << 11 << 30 << qint64(2451879) << true;
176 QTest::newRow("dec") << 2000 << 12 << 31 << qint64(2451910) << true;
178 // and invalid dates:
179 QTest::newRow("ijan") << 2000 << 1 << 32 << nullJd << false;
180 QTest::newRow("ifeb") << 2000 << 2 << 30 << nullJd << false;
181 QTest::newRow("imar") << 2000 << 3 << 32 << nullJd << false;
182 QTest::newRow("iapr") << 2000 << 4 << 31 << nullJd << false;
183 QTest::newRow("imay") << 2000 << 5 << 32 << nullJd << false;
184 QTest::newRow("ijun") << 2000 << 6 << 31 << nullJd << false;
185 QTest::newRow("ijul") << 2000 << 7 << 32 << nullJd << false;
186 QTest::newRow("iaug") << 2000 << 8 << 32 << nullJd << false;
187 QTest::newRow("isep") << 2000 << 9 << 31 << nullJd << false;
188 QTest::newRow("ioct") << 2000 << 10 << 32 << nullJd << false;
189 QTest::newRow("inov") << 2000 << 11 << 31 << nullJd << false;
190 QTest::newRow("idec") << 2000 << 12 << 32 << nullJd << false;
192 // the beginning of the Julian Day calendar:
193 QTest::newRow("jd earliest formula") << -4800 << 1 << 1 << qint64( -31738) << true;
194 QTest::newRow("jd -1") << -4714 << 11 << 23 << qint64( -1) << true;
195 QTest::newRow("jd 0") << -4714 << 11 << 24 << qint64( 0) << true;
196 QTest::newRow("jd 1") << -4714 << 11 << 25 << qint64( 1) << true;
197 QTest::newRow("jd latest formula") << 1400000 << 12 << 31 << qint64(513060925) << true;
200 void tst_QDate::isValid()
208 QCOMPARE(QDate::isValid(year, month, day), valid);
211 d.setDate(year, month, day);
212 QCOMPARE(d.isValid(), valid);
213 QCOMPARE(d.toJulianDay(), jd);
216 QCOMPARE(d.year(), year);
217 QCOMPARE(d.month(), month);
218 QCOMPARE(d.day(), day);
220 QCOMPARE(d.year(), 0);
221 QCOMPARE(d.month(), 0);
222 QCOMPARE(d.day(), 0);
226 void tst_QDate::julianDay_data()
231 void tst_QDate::julianDay()
240 d.setDate(year, month, day);
241 QCOMPARE(d.toJulianDay(), jd);
244 if (jd != std::numeric_limits<qint64>::min()) {
245 QDate d = QDate::fromJulianDay(jd);
246 QCOMPARE(d.year(), year);
247 QCOMPARE(d.month(), month);
248 QCOMPARE(d.day(), day);
252 void tst_QDate::dayOfWeek_data()
254 QTest::addColumn<int>("year");
255 QTest::addColumn<int>("month");
256 QTest::addColumn<int>("day");
257 QTest::addColumn<int>("dayOfWeek");
259 QTest::newRow("data0") << 0 << 0 << 0 << 0;
260 QTest::newRow("data1") << 2000 << 1 << 3 << 1;
261 QTest::newRow("data2") << 2000 << 1 << 4 << 2;
262 QTest::newRow("data3") << 2000 << 1 << 5 << 3;
263 QTest::newRow("data4") << 2000 << 1 << 6 << 4;
264 QTest::newRow("data5") << 2000 << 1 << 7 << 5;
265 QTest::newRow("data6") << 2000 << 1 << 8 << 6;
266 QTest::newRow("data7") << 2000 << 1 << 9 << 7;
267 QTest::newRow("data8") << -4800 << 1 << 1 << 1;
268 QTest::newRow("data9") << -4800 << 1 << 2 << 2;
269 QTest::newRow("data10") << -4800 << 1 << 3 << 3;
270 QTest::newRow("data11") << -4800 << 1 << 4 << 4;
271 QTest::newRow("data12") << -4800 << 1 << 5 << 5;
272 QTest::newRow("data13") << -4800 << 1 << 6 << 6;
273 QTest::newRow("data14") << -4800 << 1 << 7 << 7;
274 QTest::newRow("data15") << -4800 << 1 << 8 << 1;
277 void tst_QDate::dayOfWeek()
282 QFETCH(int, dayOfWeek);
284 QDate dt(year, month, day);
285 QCOMPARE(dt.dayOfWeek(), dayOfWeek);
288 void tst_QDate::dayOfYear_data()
290 QTest::addColumn<int>("year");
291 QTest::addColumn<int>("month");
292 QTest::addColumn<int>("day");
293 QTest::addColumn<int>("dayOfYear");
295 QTest::newRow("data0") << 0 << 0 << 0 << 0;
296 QTest::newRow("data1") << 2000 << 1 << 1 << 1;
297 QTest::newRow("data2") << 2000 << 1 << 2 << 2;
298 QTest::newRow("data3") << 2000 << 1 << 3 << 3;
299 QTest::newRow("data4") << 2000 << 12 << 31 << 366;
300 QTest::newRow("data5") << 2001 << 12 << 31 << 365;
301 QTest::newRow("data6") << 1815 << 1 << 1 << 1;
302 QTest::newRow("data7") << 1815 << 12 << 31 << 365;
303 QTest::newRow("data8") << 1500 << 1 << 1 << 1;
304 QTest::newRow("data9") << 1500 << 12 << 31 << 365;
305 QTest::newRow("data10") << -1500 << 1 << 1 << 1;
306 QTest::newRow("data11") << -1500 << 12 << 31 << 365;
307 QTest::newRow("data12") << -4800 << 1 << 1 << 1;
308 QTest::newRow("data13") << -4800 << 12 << 31 << 365;
311 void tst_QDate::dayOfYear()
316 QFETCH(int, dayOfYear);
318 QDate dt(year, month, day);
319 QCOMPARE(dt.dayOfYear(), dayOfYear);
322 void tst_QDate::daysInMonth_data()
324 QTest::addColumn<int>("year");
325 QTest::addColumn<int>("month");
326 QTest::addColumn<int>("day");
327 QTest::addColumn<int>("daysInMonth");
329 QTest::newRow("data0") << 0 << 0 << 0 << 0;
330 QTest::newRow("data1") << 2000 << 1 << 1 << 31;
331 QTest::newRow("data2") << 2000 << 2 << 1 << 29;
332 QTest::newRow("data3") << 2000 << 3 << 1 << 31;
333 QTest::newRow("data4") << 2000 << 4 << 1 << 30;
334 QTest::newRow("data5") << 2000 << 5 << 1 << 31;
335 QTest::newRow("data6") << 2000 << 6 << 1 << 30;
336 QTest::newRow("data7") << 2000 << 7 << 1 << 31;
337 QTest::newRow("data8") << 2000 << 8 << 1 << 31;
338 QTest::newRow("data9") << 2000 << 9 << 1 << 30;
339 QTest::newRow("data10") << 2000 << 10 << 1 << 31;
340 QTest::newRow("data11") << 2000 << 11 << 1 << 30;
341 QTest::newRow("data12") << 2000 << 12 << 1 << 31;
342 QTest::newRow("data13") << 2001 << 2 << 1 << 28;
345 void tst_QDate::daysInMonth()
350 QFETCH(int, daysInMonth);
352 QDate dt(year, month, day);
353 QCOMPARE(dt.daysInMonth(), daysInMonth);
356 void tst_QDate::daysInYear()
358 QDate dt(2000, 1, 1);
359 QCOMPARE(dt.daysInYear(), 366);
360 dt.setDate(2001, 1, 1);
361 QCOMPARE(dt.daysInYear(), 365);
363 QCOMPARE(dt.daysInYear(), 366);
365 QCOMPARE(dt.daysInYear(), 365);
368 void tst_QDate::getDate()
371 QDate dt(2000, 1, 1);
372 dt.getDate(&y, &m, &d);
377 dt.getDate(&y, &m, &d);
383 void tst_QDate::weekNumber_data()
385 QTest::addColumn<int>("expectedWeekNum");
386 QTest::addColumn<int>("expectedYearNum");
387 QTest::addColumn<int>("year");
388 QTest::addColumn<int>("month");
389 QTest::addColumn<int>("day");
391 //next we fill it with data
392 QTest::newRow( "data0" ) << 10 << 2002 << 2002 << 3 << 8;
393 QTest::newRow( "data1" ) << 10 << 2002 << 2002 << 3 << 8;
394 QTest::newRow( "data2" ) << 52 << 1999 << 2000 << 1 << 1;
395 QTest::newRow( "data3" ) << 52 << 1999 << 1999 << 12 << 31;
396 QTest::newRow( "data4" ) << 1 << 2001 << 2001 << 1 << 1;
397 QTest::newRow( "data5" ) << 53 << 1998 << 1998 << 12 << 31;
398 QTest::newRow( "data6" ) << 1 << 1985 << 1984 << 12 << 31;
399 QTest::newRow( "data7" ) << 52 << 2006 << 2006 << 12 << 31;
402 void tst_QDate::weekNumber()
406 QFETCH( int, month );
408 QFETCH( int, expectedWeekNum );
409 QFETCH( int, expectedYearNum );
410 QDate dt1( year, month, day );
411 QCOMPARE( dt1.weekNumber( &yearNumber ), expectedWeekNum );
412 QCOMPARE( yearNumber, expectedYearNum );
415 void tst_QDate::weekNumber_invalid_data()
417 QTest::addColumn<int>("year");
418 QTest::addColumn<int>("month");
419 QTest::addColumn<int>("day");
421 //next we fill it with data
422 QTest::newRow( "data0" ) << 0 << 0 << 0;
423 QTest::newRow( "data1" ) << 2001 << 1 << 32;
424 QTest::newRow( "data2" ) << 1999 << 2 << 29;
427 void tst_QDate::weekNumber_invalid()
431 QCOMPARE( dt.weekNumber( &yearNumber ), 0 );
434 void tst_QDate::julianDaysLimits()
436 qint64 min = std::numeric_limits<qint64>::min();
437 qint64 max = std::numeric_limits<qint64>::max();
438 qint64 minJd = std::numeric_limits<qint64>::min() / 2;
439 qint64 maxJd = std::numeric_limits<qint64>::max() / 2;
441 QDate maxDate = QDate::fromJulianDay(maxJd);
442 QDate minDate = QDate::fromJulianDay(minJd);
443 QDate zeroDate = QDate::fromJulianDay(0);
445 QDate dt = QDate::fromJulianDay(min);
446 QCOMPARE(dt.isValid(), false);
447 dt = QDate::fromJulianDay(minJd - 1);
448 QCOMPARE(dt.isValid(), false);
449 dt = QDate::fromJulianDay(minJd);
450 QCOMPARE(dt.isValid(), true);
451 dt = QDate::fromJulianDay(minJd + 1);
452 QCOMPARE(dt.isValid(), true);
453 dt = QDate::fromJulianDay(maxJd - 1);
454 QCOMPARE(dt.isValid(), true);
455 dt = QDate::fromJulianDay(maxJd);
456 QCOMPARE(dt.isValid(), true);
457 dt = QDate::fromJulianDay(maxJd + 1);
458 QCOMPARE(dt.isValid(), false);
459 dt = QDate::fromJulianDay(max);
460 QCOMPARE(dt.isValid(), false);
462 dt = maxDate.addDays(1);
463 QCOMPARE(dt.isValid(), false);
464 dt = maxDate.addDays(0);
465 QCOMPARE(dt.isValid(), true);
466 dt = maxDate.addDays(-1);
467 QCOMPARE(dt.isValid(), true);
468 dt = maxDate.addDays(max);
469 QCOMPARE(dt.isValid(), false);
470 dt = maxDate.addDays(min);
471 QCOMPARE(dt.isValid(), false);
473 dt = minDate.addDays(-1);
474 QCOMPARE(dt.isValid(), false);
475 dt = minDate.addDays(0);
476 QCOMPARE(dt.isValid(), true);
477 dt = minDate.addDays(1);
478 QCOMPARE(dt.isValid(), true);
479 dt = minDate.addDays(min);
480 QCOMPARE(dt.isValid(), false);
481 dt = minDate.addDays(max);
482 QCOMPARE(dt.isValid(), true);
484 dt = zeroDate.addDays(-1);
485 QCOMPARE(dt.isValid(), true);
486 dt = zeroDate.addDays(0);
487 QCOMPARE(dt.isValid(), true);
488 dt = zeroDate.addDays(1);
489 QCOMPARE(dt.isValid(), true);
490 dt = zeroDate.addDays(min);
491 QCOMPARE(dt.isValid(), false);
492 dt = zeroDate.addDays(max);
493 QCOMPARE(dt.isValid(), false);
496 void tst_QDate::addDays()
499 QFETCH( int, month );
501 QFETCH( int, amountToAdd );
502 QFETCH( int, expectedYear );
503 QFETCH( int, expectedMonth );
504 QFETCH( int, expectedDay );
506 QDate dt( year, month, day );
507 dt = dt.addDays( amountToAdd );
509 QCOMPARE( dt.year(), expectedYear );
510 QCOMPARE( dt.month(), expectedMonth );
511 QCOMPARE( dt.day(), expectedDay );
514 void tst_QDate::addDays_data()
516 QTest::addColumn<int>("year");
517 QTest::addColumn<int>("month");
518 QTest::addColumn<int>("day");
519 QTest::addColumn<int>("amountToAdd");
520 QTest::addColumn<int>("expectedYear");
521 QTest::addColumn<int>("expectedMonth");
522 QTest::addColumn<int>("expectedDay");
524 QTest::newRow( "data0" ) << 2000 << 1 << 1 << 1 << 2000 << 1 << 2;
525 QTest::newRow( "data1" ) << 2000 << 1 << 31 << 1 << 2000 << 2 << 1;
526 QTest::newRow( "data2" ) << 2000 << 2 << 28 << 1 << 2000 << 2 << 29;
527 QTest::newRow( "data3" ) << 2000 << 2 << 29 << 1 << 2000 << 3 << 1;
528 QTest::newRow( "data4" ) << 2000 << 12 << 31 << 1 << 2001 << 1 << 1;
529 QTest::newRow( "data5" ) << 2001 << 2 << 28 << 1 << 2001 << 3 << 1;
530 QTest::newRow( "data6" ) << 2001 << 2 << 28 << 30 << 2001 << 3 << 30;
531 QTest::newRow( "data7" ) << 2001 << 3 << 30 << 5 << 2001 << 4 << 4;
533 QTest::newRow( "data8" ) << 2000 << 1 << 1 << -1 << 1999 << 12 << 31;
534 QTest::newRow( "data9" ) << 2000 << 1 << 31 << -1 << 2000 << 1 << 30;
535 QTest::newRow( "data10" ) << 2000 << 2 << 28 << -1 << 2000 << 2 << 27;
536 QTest::newRow( "data11" ) << 2001 << 2 << 28 << -30 << 2001 << 1 << 29;
538 QTest::newRow( "data12" ) << -4713 << 1 << 2 << -2 << -4714 << 12 << 31;
539 QTest::newRow( "data13" ) << -4713 << 1 << 2 << 2 << -4713 << 1 << 4;
542 void tst_QDate::addMonths()
545 QFETCH( int, month );
547 QFETCH( int, amountToAdd );
548 QFETCH( int, expectedYear );
549 QFETCH( int, expectedMonth );
550 QFETCH( int, expectedDay );
552 QDate dt( year, month, day );
553 dt = dt.addMonths( amountToAdd );
555 QCOMPARE( dt.year(), expectedYear );
556 QCOMPARE( dt.month(), expectedMonth );
557 QCOMPARE( dt.day(), expectedDay );
560 void tst_QDate::addMonths_data()
562 QTest::addColumn<int>("year");
563 QTest::addColumn<int>("month");
564 QTest::addColumn<int>("day");
565 QTest::addColumn<int>("amountToAdd");
566 QTest::addColumn<int>("expectedYear");
567 QTest::addColumn<int>("expectedMonth");
568 QTest::addColumn<int>("expectedDay");
570 QTest::newRow( "data0" ) << 2000 << 1 << 1 << 1 << 2000 << 2 << 1;
571 QTest::newRow( "data1" ) << 2000 << 1 << 31 << 1 << 2000 << 2 << 29;
572 QTest::newRow( "data2" ) << 2000 << 2 << 28 << 1 << 2000 << 3 << 28;
573 QTest::newRow( "data3" ) << 2000 << 2 << 29 << 1 << 2000 << 3 << 29;
574 QTest::newRow( "data4" ) << 2000 << 12 << 31 << 1 << 2001 << 1 << 31;
575 QTest::newRow( "data5" ) << 2001 << 2 << 28 << 1 << 2001 << 3 << 28;
576 QTest::newRow( "data6" ) << 2001 << 2 << 28 << 12 << 2002 << 2 << 28;
577 QTest::newRow( "data7" ) << 2000 << 2 << 29 << 12 << 2001 << 2 << 28;
578 QTest::newRow( "data8" ) << 2000 << 10 << 15 << 4 << 2001 << 2 << 15;
580 QTest::newRow( "data9" ) << 2000 << 1 << 1 << -1 << 1999 << 12 << 1;
581 QTest::newRow( "data10" ) << 2000 << 1 << 31 << -1 << 1999 << 12 << 31;
582 QTest::newRow( "data11" ) << 2000 << 12 << 31 << -1 << 2000 << 11 << 30;
583 QTest::newRow( "data12" ) << 2001 << 2 << 28 << -12 << 2000 << 2 << 28;
584 QTest::newRow( "data13" ) << 2000 << 1 << 31 << -7 << 1999 << 6 << 30;
585 QTest::newRow( "data14" ) << 2000 << 2 << 29 << -12 << 1999 << 2 << 28;
588 QTest::newRow( "data15" ) << 1 << 1 << 1 << -1 << -1 << 12 << 1;
589 QTest::newRow( "data16" ) << 1 << 1 << 1 << -12 << -1 << 1 << 1;
590 QTest::newRow( "data17" ) << -1 << 12 << 1 << 1 << 1 << 1 << 1;
591 QTest::newRow( "data18" ) << -1 << 1 << 1 << 12 << 1 << 1 << 1;
594 void tst_QDate::addYears()
597 QFETCH( int, month );
599 QFETCH( int, amountToAdd );
600 QFETCH( int, expectedYear );
601 QFETCH( int, expectedMonth );
602 QFETCH( int, expectedDay );
604 QDate dt( year, month, day );
605 dt = dt.addYears( amountToAdd );
607 QCOMPARE( dt.year(), expectedYear );
608 QCOMPARE( dt.month(), expectedMonth );
609 QCOMPARE( dt.day(), expectedDay );
612 void tst_QDate::addYears_data()
614 QTest::addColumn<int>("year");
615 QTest::addColumn<int>("month");
616 QTest::addColumn<int>("day");
617 QTest::addColumn<int>("amountToAdd");
618 QTest::addColumn<int>("expectedYear");
619 QTest::addColumn<int>("expectedMonth");
620 QTest::addColumn<int>("expectedDay");
622 QTest::newRow( "data0" ) << 2000 << 1 << 1 << 1 << 2001 << 1 << 1;
623 QTest::newRow( "data1" ) << 2000 << 1 << 31 << 1 << 2001 << 1 << 31;
624 QTest::newRow( "data2" ) << 2000 << 2 << 28 << 1 << 2001 << 2 << 28;
625 QTest::newRow( "data3" ) << 2000 << 2 << 29 << 1 << 2001 << 2 << 28;
626 QTest::newRow( "data4" ) << 2000 << 12 << 31 << 1 << 2001 << 12 << 31;
627 QTest::newRow( "data5" ) << 2001 << 2 << 28 << 3 << 2004 << 2 << 28;
628 QTest::newRow( "data6" ) << 2000 << 2 << 29 << 4 << 2004 << 2 << 29;
630 QTest::newRow( "data7" ) << 2000 << 1 << 31 << -1 << 1999 << 1 << 31;
631 QTest::newRow( "data9" ) << 2000 << 2 << 29 << -1 << 1999 << 2 << 28;
632 QTest::newRow( "data10" ) << 2000 << 12 << 31 << -1 << 1999 << 12 << 31;
633 QTest::newRow( "data11" ) << 2001 << 2 << 28 << -3 << 1998 << 2 << 28;
634 QTest::newRow( "data12" ) << 2000 << 2 << 29 << -4 << 1996 << 2 << 29;
635 QTest::newRow( "data13" ) << 2000 << 2 << 29 << -5 << 1995 << 2 << 28;
637 QTest::newRow( "data14" ) << 2000 << 1 << 1 << -1999 << 1 << 1 << 1;
638 QTest::newRow( "data15" ) << 2000 << 1 << 1 << -2000 << -1 << 1 << 1;
639 QTest::newRow( "data16" ) << 2000 << 1 << 1 << -2001 << -2 << 1 << 1;
640 QTest::newRow( "data17" ) << -2000 << 1 << 1 << 1999 << -1 << 1 << 1;
641 QTest::newRow( "data18" ) << -2000 << 1 << 1 << 2000 << 1 << 1 << 1;
642 QTest::newRow( "data19" ) << -2000 << 1 << 1 << 2001 << 2 << 1 << 1;
645 void tst_QDate::daysTo()
647 qint64 minJd = std::numeric_limits<qint64>::min() / 2;
648 qint64 maxJd = std::numeric_limits<qint64>::max() / 2;
650 QDate dt1(2000, 1, 1);
651 QDate dt2(2000, 1, 5);
652 QCOMPARE(dt1.daysTo(dt2), (qint64) 4);
653 QCOMPARE(dt2.daysTo(dt1), (qint64) -4);
655 dt1.setDate(0, 0, 0);
656 QCOMPARE(dt1.daysTo(dt2), (qint64) 0);
657 dt1.setDate(2000, 1, 1);
658 dt2.setDate(0, 0, 0);
659 QCOMPARE(dt1.daysTo(dt2), (qint64) 0);
662 QDate maxDate = QDate::fromJulianDay(maxJd);
663 QDate minDate = QDate::fromJulianDay(minJd);
664 QDate zeroDate = QDate::fromJulianDay(0);
666 QCOMPARE(maxDate.daysTo(minDate), minJd - maxJd);
667 QCOMPARE(minDate.daysTo(maxDate), maxJd - minJd);
668 QCOMPARE(maxDate.daysTo(zeroDate), -maxJd);
669 QCOMPARE(zeroDate.daysTo(maxDate), maxJd);
670 QCOMPARE(minDate.daysTo(zeroDate), -minJd);
671 QCOMPARE(zeroDate.daysTo(minDate), minJd);
674 void tst_QDate::operator_eq_eq()
680 d1 = QDate(2001,12,5);
681 d2 = QDate(2001,12,5);
684 d2 = QDate(2002,12,5);
685 QVERIFY( !(d1 == d2) );
688 void tst_QDate::operator_not_eq()
692 QVERIFY( !(d1 != d2) );
694 d1 = QDate(2001,12,5);
695 d2 = QDate(2001,12,5);
696 QVERIFY( !(d1 != d2) );
698 d2 = QDate(2002,12,5);
702 void tst_QDate::operator_lt()
706 QVERIFY( !(d1 < d2) );
708 d1 = QDate(2001,12,4);
709 d2 = QDate(2001,12,5);
712 d1 = QDate(2001,11,5);
713 d2 = QDate(2001,12,5);
716 d1 = QDate(2000,12,5);
717 d2 = QDate(2001,12,5);
720 d1 = QDate(2002,12,5);
721 d2 = QDate(2001,12,5);
722 QVERIFY( !(d1 < d2) );
724 d1 = QDate(2001,12,5);
725 d2 = QDate(2001,11,5);
726 QVERIFY( !(d1 < d2) );
728 d1 = QDate(2001,12,6);
729 d2 = QDate(2001,12,5);
730 QVERIFY( !(d1 < d2) );
733 void tst_QDate::operator_gt()
737 QVERIFY( !(d1 > d2) );
739 d1 = QDate(2001,12,4);
740 d2 = QDate(2001,12,5);
741 QVERIFY( !(d1 > d2) );
743 d1 = QDate(2001,11,5);
744 d2 = QDate(2001,12,5);
745 QVERIFY( !(d1 > d2) );
747 d1 = QDate(2000,12,5);
748 d2 = QDate(2001,12,5);
749 QVERIFY( !(d1 > d2) );
751 d1 = QDate(2002,12,5);
752 d2 = QDate(2001,12,5);
755 d1 = QDate(2001,12,5);
756 d2 = QDate(2001,11,5);
759 d1 = QDate(2001,12,6);
760 d2 = QDate(2001,12,5);
764 void tst_QDate::operator_lt_eq()
770 d1 = QDate(2001,12,4);
771 d2 = QDate(2001,12,5);
774 d1 = QDate(2001,11,5);
775 d2 = QDate(2001,12,5);
778 d1 = QDate(2000,12,5);
779 d2 = QDate(2001,12,5);
782 d1 = QDate(2002,12,5);
783 d2 = QDate(2001,12,5);
784 QVERIFY( !(d1 <= d2) );
786 d1 = QDate(2001,12,5);
787 d2 = QDate(2001,11,5);
788 QVERIFY( !(d1 <= d2) );
790 d1 = QDate(2001,12,6);
791 d2 = QDate(2001,12,5);
792 QVERIFY( !(d1 <= d2) );
795 void tst_QDate::operator_gt_eq()
801 d1 = QDate(2001,12,4);
802 d2 = QDate(2001,12,5);
803 QVERIFY( !(d1 >= d2) );
805 d1 = QDate(2001,11,5);
806 d2 = QDate(2001,12,5);
807 QVERIFY( !(d1 >= d2) );
809 d1 = QDate(2000,12,5);
810 d2 = QDate(2001,12,5);
811 QVERIFY( !(d1 >= d2) );
813 d1 = QDate(2002,12,5);
814 d2 = QDate(2001,12,5);
817 d1 = QDate(2001,12,5);
818 d2 = QDate(2001,11,5);
821 d1 = QDate(2001,12,6);
822 d2 = QDate(2001,12,5);
826 void tst_QDate::fromStringDateFormat_data()
828 // Since we can't define an element of Qt::DateFormat, d1 will be the date
829 // expected when we have a TextDate, and d2 will be the date expected when
830 // we have an ISODate.
832 QTest::addColumn<QString>("str1");
833 QTest::addColumn<QString>("str2");
834 QTest::addColumn<QDate>("d1");
836 QTest::newRow( "data0" ) << QString("Sat May 20 1995") << QString("1995-05-20") << QDate(1995,5,20);
837 QTest::newRow( "data1" ) << QString("Tue Dec 17 2002") << QString("2002-12-17") << QDate(2002,12,17);
838 QDate d( 1999, 11, 14 );
839 QTest::newRow( "data2" ) << d.toString( Qt::TextDate ) << d.toString( Qt::ISODate ) << d;
841 QTest::newRow( "data3" ) << QString("xxx Jan 1 0999") << QString("0999-01-01") << QDate(999, 1, 1);
842 QTest::newRow( "data3b" ) << QString("xxx Jan 1 999") << QString("0999-01-01") << QDate(999, 1, 1);
843 QTest::newRow( "data4" ) << QString("xxx Jan 1 12345") << QString() << QDate(12345, 1, 1);
844 QTest::newRow( "data5" ) << QString("xxx Jan 1 -0001") << QString() << QDate(-1, 1, 1);
845 QTest::newRow( "data6" ) << QString("xxx Jan 1 -4712") << QString() << QDate(-4712, 1, 1);
846 QTest::newRow( "data7" ) << QString("xxx Nov 25 -4713") << QString() << QDate(-4713, 11, 25);
849 void tst_QDate::fromStringDateFormat()
851 QFETCH( QString, str1 );
852 QFETCH( QString, str2 );
855 QCOMPARE( QDate::fromString( str1, Qt::TextDate ), d1 );
857 QCOMPARE( QDate::fromString( str2, Qt::ISODate ), d1 );
860 void tst_QDate::fromStringFormat_data()
862 QTest::addColumn<QString>("string");
863 QTest::addColumn<QString>("format");
864 QTest::addColumn<QDate>("expected");
866 //get localized names
867 QString january = QDate::longMonthName(1);
868 QString february = QDate::longMonthName(2);
869 QString march = QDate::longMonthName(3);
870 QString august = QDate::longMonthName(8);
871 QString mon = QDate::shortDayName(1);
872 QString monday = QDate::longDayName(1);
873 QString tuesday = QDate::longDayName(2);
874 QString wednesday = QDate::longDayName(3);
875 QString thursday = QDate::longDayName(4);
876 QString friday = QDate::longDayName(5);
877 QString saturday = QDate::longDayName(6);
878 QString sunday = QDate::longDayName(7);
880 QTest::newRow("data0") << QString("") << QString("") << defDate();
881 QTest::newRow("data1") << QString(" ") << QString("") << invalidDate();
882 QTest::newRow("data2") << QString(" ") << QString(" ") << defDate();
883 QTest::newRow("data3") << QString("-%$%#") << QString("$*(#@") << invalidDate();
884 QTest::newRow("data4") << QString("d") << QString("'d'") << defDate();
885 QTest::newRow("data5") << QString("101010") << QString("dMyy") << QDate(1910, 10, 10);
886 QTest::newRow("data6") << QString("101010b") << QString("dMyy") << invalidDate();
887 QTest::newRow("data7") << january << QString("MMMM") << defDate();
888 QTest::newRow("data8") << QString("ball") << QString("balle") << invalidDate();
889 QTest::newRow("data9") << QString("balleh") << QString("balleh") << defDate();
890 QTest::newRow("data10") << QString("10.01.1") << QString("M.dd.d") << QDate(defDate().year(), 10, 1);
891 QTest::newRow("data11") << QString("-1.01.1") << QString("M.dd.d") << invalidDate();
892 QTest::newRow("data12") << QString("11010") << QString("dMMyy") << invalidDate();
893 QTest::newRow("data13") << QString("-2") << QString("d") << invalidDate();
894 QTest::newRow("data14") << QString("132") << QString("Md") << invalidDate();
895 QTest::newRow("data15") << february << QString("MMMM") << QDate(defDate().year(), 2, 1);
897 QString date = mon + " " + august + " 8 2005";
898 QTest::newRow("data16") << date << QString("ddd MMMM d yyyy") << QDate(2005, 8, 8);
899 QTest::newRow("data17") << QString("2000:00") << QString("yyyy:yy") << QDate(2000, 1, 1);
900 QTest::newRow("data18") << QString("1999:99") << QString("yyyy:yy") << QDate(1999, 1, 1);
901 QTest::newRow("data19") << QString("2099:99") << QString("yyyy:yy") << QDate(2099, 1, 1);
902 QTest::newRow("data20") << QString("2001:01") << QString("yyyy:yy") << QDate(2001, 1, 1);
903 QTest::newRow("data21") << QString("99") << QString("yy") << QDate(1999, 1, 1);
904 QTest::newRow("data22") << QString("01") << QString("yy") << QDate(1901, 1, 1);
906 QTest::newRow("data23") << monday << QString("dddd") << QDate(1900, 1, 1);
907 QTest::newRow("data24") << tuesday << QString("dddd") << QDate(1900, 1, 2);
908 QTest::newRow("data25") << wednesday << QString("dddd") << QDate(1900, 1, 3);
909 QTest::newRow("data26") << thursday << QString("dddd") << QDate(1900, 1, 4);
910 QTest::newRow("data27") << friday << QString("dddd") << QDate(1900, 1, 5);
911 QTest::newRow("data28") << saturday << QString("dddd") << QDate(1900, 1, 6);
912 QTest::newRow("data29") << sunday << QString("dddd") << QDate(1900, 1, 7);
914 QTest::newRow("data30") << monday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 2);
915 QTest::newRow("data31") << tuesday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 3);
916 QTest::newRow("data32") << wednesday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 4);
917 QTest::newRow("data33") << thursday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 5);
918 QTest::newRow("data34") << friday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 6);
919 QTest::newRow("data35") << saturday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 7);
920 QTest::newRow("data36") << sunday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 1);
922 QTest::newRow("data37") << tuesday + " 2007 " + march << QString("dddd yyyy MMMM") << QDate(2007, 3, 6);
924 QTest::newRow("data38") << QString("21052006") << QString("ddMMyyyy") << QDate(2006,5,21);
925 QTest::newRow("data39") << QString("210506") << QString("ddMMyy") << QDate(1906,5,21);
926 QTest::newRow("data40") << QString("21/5/2006") << QString("d/M/yyyy") << QDate(2006,5,21);
927 QTest::newRow("data41") << QString("21/5/06") << QString("d/M/yy") << QDate(1906,5,21);
928 QTest::newRow("data42") << QString("20060521") << QString("yyyyMMdd") << QDate(2006,5,21);
929 QTest::newRow("data43") << QString("060521") << QString("yyMMdd") << QDate(1906,5,21);
933 void tst_QDate::fromStringFormat()
935 QFETCH(QString, string);
936 QFETCH(QString, format);
937 QFETCH(QDate, expected);
939 QDate dt = QDate::fromString(string, format);
940 QCOMPARE(dt, expected);
943 void tst_QDate::toStringFormat_data()
945 QTest::addColumn<QDate>("t");
946 QTest::addColumn<QString>("format");
947 QTest::addColumn<QString>("str");
949 QTest::newRow( "data0" ) << QDate(1995,5,20) << QString("d-M-yy") << QString("20-5-95");
950 QTest::newRow( "data1" ) << QDate(2002,12,17) << QString("dd-MM-yyyy") << QString("17-12-2002");
951 QTest::newRow( "data2" ) << QDate(1995,5,20) << QString("M-yy") << QString("5-95");
952 QTest::newRow( "data3" ) << QDate(2002,12,17) << QString("dd") << QString("17");
953 QTest::newRow( "data4" ) << QDate() << QString("dd-mm-yyyy") << QString();
956 void tst_QDate::toStringFormat()
959 QFETCH( QString, format );
960 QFETCH( QString, str );
962 QCOMPARE( t.toString( format ), str );
965 void tst_QDate::toStringDateFormat_data()
967 QTest::addColumn<QDate>("date");
968 QTest::addColumn<Qt::DateFormat>("format");
969 QTest::addColumn<QString>("expectedStr");
971 QTest::newRow("data0") << QDate(1,1,1) << Qt::ISODate << QString("0001-01-01");
972 QTest::newRow("data1") << QDate(11,1,1) << Qt::ISODate << QString("0011-01-01");
973 QTest::newRow("data2") << QDate(111,1,1) << Qt::ISODate << QString("0111-01-01");
974 QTest::newRow("data3") << QDate(1974,12,1) << Qt::ISODate << QString("1974-12-01");
977 void tst_QDate::toStringDateFormat()
980 QFETCH(Qt::DateFormat, format);
981 QFETCH(QString, expectedStr);
983 QCOMPARE(date.toString(Qt::SystemLocaleShortDate), QLocale::system().toString(date, QLocale::ShortFormat));
984 QCOMPARE(date.toString(Qt::LocaleDate), QLocale().toString(date, QLocale::ShortFormat));
985 QLocale::setDefault(QLocale::German);
986 QCOMPARE(date.toString(Qt::SystemLocaleShortDate), QLocale::system().toString(date, QLocale::ShortFormat));
987 QCOMPARE(date.toString(Qt::LocaleDate), QLocale().toString(date, QLocale::ShortFormat));
989 QCOMPARE(date.toString(format), expectedStr);
992 void tst_QDate::isLeapYear()
994 QVERIFY(QDate::isLeapYear(-4801));
995 QVERIFY(!QDate::isLeapYear(-4800));
996 QVERIFY(QDate::isLeapYear(-4445));
997 QVERIFY(!QDate::isLeapYear(-4444));
998 QVERIFY(!QDate::isLeapYear(-6));
999 QVERIFY(QDate::isLeapYear(-5));
1000 QVERIFY(!QDate::isLeapYear(-4));
1001 QVERIFY(!QDate::isLeapYear(-3));
1002 QVERIFY(!QDate::isLeapYear(-2));
1003 QVERIFY(QDate::isLeapYear(-1));
1004 QVERIFY(!QDate::isLeapYear(0)); // Doesn't exist
1005 QVERIFY(!QDate::isLeapYear(1));
1006 QVERIFY(!QDate::isLeapYear(2));
1007 QVERIFY(!QDate::isLeapYear(3));
1008 QVERIFY(QDate::isLeapYear(4));
1009 QVERIFY(!QDate::isLeapYear(7));
1010 QVERIFY(QDate::isLeapYear(8));
1011 QVERIFY(!QDate::isLeapYear(100));
1012 QVERIFY(QDate::isLeapYear(400));
1013 QVERIFY(!QDate::isLeapYear(700));
1014 QVERIFY(!QDate::isLeapYear(1500));
1015 QVERIFY(QDate::isLeapYear(1600));
1016 QVERIFY(!QDate::isLeapYear(1700));
1017 QVERIFY(!QDate::isLeapYear(1800));
1018 QVERIFY(!QDate::isLeapYear(1900));
1019 QVERIFY(QDate::isLeapYear(2000));
1020 QVERIFY(!QDate::isLeapYear(2100));
1021 QVERIFY(!QDate::isLeapYear(2200));
1022 QVERIFY(!QDate::isLeapYear(2300));
1023 QVERIFY(QDate::isLeapYear(2400));
1024 QVERIFY(!QDate::isLeapYear(2500));
1025 QVERIFY(!QDate::isLeapYear(2600));
1026 QVERIFY(!QDate::isLeapYear(2700));
1027 QVERIFY(QDate::isLeapYear(2800));
1029 for (int i = -4713; i <= 10000; ++i) {
1032 QVERIFY(!QDate(i, 2, 29).isValid() == !QDate::isLeapYear(i));
1036 void tst_QDate::yearsZeroToNinetyNine()
1040 QCOMPARE(dt.year(), -1);
1041 QCOMPARE(dt.month(), 2);
1042 QCOMPARE(dt.day(), 3);
1047 QCOMPARE(dt.year(), 1);
1048 QCOMPARE(dt.month(), 2);
1049 QCOMPARE(dt.day(), 3);
1054 QCOMPARE(dt.year(), 99);
1055 QCOMPARE(dt.month(), 2);
1056 QCOMPARE(dt.day(), 3);
1059 QVERIFY(!QDate::isValid(0, 2, 3));
1060 QVERIFY(QDate::isValid(1, 2, 3));
1061 QVERIFY(QDate::isValid(-1, 2, 3));
1063 #if QT_DEPRECATED_SINCE(5,0)
1067 QCOMPARE(dt.year(), 1901);
1068 QCOMPARE(dt.month(), 2);
1069 QCOMPARE(dt.day(), 3);
1075 dt.setDate(1, 2, 3);
1076 QCOMPARE(dt.year(), 1);
1077 QCOMPARE(dt.month(), 2);
1078 QCOMPARE(dt.day(), 3);
1080 dt.setDate(0, 2, 3);
1081 QVERIFY(!dt.isValid());
1086 void tst_QDate::negativeYear() const
1089 QVERIFY(y.isValid());
1090 QCOMPARE(y.year(), -20);
1093 void tst_QDate::printNegativeYear() const
1096 QDate date(-500, 3, 4);
1097 QVERIFY(date.isValid());
1098 QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0500"));
1102 QDate date(-10, 3, 4);
1103 QVERIFY(date.isValid());
1104 QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0010"));
1108 QDate date(-2, 3, 4);
1109 QVERIFY(date.isValid());
1110 QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0002"));
1114 void tst_QDate::roundtripGermanLocale() const
1116 /* This code path should not result in warnings. */
1117 const QDate theDate(QDate::currentDate());
1118 theDate.fromString(theDate.toString(Qt::TextDate), Qt::TextDate);
1120 const QDateTime theDateTime(QDateTime::currentDateTime());
1121 theDateTime.fromString(theDateTime.toString(Qt::TextDate), Qt::TextDate);
1124 void tst_QDate::shortDayName() const
1126 QCOMPARE(QDate::shortDayName(0), QString());
1128 if (QLocale::system().language() == QLocale::C) {
1129 QCOMPARE(QDate::shortDayName(1), QLatin1String("Mon"));
1130 QCOMPARE(QDate::shortDayName(7), QLatin1String("Sun"));
1133 QLocale locale = QLocale::system();
1134 for(int i = 1; i <= 7; ++i) {
1135 QCOMPARE(QDate::shortDayName(i), locale.dayName(i, QLocale::ShortFormat));
1139 void tst_QDate::standaloneShortDayName() const
1141 QCOMPARE(QDate::shortDayName(0, QDate::StandaloneFormat), QString());
1143 if (QLocale::system().language() == QLocale::C) {
1144 QCOMPARE(QDate::shortDayName(1, QDate::StandaloneFormat), QLatin1String("Mon"));
1145 QCOMPARE(QDate::shortDayName(7, QDate::StandaloneFormat), QLatin1String("Sun"));
1148 QLocale locale = QLocale::system();
1149 for(int i = 1; i <= 7; ++i) {
1150 QCOMPARE(QDate::shortDayName(i, QDate::StandaloneFormat), locale.standaloneDayName(i, QLocale::ShortFormat));
1154 void tst_QDate::longDayName() const
1156 QCOMPARE(QDate::longDayName(0), QString());
1158 if (QLocale::system().language() == QLocale::C) {
1159 QCOMPARE(QDate::longDayName(1), QLatin1String("Monday"));
1160 QCOMPARE(QDate::longDayName(7), QLatin1String("Sunday"));
1163 QLocale locale = QLocale::system();
1164 for(int i = 1; i <= 7; ++i) {
1165 QCOMPARE(QDate::longDayName(i), locale.dayName(i, QLocale::LongFormat));
1169 void tst_QDate::standaloneLongDayName() const
1171 QCOMPARE(QDate::longDayName(0, QDate::StandaloneFormat), QString());
1173 if (QLocale::system().language() == QLocale::C) {
1174 QCOMPARE(QDate::longDayName(1, QDate::StandaloneFormat), QLatin1String("Monday"));
1175 QCOMPARE(QDate::longDayName(7, QDate::StandaloneFormat), QLatin1String("Sunday"));
1178 QLocale locale = QLocale::system();
1179 for(int i = 1; i <= 7; ++i) {
1180 QCOMPARE(QDate::longDayName(i, QDate::StandaloneFormat), locale.standaloneDayName(i, QLocale::LongFormat));
1184 void tst_QDate::shortMonthName() const
1186 QCOMPARE(QDate::shortMonthName(0), QString());
1188 if (QLocale::system().language() == QLocale::C) {
1189 QCOMPARE(QDate::shortMonthName(1), QLatin1String("Jan"));
1190 QCOMPARE(QDate::shortMonthName(8), QLatin1String("Aug"));
1193 QLocale locale = QLocale::system();
1194 for(int i = 1; i <= 12; ++i) {
1195 QCOMPARE(QDate::shortMonthName(i), locale.monthName(i, QLocale::ShortFormat));
1199 void tst_QDate::standaloneShortMonthName() const
1201 QCOMPARE(QDate::shortMonthName(0, QDate::StandaloneFormat), QString());
1203 if (QLocale::system().language() == QLocale::C) {
1204 QCOMPARE(QDate::shortMonthName(1, QDate::StandaloneFormat), QLatin1String("Jan"));
1205 QCOMPARE(QDate::shortMonthName(8, QDate::StandaloneFormat), QLatin1String("Aug"));
1208 QLocale locale = QLocale::system();
1209 for(int i = 1; i <= 12; ++i) {
1210 QCOMPARE(QDate::shortMonthName(i, QDate::StandaloneFormat), locale.standaloneMonthName(i, QLocale::ShortFormat));
1214 void tst_QDate::longMonthName() const
1216 QCOMPARE(QDate::longMonthName(0), QString());
1218 if (QLocale::system().language() == QLocale::C) {
1219 QCOMPARE(QDate::longMonthName(1), QLatin1String("January"));
1220 QCOMPARE(QDate::longMonthName(8), QLatin1String("August"));
1223 QLocale locale = QLocale::system();
1224 for(int i = 1; i <= 12; ++i) {
1225 QCOMPARE(QDate::longMonthName(i), locale.monthName(i, QLocale::LongFormat));
1229 void tst_QDate::standaloneLongMonthName() const
1231 QCOMPARE(QDate::longMonthName(0, QDate::StandaloneFormat), QString());
1233 if (QLocale::system().language() == QLocale::C) {
1234 QCOMPARE(QDate::longMonthName(1, QDate::StandaloneFormat), QLatin1String("January"));
1235 QCOMPARE(QDate::longMonthName(8, QDate::StandaloneFormat), QLatin1String("August"));
1238 QLocale locale = QLocale::system();
1239 for(int i = 1; i <= 12; ++i) {
1240 QCOMPARE(QDate::longMonthName(i, QDate::StandaloneFormat), locale.standaloneMonthName(i, QLocale::LongFormat));
1244 void tst_QDate::roundtrip() const
1246 // Test round trip, this exercises setDate(), isValid(), isLeapYear(),
1247 // year(), month(), day(), julianDayFromDate(), and getDateFromJulianDay()
1248 // to ensure they are internally consistent (but doesn't guarantee correct)
1250 // Test Julian round trip around JD 0 and current low end of valid range
1252 QDate loopDate = QDate::fromJulianDay(-31738); // 1 Jan 4800 BC
1253 while (loopDate.toJulianDay() <= 5150) { // 31 Dec 4700 BC
1254 testDate.setDate(loopDate.year(), loopDate.month(), loopDate.day());
1255 QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
1256 loopDate = loopDate.addDays(1);
1259 // Test Julian round trip in both BC and AD
1260 loopDate = QDate::fromJulianDay(1684901); // 1 Jan 100 BC
1261 while (loopDate.toJulianDay() <= 1757949) { // 31 Dec 100 AD
1262 testDate.setDate(loopDate.year(), loopDate.month(), loopDate.day());
1263 QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
1264 loopDate = loopDate.addDays(1);
1267 // Test Gregorian round trip during current useful period
1268 loopDate = QDate::fromJulianDay(2378497); // 1 Jan 1900 AD
1269 while (loopDate.toJulianDay() <= 2488433) { // 31 Dec 2100 AD
1270 testDate.setDate(loopDate.year(), loopDate.month(), loopDate.day());
1271 QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
1272 loopDate = loopDate.addDays(1);
1275 // Test Gregorian round trip at top end of widget/format range
1276 loopDate = QDate::fromJulianDay(5336961); // 1 Jan 9900 AD
1277 while (loopDate.toJulianDay() <= 5373484) { // 31 Dec 9999 AD
1278 testDate.setDate(loopDate.year(), loopDate.month(), loopDate.day());
1279 QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
1280 loopDate = loopDate.addDays(1);
1283 // Test Gregorian round trip at top end of conversion range
1284 loopDate = QDate::fromJulianDay(513024036); // 1 Jan 1399900 AD
1285 while (loopDate.toJulianDay() <= 513060925) { // 31 Dec 1400000 AD
1286 testDate.setDate(loopDate.year(), loopDate.month(), loopDate.day());
1287 QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
1288 loopDate = loopDate.addDays(1);
1292 QTEST_APPLESS_MAIN(tst_QDate)
1293 #include "tst_qdate.moc"