summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/time
diff options
context:
space:
mode:
authorAlexandru Croitor <alexandru.croitor@qt.io>2019-06-14 12:59:07 +0200
committerAlexandru Croitor <alexandru.croitor@qt.io>2019-06-14 16:31:09 +0200
commitfe3bd212fc85b6c3a64bb8bd5beca4ff4d3493a1 (patch)
treeb96dd84abfb6ac25b26539999bf2d7d427f347dd /tests/auto/corelib/time
parent72066a3a585b4f4fed499c64464ca5ad2ba9f71d (diff)
parentae97d11589dd03edeea0475163e6110869143b35 (diff)
Merge remote-tracking branch 'origin/wip/qt6' into wip/cmake
This changes many different CMake places to mention Qt6 instead of Qt5. Note that some old qt5 cmake config files in corelib are probably not needed anymore, but I still renamed and kept them for now. Change-Id: Ie69e81540386a5af153f76c0242e18d48211bec4
Diffstat (limited to 'tests/auto/corelib/time')
-rw-r--r--tests/auto/corelib/time/CMakeLists.txt4
-rw-r--r--tests/auto/corelib/time/qdate/.gitignore1
-rw-r--r--tests/auto/corelib/time/qdate/CMakeLists.txt15
-rw-r--r--tests/auto/corelib/time/qdate/qdate.pro4
-rw-r--r--tests/auto/corelib/time/qdate/tst_qdate.cpp1679
-rw-r--r--tests/auto/corelib/time/qdatetime/.gitignore1
-rw-r--r--tests/auto/corelib/time/qdatetime/BLACKLIST2
-rw-r--r--tests/auto/corelib/time/qdatetime/qdatetime.pro17
-rw-r--r--tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp3486
-rw-r--r--tests/auto/corelib/time/qdatetime/tst_qdatetime_mac.mm70
-rw-r--r--tests/auto/corelib/time/qtime/.gitignore1
-rw-r--r--tests/auto/corelib/time/qtime/CMakeLists.txt1
-rw-r--r--tests/auto/corelib/time/qtime/qtime.pro4
-rw-r--r--tests/auto/corelib/time/qtime/tst_qtime.cpp803
-rw-r--r--tests/auto/corelib/time/qtimezone/BLACKLIST171
-rw-r--r--tests/auto/corelib/time/qtimezone/qtimezone.pro12
-rw-r--r--tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp1340
-rw-r--r--tests/auto/corelib/time/qtimezone/tst_qtimezone_darwin.mm68
-rw-r--r--tests/auto/corelib/time/time.pro6
19 files changed, 7685 insertions, 0 deletions
diff --git a/tests/auto/corelib/time/CMakeLists.txt b/tests/auto/corelib/time/CMakeLists.txt
new file mode 100644
index 0000000000..733f75b8af
--- /dev/null
+++ b/tests/auto/corelib/time/CMakeLists.txt
@@ -0,0 +1,4 @@
+# Generated from time.pro.
+
+add_subdirectory(qdate)
+add_subdirectory(qtime)
diff --git a/tests/auto/corelib/time/qdate/.gitignore b/tests/auto/corelib/time/qdate/.gitignore
new file mode 100644
index 0000000000..70945d4a86
--- /dev/null
+++ b/tests/auto/corelib/time/qdate/.gitignore
@@ -0,0 +1 @@
+tst_qdate
diff --git a/tests/auto/corelib/time/qdate/CMakeLists.txt b/tests/auto/corelib/time/qdate/CMakeLists.txt
new file mode 100644
index 0000000000..8514c915ad
--- /dev/null
+++ b/tests/auto/corelib/time/qdate/CMakeLists.txt
@@ -0,0 +1,15 @@
+# Generated from qdate.pro.
+
+#####################################################################
+## tst_qdate Test:
+#####################################################################
+
+add_qt_test(tst_qdate
+ SOURCES
+ tst_qdate.cpp
+ LIBRARIES
+ Qt::CorePrivate
+)
+
+#### Keys ignored in scope 1:.:.:qdate.pro:<TRUE>:
+# CONFIG = "testcase"
diff --git a/tests/auto/corelib/time/qdate/qdate.pro b/tests/auto/corelib/time/qdate/qdate.pro
new file mode 100644
index 0000000000..925c3b4c78
--- /dev/null
+++ b/tests/auto/corelib/time/qdate/qdate.pro
@@ -0,0 +1,4 @@
+CONFIG += testcase
+TARGET = tst_qdate
+QT = core-private testlib
+SOURCES = tst_qdate.cpp
diff --git a/tests/auto/corelib/time/qdate/tst_qdate.cpp b/tests/auto/corelib/time/qdate/tst_qdate.cpp
new file mode 100644
index 0000000000..0ef494b229
--- /dev/null
+++ b/tests/auto/corelib/time/qdate/tst_qdate.cpp
@@ -0,0 +1,1679 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 The Qt Company Ltd.
+** Copyright (C) 2016 Intel Corporation.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <private/qglobal_p.h> // for the icu feature test
+#include <QtTest/QtTest>
+#include <qdatetime.h>
+#include <qlocale.h>
+
+class tst_QDate : public QObject
+{
+ Q_OBJECT
+private slots:
+ void isNull_data();
+ void isNull();
+ void isValid_data();
+ void isValid();
+ void julianDay_data();
+ void julianDay();
+ void dayOfWeek_data();
+ void dayOfWeek();
+ void dayOfYear_data();
+ void dayOfYear();
+ void daysInMonth_data();
+ void daysInMonth();
+ void daysInYear_data();
+ void daysInYear();
+ void getDate();
+ void weekNumber_invalid_data();
+ void weekNumber_invalid();
+ void weekNumber_data();
+ void weekNumber();
+#if QT_CONFIG(timezone)
+ void startOfDay_endOfDay_data();
+ void startOfDay_endOfDay();
+#endif
+ void startOfDay_endOfDay_fixed_data();
+ void startOfDay_endOfDay_fixed();
+ void startOfDay_endOfDay_bounds();
+ void julianDaysLimits();
+ void addDays_data();
+ void addDays();
+ void addMonths_data();
+ void addMonths();
+ void addYears_data();
+ void addYears();
+ void daysTo();
+ void operator_eq_eq_data();
+ void operator_eq_eq();
+ void operator_lt();
+ void operator_gt();
+ void operator_lt_eq();
+ void operator_gt_eq();
+ void operator_insert_extract_data();
+ void operator_insert_extract();
+ void fromStringDateFormat_data();
+ void fromStringDateFormat();
+ void fromStringFormat_data();
+ void fromStringFormat();
+ void toStringFormat_data();
+ void toStringFormat();
+ void toStringDateFormat_data();
+ void toStringDateFormat();
+ void isLeapYear();
+ void yearsZeroToNinetyNine();
+ void negativeYear() const;
+ void printNegativeYear() const;
+ void roundtripGermanLocale() const;
+#if QT_CONFIG(textdate)
+ void shortDayName() const;
+ void standaloneShortDayName() const;
+ void longDayName() const;
+ void standaloneLongDayName() const;
+ void shortMonthName() const;
+ void standaloneShortMonthName() const;
+ void longMonthName() const;
+ void standaloneLongMonthName() const;
+#endif // textdate
+ void roundtrip() const;
+ void qdebug() const;
+private:
+ QDate defDate() const { return QDate(1900, 1, 1); }
+ QDate invalidDate() const { return QDate(); }
+};
+
+Q_DECLARE_METATYPE(Qt::DateFormat)
+
+void tst_QDate::isNull_data()
+{
+ QTest::addColumn<qint64>("jd");
+ QTest::addColumn<bool>("null");
+
+ qint64 minJd = Q_INT64_C(-784350574879);
+ qint64 maxJd = Q_INT64_C( 784354017364);
+
+ QTest::newRow("qint64 min") << std::numeric_limits<qint64>::min() << true;
+ QTest::newRow("minJd - 1") << minJd - 1 << true;
+ QTest::newRow("minJd") << minJd << false;
+ QTest::newRow("minJd + 1") << minJd + 1 << false;
+ QTest::newRow("maxJd - 1") << maxJd - 1 << false;
+ QTest::newRow("maxJd") << maxJd << false;
+ QTest::newRow("maxJd + 1") << maxJd + 1 << true;
+ QTest::newRow("qint64 max") << std::numeric_limits<qint64>::max() << true;
+}
+
+void tst_QDate::isNull()
+{
+ QFETCH(qint64, jd);
+
+ QDate d = QDate::fromJulianDay(jd);
+ QTEST(d.isNull(), "null");
+}
+
+void tst_QDate::isValid_data()
+{
+ qint64 nullJd = std::numeric_limits<qint64>::min();
+
+ QTest::addColumn<int>("year");
+ QTest::addColumn<int>("month");
+ QTest::addColumn<int>("day");
+ QTest::addColumn<qint64>("jd");
+ QTest::addColumn<bool>("valid");
+
+ QTest::newRow("0-0-0") << 0 << 0 << 0 << nullJd << false;
+ QTest::newRow("month 0") << 2000 << 0 << 1 << nullJd << false;
+ QTest::newRow("day 0") << 2000 << 1 << 0 << nullJd << false;
+
+ QTest::newRow("month 13") << 2000 << 13 << 1 << nullJd << false;
+
+ // test leap years
+ QTest::newRow("non-leap") << 2006 << 2 << 29 << nullJd << false;
+ QTest::newRow("normal leap") << 2004 << 2 << 29 << qint64(2453065) << true;
+ QTest::newRow("century leap 1900") << 1900 << 2 << 29 << nullJd << false;
+ QTest::newRow("century leap 2100") << 2100 << 2 << 29 << nullJd << false;
+ QTest::newRow("400-years leap 2000") << 2000 << 2 << 29 << qint64(2451604) << true;
+ QTest::newRow("400-years leap 2400") << 2400 << 2 << 29 << qint64(2597701) << true;
+ QTest::newRow("400-years leap 1600") << 1600 << 2 << 29 << qint64(2305507) << true;
+ QTest::newRow("year 0") << 0 << 2 << 27 << nullJd << false;
+
+ // Test end of four-digit years:
+ QTest::newRow("late") << 9999 << 12 << 31 << qint64(5373484) << true;
+
+ // test the number of days in months:
+ QTest::newRow("jan") << 2000 << 1 << 31 << qint64(2451575) << true;
+ QTest::newRow("feb") << 2000 << 2 << 29 << qint64(2451604) << true; // same data as 400-years leap
+ QTest::newRow("mar") << 2000 << 3 << 31 << qint64(2451635) << true;
+ QTest::newRow("apr") << 2000 << 4 << 30 << qint64(2451665) << true;
+ QTest::newRow("may") << 2000 << 5 << 31 << qint64(2451696) << true;
+ QTest::newRow("jun") << 2000 << 6 << 30 << qint64(2451726) << true;
+ QTest::newRow("jul") << 2000 << 7 << 31 << qint64(2451757) << true;
+ QTest::newRow("aug") << 2000 << 8 << 31 << qint64(2451788) << true;
+ QTest::newRow("sep") << 2000 << 9 << 30 << qint64(2451818) << true;
+ QTest::newRow("oct") << 2000 << 10 << 31 << qint64(2451849) << true;
+ QTest::newRow("nov") << 2000 << 11 << 30 << qint64(2451879) << true;
+ QTest::newRow("dec") << 2000 << 12 << 31 << qint64(2451910) << true;
+
+ // and invalid dates:
+ QTest::newRow("ijan") << 2000 << 1 << 32 << nullJd << false;
+ QTest::newRow("ifeb") << 2000 << 2 << 30 << nullJd << false;
+ QTest::newRow("imar") << 2000 << 3 << 32 << nullJd << false;
+ QTest::newRow("iapr") << 2000 << 4 << 31 << nullJd << false;
+ QTest::newRow("imay") << 2000 << 5 << 32 << nullJd << false;
+ QTest::newRow("ijun") << 2000 << 6 << 31 << nullJd << false;
+ QTest::newRow("ijul") << 2000 << 7 << 32 << nullJd << false;
+ QTest::newRow("iaug") << 2000 << 8 << 32 << nullJd << false;
+ QTest::newRow("isep") << 2000 << 9 << 31 << nullJd << false;
+ QTest::newRow("ioct") << 2000 << 10 << 32 << nullJd << false;
+ QTest::newRow("inov") << 2000 << 11 << 31 << nullJd << false;
+ QTest::newRow("idec") << 2000 << 12 << 32 << nullJd << false;
+
+ // the beginning of the Julian Day calendar:
+ QTest::newRow("jd earliest formula") << -4800 << 1 << 1 << qint64( -31738) << true;
+ QTest::newRow("jd -1") << -4714 << 11 << 23 << qint64( -1) << true;
+ QTest::newRow("jd 0") << -4714 << 11 << 24 << qint64( 0) << true;
+ QTest::newRow("jd 1") << -4714 << 11 << 25 << qint64( 1) << true;
+ QTest::newRow("jd latest formula") << 1400000 << 12 << 31 << qint64(513060925) << true;
+}
+
+void tst_QDate::isValid()
+{
+ QFETCH(int, year);
+ QFETCH(int, month);
+ QFETCH(int, day);
+ QFETCH(qint64, jd);
+ QFETCH(bool, valid);
+
+ QCOMPARE(QDate::isValid(year, month, day), valid);
+
+ QDate d;
+ d.setDate(year, month, day);
+ QCOMPARE(d.isValid(), valid);
+ QCOMPARE(d.toJulianDay(), jd);
+
+ if (valid) {
+ QCOMPARE(d.year(), year);
+ QCOMPARE(d.month(), month);
+ QCOMPARE(d.day(), day);
+ } else {
+ QCOMPARE(d.year(), 0);
+ QCOMPARE(d.month(), 0);
+ QCOMPARE(d.day(), 0);
+ }
+}
+
+void tst_QDate::julianDay_data()
+{
+ isValid_data();
+}
+
+void tst_QDate::julianDay()
+{
+ QFETCH(int, year);
+ QFETCH(int, month);
+ QFETCH(int, day);
+ QFETCH(qint64, jd);
+
+ {
+ QDate d;
+ d.setDate(year, month, day);
+ QCOMPARE(d.toJulianDay(), jd);
+ }
+
+ if (jd != std::numeric_limits<qint64>::min()) {
+ QDate d = QDate::fromJulianDay(jd);
+ QCOMPARE(d.year(), year);
+ QCOMPARE(d.month(), month);
+ QCOMPARE(d.day(), day);
+ }
+}
+
+void tst_QDate::dayOfWeek_data()
+{
+ QTest::addColumn<int>("year");
+ QTest::addColumn<int>("month");
+ QTest::addColumn<int>("day");
+ QTest::addColumn<int>("dayOfWeek");
+
+ QTest::newRow("data0") << 0 << 0 << 0 << 0;
+ QTest::newRow("data1") << 2000 << 1 << 3 << 1;
+ QTest::newRow("data2") << 2000 << 1 << 4 << 2;
+ QTest::newRow("data3") << 2000 << 1 << 5 << 3;
+ QTest::newRow("data4") << 2000 << 1 << 6 << 4;
+ QTest::newRow("data5") << 2000 << 1 << 7 << 5;
+ QTest::newRow("data6") << 2000 << 1 << 8 << 6;
+ QTest::newRow("data7") << 2000 << 1 << 9 << 7;
+ QTest::newRow("data8") << -4800 << 1 << 1 << 1;
+ QTest::newRow("data9") << -4800 << 1 << 2 << 2;
+ QTest::newRow("data10") << -4800 << 1 << 3 << 3;
+ QTest::newRow("data11") << -4800 << 1 << 4 << 4;
+ QTest::newRow("data12") << -4800 << 1 << 5 << 5;
+ QTest::newRow("data13") << -4800 << 1 << 6 << 6;
+ QTest::newRow("data14") << -4800 << 1 << 7 << 7;
+ QTest::newRow("data15") << -4800 << 1 << 8 << 1;
+}
+
+void tst_QDate::dayOfWeek()
+{
+ QFETCH(int, year);
+ QFETCH(int, month);
+ QFETCH(int, day);
+ QFETCH(int, dayOfWeek);
+
+ QDate dt(year, month, day);
+ QCOMPARE(dt.dayOfWeek(), dayOfWeek);
+}
+
+void tst_QDate::dayOfYear_data()
+{
+ QTest::addColumn<int>("year");
+ QTest::addColumn<int>("month");
+ QTest::addColumn<int>("day");
+ QTest::addColumn<int>("dayOfYear");
+
+ QTest::newRow("data0") << 0 << 0 << 0 << 0;
+ QTest::newRow("data1") << 2000 << 1 << 1 << 1;
+ QTest::newRow("data2") << 2000 << 1 << 2 << 2;
+ QTest::newRow("data3") << 2000 << 1 << 3 << 3;
+ QTest::newRow("data4") << 2000 << 12 << 31 << 366;
+ QTest::newRow("data5") << 2001 << 12 << 31 << 365;
+ QTest::newRow("data6") << 1815 << 1 << 1 << 1;
+ QTest::newRow("data7") << 1815 << 12 << 31 << 365;
+ QTest::newRow("data8") << 1500 << 1 << 1 << 1;
+ QTest::newRow("data9") << 1500 << 12 << 31 << 365;
+ QTest::newRow("data10") << -1500 << 1 << 1 << 1;
+ QTest::newRow("data11") << -1500 << 12 << 31 << 365;
+ QTest::newRow("data12") << -4800 << 1 << 1 << 1;
+ QTest::newRow("data13") << -4800 << 12 << 31 << 365;
+}
+
+void tst_QDate::dayOfYear()
+{
+ QFETCH(int, year);
+ QFETCH(int, month);
+ QFETCH(int, day);
+ QFETCH(int, dayOfYear);
+
+ QDate dt(year, month, day);
+ QCOMPARE(dt.dayOfYear(), dayOfYear);
+}
+
+void tst_QDate::daysInMonth_data()
+{
+ QTest::addColumn<int>("year");
+ QTest::addColumn<int>("month");
+ QTest::addColumn<int>("day");
+ QTest::addColumn<int>("daysInMonth");
+
+ QTest::newRow("data0") << 0 << 0 << 0 << 0;
+ QTest::newRow("data1") << 2000 << 1 << 1 << 31;
+ QTest::newRow("data2") << 2000 << 2 << 1 << 29;
+ QTest::newRow("data3") << 2000 << 3 << 1 << 31;
+ QTest::newRow("data4") << 2000 << 4 << 1 << 30;
+ QTest::newRow("data5") << 2000 << 5 << 1 << 31;
+ QTest::newRow("data6") << 2000 << 6 << 1 << 30;
+ QTest::newRow("data7") << 2000 << 7 << 1 << 31;
+ QTest::newRow("data8") << 2000 << 8 << 1 << 31;
+ QTest::newRow("data9") << 2000 << 9 << 1 << 30;
+ QTest::newRow("data10") << 2000 << 10 << 1 << 31;
+ QTest::newRow("data11") << 2000 << 11 << 1 << 30;
+ QTest::newRow("data12") << 2000 << 12 << 1 << 31;
+ QTest::newRow("data13") << 2001 << 2 << 1 << 28;
+ QTest::newRow("data14") << 2000 << 0 << 1 << 0;
+}
+
+void tst_QDate::daysInMonth()
+{
+ QFETCH(int, year);
+ QFETCH(int, month);
+ QFETCH(int, day);
+ QFETCH(int, daysInMonth);
+
+ QDate dt(year, month, day);
+ QCOMPARE(dt.daysInMonth(), daysInMonth);
+}
+
+void tst_QDate::daysInYear_data()
+{
+ QTest::addColumn<QDate>("date");
+ QTest::addColumn<int>("expectedDaysInYear");
+
+ QTest::newRow("2000, 1, 1") << QDate(2000, 1, 1) << 366;
+ QTest::newRow("2001, 1, 1") << QDate(2001, 1, 1) << 365;
+ QTest::newRow("4, 1, 1") << QDate(4, 1, 1) << 366;
+ QTest::newRow("5, 1, 1") << QDate(5, 1, 1) << 365;
+ QTest::newRow("0, 0, 0") << QDate(0, 0, 0) << 0;
+}
+
+void tst_QDate::daysInYear()
+{
+ QFETCH(QDate, date);
+ QFETCH(int, expectedDaysInYear);
+
+ QCOMPARE(date.daysInYear(), expectedDaysInYear);
+}
+
+void tst_QDate::getDate()
+{
+ int y, m, d;
+ QDate dt(2000, 1, 1);
+ dt.getDate(&y, &m, &d);
+ QCOMPARE(y, 2000);
+ QCOMPARE(m, 1);
+ QCOMPARE(d, 1);
+ dt.setDate(0, 0, 0);
+ dt.getDate(&y, &m, &d);
+ QCOMPARE(y, 0);
+ QCOMPARE(m, 0);
+ QCOMPARE(d, 0);
+}
+
+void tst_QDate::weekNumber_data()
+{
+ QTest::addColumn<int>("expectedWeekNum");
+ QTest::addColumn<int>("expectedYearNum");
+ QTest::addColumn<int>("year");
+ QTest::addColumn<int>("month");
+ QTest::addColumn<int>("day");
+
+ enum { Thursday = 4 };
+ bool wasLastYearLong = false; // 1999 was not a long (53-week) year
+ bool isLongYear;
+
+ // full 400-year cycle for Jan 1, 4 and Dec 28, 31
+ for (int yr = 2000; yr < 2400; ++yr, wasLastYearLong = isLongYear) {
+ QByteArray yrstr = QByteArray::number(yr);
+ int wday = QDate(yr, 1, 1).dayOfWeek();
+
+ // the year is 53-week long if Jan 1 is Thursday or, if it's a leap year, a Wednesday
+ isLongYear = (wday == Thursday) || (QDate::isLeapYear(yr) && wday == Thursday - 1);
+
+ // Jan 4 is always on week 1
+ QTest::newRow(yrstr + "-01-04") << 1 << yr << yr << 1 << 4;
+
+ // Dec 28 is always on the last week
+ QTest::newRow(yrstr + "-12-28") << (52 + isLongYear) << yr << yr << 12 << 28;
+
+ // Jan 1 is on either on week 1 or on the last week of the previous year
+ QTest::newRow(yrstr + "-01-01")
+ << (wday <= Thursday ? 1 : 52 + wasLastYearLong)
+ << (wday <= Thursday ? yr : yr - 1)
+ << yr << 1 << 1;
+
+ // Dec 31 is either on the last week or week 1 of the next year
+ wday = QDate(yr, 12, 31).dayOfWeek();
+ QTest::newRow(yrstr + "-12-31")
+ << (wday >= Thursday ? 52 + isLongYear : 1)
+ << (wday >= Thursday ? yr : yr + 1)
+ << yr << 12 << 31;
+ }
+}
+
+void tst_QDate::weekNumber()
+{
+ int yearNumber;
+ QFETCH( int, year );
+ QFETCH( int, month );
+ QFETCH( int, day );
+ QFETCH( int, expectedWeekNum );
+ QFETCH( int, expectedYearNum );
+ QDate dt1( year, month, day );
+ QCOMPARE( dt1.weekNumber( &yearNumber ), expectedWeekNum );
+ QCOMPARE( yearNumber, expectedYearNum );
+}
+
+void tst_QDate::weekNumber_invalid_data()
+{
+ QTest::addColumn<int>("year");
+ QTest::addColumn<int>("month");
+ QTest::addColumn<int>("day");
+
+ //next we fill it with data
+ QTest::newRow( "data0" ) << 0 << 0 << 0;
+ QTest::newRow( "data1" ) << 2001 << 1 << 32;
+ QTest::newRow( "data2" ) << 1999 << 2 << 29;
+}
+
+void tst_QDate::weekNumber_invalid()
+{
+ QDate dt;
+ int yearNumber;
+ QCOMPARE( dt.weekNumber( &yearNumber ), 0 );
+}
+
+#if QT_CONFIG(timezone)
+void tst_QDate::startOfDay_endOfDay_data()
+{
+ QTest::addColumn<QDate>("date"); // Typically a spring-forward.
+ // A zone in which that date's start and end are worth checking:
+ QTest::addColumn<QByteArray>("zoneName");
+ // The start and end times in that zone:
+ QTest::addColumn<QTime>("start");
+ QTest::addColumn<QTime>("end");
+
+ const QTime initial(0, 0), final(23, 59, 59, 999), invalid(QDateTime().time());
+
+ QTest::newRow("epoch")
+ << QDate(1970, 1, 1) << QByteArray("UTC")
+ << initial << final;
+ QTest::newRow("Brazil")
+ << QDate(2008, 10, 19) << QByteArray("America/Sao_Paulo")
+ << QTime(1, 0) << final;
+#if QT_CONFIG(icu) || !defined(Q_OS_WIN) // MS's TZ APIs lack data
+ QTest::newRow("Sofia")
+ << QDate(1994, 3, 27) << QByteArray("Europe/Sofia")
+ << QTime(1, 0) << final;
+#endif
+ QTest::newRow("Kiritimati")
+ << QDate(1994, 12, 31) << QByteArray("Pacific/Kiritimati")
+ << invalid << invalid;
+ QTest::newRow("Samoa")
+ << QDate(2011, 12, 30) << QByteArray("Pacific/Apia")
+ << invalid << invalid;
+ // TODO: find other zones with transitions at/crossing midnight.
+}
+
+void tst_QDate::startOfDay_endOfDay()
+{
+ QFETCH(QDate, date);
+ QFETCH(QByteArray, zoneName);
+ QFETCH(QTime, start);
+ QFETCH(QTime, end);
+ const QTimeZone zone(zoneName);
+ const bool isSystem = QTimeZone::systemTimeZone() == zone;
+ QDateTime front(date.startOfDay(zone)), back(date.endOfDay(zone));
+ if (end.isValid())
+ QCOMPARE(date.addDays(1).startOfDay(zone).addMSecs(-1), back);
+ if (start.isValid())
+ QCOMPARE(date.addDays(-1).endOfDay(zone).addMSecs(1), front);
+ do { // Avoids duplicating these tests for local-time when it *is* zone:
+ if (start.isValid()) {
+ QCOMPARE(front.date(), date);
+ QCOMPARE(front.time(), start);
+ }
+ if (end.isValid()) {
+ QCOMPARE(back.date(), date);
+ QCOMPARE(back.time(), end);
+ }
+ if (front.timeSpec() == Qt::LocalTime)
+ break;
+ front = date.startOfDay(Qt::LocalTime);
+ back = date.endOfDay(Qt::LocalTime);
+ } while (isSystem);
+ if (end.isValid())
+ QCOMPARE(date.addDays(1).startOfDay(Qt::LocalTime).addMSecs(-1), back);
+ if (start.isValid())
+ QCOMPARE(date.addDays(-1).endOfDay(Qt::LocalTime).addMSecs(1), front);
+ if (!isSystem) {
+ // These might fail if system zone coincides with zone; but only if it
+ // did something similarly unusual on the date picked for this test.
+ if (start.isValid()) {
+ QCOMPARE(front.date(), date);
+ QCOMPARE(front.time(), QTime(0, 0));
+ }
+ if (end.isValid()) {
+ QCOMPARE(back.date(), date);
+ QCOMPARE(back.time(), QTime(23, 59, 59, 999));
+ }
+ }
+}
+#endif // timezone
+
+void tst_QDate::startOfDay_endOfDay_fixed_data()
+{
+ const qint64 kilo(1000);
+ using Bounds = std::numeric_limits<qint64>;
+ const QDateTime
+ first(QDateTime::fromMSecsSinceEpoch(Bounds::min() + 1, Qt::UTC)),
+ start32sign(QDateTime::fromMSecsSinceEpoch(-0x80000000L * kilo, Qt::UTC)),
+ end32sign(QDateTime::fromMSecsSinceEpoch(0x80000000L * kilo, Qt::UTC)),
+ end32unsign(QDateTime::fromMSecsSinceEpoch(0x100000000L * kilo, Qt::UTC)),
+ last(QDateTime::fromMSecsSinceEpoch(Bounds::max(), Qt::UTC));
+
+ const struct {
+ const char *name;
+ QDate date;
+ } data[] = {
+ { "epoch", QDate(1970, 1, 1) },
+ { "y2k-leap-day", QDate(2000, 2, 29) },
+ // Just outside the start and end of 32-bit time_t:
+ { "pre-sign32", QDate(start32sign.date().year(), 1, 1) },
+ { "post-sign32", QDate(end32sign.date().year(), 12, 31) },
+ { "post-uint32", QDate(end32unsign.date().year(), 12, 31) },
+ // Just inside the start and end of QDateTime's range:
+ { "first-full", first.date().addDays(1) },
+ { "last-full", last.date().addDays(-1) }
+ };
+
+ QTest::addColumn<QDate>("date");
+ for (const auto &r : data)
+ QTest::newRow(r.name) << r.date;
+}
+
+void tst_QDate::startOfDay_endOfDay_fixed()
+{
+ const QTime early(0, 0), late(23, 59, 59, 999);
+ QFETCH(QDate, date);
+
+ QDateTime start(date.startOfDay(Qt::UTC));
+ QDateTime end(date.endOfDay(Qt::UTC));
+ QCOMPARE(start.date(), date);
+ QCOMPARE(end.date(), date);
+ QCOMPARE(start.time(), early);
+ QCOMPARE(end.time(), late);
+ QCOMPARE(date.addDays(1).startOfDay(Qt::UTC).addMSecs(-1), end);
+ QCOMPARE(date.addDays(-1).endOfDay(Qt::UTC).addMSecs(1), start);
+ for (int offset = -60 * 16; offset <= 60 * 16; offset += 65) {
+ start = date.startOfDay(Qt::OffsetFromUTC, offset);
+ end = date.endOfDay(Qt::OffsetFromUTC, offset);
+ QCOMPARE(start.date(), date);
+ QCOMPARE(end.date(), date);
+ QCOMPARE(start.time(), early);
+ QCOMPARE(end.time(), late);
+ QCOMPARE(date.addDays(1).startOfDay(Qt::OffsetFromUTC, offset).addMSecs(-1), end);
+ QCOMPARE(date.addDays(-1).endOfDay(Qt::OffsetFromUTC, offset).addMSecs(1), start);
+ }
+}
+
+void tst_QDate::startOfDay_endOfDay_bounds()
+{
+ // Check the days in which QDateTime's range starts and ends:
+ using Bounds = std::numeric_limits<qint64>;
+ const QDateTime
+ first(QDateTime::fromMSecsSinceEpoch(Bounds::min(), Qt::UTC)),
+ last(QDateTime::fromMSecsSinceEpoch(Bounds::max(), Qt::UTC)),
+ epoch(QDateTime::fromMSecsSinceEpoch(0, Qt::UTC));
+ // First, check these *are* the start and end of QDateTime's range:
+ QVERIFY(first.isValid());
+ QVERIFY(last.isValid());
+ QVERIFY(first < epoch);
+ QVERIFY(last > epoch);
+ // QDateTime's addMSecs doesn't check against {und,ov}erflow ...
+ QVERIFY(!first.addMSecs(-1).isValid() || first.addMSecs(-1) > first);
+ QVERIFY(!last.addMSecs(1).isValid() || last.addMSecs(1) < last);
+
+ // Now test start/end methods with them:
+ QCOMPARE(first.date().endOfDay(Qt::UTC).time(), QTime(23, 59, 59, 999));
+ QCOMPARE(last.date().startOfDay(Qt::UTC).time(), QTime(0, 0));
+ QVERIFY(!first.date().startOfDay(Qt::UTC).isValid());
+ QVERIFY(!last.date().endOfDay(Qt::UTC).isValid());
+}
+
+void tst_QDate::julianDaysLimits()
+{
+ qint64 min = std::numeric_limits<qint64>::min();
+ qint64 max = std::numeric_limits<qint64>::max();
+ qint64 minJd = Q_INT64_C(-784350574879);
+ qint64 maxJd = Q_INT64_C( 784354017364);
+
+ QDate maxDate = QDate::fromJulianDay(maxJd);
+ QDate minDate = QDate::fromJulianDay(minJd);
+ QDate zeroDate = QDate::fromJulianDay(0);
+
+ QDate dt = QDate::fromJulianDay(min);
+ QCOMPARE(dt.isValid(), false);
+ dt = QDate::fromJulianDay(minJd - 1);
+ QCOMPARE(dt.isValid(), false);
+ dt = QDate::fromJulianDay(minJd);
+ QCOMPARE(dt.isValid(), true);
+ dt = QDate::fromJulianDay(minJd + 1);
+ QCOMPARE(dt.isValid(), true);
+ dt = QDate::fromJulianDay(maxJd - 1);
+ QCOMPARE(dt.isValid(), true);
+ dt = QDate::fromJulianDay(maxJd);
+ QCOMPARE(dt.isValid(), true);
+ dt = QDate::fromJulianDay(maxJd + 1);
+ QCOMPARE(dt.isValid(), false);
+ dt = QDate::fromJulianDay(max);
+ QCOMPARE(dt.isValid(), false);
+
+ dt = maxDate.addDays(1);
+ QCOMPARE(dt.isValid(), false);
+ dt = maxDate.addDays(0);
+ QCOMPARE(dt.isValid(), true);
+ dt = maxDate.addDays(-1);
+ QCOMPARE(dt.isValid(), true);
+ dt = maxDate.addDays(max);
+ QCOMPARE(dt.isValid(), false);
+ dt = maxDate.addDays(min);
+ QCOMPARE(dt.isValid(), false);
+
+ dt = minDate.addDays(-1);
+ QCOMPARE(dt.isValid(), false);
+ dt = minDate.addDays(0);
+ QCOMPARE(dt.isValid(), true);
+ dt = minDate.addDays(1);
+ QCOMPARE(dt.isValid(), true);
+ dt = minDate.addDays(min);
+ QCOMPARE(dt.isValid(), false);
+ dt = minDate.addDays(max);
+ QCOMPARE(dt.isValid(), false);
+
+ dt = zeroDate.addDays(-1);
+ QCOMPARE(dt.isValid(), true);
+ dt = zeroDate.addDays(0);
+ QCOMPARE(dt.isValid(), true);
+ dt = zeroDate.addDays(1);
+ QCOMPARE(dt.isValid(), true);
+ dt = zeroDate.addDays(min);
+ QCOMPARE(dt.isValid(), false);
+ dt = zeroDate.addDays(max);
+ QCOMPARE(dt.isValid(), false);
+}
+
+void tst_QDate::addDays()
+{
+ QFETCH( int, year );
+ QFETCH( int, month );
+ QFETCH( int, day );
+ QFETCH( int, amountToAdd );
+ QFETCH( int, expectedYear );
+ QFETCH( int, expectedMonth );
+ QFETCH( int, expectedDay );
+
+ QDate dt( year, month, day );
+ dt = dt.addDays( amountToAdd );
+
+ QCOMPARE( dt.year(), expectedYear );
+ QCOMPARE( dt.month(), expectedMonth );
+ QCOMPARE( dt.day(), expectedDay );
+}
+
+void tst_QDate::addDays_data()
+{
+ QTest::addColumn<int>("year");
+ QTest::addColumn<int>("month");
+ QTest::addColumn<int>("day");
+ QTest::addColumn<int>("amountToAdd");
+ QTest::addColumn<int>("expectedYear");
+ QTest::addColumn<int>("expectedMonth");
+ QTest::addColumn<int>("expectedDay");
+
+ QTest::newRow( "data0" ) << 2000 << 1 << 1 << 1 << 2000 << 1 << 2;
+ QTest::newRow( "data1" ) << 2000 << 1 << 31 << 1 << 2000 << 2 << 1;
+ QTest::newRow( "data2" ) << 2000 << 2 << 28 << 1 << 2000 << 2 << 29;
+ QTest::newRow( "data3" ) << 2000 << 2 << 29 << 1 << 2000 << 3 << 1;
+ QTest::newRow( "data4" ) << 2000 << 12 << 31 << 1 << 2001 << 1 << 1;
+ QTest::newRow( "data5" ) << 2001 << 2 << 28 << 1 << 2001 << 3 << 1;
+ QTest::newRow( "data6" ) << 2001 << 2 << 28 << 30 << 2001 << 3 << 30;
+ QTest::newRow( "data7" ) << 2001 << 3 << 30 << 5 << 2001 << 4 << 4;
+
+ QTest::newRow( "data8" ) << 2000 << 1 << 1 << -1 << 1999 << 12 << 31;
+ QTest::newRow( "data9" ) << 2000 << 1 << 31 << -1 << 2000 << 1 << 30;
+ QTest::newRow( "data10" ) << 2000 << 2 << 28 << -1 << 2000 << 2 << 27;
+ QTest::newRow( "data11" ) << 2001 << 2 << 28 << -30 << 2001 << 1 << 29;
+
+ QTest::newRow( "data12" ) << -4713 << 1 << 2 << -2 << -4714 << 12 << 31;
+ QTest::newRow( "data13" ) << -4713 << 1 << 2 << 2 << -4713 << 1 << 4;
+
+ QTest::newRow( "invalid" ) << 0 << 0 << 0 << 1 << 0 << 0 << 0;
+}
+
+void tst_QDate::addMonths()
+{
+ QFETCH( int, year );
+ QFETCH( int, month );
+ QFETCH( int, day );
+ QFETCH( int, amountToAdd );
+ QFETCH( int, expectedYear );
+ QFETCH( int, expectedMonth );
+ QFETCH( int, expectedDay );
+
+ QDate dt( year, month, day );
+ dt = dt.addMonths( amountToAdd );
+
+ QCOMPARE( dt.year(), expectedYear );
+ QCOMPARE( dt.month(), expectedMonth );
+ QCOMPARE( dt.day(), expectedDay );
+}
+
+void tst_QDate::addMonths_data()
+{
+ QTest::addColumn<int>("year");
+ QTest::addColumn<int>("month");
+ QTest::addColumn<int>("day");
+ QTest::addColumn<int>("amountToAdd");
+ QTest::addColumn<int>("expectedYear");
+ QTest::addColumn<int>("expectedMonth");
+ QTest::addColumn<int>("expectedDay");
+
+ QTest::newRow( "data0" ) << 2000 << 1 << 1 << 1 << 2000 << 2 << 1;
+ QTest::newRow( "data1" ) << 2000 << 1 << 31 << 1 << 2000 << 2 << 29;
+ QTest::newRow( "data2" ) << 2000 << 2 << 28 << 1 << 2000 << 3 << 28;
+ QTest::newRow( "data3" ) << 2000 << 2 << 29 << 1 << 2000 << 3 << 29;
+ QTest::newRow( "data4" ) << 2000 << 12 << 31 << 1 << 2001 << 1 << 31;
+ QTest::newRow( "data5" ) << 2001 << 2 << 28 << 1 << 2001 << 3 << 28;
+ QTest::newRow( "data6" ) << 2001 << 2 << 28 << 12 << 2002 << 2 << 28;
+ QTest::newRow( "data7" ) << 2000 << 2 << 29 << 12 << 2001 << 2 << 28;
+ QTest::newRow( "data8" ) << 2000 << 10 << 15 << 4 << 2001 << 2 << 15;
+
+ QTest::newRow( "data9" ) << 2000 << 1 << 1 << -1 << 1999 << 12 << 1;
+ QTest::newRow( "data10" ) << 2000 << 1 << 31 << -1 << 1999 << 12 << 31;
+ QTest::newRow( "data11" ) << 2000 << 12 << 31 << -1 << 2000 << 11 << 30;
+ QTest::newRow( "data12" ) << 2001 << 2 << 28 << -12 << 2000 << 2 << 28;
+ QTest::newRow( "data13" ) << 2000 << 1 << 31 << -7 << 1999 << 6 << 30;
+ QTest::newRow( "data14" ) << 2000 << 2 << 29 << -12 << 1999 << 2 << 28;
+
+ // year sign change:
+ QTest::newRow( "data15" ) << 1 << 1 << 1 << -1 << -1 << 12 << 1;
+ QTest::newRow( "data16" ) << 1 << 1 << 1 << -12 << -1 << 1 << 1;
+ QTest::newRow( "data17" ) << -1 << 12 << 1 << 1 << 1 << 1 << 1;
+ QTest::newRow( "data18" ) << -1 << 1 << 1 << 12 << 1 << 1 << 1;
+ QTest::newRow( "data19" ) << -2 << 1 << 1 << 12 << -1 << 1 << 1;
+
+ QTest::newRow( "invalid" ) << 0 << 0 << 0 << 1 << 0 << 0 << 0;
+}
+
+void tst_QDate::addYears()
+{
+ QFETCH( int, year );
+ QFETCH( int, month );
+ QFETCH( int, day );
+ QFETCH( int, amountToAdd );
+ QFETCH( int, expectedYear );
+ QFETCH( int, expectedMonth );
+ QFETCH( int, expectedDay );
+
+ QDate dt( year, month, day );
+ dt = dt.addYears( amountToAdd );
+
+ QCOMPARE( dt.year(), expectedYear );
+ QCOMPARE( dt.month(), expectedMonth );
+ QCOMPARE( dt.day(), expectedDay );
+}
+
+void tst_QDate::addYears_data()
+{
+ QTest::addColumn<int>("year");
+ QTest::addColumn<int>("month");
+ QTest::addColumn<int>("day");
+ QTest::addColumn<int>("amountToAdd");
+ QTest::addColumn<int>("expectedYear");
+ QTest::addColumn<int>("expectedMonth");
+ QTest::addColumn<int>("expectedDay");
+
+ QTest::newRow( "data0" ) << 2000 << 1 << 1 << 1 << 2001 << 1 << 1;
+ QTest::newRow( "data1" ) << 2000 << 1 << 31 << 1 << 2001 << 1 << 31;
+ QTest::newRow( "data2" ) << 2000 << 2 << 28 << 1 << 2001 << 2 << 28;
+ QTest::newRow( "data3" ) << 2000 << 2 << 29 << 1 << 2001 << 2 << 28;
+ QTest::newRow( "data4" ) << 2000 << 12 << 31 << 1 << 2001 << 12 << 31;
+ QTest::newRow( "data5" ) << 2001 << 2 << 28 << 3 << 2004 << 2 << 28;
+ QTest::newRow( "data6" ) << 2000 << 2 << 29 << 4 << 2004 << 2 << 29;
+
+ QTest::newRow( "data7" ) << 2000 << 1 << 31 << -1 << 1999 << 1 << 31;
+ QTest::newRow( "data9" ) << 2000 << 2 << 29 << -1 << 1999 << 2 << 28;
+ QTest::newRow( "data10" ) << 2000 << 12 << 31 << -1 << 1999 << 12 << 31;
+ QTest::newRow( "data11" ) << 2001 << 2 << 28 << -3 << 1998 << 2 << 28;
+ QTest::newRow( "data12" ) << 2000 << 2 << 29 << -4 << 1996 << 2 << 29;
+ QTest::newRow( "data13" ) << 2000 << 2 << 29 << -5 << 1995 << 2 << 28;
+
+ QTest::newRow( "data14" ) << 2000 << 1 << 1 << -1999 << 1 << 1 << 1;
+ QTest::newRow( "data15" ) << 2000 << 1 << 1 << -2000 << -1 << 1 << 1;
+ QTest::newRow( "data16" ) << 2000 << 1 << 1 << -2001 << -2 << 1 << 1;
+ QTest::newRow( "data17" ) << -2000 << 1 << 1 << 1999 << -1 << 1 << 1;
+ QTest::newRow( "data18" ) << -2000 << 1 << 1 << 2000 << 1 << 1 << 1;
+ QTest::newRow( "data19" ) << -2000 << 1 << 1 << 2001 << 2 << 1 << 1;
+
+ QTest::newRow( "invalid" ) << 0 << 0 << 0 << 1 << 0 << 0 << 0;
+}
+
+void tst_QDate::daysTo()
+{
+ qint64 minJd = Q_INT64_C(-784350574879);
+ qint64 maxJd = Q_INT64_C( 784354017364);
+
+ QDate dt1(2000, 1, 1);
+ QDate dt2(2000, 1, 5);
+ QCOMPARE(dt1.daysTo(dt2), (qint64) 4);
+ QCOMPARE(dt2.daysTo(dt1), (qint64) -4);
+
+ dt1.setDate(0, 0, 0);
+ QCOMPARE(dt1.daysTo(dt2), (qint64) 0);
+ dt1.setDate(2000, 1, 1);
+ dt2.setDate(0, 0, 0);
+ QCOMPARE(dt1.daysTo(dt2), (qint64) 0);
+
+
+ QDate maxDate = QDate::fromJulianDay(maxJd);
+ QDate minDate = QDate::fromJulianDay(minJd);
+ QDate zeroDate = QDate::fromJulianDay(0);
+
+ QCOMPARE(maxDate.daysTo(minDate), minJd - maxJd);
+ QCOMPARE(minDate.daysTo(maxDate), maxJd - minJd);
+ QCOMPARE(maxDate.daysTo(zeroDate), -maxJd);
+ QCOMPARE(zeroDate.daysTo(maxDate), maxJd);
+ QCOMPARE(minDate.daysTo(zeroDate), -minJd);
+ QCOMPARE(zeroDate.daysTo(minDate), minJd);
+}
+
+void tst_QDate::operator_eq_eq_data()
+{
+ QTest::addColumn<QDate>("d1");
+ QTest::addColumn<QDate>("d2");
+ QTest::addColumn<bool>("expectEqual");
+
+ QTest::newRow("data0") << QDate(2000,1,2) << QDate(2000,1,2) << true;
+ QTest::newRow("data1") << QDate(2001,12,5) << QDate(2001,12,5) << true;
+ QTest::newRow("data2") << QDate(2001,12,5) << QDate(2001,12,5) << true;
+ QTest::newRow("data3") << QDate(2001,12,5) << QDate(2002,12,5) << false;
+
+ QDate date1(1900, 1, 1);
+ QDate date2 = date1.addDays(1);
+ QDate date3 = date1.addDays(-1);
+ QDate date4 = date1.addMonths(1);
+ QDate date5 = date1.addMonths(-1);
+ QDate date6 = date1.addYears(1);
+ QDate date7 = date1.addYears(-1);
+
+ QTest::newRow("data4") << date2 << date3 << false;
+ QTest::newRow("data5") << date4 << date5 << false;
+ QTest::newRow("data6") << date6 << date7 << false;
+ QTest::newRow("data7") << date1 << date2 << false;
+ QTest::newRow("data8") << date1 << date3 << false;
+ QTest::newRow("data9") << date1 << date4 << false;
+ QTest::newRow("data10") << date1 << date5 << false;
+ QTest::newRow("data11") << date1 << date6 << false;
+ QTest::newRow("data12") << date1 << date7 << false;
+}
+
+void tst_QDate::operator_eq_eq()
+{
+ QFETCH(QDate, d1);
+ QFETCH(QDate, d2);
+ QFETCH(bool, expectEqual);
+
+ bool equal = d1 == d2;
+ QCOMPARE(equal, expectEqual);
+ bool notEqual = d1 != d2;
+ QCOMPARE(notEqual, !expectEqual);
+
+ if (equal)
+ QVERIFY(qHash(d1) == qHash(d2));
+}
+
+void tst_QDate::operator_lt()
+{
+ QDate d1(2000,1,2);
+ QDate d2(2000,1,2);
+ QVERIFY( !(d1 < d2) );
+
+ d1 = QDate(2001,12,4);
+ d2 = QDate(2001,12,5);
+ QVERIFY( d1 < d2 );
+
+ d1 = QDate(2001,11,5);
+ d2 = QDate(2001,12,5);
+ QVERIFY( d1 < d2 );
+
+ d1 = QDate(2000,12,5);
+ d2 = QDate(2001,12,5);
+ QVERIFY( d1 < d2 );
+
+ d1 = QDate(2002,12,5);
+ d2 = QDate(2001,12,5);
+ QVERIFY( !(d1 < d2) );
+
+ d1 = QDate(2001,12,5);
+ d2 = QDate(2001,11,5);
+ QVERIFY( !(d1 < d2) );
+
+ d1 = QDate(2001,12,6);
+ d2 = QDate(2001,12,5);
+ QVERIFY( !(d1 < d2) );
+}
+
+void tst_QDate::operator_gt()
+{
+ QDate d1(2000,1,2);
+ QDate d2(2000,1,2);
+ QVERIFY( !(d1 > d2) );
+
+ d1 = QDate(2001,12,4);
+ d2 = QDate(2001,12,5);
+ QVERIFY( !(d1 > d2) );
+
+ d1 = QDate(2001,11,5);
+ d2 = QDate(2001,12,5);
+ QVERIFY( !(d1 > d2) );
+
+ d1 = QDate(2000,12,5);
+ d2 = QDate(2001,12,5);
+ QVERIFY( !(d1 > d2) );
+
+ d1 = QDate(2002,12,5);
+ d2 = QDate(2001,12,5);
+ QVERIFY( d1 > d2 );
+
+ d1 = QDate(2001,12,5);
+ d2 = QDate(2001,11,5);
+ QVERIFY( d1 > d2 );
+
+ d1 = QDate(2001,12,6);
+ d2 = QDate(2001,12,5);
+ QVERIFY( d1 > d2 );
+}
+
+void tst_QDate::operator_lt_eq()
+{
+ QDate d1(2000,1,2);
+ QDate d2(2000,1,2);
+ QVERIFY( d1 <= d2 );
+
+ d1 = QDate(2001,12,4);
+ d2 = QDate(2001,12,5);
+ QVERIFY( d1 <= d2 );
+
+ d1 = QDate(2001,11,5);
+ d2 = QDate(2001,12,5);
+ QVERIFY( d1 <= d2 );
+
+ d1 = QDate(2000,12,5);
+ d2 = QDate(2001,12,5);
+ QVERIFY( d1 <= d2 );
+
+ d1 = QDate(2002,12,5);
+ d2 = QDate(2001,12,5);
+ QVERIFY( !(d1 <= d2) );
+
+ d1 = QDate(2001,12,5);
+ d2 = QDate(2001,11,5);
+ QVERIFY( !(d1 <= d2) );
+
+ d1 = QDate(2001,12,6);
+ d2 = QDate(2001,12,5);
+ QVERIFY( !(d1 <= d2) );
+}
+
+void tst_QDate::operator_gt_eq()
+{
+ QDate d1(2000,1,2);
+ QDate d2(2000,1,2);
+ QVERIFY( d1 >= d2 );
+
+ d1 = QDate(2001,12,4);
+ d2 = QDate(2001,12,5);
+ QVERIFY( !(d1 >= d2) );
+
+ d1 = QDate(2001,11,5);
+ d2 = QDate(2001,12,5);
+ QVERIFY( !(d1 >= d2) );
+
+ d1 = QDate(2000,12,5);
+ d2 = QDate(2001,12,5);
+ QVERIFY( !(d1 >= d2) );
+
+ d1 = QDate(2002,12,5);
+ d2 = QDate(2001,12,5);
+ QVERIFY( d1 >= d2 );
+
+ d1 = QDate(2001,12,5);
+ d2 = QDate(2001,11,5);
+ QVERIFY( d1 >= d2 );
+
+ d1 = QDate(2001,12,6);
+ d2 = QDate(2001,12,5);
+ QVERIFY( d1 >= d2 );
+}
+
+Q_DECLARE_METATYPE(QDataStream::Version)
+
+void tst_QDate::operator_insert_extract_data()
+{
+ QTest::addColumn<QDate>("date");
+ QTest::addColumn<QDataStream::Version>("dataStreamVersion");
+
+ QMap<QDataStream::Version, QString> versionsToTest;
+ versionsToTest.insert(QDataStream::Qt_1_0, QString::fromLatin1("Qt_1_0"));
+ versionsToTest.insert(QDataStream::Qt_2_0, QString::fromLatin1("Qt_2_0"));
+ versionsToTest.insert(QDataStream::Qt_2_1, QString::fromLatin1("Qt_2_1"));
+ versionsToTest.insert(QDataStream::Qt_3_0, QString::fromLatin1("Qt_3_0"));
+ versionsToTest.insert(QDataStream::Qt_3_1, QString::fromLatin1("Qt_3_1"));
+ versionsToTest.insert(QDataStream::Qt_3_3, QString::fromLatin1("Qt_3_3"));
+ versionsToTest.insert(QDataStream::Qt_4_0, QString::fromLatin1("Qt_4_0"));
+ versionsToTest.insert(QDataStream::Qt_4_1, QString::fromLatin1("Qt_4_1"));
+ versionsToTest.insert(QDataStream::Qt_4_2, QString::fromLatin1("Qt_4_2"));
+ versionsToTest.insert(QDataStream::Qt_4_3, QString::fromLatin1("Qt_4_3"));
+ versionsToTest.insert(QDataStream::Qt_4_4, QString::fromLatin1("Qt_4_4"));
+ versionsToTest.insert(QDataStream::Qt_4_5, QString::fromLatin1("Qt_4_5"));
+ versionsToTest.insert(QDataStream::Qt_4_6, QString::fromLatin1("Qt_4_6"));
+ versionsToTest.insert(QDataStream::Qt_4_7, QString::fromLatin1("Qt_4_7"));
+ versionsToTest.insert(QDataStream::Qt_4_8, QString::fromLatin1("Qt_4_8"));
+ versionsToTest.insert(QDataStream::Qt_4_9, QString::fromLatin1("Qt_4_9"));
+ versionsToTest.insert(QDataStream::Qt_5_0, QString::fromLatin1("Qt_5_0"));
+
+ for (QMap<QDataStream::Version, QString>::ConstIterator it = versionsToTest.constBegin();
+ it != versionsToTest.constEnd(); ++it) {
+ const QString &version(it.value());
+ QTest::newRow(("(invalid) " + version).toLocal8Bit().constData()) << invalidDate() << it.key();
+ QTest::newRow(("(1, 1, 1) " + version).toLocal8Bit().constData()) << QDate(1, 1, 1) << it.key();
+ QTest::newRow(("(-1, 1, 1) " + version).toLocal8Bit().constData()) << QDate(-1, 1, 1) << it.key();
+ QTest::newRow(("(1995, 5, 20) " + version).toLocal8Bit().constData()) << QDate(1995, 5, 20) << it.key();
+
+ // Test minimums for quint32/qint64.
+ if (it.key() >= QDataStream::Qt_5_0)
+ QTest::newRow(("(-4714, 11, 24) " + version).toLocal8Bit().constData()) << QDate(-4714, 11, 24) << it.key();
+ else
+ QTest::newRow(("(-4713, 1, 2) " + version).toLocal8Bit().constData()) << QDate(-4713, 1, 2) << it.key();
+ }
+}
+
+void tst_QDate::operator_insert_extract()
+{
+ QFETCH(QDate, date);
+ QFETCH(QDataStream::Version, dataStreamVersion);
+
+ QByteArray byteArray;
+ QDataStream dataStream(&byteArray, QIODevice::ReadWrite);
+ dataStream.setVersion(dataStreamVersion);
+ dataStream << date;
+ dataStream.device()->reset();
+ QDate deserialised;
+ dataStream >> deserialised;
+ QCOMPARE(dataStream.status(), QDataStream::Ok);
+
+ QCOMPARE(deserialised, date);
+}
+
+void tst_QDate::fromStringDateFormat_data()
+{
+ QTest::addColumn<QString>("dateStr");
+ QTest::addColumn<Qt::DateFormat>("dateFormat");
+ QTest::addColumn<QDate>("expectedDate");
+
+ QTest::newRow("text0") << QString("Sat May 20 1995") << Qt::TextDate << QDate(1995, 5, 20);
+ QTest::newRow("text1") << QString("Tue Dec 17 2002") << Qt::TextDate << QDate(2002, 12, 17);
+ QTest::newRow("text2") << QDate(1999, 11, 14).toString(Qt::TextDate) << Qt::TextDate << QDate(1999, 11, 14);
+ QTest::newRow("text3") << QString("xxx Jan 1 0999") << Qt::TextDate << QDate(999, 1, 1);
+ QTest::newRow("text3b") << QString("xxx Jan 1 999") << Qt::TextDate << QDate(999, 1, 1);
+ QTest::newRow("text4") << QString("xxx Jan 1 12345") << Qt::TextDate << QDate(12345, 1, 1);
+ QTest::newRow("text5") << QString("xxx Jan 1 -0001") << Qt::TextDate << QDate(-1, 1, 1);
+ QTest::newRow("text6") << QString("xxx Jan 1 -4712") << Qt::TextDate << QDate(-4712, 1, 1);
+ QTest::newRow("text7") << QString("xxx Nov 25 -4713") << Qt::TextDate << QDate(-4713, 11, 25);
+ QTest::newRow("text, empty") << QString() << Qt::TextDate << QDate();
+ QTest::newRow("text, 3 part") << QString("part1 part2 part3") << Qt::TextDate << QDate();
+ QTest::newRow("text, invalid month name") << QString("Wed BabytownFrolics 8 2012") << Qt::TextDate << QDate();
+ QTest::newRow("text, invalid day") << QString("Wed May Wilhelm 2012") << Qt::TextDate << QDate();
+ QTest::newRow("text, invalid year") << QString("Wed May 8 Cats") << Qt::TextDate << QDate();
+
+ QTest::newRow("iso0") << QString("1995-05-20") << Qt::ISODate << QDate(1995, 5, 20);
+ QTest::newRow("iso1") << QString("2002-12-17") << Qt::ISODate << QDate(2002, 12, 17);
+ QTest::newRow("iso2") << QDate(1999, 11, 14).toString(Qt::ISODate) << Qt::ISODate << QDate(1999, 11, 14);
+ QTest::newRow("iso3") << QString("0999-01-01") << Qt::ISODate << QDate(999, 1, 1);
+ QTest::newRow("iso3b") << QString("0999-01-01") << Qt::ISODate << QDate(999, 1, 1);
+ QTest::newRow("iso4") << QString("2000101101") << Qt::ISODate << QDate();
+ QTest::newRow("iso5") << QString("2000/01/01") << Qt::ISODate << QDate(2000, 1, 1);
+ QTest::newRow("iso6") << QString("2000-01-01 blah") << Qt::ISODate << QDate(2000, 1, 1);
+ QTest::newRow("iso7") << QString("2000-01-011blah") << Qt::ISODate << QDate();
+ QTest::newRow("iso8") << QString("2000-01-01blah") << Qt::ISODate << QDate(2000, 1, 1);
+ QTest::newRow("iso9") << QString("-001-01-01") << Qt::ISODate << QDate();
+ QTest::newRow("iso10") << QString("99999-01-01") << Qt::ISODate << QDate();
+
+ // Test Qt::RFC2822Date format (RFC 2822).
+ QTest::newRow("RFC 2822") << QString::fromLatin1("13 Feb 1987 13:24:51 +0100")
+ << Qt::RFC2822Date << QDate(1987, 2, 13);
+ QTest::newRow("RFC 2822 with day") << QString::fromLatin1("Thu, 01 Jan 1970 00:12:34 +0000")
+ << Qt::RFC2822Date << QDate(1970, 1, 1);
+ // No timezone
+ QTest::newRow("RFC 2822 no timezone") << QString::fromLatin1("01 Jan 1970 00:12:34")
+ << Qt::RFC2822Date << QDate(1970, 1, 1);
+ // No time specified
+ QTest::newRow("RFC 2822 date only") << QString::fromLatin1("01 Nov 2002")
+ << Qt::RFC2822Date << QDate(2002, 11, 1);
+ QTest::newRow("RFC 2822 with day date only") << QString::fromLatin1("Fri, 01 Nov 2002")
+ << Qt::RFC2822Date << QDate(2002, 11, 1);
+ // Test invalid month, day, year
+ QTest::newRow("RFC 2822 invalid month name") << QString::fromLatin1("13 Fev 1987 13:24:51 +0100")
+ << Qt::RFC2822Date << QDate();
+ QTest::newRow("RFC 2822 invalid day") << QString::fromLatin1("36 Fev 1987 13:24:51 +0100")
+ << Qt::RFC2822Date << QDate();
+ QTest::newRow("RFC 2822 invalid year") << QString::fromLatin1("13 Fev 0000 13:24:51 +0100")
+ << Qt::RFC2822Date << QDate();
+ // Test invalid characters (should ignore invalid characters at end of string).
+ QTest::newRow("RFC 2822 invalid character at end") << QString::fromLatin1("01 Jan 2012 08:00:00 +0100!")
+ << Qt::RFC2822Date << QDate(2012, 1, 1);
+ QTest::newRow("RFC 2822 invalid character at front") << QString::fromLatin1("!01 Jan 2012 08:00:00 +0000")
+ << Qt::RFC2822Date << QDate();
+ QTest::newRow("RFC 2822 invalid character both ends") << QString::fromLatin1("!01 Jan 2012 08:00:00 +0000!")
+ << Qt::RFC2822Date << QDate();
+ QTest::newRow("RFC 2822 invalid character at front, 2 at back") << QString::fromLatin1("!01 Jan 2012 08:00:00 +0000..")
+ << Qt::RFC2822Date << QDate();
+ QTest::newRow("RFC 2822 invalid character 2 at front") << QString::fromLatin1("!!01 Jan 2012 08:00:00 +0000")
+ << Qt::RFC2822Date << QDate();
+
+ // Test Qt::RFC2822Date format (RFC 850 and 1036).
+ QTest::newRow("RFC 850 and 1036") << QString::fromLatin1("Fri Feb 13 13:24:51 1987 +0100")
+ << Qt::RFC2822Date << QDate(1987, 2, 13);
+ // No timezone
+ QTest::newRow("RFC 850 and 1036 no timezone") << QString::fromLatin1("Thu Jan 01 00:12:34 1970")
+ << Qt::RFC2822Date << QDate(1970, 1, 1);
+ // No time specified
+ QTest::newRow("RFC 850 and 1036 date only") << QString::fromLatin1("Fri Nov 01 2002")
+ << Qt::RFC2822Date << QDate(2002, 11, 1);
+ // Test invalid characters (should ignore invalid characters at end of string).
+ QTest::newRow("RFC 850 and 1036 invalid character at end") << QString::fromLatin1("Sun Jan 01 08:00:00 2012 +0100!")
+ << Qt::RFC2822Date << QDate(2012, 1, 1);
+ QTest::newRow("RFC 850 and 1036 invalid character at front") << QString::fromLatin1("!Sun Jan 01 08:00:00 2012 +0000")
+ << Qt::RFC2822Date << QDate();
+ QTest::newRow("RFC 850 and 1036 invalid character both ends") << QString::fromLatin1("!Sun Jan 01 08:00:00 2012 +0000!")
+ << Qt::RFC2822Date << QDate();
+ QTest::newRow("RFC 850 and 1036 invalid character at front, 2 at back") << QString::fromLatin1("!Sun Jan 01 08:00:00 2012 +0000..")
+ << Qt::RFC2822Date << QDate();
+ QTest::newRow("RFC 850 and 1036 invalid character 2 at front") << QString::fromLatin1("!!Sun Jan 01 08:00:00 2012 +0000")
+ << Qt::RFC2822Date << QDate();
+
+ QTest::newRow("RFC empty") << QString::fromLatin1("") << Qt::RFC2822Date << QDate();
+}
+
+void tst_QDate::fromStringDateFormat()
+{
+ QFETCH(QString, dateStr);
+ QFETCH(Qt::DateFormat, dateFormat);
+ QFETCH(QDate, expectedDate);
+
+ QCOMPARE(QDate::fromString(dateStr, dateFormat), expectedDate);
+}
+
+void tst_QDate::fromStringFormat_data()
+{
+ QTest::addColumn<QString>("string");
+ QTest::addColumn<QString>("format");
+ QTest::addColumn<QDate>("expected");
+
+ // Undo this (inline the C-locale versions) for ### Qt 6
+ // Get localized names:
+ QString january = QLocale::system().monthName(1, QLocale::LongFormat);
+ QString february = QLocale::system().monthName(2, QLocale::LongFormat);
+ QString march = QLocale::system().monthName(3, QLocale::LongFormat);
+ QString august = QLocale::system().monthName(8, QLocale::LongFormat);
+ QString mon = QLocale::system().dayName(1, QLocale::ShortFormat);
+ QString monday = QLocale::system().dayName(1, QLocale::LongFormat);
+ QString tuesday = QLocale::system().dayName(2, QLocale::LongFormat);
+ QString wednesday = QLocale::system().dayName(3, QLocale::LongFormat);
+ QString thursday = QLocale::system().dayName(4, QLocale::LongFormat);
+ QString friday = QLocale::system().dayName(5, QLocale::LongFormat);
+ QString saturday = QLocale::system().dayName(6, QLocale::LongFormat);
+ QString sunday = QLocale::system().dayName(7, QLocale::LongFormat);
+
+ QTest::newRow("data0") << QString("") << QString("") << defDate();
+ QTest::newRow("data1") << QString(" ") << QString("") << invalidDate();
+ QTest::newRow("data2") << QString(" ") << QString(" ") << defDate();
+ QTest::newRow("data3") << QString("-%$%#") << QString("$*(#@") << invalidDate();
+ QTest::newRow("data4") << QString("d") << QString("'d'") << defDate();
+ QTest::newRow("data5") << QString("101010") << QString("dMyy") << QDate(1910, 10, 10);
+ QTest::newRow("data6") << QString("101010b") << QString("dMyy") << invalidDate();
+ QTest::newRow("data7") << january << QString("MMMM") << defDate();
+ QTest::newRow("data8") << QString("ball") << QString("balle") << invalidDate();
+ QTest::newRow("data9") << QString("balleh") << QString("balleh") << defDate();
+ QTest::newRow("data10") << QString("10.01.1") << QString("M.dd.d") << QDate(defDate().year(), 10, 1);
+ QTest::newRow("data11") << QString("-1.01.1") << QString("M.dd.d") << invalidDate();
+ QTest::newRow("data12") << QString("11010") << QString("dMMyy") << invalidDate();
+ QTest::newRow("data13") << QString("-2") << QString("d") << invalidDate();
+ QTest::newRow("data14") << QString("132") << QString("Md") << invalidDate();
+ QTest::newRow("data15") << february << QString("MMMM") << QDate(defDate().year(), 2, 1);
+
+ QString date = mon + QLatin1Char(' ') + august + " 8 2005";
+ QTest::newRow("data16") << date << QString("ddd MMMM d yyyy") << QDate(2005, 8, 8);
+ QTest::newRow("data17") << QString("2000:00") << QString("yyyy:yy") << QDate(2000, 1, 1);
+ QTest::newRow("data18") << QString("1999:99") << QString("yyyy:yy") << QDate(1999, 1, 1);
+ QTest::newRow("data19") << QString("2099:99") << QString("yyyy:yy") << QDate(2099, 1, 1);
+ QTest::newRow("data20") << QString("2001:01") << QString("yyyy:yy") << QDate(2001, 1, 1);
+ QTest::newRow("data21") << QString("99") << QString("yy") << QDate(1999, 1, 1);
+ QTest::newRow("data22") << QString("01") << QString("yy") << QDate(1901, 1, 1);
+
+ QTest::newRow("data23") << monday << QString("dddd") << QDate(1900, 1, 1);
+ QTest::newRow("data24") << tuesday << QString("dddd") << QDate(1900, 1, 2);
+ QTest::newRow("data25") << wednesday << QString("dddd") << QDate(1900, 1, 3);
+ QTest::newRow("data26") << thursday << QString("dddd") << QDate(1900, 1, 4);
+ QTest::newRow("data27") << friday << QString("dddd") << QDate(1900, 1, 5);
+ QTest::newRow("data28") << saturday << QString("dddd") << QDate(1900, 1, 6);
+ QTest::newRow("data29") << sunday << QString("dddd") << QDate(1900, 1, 7);
+
+ QTest::newRow("data30") << monday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 2);
+ QTest::newRow("data31") << tuesday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 3);
+ QTest::newRow("data32") << wednesday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 4);
+ QTest::newRow("data33") << thursday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 5);
+ QTest::newRow("data34") << friday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 6);
+ QTest::newRow("data35") << saturday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 7);
+ QTest::newRow("data36") << sunday + " 2006" << QString("dddd yyyy") << QDate(2006, 1, 1);
+
+ QTest::newRow("data37") << tuesday + " 2007 " + march << QString("dddd yyyy MMMM") << QDate(2007, 3, 6);
+
+ QTest::newRow("data38") << QString("21052006") << QString("ddMMyyyy") << QDate(2006,5,21);
+ QTest::newRow("data39") << QString("210506") << QString("ddMMyy") << QDate(1906,5,21);
+ QTest::newRow("data40") << QString("21/5/2006") << QString("d/M/yyyy") << QDate(2006,5,21);
+ QTest::newRow("data41") << QString("21/5/06") << QString("d/M/yy") << QDate(1906,5,21);
+ QTest::newRow("data42") << QString("20060521") << QString("yyyyMMdd") << QDate(2006,5,21);
+ QTest::newRow("data43") << QString("060521") << QString("yyMMdd") << QDate(1906,5,21);
+ QTest::newRow("lateMarch") << QString("9999-03-06") << QString("yyyy-MM-dd") << QDate(9999, 3, 6);
+ QTest::newRow("late") << QString("9999-12-31") << QString("yyyy-MM-dd") << QDate(9999, 12, 31);
+}
+
+
+void tst_QDate::fromStringFormat()
+{
+ QFETCH(QString, string);
+ QFETCH(QString, format);
+ QFETCH(QDate, expected);
+
+ QDate dt = QDate::fromString(string, format);
+ QCOMPARE(dt, expected);
+}
+
+void tst_QDate::toStringFormat_data()
+{
+ QTest::addColumn<QDate>("t");
+ QTest::addColumn<QString>("format");
+ QTest::addColumn<QString>("str");
+
+ QTest::newRow( "data0" ) << QDate(1995,5,20) << QString("d-M-yy") << QString("20-5-95");
+ QTest::newRow( "data1" ) << QDate(2002,12,17) << QString("dd-MM-yyyy") << QString("17-12-2002");
+ QTest::newRow( "data2" ) << QDate(1995,5,20) << QString("M-yy") << QString("5-95");
+ QTest::newRow( "data3" ) << QDate(2002,12,17) << QString("dd") << QString("17");
+ QTest::newRow( "data4" ) << QDate() << QString("dd-mm-yyyy") << QString();
+}
+
+void tst_QDate::toStringFormat()
+{
+ QFETCH( QDate, t );
+ QFETCH( QString, format );
+ QFETCH( QString, str );
+
+ QCOMPARE( t.toString( format ), str );
+}
+
+void tst_QDate::toStringDateFormat_data()
+{
+ QTest::addColumn<QDate>("date");
+ QTest::addColumn<Qt::DateFormat>("format");
+ QTest::addColumn<QString>("expectedStr");
+
+ QTest::newRow("data0") << QDate(1,1,1) << Qt::ISODate << QString("0001-01-01");
+ QTest::newRow("data1") << QDate(11,1,1) << Qt::ISODate << QString("0011-01-01");
+ QTest::newRow("data2") << QDate(111,1,1) << Qt::ISODate << QString("0111-01-01");
+ QTest::newRow("data3") << QDate(1974,12,1) << Qt::ISODate << QString("1974-12-01");
+ QTest::newRow("year < 0") << QDate(-1,1,1) << Qt::ISODate << QString();
+ QTest::newRow("year > 9999") << QDate(-1,1,1) << Qt::ISODate << QString();
+ QTest::newRow("RFC2822Date") << QDate(1974,12,1) << Qt::RFC2822Date << QString("01 Dec 1974");
+ QTest::newRow("ISODateWithMs") << QDate(1974,12,1) << Qt::ISODateWithMs << QString("1974-12-01");
+}
+
+void tst_QDate::toStringDateFormat()
+{
+ QFETCH(QDate, date);
+ QFETCH(Qt::DateFormat, format);
+ QFETCH(QString, expectedStr);
+
+ QCOMPARE(date.toString(Qt::SystemLocaleShortDate), QLocale::system().toString(date, QLocale::ShortFormat));
+ QCOMPARE(date.toString(Qt::DefaultLocaleShortDate), QLocale().toString(date, QLocale::ShortFormat));
+ QCOMPARE(date.toString(Qt::SystemLocaleLongDate), QLocale::system().toString(date, QLocale::LongFormat));
+ QCOMPARE(date.toString(Qt::DefaultLocaleLongDate), QLocale().toString(date, QLocale::LongFormat));
+ QLocale::setDefault(QLocale::German);
+ QCOMPARE(date.toString(Qt::SystemLocaleShortDate), QLocale::system().toString(date, QLocale::ShortFormat));
+ QCOMPARE(date.toString(Qt::DefaultLocaleShortDate), QLocale().toString(date, QLocale::ShortFormat));
+ QCOMPARE(date.toString(Qt::SystemLocaleLongDate), QLocale::system().toString(date, QLocale::LongFormat));
+ QCOMPARE(date.toString(Qt::DefaultLocaleLongDate), QLocale().toString(date, QLocale::LongFormat));
+
+ QCOMPARE(date.toString(format), expectedStr);
+}
+
+void tst_QDate::isLeapYear()
+{
+ QVERIFY(QDate::isLeapYear(-4801));
+ QVERIFY(!QDate::isLeapYear(-4800));
+ QVERIFY(QDate::isLeapYear(-4445));
+ QVERIFY(!QDate::isLeapYear(-4444));
+ QVERIFY(!QDate::isLeapYear(-6));
+ QVERIFY(QDate::isLeapYear(-5));
+ QVERIFY(!QDate::isLeapYear(-4));
+ QVERIFY(!QDate::isLeapYear(-3));
+ QVERIFY(!QDate::isLeapYear(-2));
+ QVERIFY(QDate::isLeapYear(-1));
+ QVERIFY(!QDate::isLeapYear(0)); // Doesn't exist
+ QVERIFY(!QDate::isLeapYear(1));
+ QVERIFY(!QDate::isLeapYear(2));
+ QVERIFY(!QDate::isLeapYear(3));
+ QVERIFY(QDate::isLeapYear(4));
+ QVERIFY(!QDate::isLeapYear(7));
+ QVERIFY(QDate::isLeapYear(8));
+ QVERIFY(!QDate::isLeapYear(100));
+ QVERIFY(QDate::isLeapYear(400));
+ QVERIFY(!QDate::isLeapYear(700));
+ QVERIFY(!QDate::isLeapYear(1500));
+ QVERIFY(QDate::isLeapYear(1600));
+ QVERIFY(!QDate::isLeapYear(1700));
+ QVERIFY(!QDate::isLeapYear(1800));
+ QVERIFY(!QDate::isLeapYear(1900));
+ QVERIFY(QDate::isLeapYear(2000));
+ QVERIFY(!QDate::isLeapYear(2100));
+ QVERIFY(!QDate::isLeapYear(2200));
+ QVERIFY(!QDate::isLeapYear(2300));
+ QVERIFY(QDate::isLeapYear(2400));
+ QVERIFY(!QDate::isLeapYear(2500));
+ QVERIFY(!QDate::isLeapYear(2600));
+ QVERIFY(!QDate::isLeapYear(2700));
+ QVERIFY(QDate::isLeapYear(2800));
+
+ for (int i = -4713; i <= 10000; ++i) {
+ if (i == 0)
+ continue;
+ QVERIFY(!QDate(i, 2, 29).isValid() == !QDate::isLeapYear(i));
+ }
+}
+
+void tst_QDate::yearsZeroToNinetyNine()
+{
+ {
+ QDate dt(-1, 2, 3);
+ QCOMPARE(dt.year(), -1);
+ QCOMPARE(dt.month(), 2);
+ QCOMPARE(dt.day(), 3);
+ }
+
+ {
+ QDate dt(1, 2, 3);
+ QCOMPARE(dt.year(), 1);
+ QCOMPARE(dt.month(), 2);
+ QCOMPARE(dt.day(), 3);
+ }
+
+ {
+ QDate dt(99, 2, 3);
+ QCOMPARE(dt.year(), 99);
+ QCOMPARE(dt.month(), 2);
+ QCOMPARE(dt.day(), 3);
+ }
+
+ QVERIFY(!QDate::isValid(0, 2, 3));
+ QVERIFY(QDate::isValid(1, 2, 3));
+ QVERIFY(QDate::isValid(-1, 2, 3));
+
+#if QT_DEPRECATED_SINCE(5,0)
+ {
+ QDate dt;
+ dt.setYMD(1, 2, 3);
+ QCOMPARE(dt.year(), 1901);
+ QCOMPARE(dt.month(), 2);
+ QCOMPARE(dt.day(), 3);
+ }
+#endif
+
+ {
+ QDate dt;
+ dt.setDate(1, 2, 3);
+ QCOMPARE(dt.year(), 1);
+ QCOMPARE(dt.month(), 2);
+ QCOMPARE(dt.day(), 3);
+
+ dt.setDate(0, 2, 3);
+ QVERIFY(!dt.isValid());
+ }
+}
+
+
+void tst_QDate::negativeYear() const
+{
+ QDate y(-20, 3, 4);
+ QVERIFY(y.isValid());
+ QCOMPARE(y.year(), -20);
+}
+
+void tst_QDate::printNegativeYear() const
+{
+ {
+ QDate date(-500, 3, 4);
+ QVERIFY(date.isValid());
+ QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0500"));
+ }
+
+ {
+ QDate date(-10, 3, 4);
+ QVERIFY(date.isValid());
+ QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0010"));
+ }
+
+ {
+ QDate date(-2, 3, 4);
+ QVERIFY(date.isValid());
+ QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0002"));
+ }
+}
+
+void tst_QDate::roundtripGermanLocale() const
+{
+ /* This code path should not result in warnings. */
+ const QDate theDate(QDate::currentDate());
+ theDate.fromString(theDate.toString(Qt::TextDate), Qt::TextDate);
+
+ const QDateTime theDateTime(QDateTime::currentDateTime());
+ theDateTime.fromString(theDateTime.toString(Qt::TextDate), Qt::TextDate);
+}
+
+#if QT_CONFIG(textdate)
+QT_WARNING_PUSH // the methods tested here are all deprecated
+QT_WARNING_DISABLE_GCC("-Wdeprecated-declarations")
+
+void tst_QDate::shortDayName() const
+{
+ QCOMPARE(QDate::shortDayName(0), QString());
+ QCOMPARE(QDate::shortDayName(8), QString());
+
+ if (QLocale::system().language() == QLocale::C) {
+ QCOMPARE(QDate::shortDayName(1), QLatin1String("Mon"));
+ QCOMPARE(QDate::shortDayName(7), QLatin1String("Sun"));
+ }
+
+ QLocale locale = QLocale::system();
+ for(int i = 1; i <= 7; ++i) {
+ QCOMPARE(QDate::shortDayName(i), locale.dayName(i, QLocale::ShortFormat));
+ }
+}
+
+void tst_QDate::standaloneShortDayName() const
+{
+ QCOMPARE(QDate::shortDayName(0, QDate::StandaloneFormat), QString());
+ QCOMPARE(QDate::shortDayName(8, QDate::StandaloneFormat), QString());
+
+ if (QLocale::system().language() == QLocale::C) {
+ QCOMPARE(QDate::shortDayName(1, QDate::StandaloneFormat), QLatin1String("Mon"));
+ QCOMPARE(QDate::shortDayName(7, QDate::StandaloneFormat), QLatin1String("Sun"));
+ }
+
+ QLocale locale = QLocale::system();
+ for(int i = 1; i <= 7; ++i) {
+ QCOMPARE(QDate::shortDayName(i, QDate::StandaloneFormat), locale.standaloneDayName(i, QLocale::ShortFormat));
+ }
+}
+
+void tst_QDate::longDayName() const
+{
+ QCOMPARE(QDate::longDayName(0), QString());
+ QCOMPARE(QDate::longDayName(8), QString());
+
+ if (QLocale::system().language() == QLocale::C) {
+ QCOMPARE(QDate::longDayName(1), QLatin1String("Monday"));
+ QCOMPARE(QDate::longDayName(7), QLatin1String("Sunday"));
+ }
+
+ QLocale locale = QLocale::system();
+ for(int i = 1; i <= 7; ++i) {
+ QCOMPARE(QDate::longDayName(i), locale.dayName(i, QLocale::LongFormat));
+ }
+}
+
+void tst_QDate::standaloneLongDayName() const
+{
+ QCOMPARE(QDate::longDayName(0, QDate::StandaloneFormat), QString());
+ QCOMPARE(QDate::longDayName(8, QDate::StandaloneFormat), QString());
+
+ if (QLocale::system().language() == QLocale::C) {
+ QCOMPARE(QDate::longDayName(1, QDate::StandaloneFormat), QLatin1String("Monday"));
+ QCOMPARE(QDate::longDayName(7, QDate::StandaloneFormat), QLatin1String("Sunday"));
+ }
+
+ QLocale locale = QLocale::system();
+ for(int i = 1; i <= 7; ++i) {
+ QCOMPARE(QDate::longDayName(i, QDate::StandaloneFormat), locale.standaloneDayName(i, QLocale::LongFormat));
+ }
+}
+
+void tst_QDate::shortMonthName() const
+{
+ QCOMPARE(QDate::shortMonthName(0), QString());
+ QCOMPARE(QDate::shortMonthName(13), QString());
+
+ if (QLocale::system().language() == QLocale::C) {
+ QCOMPARE(QDate::shortMonthName(1), QLatin1String("Jan"));
+ QCOMPARE(QDate::shortMonthName(8), QLatin1String("Aug"));
+ }
+
+ QLocale locale = QLocale::system();
+ for(int i = 1; i <= 12; ++i) {
+ QCOMPARE(QDate::shortMonthName(i), locale.monthName(i, QLocale::ShortFormat));
+ }
+}
+
+void tst_QDate::standaloneShortMonthName() const
+{
+ QCOMPARE(QDate::shortMonthName(0, QDate::StandaloneFormat), QString());
+ QCOMPARE(QDate::shortMonthName(13, QDate::StandaloneFormat), QString());
+
+ if (QLocale::system().language() == QLocale::C) {
+ QCOMPARE(QDate::shortMonthName(1, QDate::StandaloneFormat), QLatin1String("Jan"));
+ QCOMPARE(QDate::shortMonthName(8, QDate::StandaloneFormat), QLatin1String("Aug"));
+ }
+
+ QLocale locale = QLocale::system();
+ for(int i = 1; i <= 12; ++i) {
+ QCOMPARE(QDate::shortMonthName(i, QDate::StandaloneFormat), locale.standaloneMonthName(i, QLocale::ShortFormat));
+ }
+}
+
+void tst_QDate::longMonthName() const
+{
+ QCOMPARE(QDate::longMonthName(0), QString());
+ QCOMPARE(QDate::longMonthName(13), QString());
+
+ if (QLocale::system().language() == QLocale::C) {
+ QCOMPARE(QDate::longMonthName(1), QLatin1String("January"));
+ QCOMPARE(QDate::longMonthName(8), QLatin1String("August"));
+ }
+
+ QLocale locale = QLocale::system();
+ for(int i = 1; i <= 12; ++i) {
+ QCOMPARE(QDate::longMonthName(i), locale.monthName(i, QLocale::LongFormat));
+ }
+}
+
+void tst_QDate::standaloneLongMonthName() const
+{
+ QCOMPARE(QDate::longMonthName(0, QDate::StandaloneFormat), QString());
+ QCOMPARE(QDate::longMonthName(13, QDate::StandaloneFormat), QString());
+
+ if (QLocale::system().language() == QLocale::C) {
+ QCOMPARE(QDate::longMonthName(1, QDate::StandaloneFormat), QLatin1String("January"));
+ QCOMPARE(QDate::longMonthName(8, QDate::StandaloneFormat), QLatin1String("August"));
+ }
+
+ QLocale locale = QLocale::system();
+ for(int i = 1; i <= 12; ++i) {
+ QCOMPARE(QDate::longMonthName(i, QDate::StandaloneFormat), locale.standaloneMonthName(i, QLocale::LongFormat));
+ }
+}
+QT_WARNING_POP
+#endif // textdate
+
+void tst_QDate::roundtrip() const
+{
+ // Test round trip, this exercises setDate(), isValid(), isLeapYear(),
+ // year(), month(), day(), julianDayFromDate(), and getDateFromJulianDay()
+ // to ensure they are internally consistent (but doesn't guarantee correct)
+
+ // Test Julian round trip around JD 0 and the c++ integer division rounding
+ // problem point (eg. negative numbers) in the conversion functions.
+ QDate testDate;
+ QDate loopDate = QDate::fromJulianDay(-50001); // 1 Jan 4850 BC
+ while (loopDate.toJulianDay() <= 5150) { // 31 Dec 4700 BC
+ testDate.setDate(loopDate.year(), loopDate.month(), loopDate.day());
+ QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
+ loopDate = loopDate.addDays(1);
+ }
+
+ // Test Julian round trip in both BC and AD
+ loopDate = QDate::fromJulianDay(1684901); // 1 Jan 100 BC
+ while (loopDate.toJulianDay() <= 1757949) { // 31 Dec 100 AD
+ testDate.setDate(loopDate.year(), loopDate.month(), loopDate.day());
+ QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
+ loopDate = loopDate.addDays(1);
+ }
+
+ // Test Gregorian round trip during current useful period
+ loopDate = QDate::fromJulianDay(2378497); // 1 Jan 1900 AD
+ while (loopDate.toJulianDay() <= 2488433) { // 31 Dec 2100 AD
+ testDate.setDate(loopDate.year(), loopDate.month(), loopDate.day());
+ QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
+ loopDate = loopDate.addDays(1);
+ }
+
+ // Test Gregorian round trip at top end of widget/format range
+ loopDate = QDate::fromJulianDay(5336961); // 1 Jan 9900 AD
+ while (loopDate.toJulianDay() <= 5373484) { // 31 Dec 9999 AD
+ testDate.setDate(loopDate.year(), loopDate.month(), loopDate.day());
+ QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
+ loopDate = loopDate.addDays(1);
+ }
+
+ qint64 minJd = Q_INT64_C(-784350574879);
+ qint64 maxJd = Q_INT64_C( 784354017364);
+
+ // Test Gregorian round trip at top end of conversion range
+ loopDate = QDate::fromJulianDay(maxJd);
+ while (loopDate.toJulianDay() >= maxJd - 146397) {
+ testDate.setDate(loopDate.year(), loopDate.month(), loopDate.day());
+ QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
+ loopDate = loopDate.addDays(-1);
+ }
+
+ // Test Gregorian round trip at low end of conversion range
+ loopDate = QDate::fromJulianDay(minJd);
+ while (loopDate.toJulianDay() <= minJd + 146397) {
+ testDate.setDate(loopDate.year(), loopDate.month(), loopDate.day());
+ QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
+ loopDate = loopDate.addDays(1);
+ }
+}
+
+void tst_QDate::qdebug() const
+{
+ QTest::ignoreMessage(QtDebugMsg, "QDate(Invalid)");
+ qDebug() << QDate();
+ QTest::ignoreMessage(QtDebugMsg, "QDate(\"1983-08-07\")");
+ qDebug() << QDate(1983, 8, 7);
+}
+
+QTEST_APPLESS_MAIN(tst_QDate)
+#include "tst_qdate.moc"
diff --git a/tests/auto/corelib/time/qdatetime/.gitignore b/tests/auto/corelib/time/qdatetime/.gitignore
new file mode 100644
index 0000000000..7784f3a3eb
--- /dev/null
+++ b/tests/auto/corelib/time/qdatetime/.gitignore
@@ -0,0 +1 @@
+tst_qdatetime
diff --git a/tests/auto/corelib/time/qdatetime/BLACKLIST b/tests/auto/corelib/time/qdatetime/BLACKLIST
new file mode 100644
index 0000000000..3a42ee066b
--- /dev/null
+++ b/tests/auto/corelib/time/qdatetime/BLACKLIST
@@ -0,0 +1,2 @@
+[timeZoneAbbreviation]
+osx
diff --git a/tests/auto/corelib/time/qdatetime/qdatetime.pro b/tests/auto/corelib/time/qdatetime/qdatetime.pro
new file mode 100644
index 0000000000..742eb47075
--- /dev/null
+++ b/tests/auto/corelib/time/qdatetime/qdatetime.pro
@@ -0,0 +1,17 @@
+CONFIG += testcase
+TARGET = tst_qdatetime
+QT = core-private testlib
+SOURCES = tst_qdatetime.cpp
+
+# For some reason using optimization here triggers a compiler issue, which causes an exception
+# However, the code is correct
+msvc {
+ !build_pass:message ( "Compiler issue, removing -O1 flag" )
+ QMAKE_CFLAGS_RELEASE -= -O1
+ QMAKE_CXXFLAGS_RELEASE -= -O1
+}
+
+mac {
+ OBJECTIVE_SOURCES += tst_qdatetime_mac.mm
+ LIBS += -framework Foundation
+}
diff --git a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp
new file mode 100644
index 0000000000..6f0aebb071
--- /dev/null
+++ b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp
@@ -0,0 +1,3486 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 The Qt Company Ltd.
+** Copyright (C) 2016 Intel Corporation.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtTest/QtTest>
+#include <time.h>
+#include <qdatetime.h>
+#include <private/qdatetime_p.h>
+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
+# include <locale.h>
+#endif
+
+#ifdef Q_OS_WIN
+# include <qt_windows.h>
+#endif
+
+class tst_QDateTime : public QObject
+{
+ Q_OBJECT
+
+public:
+ tst_QDateTime();
+
+ static QString str( int y, int month, int d, int h, int min, int s );
+ static QDateTime dt( const QString& str );
+public slots:
+ void initTestCase();
+ void init();
+private slots:
+ void ctor();
+ void operator_eq();
+ void isNull();
+ void isValid();
+ void date();
+ void time();
+ void timeSpec();
+ void toSecsSinceEpoch_data();
+ void toSecsSinceEpoch();
+ void daylightSavingsTimeChange_data();
+ void daylightSavingsTimeChange();
+ void springForward_data();
+ void springForward();
+ void setDate();
+ void setTime_data();
+ void setTime();
+ void setTimeSpec_data();
+ void setTimeSpec();
+ void setSecsSinceEpoch();
+ void setMSecsSinceEpoch_data();
+ void setMSecsSinceEpoch();
+ void fromMSecsSinceEpoch_data();
+ void fromMSecsSinceEpoch();
+ void toString_isoDate_data();
+ void toString_isoDate();
+ void toString_isoDate_extra();
+#if QT_CONFIG(datestring)
+ void toString_textDate_data();
+ void toString_textDate();
+ void toString_textDate_extra();
+#endif
+ void toString_rfcDate_data();
+ void toString_rfcDate();
+ void toString_enumformat();
+ void toString_strformat();
+ void addDays();
+ void addMonths();
+ void addMonths_data();
+ void addYears();
+ void addYears_data();
+ void addSecs_data();
+ void addSecs();
+ void addMSecs_data();
+ void addMSecs();
+ void toTimeSpec_data();
+ void toTimeSpec();
+ void toLocalTime_data();
+ void toLocalTime();
+ void toUTC_data();
+ void toUTC();
+ void daysTo();
+ void secsTo_data();
+ void secsTo();
+ void msecsTo_data();
+ void msecsTo();
+ void operator_eqeq_data();
+ void operator_eqeq();
+ void operator_insert_extract_data();
+ void operator_insert_extract();
+ void currentDateTime();
+ void currentDateTimeUtc();
+ void currentDateTimeUtc2();
+ void fromStringDateFormat_data();
+ void fromStringDateFormat();
+ void fromStringStringFormat_data();
+ void fromStringStringFormat();
+ void fromStringStringFormatLocale_data();
+ void fromStringStringFormatLocale();
+#ifdef Q_OS_WIN
+ void fromString_LOCALE_ILDATE();
+#endif
+ void fromStringToStringLocale_data();
+ void fromStringToStringLocale();
+
+ void offsetFromUtc();
+ void setOffsetFromUtc();
+ void toOffsetFromUtc();
+
+ void zoneAtTime_data();
+ void zoneAtTime();
+ void timeZoneAbbreviation();
+
+ void getDate();
+
+ void fewDigitsInYear() const;
+ void printNegativeYear() const;
+ void roundtripGermanLocale() const;
+ void utcOffsetLessThan() const;
+
+ void isDaylightTime() const;
+ void daylightTransitions() const;
+ void timeZones() const;
+ void systemTimeZoneChange() const;
+
+ void invalid() const;
+
+ void macTypes();
+
+private:
+ enum { LocalTimeIsUtc = 0, LocalTimeAheadOfUtc = 1, LocalTimeBehindUtc = -1} localTimeType;
+ bool zoneIsCET;
+ QDate defDate() const { return QDate(1900, 1, 1); }
+ QTime defTime() const { return QTime(0, 0, 0); }
+ QDateTime defDateTime() const { return QDateTime(defDate(), defTime()); }
+ QDateTime invalidDateTime() const { return QDateTime(invalidDate(), invalidTime()); }
+ QDate invalidDate() const { return QDate(); }
+ QTime invalidTime() const { return QTime(-1, -1, -1); }
+
+ class TimeZoneRollback
+ {
+ const QByteArray prior;
+ public:
+ // Save the previous timezone so we can restore it afterwards, otherwise
+ // later tests may break:
+ explicit TimeZoneRollback(const QByteArray &zone) : prior(qgetenv("TZ"))
+ { reset(zone); }
+ void reset(const QByteArray &zone)
+ {
+ qputenv("TZ", zone.constData());
+ qTzSet();
+ }
+ ~TimeZoneRollback()
+ {
+ if (prior.isNull())
+ qunsetenv("TZ");
+ else
+ qputenv("TZ", prior.constData());
+ qTzSet();
+ }
+ };
+};
+
+Q_DECLARE_METATYPE(Qt::TimeSpec)
+Q_DECLARE_METATYPE(Qt::DateFormat)
+
+tst_QDateTime::tst_QDateTime()
+{
+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
+ // Some tests depend on C locale - BF&I it with belt *and* braces:
+ qputenv("LC_ALL", "C");
+ setlocale(LC_ALL, "C");
+ // Need to do this as early as possible, before anything accesses the
+ // QSystemLocale singleton; once it exists, there's no changing it.
+#endif // remove for ### Qt 6
+
+ /*
+ Due to some jurisdictions changing their zones and rules, it's possible
+ for a non-CET zone to accidentally match CET at a few tested moments but
+ be different a few years later or earlier. This would lead to tests
+ failing if run in the partially-aliasing zone (e.g. Algeria, Lybia). So
+ test thoroughly; ideally at every mid-winter or mid-summer in whose
+ half-year any test below assumes zoneIsCET means what it says. (Tests at
+ or near a DST transition implicate both of the half-years that meet
+ there.) Years outside the 1970--2038 range, however, are likely not
+ properly handled by the TZ-database; and QDateTime explicitly handles them
+ differently, so don't probe them here.
+ */
+ const uint day = 24 * 3600; // in seconds
+ zoneIsCET = (QDateTime(QDate(2038, 1, 19), QTime(4, 14, 7)).toSecsSinceEpoch() == 0x7fffffff
+ // Entries a year apart robustly differ by multiples of day.
+ && QDateTime(QDate(2015, 7, 1), QTime()).toSecsSinceEpoch() == 1435701600
+ && QDateTime(QDate(2015, 1, 1), QTime()).toSecsSinceEpoch() == 1420066800
+ && QDateTime(QDate(2013, 7, 1), QTime()).toSecsSinceEpoch() == 1372629600
+ && QDateTime(QDate(2013, 1, 1), QTime()).toSecsSinceEpoch() == 1356994800
+ && QDateTime(QDate(2012, 7, 1), QTime()).toSecsSinceEpoch() == 1341093600
+ && QDateTime(QDate(2012, 1, 1), QTime()).toSecsSinceEpoch() == 1325372400
+ && QDateTime(QDate(2008, 7, 1), QTime()).toSecsSinceEpoch() == 1214863200
+ && QDateTime(QDate(2004, 1, 1), QTime()).toSecsSinceEpoch() == 1072911600
+ && QDateTime(QDate(2000, 1, 1), QTime()).toSecsSinceEpoch() == 946681200
+ && QDateTime(QDate(1990, 7, 1), QTime()).toSecsSinceEpoch() == 646783200
+ && QDateTime(QDate(1990, 1, 1), QTime()).toSecsSinceEpoch() == 631148400
+ && QDateTime(QDate(1979, 1, 1), QTime()).toSecsSinceEpoch() == 283993200
+ // .toSecsSinceEpoch() returns -1 for everything before this:
+ && QDateTime(QDate(1970, 1, 1), QTime(1, 0, 0)).toSecsSinceEpoch() == 0);
+ // Use .toMSecsSinceEpoch() if you really need to test anything earlier.
+
+ /*
+ Again, rule changes can cause a TZ to look like UTC at some sample dates
+ but deviate at some date relevant to a test using localTimeType. These
+ tests mostly use years outside the 1970--2038 range for which TZ data is
+ credible, so we can't helpfully be exhaustive. So scan a sample of years'
+ starts and middles.
+ */
+ const int sampled = 3;
+ // UTC starts of months in 2004, 2038 and 1970:
+ qint64 jans[sampled] = { 12418 * day, 24837 * day, 0 };
+ qint64 juls[sampled] = { 12600 * day, 25018 * day, 181 * day };
+ localTimeType = LocalTimeIsUtc;
+ for (int i = sampled; i-- > 0; ) {
+ QDateTime jan = QDateTime::fromSecsSinceEpoch(jans[i]);
+ QDateTime jul = QDateTime::fromSecsSinceEpoch(juls[i]);
+ if (jan.date().year() < 1970 || jul.date().month() < 7) {
+ localTimeType = LocalTimeBehindUtc;
+ break;
+ } else if (jan.time().hour() > 0 || jul.time().hour() > 0
+ || jan.date().day() > 1 || jul.date().day() > 1) {
+ localTimeType = LocalTimeAheadOfUtc;
+ break;
+ }
+ }
+ /*
+ Even so, TZ=Africa/Algiers will fail fromMSecsSinceEpoch(-1) because it
+ switched from WET without DST (i.e. UTC) in the late 1960s to WET with DST
+ for all of 1970 - so they had a DST transition *on the epoch*. They've
+ since switched to CET with no DST, making life simple; but our tests for
+ mistakes around the epoch can't tell the difference between what Algeria
+ really did and the symptoms we can believe a bug might produce: there's
+ not much we can do about that, that wouldn't hide real bugs.
+ */
+}
+
+void tst_QDateTime::initTestCase()
+{
+ // Never construct a message like this in an i18n context...
+ const char *typemsg1 = "exactly";
+ const char *typemsg2 = "and therefore not";
+ switch (localTimeType) {
+ case LocalTimeIsUtc:
+ break;
+ case LocalTimeBehindUtc:
+ typemsg1 = "behind";
+ break;
+ case LocalTimeAheadOfUtc:
+ typemsg1 = "ahead of";
+ typemsg2 = zoneIsCET ? "and is" : "but isn't";
+ break;
+ }
+
+ qDebug() << "Current local time detected to be"
+ << typemsg1
+ << "UTC"
+ << typemsg2
+ << "the Central European timezone";
+}
+
+void tst_QDateTime::init()
+{
+#if defined(Q_OS_WIN32)
+ SetThreadLocale(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
+#endif
+}
+
+QString tst_QDateTime::str( int y, int month, int d, int h, int min, int s )
+{
+ return QDateTime( QDate(y, month, d), QTime(h, min, s) ).toString( Qt::ISODate );
+}
+
+QDateTime tst_QDateTime::dt( const QString& str )
+{
+ if ( str == "INVALID" ) {
+ return QDateTime();
+ } else {
+ return QDateTime::fromString( str, Qt::ISODate );
+ }
+}
+
+void tst_QDateTime::ctor()
+{
+ QDateTime dt1(QDate(2004, 1, 2), QTime(1, 2, 3));
+ QCOMPARE(dt1.timeSpec(), Qt::LocalTime);
+ QDateTime dt2(QDate(2004, 1, 2), QTime(1, 2, 3), Qt::LocalTime);
+ QCOMPARE(dt2.timeSpec(), Qt::LocalTime);
+ QDateTime dt3(QDate(2004, 1, 2), QTime(1, 2, 3), Qt::UTC);
+ QCOMPARE(dt3.timeSpec(), Qt::UTC);
+
+ QVERIFY(dt1 == dt2);
+ if (zoneIsCET) {
+ QVERIFY(dt1 != dt3);
+ QVERIFY(dt1 < dt3);
+ QVERIFY(dt1.addSecs(3600).toUTC() == dt3);
+ }
+
+ // Test OffsetFromUTC constructors
+ QDate offsetDate(2013, 1, 1);
+ QTime offsetTime(1, 2, 3);
+
+ QDateTime offset1(offsetDate, offsetTime, Qt::OffsetFromUTC);
+ QCOMPARE(offset1.timeSpec(), Qt::UTC);
+ QCOMPARE(offset1.offsetFromUtc(), 0);
+ QCOMPARE(offset1.date(), offsetDate);
+ QCOMPARE(offset1.time(), offsetTime);
+
+ QDateTime offset2(offsetDate, offsetTime, Qt::OffsetFromUTC, 0);
+ QCOMPARE(offset2.timeSpec(), Qt::UTC);
+ QCOMPARE(offset2.offsetFromUtc(), 0);
+ QCOMPARE(offset2.date(), offsetDate);
+ QCOMPARE(offset2.time(), offsetTime);
+
+ QDateTime offset3(offsetDate, offsetTime, Qt::OffsetFromUTC, 60 * 60);
+ QCOMPARE(offset3.timeSpec(), Qt::OffsetFromUTC);
+ QCOMPARE(offset3.offsetFromUtc(), 60 * 60);
+ QCOMPARE(offset3.date(), offsetDate);
+ QCOMPARE(offset3.time(), offsetTime);
+
+ QDateTime offset4(offsetDate, QTime(), Qt::OffsetFromUTC, 60 * 60);
+ QCOMPARE(offset4.timeSpec(), Qt::OffsetFromUTC);
+ QCOMPARE(offset4.offsetFromUtc(), 60 * 60);
+ QCOMPARE(offset4.date(), offsetDate);
+ QCOMPARE(offset4.time(), QTime(0, 0, 0));
+}
+
+void tst_QDateTime::operator_eq()
+{
+ QDateTime dt1(QDate(2004, 3, 24), QTime(23, 45, 57), Qt::UTC);
+ QDateTime dt2(QDate(2005, 3, 11), QTime(), Qt::UTC);
+ dt2 = dt1;
+ QVERIFY(dt1 == dt2);
+}
+
+void tst_QDateTime::isNull()
+{
+ QDateTime dt1;
+ QVERIFY(dt1.isNull());
+ dt1.setDate(QDate());
+ QVERIFY(dt1.isNull());
+ dt1.setTime(QTime());
+ QVERIFY(dt1.isNull());
+ dt1.setTimeSpec(Qt::UTC);
+ QVERIFY(dt1.isNull()); // maybe it should return false?
+
+ dt1.setDate(QDate(2004, 1, 2));
+ QVERIFY(!dt1.isNull());
+ dt1.setTime(QTime(12, 34, 56));
+ QVERIFY(!dt1.isNull());
+ dt1.setTime(QTime());
+ QVERIFY(!dt1.isNull());
+}
+
+void tst_QDateTime::isValid()
+{
+ QDateTime dt1;
+ QVERIFY(!dt1.isValid());
+ dt1.setDate(QDate());
+ QVERIFY(!dt1.isValid());
+ dt1.setTime(QTime());
+ QVERIFY(!dt1.isValid());
+ dt1.setTimeSpec(Qt::UTC);
+ QVERIFY(!dt1.isValid());
+
+ dt1.setDate(QDate(2004, 1, 2));
+ QVERIFY(dt1.isValid());
+ dt1.setDate(QDate());
+ QVERIFY(!dt1.isValid());
+ dt1.setTime(QTime(12, 34, 56));
+ QVERIFY(!dt1.isValid());
+ dt1.setTime(QTime());
+ QVERIFY(!dt1.isValid());
+}
+
+void tst_QDateTime::date()
+{
+ QDateTime dt1(QDate(2004, 3, 24), QTime(23, 45, 57), Qt::LocalTime);
+ QCOMPARE(dt1.date(), QDate(2004, 3, 24));
+
+ QDateTime dt2(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::LocalTime);
+ QCOMPARE(dt2.date(), QDate(2004, 3, 25));
+
+ QDateTime dt3(QDate(2004, 3, 24), QTime(23, 45, 57), Qt::UTC);
+ QCOMPARE(dt3.date(), QDate(2004, 3, 24));
+
+ QDateTime dt4(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::UTC);
+ QCOMPARE(dt4.date(), QDate(2004, 3, 25));
+}
+
+void tst_QDateTime::time()
+{
+ QDateTime dt1(QDate(2004, 3, 24), QTime(23, 45, 57), Qt::LocalTime);
+ QCOMPARE(dt1.time(), QTime(23, 45, 57));
+
+ QDateTime dt2(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::LocalTime);
+ QCOMPARE(dt2.time(), QTime(0, 45, 57));
+
+ QDateTime dt3(QDate(2004, 3, 24), QTime(23, 45, 57), Qt::UTC);
+ QCOMPARE(dt3.time(), QTime(23, 45, 57));
+
+ QDateTime dt4(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::UTC);
+ QCOMPARE(dt4.time(), QTime(0, 45, 57));
+}
+
+void tst_QDateTime::timeSpec()
+{
+ QDateTime dt1(QDate(2004, 1, 24), QTime(23, 45, 57));
+ QCOMPARE(dt1.timeSpec(), Qt::LocalTime);
+ QCOMPARE(dt1.addDays(0).timeSpec(), Qt::LocalTime);
+ QCOMPARE(dt1.addMonths(0).timeSpec(), Qt::LocalTime);
+ QCOMPARE(dt1.addMonths(6).timeSpec(), Qt::LocalTime);
+ QCOMPARE(dt1.addYears(0).timeSpec(), Qt::LocalTime);
+ QCOMPARE(dt1.addSecs(0).timeSpec(), Qt::LocalTime);
+ QCOMPARE(dt1.addSecs(86400 * 185).timeSpec(), Qt::LocalTime);
+ QCOMPARE(dt1.toTimeSpec(Qt::LocalTime).timeSpec(), Qt::LocalTime);
+ QCOMPARE(dt1.toTimeSpec(Qt::UTC).timeSpec(), Qt::UTC);
+
+ QDateTime dt2(QDate(2004, 1, 24), QTime(23, 45, 57), Qt::LocalTime);
+ QCOMPARE(dt2.timeSpec(), Qt::LocalTime);
+
+ QDateTime dt3(QDate(2004, 1, 25), QTime(0, 45, 57), Qt::UTC);
+ QCOMPARE(dt3.timeSpec(), Qt::UTC);
+
+ QDateTime dt4 = QDateTime::currentDateTime();
+ QCOMPARE(dt4.timeSpec(), Qt::LocalTime);
+}
+
+void tst_QDateTime::setDate()
+{
+ QDateTime dt1(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::UTC);
+ dt1.setDate(QDate(2004, 6, 25));
+ QCOMPARE(dt1.date(), QDate(2004, 6, 25));
+ QCOMPARE(dt1.time(), QTime(0, 45, 57));
+ QCOMPARE(dt1.timeSpec(), Qt::UTC);
+
+ QDateTime dt2(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::LocalTime);
+ dt2.setDate(QDate(2004, 6, 25));
+ QCOMPARE(dt2.date(), QDate(2004, 6, 25));
+ QCOMPARE(dt2.time(), QTime(0, 45, 57));
+ QCOMPARE(dt2.timeSpec(), Qt::LocalTime);
+
+ QDateTime dt3(QDate(4004, 3, 25), QTime(0, 45, 57), Qt::UTC);
+ dt3.setDate(QDate(4004, 6, 25));
+ QCOMPARE(dt3.date(), QDate(4004, 6, 25));
+ QCOMPARE(dt3.time(), QTime(0, 45, 57));
+ QCOMPARE(dt3.timeSpec(), Qt::UTC);
+
+ QDateTime dt4(QDate(4004, 3, 25), QTime(0, 45, 57), Qt::LocalTime);
+ dt4.setDate(QDate(4004, 6, 25));
+ QCOMPARE(dt4.date(), QDate(4004, 6, 25));
+ QCOMPARE(dt4.time(), QTime(0, 45, 57));
+ QCOMPARE(dt4.timeSpec(), Qt::LocalTime);
+
+ QDateTime dt5(QDate(1760, 3, 25), QTime(0, 45, 57), Qt::UTC);
+ dt5.setDate(QDate(1760, 6, 25));
+ QCOMPARE(dt5.date(), QDate(1760, 6, 25));
+ QCOMPARE(dt5.time(), QTime(0, 45, 57));
+ QCOMPARE(dt5.timeSpec(), Qt::UTC);
+
+ QDateTime dt6(QDate(1760, 3, 25), QTime(0, 45, 57), Qt::LocalTime);
+ dt6.setDate(QDate(1760, 6, 25));
+ QCOMPARE(dt6.date(), QDate(1760, 6, 25));
+ QCOMPARE(dt6.time(), QTime(0, 45, 57));
+ QCOMPARE(dt6.timeSpec(), Qt::LocalTime);
+}
+
+void tst_QDateTime::setTime_data()
+{
+ QTest::addColumn<QDateTime>("dateTime");
+ QTest::addColumn<QTime>("newTime");
+
+ QTest::newRow("data0") << QDateTime(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::UTC) << QTime(23, 11, 22);
+ QTest::newRow("data1") << QDateTime(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::LocalTime) << QTime(23, 11, 22);
+ QTest::newRow("data2") << QDateTime(QDate(4004, 3, 25), QTime(0, 45, 57), Qt::UTC) << QTime(23, 11, 22);
+ QTest::newRow("data3") << QDateTime(QDate(4004, 3, 25), QTime(0, 45, 57), Qt::LocalTime) << QTime(23, 11, 22);
+ QTest::newRow("data4") << QDateTime(QDate(1760, 3, 25), QTime(0, 45, 57), Qt::UTC) << QTime(23, 11, 22);
+ QTest::newRow("data5") << QDateTime(QDate(1760, 3, 25), QTime(0, 45, 57), Qt::LocalTime) << QTime(23, 11, 22);
+
+ QTest::newRow("set on std/dst") << QDateTime::currentDateTime() << QTime(23, 11, 22);
+}
+
+void tst_QDateTime::setTime()
+{
+ QFETCH(QDateTime, dateTime);
+ QFETCH(QTime, newTime);
+
+ const QDate expectedDate(dateTime.date());
+ const Qt::TimeSpec expectedTimeSpec(dateTime.timeSpec());
+
+ dateTime.setTime(newTime);
+
+ QCOMPARE(dateTime.date(), expectedDate);
+ QCOMPARE(dateTime.time(), newTime);
+ QCOMPARE(dateTime.timeSpec(), expectedTimeSpec);
+}
+
+void tst_QDateTime::setTimeSpec_data()
+{
+ QTest::addColumn<QDateTime>("dateTime");
+ QTest::addColumn<Qt::TimeSpec>("newTimeSpec");
+
+ QTest::newRow("UTC => UTC") << QDateTime(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::UTC) << Qt::UTC;
+ QTest::newRow("UTC => LocalTime") << QDateTime(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::UTC) << Qt::LocalTime;
+ QTest::newRow("UTC => OffsetFromUTC") << QDateTime(QDate(2004, 3, 25), QTime(0, 45, 57), Qt::UTC) << Qt::OffsetFromUTC;
+}
+
+void tst_QDateTime::setTimeSpec()
+{
+ QFETCH(QDateTime, dateTime);
+ QFETCH(Qt::TimeSpec, newTimeSpec);
+
+ const QDate expectedDate(dateTime.date());
+ const QTime expectedTime(dateTime.time());
+
+ dateTime.setTimeSpec(newTimeSpec);
+
+ QCOMPARE(dateTime.date(), expectedDate);
+ QCOMPARE(dateTime.time(), expectedTime);
+ if (newTimeSpec == Qt::OffsetFromUTC)
+ QCOMPARE(dateTime.timeSpec(), Qt::UTC);
+ else
+ QCOMPARE(dateTime.timeSpec(), newTimeSpec);
+}
+
+void tst_QDateTime::setSecsSinceEpoch()
+{
+ QDateTime dt1;
+ dt1.setSecsSinceEpoch(0);
+ QCOMPARE(dt1.toUTC(), QDateTime(QDate(1970, 1, 1), QTime(), Qt::UTC));
+ QCOMPARE(dt1.timeSpec(), Qt::LocalTime);
+
+ dt1.setTimeSpec(Qt::UTC);
+ dt1.setSecsSinceEpoch(0);
+ QCOMPARE(dt1, QDateTime(QDate(1970, 1, 1), QTime(), Qt::UTC));
+ QCOMPARE(dt1.timeSpec(), Qt::UTC);
+
+ dt1.setSecsSinceEpoch(123456);
+ QCOMPARE(dt1, QDateTime(QDate(1970, 1, 2), QTime(10, 17, 36), Qt::UTC));
+ if (zoneIsCET) {
+ QDateTime dt2;
+ dt2.setSecsSinceEpoch(123456);
+ QCOMPARE(dt2, QDateTime(QDate(1970, 1, 2), QTime(11, 17, 36), Qt::LocalTime));
+ }
+
+ dt1.setSecsSinceEpoch((uint)(quint32)-123456);
+ QCOMPARE(dt1, QDateTime(QDate(2106, 2, 5), QTime(20, 10, 40), Qt::UTC));
+ if (zoneIsCET) {
+ QDateTime dt2;
+ dt2.setSecsSinceEpoch((uint)(quint32)-123456);
+ QCOMPARE(dt2, QDateTime(QDate(2106, 2, 5), QTime(21, 10, 40), Qt::LocalTime));
+ }
+
+ dt1.setSecsSinceEpoch(1214567890);
+ QCOMPARE(dt1, QDateTime(QDate(2008, 6, 27), QTime(11, 58, 10), Qt::UTC));
+ if (zoneIsCET) {
+ QDateTime dt2;
+ dt2.setSecsSinceEpoch(1214567890);
+ QCOMPARE(dt2, QDateTime(QDate(2008, 6, 27), QTime(13, 58, 10), Qt::LocalTime));
+ }
+
+ dt1.setSecsSinceEpoch(0x7FFFFFFF);
+ QCOMPARE(dt1, QDateTime(QDate(2038, 1, 19), QTime(3, 14, 7), Qt::UTC));
+ if (zoneIsCET) {
+ QDateTime dt2;
+ dt2.setSecsSinceEpoch(0x7FFFFFFF);
+ QCOMPARE(dt2, QDateTime(QDate(2038, 1, 19), QTime(4, 14, 7), Qt::LocalTime));
+ }
+
+ dt1 = QDateTime(QDate(2013, 1, 1), QTime(0, 0, 0), Qt::OffsetFromUTC, 60 * 60);
+ dt1.setSecsSinceEpoch(123456);
+ QCOMPARE(dt1, QDateTime(QDate(1970, 1, 2), QTime(10, 17, 36), Qt::UTC));
+ QCOMPARE(dt1.timeSpec(), Qt::OffsetFromUTC);
+ QCOMPARE(dt1.offsetFromUtc(), 60 * 60);
+}
+
+void tst_QDateTime::setMSecsSinceEpoch_data()
+{
+ QTest::addColumn<qint64>("msecs");
+ QTest::addColumn<QDateTime>("utc");
+ QTest::addColumn<QDateTime>("cet");
+
+ QTest::newRow("zero")
+ << Q_INT64_C(0)
+ << QDateTime(QDate(1970, 1, 1), QTime(), Qt::UTC)
+ << QDateTime(QDate(1970, 1, 1), QTime(1, 0));
+ QTest::newRow("-1")
+ << Q_INT64_C(-1)
+ << QDateTime(QDate(1969, 12, 31), QTime(23, 59, 59, 999), Qt::UTC)
+ << QDateTime(QDate(1970, 1, 1), QTime(0, 59, 59, 999));
+ QTest::newRow("123456789")
+ << Q_INT64_C(123456789)
+ << QDateTime(QDate(1970, 1, 2), QTime(10, 17, 36, 789), Qt::UTC)
+ << QDateTime(QDate(1970, 1, 2), QTime(11, 17, 36, 789), Qt::LocalTime);
+ QTest::newRow("-123456789")
+ << Q_INT64_C(-123456789)
+ << QDateTime(QDate(1969, 12, 30), QTime(13, 42, 23, 211), Qt::UTC)
+ << QDateTime(QDate(1969, 12, 30), QTime(14, 42, 23, 211), Qt::LocalTime);
+ QTest::newRow("non-time_t")
+ << (Q_INT64_C(1000) << 32)
+ << QDateTime(QDate(2106, 2, 7), QTime(6, 28, 16), Qt::UTC)
+ << QDateTime(QDate(2106, 2, 7), QTime(7, 28, 16));
+ QTest::newRow("very-large")
+ << (Q_INT64_C(123456) << 32)
+ << QDateTime(QDate(18772, 8, 15), QTime(1, 8, 14, 976), Qt::UTC)
+ << QDateTime(QDate(18772, 8, 15), QTime(3, 8, 14, 976));
+ QTest::newRow("old min (Tue Nov 25 00:00:00 -4714)")
+ << Q_INT64_C(-210866716800000)
+ << QDateTime(QDate::fromJulianDay(1), QTime(), Qt::UTC)
+ << QDateTime(QDate::fromJulianDay(1), QTime(1, 0));
+ QTest::newRow("old max (Tue Jun 3 21:59:59 5874898)")
+ << Q_INT64_C(185331720376799999)
+ << QDateTime(QDate::fromJulianDay(0x7fffffff), QTime(21, 59, 59, 999), Qt::UTC)
+ << QDateTime(QDate::fromJulianDay(0x7fffffff), QTime(23, 59, 59, 999));
+ QTest::newRow("min")
+ // Use -max(), which is min() + 1, to simplify filtering out overflow cases:
+ << -std::numeric_limits<qint64>::max()
+ << QDateTime(QDate(-292275056, 5, 16), QTime(16, 47, 4, 193), Qt::UTC)
+ << QDateTime(QDate(-292275056, 5, 16), QTime(17, 47, 4, 193), Qt::LocalTime);
+ QTest::newRow("max")
+ << std::numeric_limits<qint64>::max()
+ << QDateTime(QDate(292278994, 8, 17), QTime(7, 12, 55, 807), Qt::UTC)
+ << QDateTime(QDate(292278994, 8, 17), QTime(9, 12, 55, 807), Qt::LocalTime);
+}
+
+void tst_QDateTime::setMSecsSinceEpoch()
+{
+ QFETCH(qint64, msecs);
+ QFETCH(QDateTime, utc);
+ QFETCH(QDateTime, cet);
+
+ QDateTime dt;
+ dt.setTimeSpec(Qt::UTC);
+ dt.setMSecsSinceEpoch(msecs);
+
+ QCOMPARE(dt, utc);
+ QCOMPARE(dt.date(), utc.date());
+ QCOMPARE(dt.time(), utc.time());
+ QCOMPARE(dt.timeSpec(), Qt::UTC);
+
+ {
+ QDateTime dt1 = QDateTime::fromMSecsSinceEpoch(msecs, Qt::UTC);
+ QCOMPARE(dt1, utc);
+ QCOMPARE(dt1.date(), utc.date());
+ QCOMPARE(dt1.time(), utc.time());
+ QCOMPARE(dt1.timeSpec(), Qt::UTC);
+ }
+ {
+ QDateTime dt1(utc.date(), utc.time(), Qt::UTC);
+ QCOMPARE(dt1, utc);
+ QCOMPARE(dt1.date(), utc.date());
+ QCOMPARE(dt1.time(), utc.time());
+ QCOMPARE(dt1.timeSpec(), Qt::UTC);
+ }
+ {
+ // used to fail to clear the ShortData bit, causing corruption
+ QDateTime dt1 = dt.addDays(0);
+ QCOMPARE(dt1, utc);
+ QCOMPARE(dt1.date(), utc.date());
+ QCOMPARE(dt1.time(), utc.time());
+ QCOMPARE(dt1.timeSpec(), Qt::UTC);
+ }
+
+ if (zoneIsCET) {
+ QCOMPARE(dt.toLocalTime(), cet);
+
+ // Test converting from LocalTime to UTC back to LocalTime.
+ QDateTime localDt;
+ localDt.setTimeSpec(Qt::LocalTime);
+ localDt.setMSecsSinceEpoch(msecs);
+
+ // LocalTime will overflow for max
+ if (msecs != std::numeric_limits<qint64>::max())
+ QCOMPARE(localDt, utc);
+ QCOMPARE(localDt.timeSpec(), Qt::LocalTime);
+
+ // Compare result for LocalTime to TimeZone
+ QTimeZone europe("Europe/Oslo");
+ QDateTime dt2;
+ dt2.setTimeZone(europe);
+ dt2.setMSecsSinceEpoch(msecs);
+ QCOMPARE(dt2.date(), cet.date());
+
+ // don't compare the time if the date is too early or too late: prior
+ // to 1916, timezones in Europe were not standardised and some OS APIs
+ // have hard limits. Let's restrict it to the 32-bit Unix range
+ if (dt2.date().year() >= 1970 && dt2.date().year() <= 2037)
+ QCOMPARE(dt2.time(), cet.time());
+ QCOMPARE(dt2.timeSpec(), Qt::TimeZone);
+ QCOMPARE(dt2.timeZone(), europe);
+ }
+
+ QCOMPARE(dt.toMSecsSinceEpoch(), msecs);
+
+ if (quint64(msecs / 1000) < 0xFFFFFFFF) {
+ QCOMPARE(qint64(dt.toSecsSinceEpoch()), msecs / 1000);
+ }
+
+ QDateTime reference(QDate(1970, 1, 1), QTime(), Qt::UTC);
+ QCOMPARE(dt, reference.addMSecs(msecs));
+}
+
+void tst_QDateTime::fromMSecsSinceEpoch_data()
+{
+ setMSecsSinceEpoch_data();
+}
+
+void tst_QDateTime::fromMSecsSinceEpoch()
+{
+ QFETCH(qint64, msecs);
+ QFETCH(QDateTime, utc);
+ QFETCH(QDateTime, cet);
+
+ QDateTime dtLocal = QDateTime::fromMSecsSinceEpoch(msecs, Qt::LocalTime);
+ QDateTime dtUtc = QDateTime::fromMSecsSinceEpoch(msecs, Qt::UTC);
+ QDateTime dtOffset = QDateTime::fromMSecsSinceEpoch(msecs, Qt::OffsetFromUTC, 60*60);
+
+ // LocalTime will overflow for "min" or "max" tests, depending on whether
+ // you're East or West of Greenwich. In UTC, we won't overflow.
+ if (localTimeType == LocalTimeIsUtc
+ || msecs != std::numeric_limits<qint64>::max() * localTimeType)
+ QCOMPARE(dtLocal, utc);
+
+ QCOMPARE(dtUtc, utc);
+ QCOMPARE(dtUtc.date(), utc.date());
+ QCOMPARE(dtUtc.time(), utc.time());
+
+ QCOMPARE(dtOffset, utc);
+ QCOMPARE(dtOffset.offsetFromUtc(), 60*60);
+ // // OffsetFromUTC will overflow for max
+ if (msecs != std::numeric_limits<qint64>::max())
+ QCOMPARE(dtOffset.time(), utc.time().addMSecs(60*60*1000));
+
+ if (zoneIsCET) {
+ QCOMPARE(dtLocal.toLocalTime(), cet);
+ QCOMPARE(dtUtc.toLocalTime(), cet);
+ QCOMPARE(dtOffset.toLocalTime(), cet);
+ }
+
+ // LocalTime will overflow for max
+ if (msecs != std::numeric_limits<qint64>::max())
+ QCOMPARE(dtLocal.toMSecsSinceEpoch(), msecs);
+ QCOMPARE(dtUtc.toMSecsSinceEpoch(), msecs);
+ QCOMPARE(dtOffset.toMSecsSinceEpoch(), msecs);
+
+ if (quint64(msecs / 1000) < 0xFFFFFFFF) {
+ QCOMPARE(qint64(dtLocal.toSecsSinceEpoch()), msecs / 1000);
+ QCOMPARE(qint64(dtUtc.toSecsSinceEpoch()), msecs / 1000);
+ QCOMPARE(qint64(dtOffset.toSecsSinceEpoch()), msecs / 1000);
+ }
+
+ QDateTime reference(QDate(1970, 1, 1), QTime(), Qt::UTC);
+ // LocalTime will overflow for max
+ if (msecs != std::numeric_limits<qint64>::max())
+ QCOMPARE(dtLocal, reference.addMSecs(msecs));
+ QCOMPARE(dtUtc, reference.addMSecs(msecs));
+ QCOMPARE(dtOffset, reference.addMSecs(msecs));
+}
+
+void tst_QDateTime::toString_isoDate_data()
+{
+ QTest::addColumn<QDateTime>("datetime");
+ QTest::addColumn<Qt::DateFormat>("format");
+ QTest::addColumn<QString>("expected");
+
+ QTest::newRow("localtime")
+ << QDateTime(QDate(1978, 11, 9), QTime(13, 28, 34))
+ << Qt::ISODate << QString("1978-11-09T13:28:34");
+ QTest::newRow("UTC")
+ << QDateTime(QDate(1978, 11, 9), QTime(13, 28, 34), Qt::UTC)
+ << Qt::ISODate << QString("1978-11-09T13:28:34Z");
+ QDateTime dt(QDate(1978, 11, 9), QTime(13, 28, 34));
+ dt.setOffsetFromUtc(19800);
+ QTest::newRow("positive OffsetFromUTC")
+ << dt << Qt::ISODate
+ << QString("1978-11-09T13:28:34+05:30");
+ dt.setOffsetFromUtc(-7200);
+ QTest::newRow("negative OffsetFromUTC")
+ << dt << Qt::ISODate
+ << QString("1978-11-09T13:28:34-02:00");
+ dt.setOffsetFromUtc(-900);
+ QTest::newRow("negative non-integral OffsetFromUTC")
+ << dt << Qt::ISODate
+ << QString("1978-11-09T13:28:34-00:15");
+ QTest::newRow("invalid")
+ << QDateTime(QDate(-1, 11, 9), QTime(13, 28, 34), Qt::UTC)
+ << Qt::ISODate << QString();
+ QTest::newRow("without-ms")
+ << QDateTime(QDate(1978, 11, 9), QTime(13, 28, 34, 20))
+ << Qt::ISODate << QString("1978-11-09T13:28:34");
+ QTest::newRow("with-ms")
+ << QDateTime(QDate(1978, 11, 9), QTime(13, 28, 34, 20))
+ << Qt::ISODateWithMs << QString("1978-11-09T13:28:34.020");
+}
+
+void tst_QDateTime::toString_isoDate()
+{
+ QFETCH(QDateTime, datetime);
+ QFETCH(Qt::DateFormat, format);
+ QFETCH(QString, expected);
+
+ QLocale oldLocale;
+ QLocale::setDefault(QLocale("en_US"));
+
+ QString result = datetime.toString(format);
+ QCOMPARE(result, expected);
+
+ QDateTime resultDatetime = QDateTime::fromString(result, format);
+ // If expecting invalid result the datetime may still be valid, i.e. year < 0 or > 9999
+ if (!expected.isEmpty()) {
+ QEXPECT_FAIL("without-ms", "Qt::ISODate truncates milliseconds (QTBUG-56552)", Abort);
+
+ QCOMPARE(resultDatetime, datetime);
+ QCOMPARE(resultDatetime.date(), datetime.date());
+ QCOMPARE(resultDatetime.time(), datetime.time());
+ QCOMPARE(resultDatetime.timeSpec(), datetime.timeSpec());
+ QCOMPARE(resultDatetime.offsetFromUtc(), datetime.offsetFromUtc());
+ } else {
+ QCOMPARE(resultDatetime, QDateTime());
+ }
+
+ QLocale::setDefault(oldLocale);
+}
+
+void tst_QDateTime::toString_isoDate_extra()
+{
+ QDateTime dt = QDateTime::fromMSecsSinceEpoch(0, Qt::UTC);
+ QCOMPARE(dt.toString(Qt::ISODate), QLatin1String("1970-01-01T00:00:00Z"));
+#if QT_CONFIG(timezone)
+ QTimeZone PST("America/Vancouver");
+ if (PST.isValid()) {
+ dt = QDateTime::fromMSecsSinceEpoch(0, PST);
+ QCOMPARE(dt.toString(Qt::ISODate), QLatin1String("1969-12-31T16:00:00-08:00"));
+ } else {
+ qDebug("Missed zone test: no America/Vancouver zone available");
+ }
+ QTimeZone CET("Europe/Berlin");
+ if (CET.isValid()) {
+ dt = QDateTime::fromMSecsSinceEpoch(0, CET);
+ QCOMPARE(dt.toString(Qt::ISODate), QLatin1String("1970-01-01T01:00:00+01:00"));
+ } else {
+ qDebug("Missed zone test: no Europe/Berlin zone available");
+ }
+#endif // timezone
+}
+
+#if QT_CONFIG(datestring)
+void tst_QDateTime::toString_textDate_data()
+{
+ QTest::addColumn<QDateTime>("datetime");
+ QTest::addColumn<QString>("expected");
+
+ QString wednesdayJanuary = QLocale::system().dayName(3, QLocale::ShortFormat)
+ + ' ' + QLocale::system().monthName(1, QLocale::ShortFormat);
+
+ QTest::newRow("localtime") << QDateTime(QDate(2013, 1, 2), QTime(1, 2, 3), Qt::LocalTime)
+ << wednesdayJanuary + QString(" 2 01:02:03 2013");
+ QTest::newRow("utc") << QDateTime(QDate(2013, 1, 2), QTime(1, 2, 3), Qt::UTC)
+ << wednesdayJanuary + QString(" 2 01:02:03 2013 GMT");
+ QTest::newRow("offset+") << QDateTime(QDate(2013, 1, 2), QTime(1, 2, 3), Qt::OffsetFromUTC,
+ 10 * 60 * 60)
+ << wednesdayJanuary + QString(" 2 01:02:03 2013 GMT+1000");
+ QTest::newRow("offset-") << QDateTime(QDate(2013, 1, 2), QTime(1, 2, 3), Qt::OffsetFromUTC,
+ -10 * 60 * 60)
+ << wednesdayJanuary + QString(" 2 01:02:03 2013 GMT-1000");
+ QTest::newRow("invalid") << QDateTime()
+ << QString("");
+}
+
+void tst_QDateTime::toString_textDate()
+{
+ QFETCH(QDateTime, datetime);
+ QFETCH(QString, expected);
+
+ QString result = datetime.toString(Qt::TextDate);
+ QCOMPARE(result, expected);
+
+ QDateTime resultDatetime = QDateTime::fromString(result, Qt::TextDate);
+ QCOMPARE(resultDatetime, datetime);
+ QCOMPARE(resultDatetime.date(), datetime.date());
+ QCOMPARE(resultDatetime.time(), datetime.time());
+ QCOMPARE(resultDatetime.timeSpec(), datetime.timeSpec());
+ QCOMPARE(resultDatetime.offsetFromUtc(), datetime.offsetFromUtc());
+}
+
+void tst_QDateTime::toString_textDate_extra()
+{
+ QLatin1String GMT("GMT");
+ QDateTime dt = QDateTime::fromMSecsSinceEpoch(0, Qt::LocalTime);
+ QVERIFY(!dt.toString().endsWith(GMT));
+ dt = QDateTime::fromMSecsSinceEpoch(0, Qt::UTC).toLocalTime();
+ QVERIFY(!dt.toString().endsWith(GMT));
+ if (QTimeZone::systemTimeZone().offsetFromUtc(dt))
+ QVERIFY(dt.toString() != QLatin1String("Thu Jan 1 00:00:00 1970"));
+ else
+ QCOMPARE(dt.toString(), QLatin1String("Thu Jan 1 00:00:00 1970"));
+#if QT_CONFIG(timezone)
+# if defined Q_OS_UNIX && !defined Q_OS_DARWIN && !defined Q_OS_ANDROID
+# define CORRECT_ZONE_ABBREV
+# endif // QTBUG-57320, QTBUG-57298, QTBUG-68833
+
+ QTimeZone PST("America/Vancouver");
+ if (PST.isValid()) {
+ dt = QDateTime::fromMSecsSinceEpoch(0, PST);
+# ifdef CORRECT_ZONE_ABBREV
+ QCOMPARE(dt.toString(), QLatin1String("Wed Dec 31 16:00:00 1969 PST"));
+# else
+ QVERIFY(dt.toString().startsWith(QLatin1String("Wed Dec 31 16:00:00 1969 ")));
+# endif
+ dt = dt.toLocalTime();
+ QVERIFY(!dt.toString().endsWith(GMT));
+ } else {
+ qDebug("Missed zone test: no America/Vancouver zone available");
+ }
+ QTimeZone CET("Europe/Berlin");
+ if (CET.isValid()) {
+ dt = QDateTime::fromMSecsSinceEpoch(0, CET);
+# ifdef CORRECT_ZONE_ABBREV
+ QCOMPARE(dt.toString(), QLatin1String("Thu Jan 1 01:00:00 1970 CET"));
+# else
+ QVERIFY(dt.toString().startsWith(QLatin1String("Thu Jan 1 01:00:00 1970 ")));
+# endif
+ dt = dt.toLocalTime();
+ QVERIFY(!dt.toString().endsWith(GMT));
+ } else {
+ qDebug("Missed zone test: no Europe/Berlin zone available");
+ }
+#endif // timezone
+ dt = QDateTime::fromMSecsSinceEpoch(0, Qt::UTC);
+ QVERIFY(dt.toString().endsWith(GMT));
+}
+#endif // datestring
+
+void tst_QDateTime::toString_rfcDate_data()
+{
+ QTest::addColumn<QDateTime>("dt");
+ QTest::addColumn<QString>("formatted");
+
+ if (zoneIsCET) {
+ QTest::newRow("localtime")
+ << QDateTime(QDate(1978, 11, 9), QTime(13, 28, 34))
+ << QString("09 Nov 1978 13:28:34 +0100");
+ }
+ QTest::newRow("UTC")
+ << QDateTime(QDate(1978, 11, 9), QTime(13, 28, 34), Qt::UTC)
+ << QString("09 Nov 1978 13:28:34 +0000");
+ QDateTime dt(QDate(1978, 11, 9), QTime(13, 28, 34));
+ dt.setOffsetFromUtc(19800);
+ QTest::newRow("positive OffsetFromUTC")
+ << dt
+ << QString("09 Nov 1978 13:28:34 +0530");
+ dt.setOffsetFromUtc(-7200);
+ QTest::newRow("negative OffsetFromUTC")
+ << dt
+ << QString("09 Nov 1978 13:28:34 -0200");
+ QTest::newRow("invalid")
+ << QDateTime(QDate(1978, 13, 9), QTime(13, 28, 34), Qt::UTC)
+ << QString();
+ QTest::newRow("999 milliseconds UTC")
+ << QDateTime(QDate(2000, 1, 1), QTime(13, 28, 34, 999), Qt::UTC)
+ << QString("01 Jan 2000 13:28:34 +0000");
+}
+
+void tst_QDateTime::toString_rfcDate()
+{
+ QFETCH(QDateTime, dt);
+ QFETCH(QString, formatted);
+
+ // Set to non-English locale to confirm still uses English
+ QLocale oldLocale;
+ QLocale::setDefault(QLocale("de_DE"));
+ QCOMPARE(dt.toString(Qt::RFC2822Date), formatted);
+ QLocale::setDefault(oldLocale);
+}
+
+void tst_QDateTime::toString_enumformat()
+{
+ QDateTime dt1(QDate(1995, 5, 20), QTime(12, 34, 56));
+
+ QString str1 = dt1.toString(Qt::TextDate);
+ QVERIFY(!str1.isEmpty()); // It's locale dependent everywhere
+
+ QString str2 = dt1.toString(Qt::ISODate);
+ QCOMPARE(str2, QString("1995-05-20T12:34:56"));
+
+ QString str3 = dt1.toString(Qt::LocalDate);
+ QVERIFY(!str3.isEmpty());
+ //check for date/time components in any order
+ //year may be 2 or 4 digits
+ QVERIFY(str3.contains("95"));
+ //day and month may be in numeric or word form
+ QVERIFY(str3.contains("12"));
+ QVERIFY(str3.contains("34"));
+ //seconds may be absent
+}
+
+void tst_QDateTime::addDays()
+{
+ for (int pass = 0; pass < 2; ++pass) {
+ QDateTime dt(QDate(2004, 1, 1), QTime(12, 34, 56), pass == 0 ? Qt::LocalTime : Qt::UTC);
+ dt = dt.addDays(185);
+ QVERIFY(dt.date().year() == 2004 && dt.date().month() == 7 && dt.date().day() == 4);
+ QVERIFY(dt.time().hour() == 12 && dt.time().minute() == 34 && dt.time().second() == 56
+ && dt.time().msec() == 0);
+ QCOMPARE(dt.timeSpec(), (pass == 0 ? Qt::LocalTime : Qt::UTC));
+
+ dt = dt.addDays(-185);
+ QCOMPARE(dt.date(), QDate(2004, 1, 1));
+ QCOMPARE(dt.time(), QTime(12, 34, 56));
+ }
+
+ QDateTime dt(QDate(1752, 9, 14), QTime(12, 34, 56));
+ while (dt.date().year() < 8000) {
+ int year = dt.date().year();
+ if (QDate::isLeapYear(year + 1))
+ dt = dt.addDays(366);
+ else
+ dt = dt.addDays(365);
+ QCOMPARE(dt.date(), QDate(year + 1, 9, 14));
+ QCOMPARE(dt.time(), QTime(12, 34, 56));
+ }
+
+ // Test preserves TimeSpec
+ QDateTime dt1(QDate(2013, 1, 1), QTime(0, 0, 0), Qt::UTC);
+ QDateTime dt2 = dt1.addDays(2);
+ QCOMPARE(dt2.date(), QDate(2013, 1, 3));
+ QCOMPARE(dt2.time(), QTime(0, 0, 0));
+ QCOMPARE(dt2.timeSpec(), Qt::UTC);
+
+ dt1 = QDateTime(QDate(2013, 1, 1), QTime(0, 0, 0), Qt::LocalTime);
+ dt2 = dt1.addDays(2);
+ QCOMPARE(dt2.date(), QDate(2013, 1, 3));
+ QCOMPARE(dt2.time(), QTime(0, 0, 0));
+ QCOMPARE(dt2.timeSpec(), Qt::LocalTime);
+
+ dt1 = QDateTime(QDate(2013, 1, 1), QTime(0, 0, 0), Qt::OffsetFromUTC, 60*60);
+ dt2 = dt1.addDays(2);
+ QCOMPARE(dt2.date(), QDate(2013, 1, 3));
+ QCOMPARE(dt2.time(), QTime(0, 0, 0));
+ QCOMPARE(dt2.timeSpec(), Qt::OffsetFromUTC);
+ QCOMPARE(dt2.offsetFromUtc(), 60 * 60);
+
+ // ### test invalid QDateTime()
+}
+
+
+void tst_QDateTime::addMonths_data()
+{
+ QTest::addColumn<int>("months");
+ QTest::addColumn<QDate>("resultDate");
+
+ QTest::newRow("-15") << -15 << QDate(2002, 10, 31);
+ QTest::newRow("-14") << -14 << QDate(2002, 11, 30);
+ QTest::newRow("-13") << -13 << QDate(2002, 12, 31);
+ QTest::newRow("-12") << -12 << QDate(2003, 1, 31);
+
+ QTest::newRow("-11") << -11 << QDate(2003, 2, 28);
+ QTest::newRow("-10") << -10 << QDate(2003, 3, 31);
+ QTest::newRow("-9") << -9 << QDate(2003, 4, 30);
+ QTest::newRow("-8") << -8 << QDate(2003, 5, 31);
+ QTest::newRow("-7") << -7 << QDate(2003, 6, 30);
+ QTest::newRow("-6") << -6 << QDate(2003, 7, 31);
+ QTest::newRow("-5") << -5 << QDate(2003, 8, 31);
+ QTest::newRow("-4") << -4 << QDate(2003, 9, 30);
+ QTest::newRow("-3") << -3 << QDate(2003, 10, 31);
+ QTest::newRow("-2") << -2 << QDate(2003, 11, 30);
+ QTest::newRow("-1") << -1 << QDate(2003, 12, 31);
+ QTest::newRow("0") << 0 << QDate(2004, 1, 31);
+ QTest::newRow("1") << 1 << QDate(2004, 2, 29);
+ QTest::newRow("2") << 2 << QDate(2004, 3, 31);
+ QTest::newRow("3") << 3 << QDate(2004, 4, 30);
+ QTest::newRow("4") << 4 << QDate(2004, 5, 31);
+ QTest::newRow("5") << 5 << QDate(2004, 6, 30);
+ QTest::newRow("6") << 6 << QDate(2004, 7, 31);
+ QTest::newRow("7") << 7 << QDate(2004, 8, 31);
+ QTest::newRow("8") << 8 << QDate(2004, 9, 30);
+ QTest::newRow("9") << 9 << QDate(2004, 10, 31);
+ QTest::newRow("10") << 10 << QDate(2004, 11, 30);
+ QTest::newRow("11") << 11 << QDate(2004, 12, 31);
+ QTest::newRow("12") << 12 << QDate(2005, 1, 31);
+ QTest::newRow("13") << 13 << QDate(2005, 2, 28);
+ QTest::newRow("14") << 14 << QDate(2005, 3, 31);
+ QTest::newRow("15") << 15 << QDate(2005, 4, 30);
+}
+
+void tst_QDateTime::addMonths()
+{
+ QFETCH(int, months);
+ QFETCH(QDate, resultDate);
+
+ QDate testDate(2004, 1, 31);
+ QTime testTime(12, 34, 56);
+ QDateTime start(testDate, testTime);
+ QDateTime end = start.addMonths(months);
+ QCOMPARE(end.date(), resultDate);
+ QCOMPARE(end.time(), testTime);
+ QCOMPARE(end.timeSpec(), Qt::LocalTime);
+
+ start = QDateTime(testDate, testTime, Qt::UTC);
+ end = start.addMonths(months);
+ QCOMPARE(end.date(), resultDate);
+ QCOMPARE(end.time(), testTime);
+ QCOMPARE(end.timeSpec(), Qt::UTC);
+
+ start = QDateTime(testDate, testTime, Qt::OffsetFromUTC, 60 * 60);
+ end = start.addMonths(months);
+ QCOMPARE(end.date(), resultDate);
+ QCOMPARE(end.time(), testTime);
+ QCOMPARE(end.timeSpec(), Qt::OffsetFromUTC);
+ QCOMPARE(end.offsetFromUtc(), 60 * 60);
+}
+
+void tst_QDateTime::addYears_data()
+{
+ QTest::addColumn<int>("years1");
+ QTest::addColumn<int>("years2");
+ QTest::addColumn<QDate>("startDate");
+ QTest::addColumn<QDate>("resultDate");
+
+ QTest::newRow("0") << 0 << 0 << QDate(1752, 9, 14) << QDate(1752, 9, 14);
+ QTest::newRow("4000 - 4000") << 4000 << -4000 << QDate(1752, 9, 14) << QDate(1752, 9, 14);
+ QTest::newRow("10") << 10 << 0 << QDate(1752, 9, 14) << QDate(1762, 9, 14);
+ QTest::newRow("0 leap year") << 0 << 0 << QDate(1760, 2, 29) << QDate(1760, 2, 29);
+ QTest::newRow("1 leap year") << 1 << 0 << QDate(1760, 2, 29) << QDate(1761, 2, 28);
+ QTest::newRow("2 leap year") << 2 << 0 << QDate(1760, 2, 29) << QDate(1762, 2, 28);
+ QTest::newRow("3 leap year") << 3 << 0 << QDate(1760, 2, 29) << QDate(1763, 2, 28);
+ QTest::newRow("4 leap year") << 4 << 0 << QDate(1760, 2, 29) << QDate(1764, 2, 29);
+
+ QTest::newRow("toNegative1") << -2000 << 0 << QDate(1752, 9, 14) << QDate(-249, 9, 14);
+ QTest::newRow("toNegative2") << -1752 << 0 << QDate(1752, 9, 14) << QDate(-1, 9, 14);
+ QTest::newRow("toNegative3") << -1751 << 0 << QDate(1752, 9, 14) << QDate(1, 9, 14);
+ QTest::newRow("toPositive1") << 2000 << 0 << QDate(-1752, 9, 14) << QDate(249, 9, 14);
+ QTest::newRow("toPositive2") << 1752 << 0 << QDate(-1752, 9, 14) << QDate(1, 9, 14);
+ QTest::newRow("toPositive3") << 1751 << 0 << QDate(-1752, 9, 14) << QDate(-1, 9, 14);
+}
+
+void tst_QDateTime::addYears()
+{
+ QFETCH(int, years1);
+ QFETCH(int, years2);
+ QFETCH(QDate, startDate);
+ QFETCH(QDate, resultDate);
+
+ QTime testTime(14, 25, 36);
+ QDateTime start(startDate, testTime);
+ QDateTime end = start.addYears(years1).addYears(years2);
+ QCOMPARE(end.date(), resultDate);
+ QCOMPARE(end.time(), testTime);
+ QCOMPARE(end.timeSpec(), Qt::LocalTime);
+
+ start = QDateTime(startDate, testTime, Qt::UTC);
+ end = start.addYears(years1).addYears(years2);
+ QCOMPARE(end.date(), resultDate);
+ QCOMPARE(end.time(), testTime);
+ QCOMPARE(end.timeSpec(), Qt::UTC);
+
+ start = QDateTime(startDate, testTime, Qt::OffsetFromUTC, 60 * 60);
+ end = start.addYears(years1).addYears(years2);
+ QCOMPARE(end.date(), resultDate);
+ QCOMPARE(end.time(), testTime);
+ QCOMPARE(end.timeSpec(), Qt::OffsetFromUTC);
+ QCOMPARE(end.offsetFromUtc(), 60 * 60);
+}
+
+void tst_QDateTime::addSecs_data()
+{
+ QTest::addColumn<QDateTime>("dt");
+ QTest::addColumn<int>("nsecs");
+ QTest::addColumn<QDateTime>("result");
+
+ QTime standardTime(12, 34, 56);
+ QTime daylightTime(13, 34, 56);
+
+ QTest::newRow("utc0") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::UTC) << 86400
+ << QDateTime(QDate(2004, 1, 2), standardTime, Qt::UTC);
+ QTest::newRow("utc1") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::UTC) << (86400 * 185)
+ << QDateTime(QDate(2004, 7, 4), standardTime, Qt::UTC);
+ QTest::newRow("utc2") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::UTC) << (86400 * 366)
+ << QDateTime(QDate(2005, 1, 1), standardTime, Qt::UTC);
+ QTest::newRow("utc3") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::UTC) << 86400
+ << QDateTime(QDate(1760, 1, 2), standardTime, Qt::UTC);
+ QTest::newRow("utc4") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::UTC) << (86400 * 185)
+ << QDateTime(QDate(1760, 7, 4), standardTime, Qt::UTC);
+ QTest::newRow("utc5") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::UTC) << (86400 * 366)
+ << QDateTime(QDate(1761, 1, 1), standardTime, Qt::UTC);
+ QTest::newRow("utc6") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::UTC) << 86400
+ << QDateTime(QDate(4000, 1, 2), standardTime, Qt::UTC);
+ QTest::newRow("utc7") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::UTC) << (86400 * 185)
+ << QDateTime(QDate(4000, 7, 4), standardTime, Qt::UTC);
+ QTest::newRow("utc8") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::UTC) << (86400 * 366)
+ << QDateTime(QDate(4001, 1, 1), standardTime, Qt::UTC);
+ QTest::newRow("utc9") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::UTC) << 0
+ << QDateTime(QDate(4000, 1, 1), standardTime, Qt::UTC);
+
+ if (zoneIsCET) {
+ QTest::newRow("cet0") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::LocalTime) << 86400
+ << QDateTime(QDate(2004, 1, 2), standardTime, Qt::LocalTime);
+ QTest::newRow("cet1") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::LocalTime) << (86400 * 185)
+ << QDateTime(QDate(2004, 7, 4), daylightTime, Qt::LocalTime);
+ QTest::newRow("cet2") << QDateTime(QDate(2004, 1, 1), standardTime, Qt::LocalTime) << (86400 * 366)
+ << QDateTime(QDate(2005, 1, 1), standardTime, Qt::LocalTime);
+ QTest::newRow("cet3") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::LocalTime) << 86400
+ << QDateTime(QDate(1760, 1, 2), standardTime, Qt::LocalTime);
+ QTest::newRow("cet4") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::LocalTime) << (86400 * 185)
+ << QDateTime(QDate(1760, 7, 4), standardTime, Qt::LocalTime);
+ QTest::newRow("cet5") << QDateTime(QDate(1760, 1, 1), standardTime, Qt::LocalTime) << (86400 * 366)
+ << QDateTime(QDate(1761, 1, 1), standardTime, Qt::LocalTime);
+ QTest::newRow("cet6") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::LocalTime) << 86400
+ << QDateTime(QDate(4000, 1, 2), standardTime, Qt::LocalTime);
+ QTest::newRow("cet7") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::LocalTime) << (86400 * 185)
+ << QDateTime(QDate(4000, 7, 4), daylightTime, Qt::LocalTime);
+ QTest::newRow("cet8") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::LocalTime) << (86400 * 366)
+ << QDateTime(QDate(4001, 1, 1), standardTime, Qt::LocalTime);
+ QTest::newRow("cet9") << QDateTime(QDate(4000, 1, 1), standardTime, Qt::LocalTime) << 0
+ << QDateTime(QDate(4000, 1, 1), standardTime, Qt::LocalTime);
+ }
+
+ // Year sign change
+ QTest::newRow("toNegative") << QDateTime(QDate(1, 1, 1), QTime(0, 0, 0), Qt::UTC)
+ << -1
+ << QDateTime(QDate(-1, 12, 31), QTime(23, 59, 59), Qt::UTC);
+ QTest::newRow("toPositive") << QDateTime(QDate(-1, 12, 31), QTime(23, 59, 59), Qt::UTC)
+ << 1
+ << QDateTime(QDate(1, 1, 1), QTime(0, 0, 0), Qt::UTC);
+
+ QTest::newRow("invalid") << invalidDateTime() << 1 << invalidDateTime();
+
+ // Check Offset details are preserved
+ QTest::newRow("offset0") << QDateTime(QDate(2013, 1, 1), QTime(1, 2, 3),
+ Qt::OffsetFromUTC, 60 * 60)
+ << 60 * 60
+ << QDateTime(QDate(2013, 1, 1), QTime(2, 2, 3),
+ Qt::OffsetFromUTC, 60 * 60);
+}
+
+void tst_QDateTime::addSecs()
+{
+ QFETCH(QDateTime, dt);
+ QFETCH(int, nsecs);
+ QFETCH(QDateTime, result);
+ QDateTime test = dt.addSecs(nsecs);
+ QCOMPARE(test, result);
+ QCOMPARE(test.timeSpec(), dt.timeSpec());
+ if (test.timeSpec() == Qt::OffsetFromUTC)
+ QCOMPARE(test.offsetFromUtc(), dt.offsetFromUtc());
+ QCOMPARE(result.addSecs(-nsecs), dt);
+}
+
+void tst_QDateTime::addMSecs_data()
+{
+ addSecs_data();
+}
+
+void tst_QDateTime::addMSecs()
+{
+ QFETCH(QDateTime, dt);
+ QFETCH(int, nsecs);
+ QFETCH(QDateTime, result);
+
+ QDateTime test = dt.addMSecs(qint64(nsecs) * 1000);
+ QCOMPARE(test, result);
+ QCOMPARE(test.timeSpec(), dt.timeSpec());
+ if (test.timeSpec() == Qt::OffsetFromUTC)
+ QCOMPARE(test.offsetFromUtc(), dt.offsetFromUtc());
+ QCOMPARE(result.addMSecs(qint64(-nsecs) * 1000), dt);
+}
+
+void tst_QDateTime::toTimeSpec_data()
+{
+ QTest::addColumn<QDateTime>("fromUtc");
+ QTest::addColumn<QDateTime>("fromLocal");
+
+ QTime utcTime(4, 20, 30);
+ QTime localStandardTime(5, 20, 30);
+ QTime localDaylightTime(6, 20, 30);
+
+ QTest::newRow("winter1") << QDateTime(QDate(2004, 1, 1), utcTime, Qt::UTC)
+ << QDateTime(QDate(2004, 1, 1), localStandardTime, Qt::LocalTime);
+ QTest::newRow("winter2") << QDateTime(QDate(2004, 2, 29), utcTime, Qt::UTC)
+ << QDateTime(QDate(2004, 2, 29), localStandardTime, Qt::LocalTime);
+ QTest::newRow("winter3") << QDateTime(QDate(1760, 2, 29), utcTime, Qt::UTC)
+ << QDateTime(QDate(1760, 2, 29), localStandardTime, Qt::LocalTime);
+ QTest::newRow("winter4") << QDateTime(QDate(6000, 2, 29), utcTime, Qt::UTC)
+ << QDateTime(QDate(6000, 2, 29), localStandardTime, Qt::LocalTime);
+
+ // Test mktime boundaries (1970 - 2038) and adjustDate().
+ QTest::newRow("1969/12/31 23:00 UTC")
+ << QDateTime(QDate(1969, 12, 31), QTime(23, 0, 0), Qt::UTC)
+ << QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0), Qt::LocalTime);
+ QTest::newRow("2037/12/31 23:00 UTC")
+ << QDateTime(QDate(2037, 12, 31), QTime(23, 0, 0), Qt::UTC)
+ << QDateTime(QDate(2038, 1, 1), QTime(0, 0, 0), Qt::LocalTime);
+
+ QTest::newRow("-271821/4/20 00:00 UTC (JavaScript min date, start of day)")
+ << QDateTime(QDate(-271821, 4, 20), QTime(0, 0, 0), Qt::UTC)
+ << QDateTime(QDate(-271821, 4, 20), QTime(1, 0, 0), Qt::LocalTime);
+ QTest::newRow("-271821/4/20 23:00 UTC (JavaScript min date, end of day)")
+ << QDateTime(QDate(-271821, 4, 20), QTime(23, 0, 0), Qt::UTC)
+ << QDateTime(QDate(-271821, 4, 21), QTime(0, 0, 0), Qt::LocalTime);
+
+ if (zoneIsCET) {
+ QTest::newRow("summer1") << QDateTime(QDate(2004, 6, 30), utcTime, Qt::UTC)
+ << QDateTime(QDate(2004, 6, 30), localDaylightTime, Qt::LocalTime);
+ QTest::newRow("summer2") << QDateTime(QDate(1760, 6, 30), utcTime, Qt::UTC)
+ << QDateTime(QDate(1760, 6, 30), localStandardTime, Qt::LocalTime);
+ QTest::newRow("summer3") << QDateTime(QDate(4000, 6, 30), utcTime, Qt::UTC)
+ << QDateTime(QDate(4000, 6, 30), localDaylightTime, Qt::LocalTime);
+
+ QTest::newRow("275760/9/23 00:00 UTC (JavaScript max date, start of day)")
+ << QDateTime(QDate(275760, 9, 23), QTime(0, 0, 0), Qt::UTC)
+ << QDateTime(QDate(275760, 9, 23), QTime(2, 0, 0), Qt::LocalTime);
+
+ QTest::newRow("275760/9/23 22:00 UTC (JavaScript max date, end of day)")
+ << QDateTime(QDate(275760, 9, 23), QTime(22, 0, 0), Qt::UTC)
+ << QDateTime(QDate(275760, 9, 24), QTime(0, 0, 0), Qt::LocalTime);
+ }
+
+ QTest::newRow("msec") << QDateTime(QDate(4000, 6, 30), utcTime.addMSecs(1), Qt::UTC)
+ << QDateTime(QDate(4000, 6, 30), localDaylightTime.addMSecs(1), Qt::LocalTime);
+}
+
+void tst_QDateTime::toTimeSpec()
+{
+ if (zoneIsCET) {
+ QFETCH(QDateTime, fromUtc);
+ QFETCH(QDateTime, fromLocal);
+
+ QDateTime utcToUtc = fromUtc.toTimeSpec(Qt::UTC);
+ QDateTime localToLocal = fromLocal.toTimeSpec(Qt::LocalTime);
+ QDateTime utcToLocal = fromUtc.toTimeSpec(Qt::LocalTime);
+ QDateTime localToUtc = fromLocal.toTimeSpec(Qt::UTC);
+ QDateTime utcToOffset = fromUtc.toTimeSpec(Qt::OffsetFromUTC);
+ QDateTime localToOffset = fromLocal.toTimeSpec(Qt::OffsetFromUTC);
+
+ QCOMPARE(utcToUtc, fromUtc);
+ QCOMPARE(utcToUtc.date(), fromUtc.date());
+ QCOMPARE(utcToUtc.time(), fromUtc.time());
+ QCOMPARE(utcToUtc.timeSpec(), Qt::UTC);
+
+ QCOMPARE(localToLocal, fromLocal);
+ QCOMPARE(localToLocal.date(), fromLocal.date());
+ QCOMPARE(localToLocal.time(), fromLocal.time());
+ QCOMPARE(localToLocal.timeSpec(), Qt::LocalTime);
+
+ QCOMPARE(utcToLocal, fromLocal);
+ QCOMPARE(utcToLocal.date(), fromLocal.date());
+ QCOMPARE(utcToLocal.time(), fromLocal.time());
+ QCOMPARE(utcToLocal.timeSpec(), Qt::LocalTime);
+ QCOMPARE(utcToLocal.toTimeSpec(Qt::UTC), fromUtc);
+
+ QCOMPARE(localToUtc, fromUtc);
+ QCOMPARE(localToUtc.date(), fromUtc.date());
+ QCOMPARE(localToUtc.time(), fromUtc.time());
+ QCOMPARE(localToUtc.timeSpec(), Qt::UTC);
+ QCOMPARE(localToUtc.toTimeSpec(Qt::LocalTime), fromLocal);
+
+ QCOMPARE(utcToUtc, localToUtc);
+ QCOMPARE(utcToUtc.date(), localToUtc.date());
+ QCOMPARE(utcToUtc.time(), localToUtc.time());
+ QCOMPARE(utcToUtc.timeSpec(), Qt::UTC);
+
+ QCOMPARE(utcToLocal, localToLocal);
+ QCOMPARE(utcToLocal.date(), localToLocal.date());
+ QCOMPARE(utcToLocal.time(), localToLocal.time());
+ QCOMPARE(utcToLocal.timeSpec(), Qt::LocalTime);
+
+ // OffsetToUTC becomes UTC
+ QCOMPARE(utcToOffset, fromUtc);
+ QCOMPARE(utcToOffset.date(), fromUtc.date());
+ QCOMPARE(utcToOffset.time(), fromUtc.time());
+ QCOMPARE(utcToOffset.timeSpec(), Qt::UTC);
+ QCOMPARE(utcToOffset.toTimeSpec(Qt::UTC), fromUtc);
+
+ QCOMPARE(localToOffset, fromUtc);
+ QCOMPARE(localToOffset.date(), fromUtc.date());
+ QCOMPARE(localToOffset.time(), fromUtc.time());
+ QCOMPARE(localToOffset.timeSpec(), Qt::UTC);
+ QCOMPARE(localToOffset.toTimeSpec(Qt::LocalTime), fromLocal);
+ } else {
+ QSKIP("Not tested with timezone other than Central European (CET/CEST)");
+ }
+}
+
+void tst_QDateTime::toLocalTime_data()
+{
+ toTimeSpec_data();
+}
+
+void tst_QDateTime::toLocalTime()
+{
+ if (zoneIsCET) {
+ QFETCH(QDateTime, fromUtc);
+ QFETCH(QDateTime, fromLocal);
+
+ QCOMPARE(fromLocal.toLocalTime(), fromLocal);
+ QCOMPARE(fromUtc.toLocalTime(), fromLocal);
+ QCOMPARE(fromUtc.toLocalTime(), fromLocal.toLocalTime());
+ } else {
+ QSKIP("Not tested with timezone other than Central European (CET/CEST)");
+ }
+}
+
+void tst_QDateTime::toUTC_data()
+{
+ toTimeSpec_data();
+}
+
+void tst_QDateTime::toUTC()
+{
+ if (zoneIsCET) {
+ QFETCH(QDateTime, fromUtc);
+ QFETCH(QDateTime, fromLocal);
+
+ QCOMPARE(fromUtc.toUTC(), fromUtc);
+ QCOMPARE(fromLocal.toUTC(), fromUtc);
+ QCOMPARE(fromUtc.toUTC(), fromLocal.toUTC());
+ } else {
+ QSKIP("Not tested with timezone other than Central European (CET/CEST)");
+ }
+
+ QDateTime dt = QDateTime::currentDateTime();
+ if(dt.time().msec() == 0){
+ dt.setTime(dt.time().addMSecs(1));
+ }
+ QString s = dt.toString("zzz");
+ QString t = dt.toUTC().toString("zzz");
+ QCOMPARE(s, t);
+}
+
+void tst_QDateTime::daysTo()
+{
+ QDateTime dt1(QDate(1760, 1, 2), QTime());
+ QDateTime dt2(QDate(1760, 2, 2), QTime());
+ QDateTime dt3(QDate(1760, 3, 2), QTime());
+
+ QCOMPARE(dt1.daysTo(dt2), (qint64) 31);
+ QCOMPARE(dt1.addDays(31), dt2);
+
+ QCOMPARE(dt2.daysTo(dt3), (qint64) 29);
+ QCOMPARE(dt2.addDays(29), dt3);
+
+ QCOMPARE(dt1.daysTo(dt3), (qint64) 60);
+ QCOMPARE(dt1.addDays(60), dt3);
+
+ QCOMPARE(dt2.daysTo(dt1), (qint64) -31);
+ QCOMPARE(dt2.addDays(-31), dt1);
+
+ QCOMPARE(dt3.daysTo(dt2), (qint64) -29);
+ QCOMPARE(dt3.addDays(-29), dt2);
+
+ QCOMPARE(dt3.daysTo(dt1), (qint64) -60);
+ QCOMPARE(dt3.addDays(-60), dt1);
+}
+
+void tst_QDateTime::secsTo_data()
+{
+ addSecs_data();
+
+ QTest::newRow("disregard milliseconds #1")
+ << QDateTime(QDate(2012, 3, 7), QTime(0, 58, 0, 0)) << 60
+ << QDateTime(QDate(2012, 3, 7), QTime(0, 59, 0, 400));
+
+ QTest::newRow("disregard milliseconds #2")
+ << QDateTime(QDate(2012, 3, 7), QTime(0, 59, 0, 0)) << 60
+ << QDateTime(QDate(2012, 3, 7), QTime(1, 0, 0, 400));
+}
+
+void tst_QDateTime::secsTo()
+{
+ QFETCH(QDateTime, dt);
+ QFETCH(int, nsecs);
+ QFETCH(QDateTime, result);
+
+ if (dt.isValid()) {
+ QCOMPARE(dt.secsTo(result), (qint64)nsecs);
+ QCOMPARE(result.secsTo(dt), (qint64)-nsecs);
+ QVERIFY((dt == result) == (0 == nsecs));
+ QVERIFY((dt != result) == (0 != nsecs));
+ QVERIFY((dt < result) == (0 < nsecs));
+ QVERIFY((dt <= result) == (0 <= nsecs));
+ QVERIFY((dt > result) == (0 > nsecs));
+ QVERIFY((dt >= result) == (0 >= nsecs));
+ } else {
+ QVERIFY(dt.secsTo(result) == 0);
+ QVERIFY(result.secsTo(dt) == 0);
+ }
+}
+
+void tst_QDateTime::msecsTo_data()
+{
+ addMSecs_data();
+}
+
+void tst_QDateTime::msecsTo()
+{
+ QFETCH(QDateTime, dt);
+ QFETCH(int, nsecs);
+ QFETCH(QDateTime, result);
+
+ if (dt.isValid()) {
+ QCOMPARE(dt.msecsTo(result), qint64(nsecs) * 1000);
+ QCOMPARE(result.msecsTo(dt), -qint64(nsecs) * 1000);
+ QVERIFY((dt == result) == (0 == (qint64(nsecs) * 1000)));
+ QVERIFY((dt != result) == (0 != (qint64(nsecs) * 1000)));
+ QVERIFY((dt < result) == (0 < (qint64(nsecs) * 1000)));
+ QVERIFY((dt <= result) == (0 <= (qint64(nsecs) * 1000)));
+ QVERIFY((dt > result) == (0 > (qint64(nsecs) * 1000)));
+ QVERIFY((dt >= result) == (0 >= (qint64(nsecs) * 1000)));
+ } else {
+ QVERIFY(dt.msecsTo(result) == 0);
+ QVERIFY(result.msecsTo(dt) == 0);
+ }
+}
+
+void tst_QDateTime::currentDateTime()
+{
+ time_t buf1, buf2;
+ ::time(&buf1);
+ QDateTime lowerBound;
+ lowerBound.setSecsSinceEpoch(buf1);
+
+ QDateTime dt1 = QDateTime::currentDateTime();
+ QDateTime dt2 = QDateTime::currentDateTime().toLocalTime();
+ QDateTime dt3 = QDateTime::currentDateTime().toUTC();
+
+ ::time(&buf2);
+
+ QDateTime upperBound;
+ upperBound.setSecsSinceEpoch(buf2);
+ // Note we must add 2 seconds here because time() may return up to
+ // 1 second difference from the more accurate method used by QDateTime::currentDateTime()
+ upperBound = upperBound.addSecs(2);
+
+ QString details = QString("\n"
+ "lowerBound: %1\n"
+ "dt1: %2\n"
+ "dt2: %3\n"
+ "dt3: %4\n"
+ "upperBound: %5\n")
+ .arg(lowerBound.toSecsSinceEpoch())
+ .arg(dt1.toSecsSinceEpoch())
+ .arg(dt2.toSecsSinceEpoch())
+ .arg(dt3.toSecsSinceEpoch())
+ .arg(upperBound.toSecsSinceEpoch());
+
+ QVERIFY2(lowerBound < upperBound, qPrintable(details));
+
+ QVERIFY2(lowerBound <= dt1, qPrintable(details));
+ QVERIFY2(dt1 < upperBound, qPrintable(details));
+ QVERIFY2(lowerBound <= dt2, qPrintable(details));
+ QVERIFY2(dt2 < upperBound, qPrintable(details));
+ QVERIFY2(lowerBound <= dt3, qPrintable(details));
+ QVERIFY2(dt3 < upperBound, qPrintable(details));
+
+ QVERIFY(dt1.timeSpec() == Qt::LocalTime);
+ QVERIFY(dt2.timeSpec() == Qt::LocalTime);
+ QVERIFY(dt3.timeSpec() == Qt::UTC);
+}
+
+void tst_QDateTime::currentDateTimeUtc()
+{
+ time_t buf1, buf2;
+ ::time(&buf1);
+
+ QDateTime lowerBound;
+ lowerBound.setSecsSinceEpoch(buf1);
+
+ QDateTime dt1 = QDateTime::currentDateTimeUtc();
+ QDateTime dt2 = QDateTime::currentDateTimeUtc().toLocalTime();
+ QDateTime dt3 = QDateTime::currentDateTimeUtc().toUTC();
+
+ ::time(&buf2);
+
+ QDateTime upperBound;
+ upperBound.setSecsSinceEpoch(buf2);
+ // Note we must add 2 seconds here because time() may return up to
+ // 1 second difference from the more accurate method used by QDateTime::currentDateTime()
+ upperBound = upperBound.addSecs(2);
+
+ QString details = QString("\n"
+ "lowerBound: %1\n"
+ "dt1: %2\n"
+ "dt2: %3\n"
+ "dt3: %4\n"
+ "upperBound: %5\n")
+ .arg(lowerBound.toSecsSinceEpoch())
+ .arg(dt1.toSecsSinceEpoch())
+ .arg(dt2.toSecsSinceEpoch())
+ .arg(dt3.toSecsSinceEpoch())
+ .arg(upperBound.toSecsSinceEpoch());
+
+ QVERIFY2(lowerBound < upperBound, qPrintable(details));
+
+ QVERIFY2(lowerBound <= dt1, qPrintable(details));
+ QVERIFY2(dt1 < upperBound, qPrintable(details));
+ QVERIFY2(lowerBound <= dt2, qPrintable(details));
+ QVERIFY2(dt2 < upperBound, qPrintable(details));
+ QVERIFY2(lowerBound <= dt3, qPrintable(details));
+ QVERIFY2(dt3 < upperBound, qPrintable(details));
+
+ QVERIFY(dt1.timeSpec() == Qt::UTC);
+ QVERIFY(dt2.timeSpec() == Qt::LocalTime);
+ QVERIFY(dt3.timeSpec() == Qt::UTC);
+}
+
+void tst_QDateTime::currentDateTimeUtc2()
+{
+ QDateTime local, utc;
+ qint64 msec;
+
+ // check that we got all down to the same milliseconds
+ int i = 20;
+ bool ok = false;
+ do {
+ local = QDateTime::currentDateTime();
+ utc = QDateTime::currentDateTimeUtc();
+ msec = QDateTime::currentMSecsSinceEpoch();
+ ok = local.time().msec() == utc.time().msec()
+ && utc.time().msec() == (msec % 1000);
+ } while (--i && !ok);
+
+ if (!i)
+ QSKIP("Failed to get the dates within 1 ms of each other");
+
+ // seconds and milliseconds should be the same:
+ QCOMPARE(utc.time().second(), local.time().second());
+ QCOMPARE(utc.time().msec(), local.time().msec());
+ QCOMPARE(msec % 1000, qint64(local.time().msec()));
+ QCOMPARE(msec / 1000 % 60, qint64(local.time().second()));
+
+ // the two dates should be equal, actually
+ QCOMPARE(local.toUTC(), utc);
+ QCOMPARE(utc.toLocalTime(), local);
+
+ // and finally, the SecsSinceEpoch should equal our number
+ QCOMPARE(qint64(utc.toSecsSinceEpoch()), msec / 1000);
+ QCOMPARE(qint64(local.toSecsSinceEpoch()), msec / 1000);
+ QCOMPARE(utc.toMSecsSinceEpoch(), msec);
+ QCOMPARE(local.toMSecsSinceEpoch(), msec);
+}
+
+void tst_QDateTime::toSecsSinceEpoch_data()
+{
+ QTest::addColumn<QString>("dateTimeStr");
+ QTest::addColumn<bool>("res");
+
+ QTest::newRow( "data1" ) << str( 1800, 1, 1, 12, 0, 0 ) << false;
+ QTest::newRow( "data2" ) << str( 1969, 1, 1, 12, 0, 0 ) << false;
+ QTest::newRow( "data3" ) << str( 2002, 1, 1, 12, 0, 0 ) << true;
+ QTest::newRow( "data4" ) << str( 2002, 6, 1, 12, 0, 0 ) << true;
+ QTest::newRow( "data5" ) << QString("INVALID") << false;
+ QTest::newRow( "data6" ) << str( 2038, 1, 1, 12, 0, 0 ) << true;
+ QTest::newRow( "data7" ) << str( 2063, 4, 5, 12, 0, 0 ) << true; // the day of First Contact
+ QTest::newRow( "data8" ) << str( 2107, 1, 1, 12, 0, 0 )
+ << bool( sizeof(uint) > 32 && sizeof(time_t) > 32 );
+}
+
+void tst_QDateTime::toSecsSinceEpoch()
+{
+ QFETCH( QString, dateTimeStr );
+ QDateTime datetime = dt( dateTimeStr );
+
+ qint64 asSecsSinceEpoch = datetime.toSecsSinceEpoch();
+ uint asTime_t = datetime.toTime_t();
+ QFETCH( bool, res );
+ if (res) {
+ QVERIFY( asTime_t != (uint)-1 );
+ } else {
+ QVERIFY( asTime_t == (uint)-1 );
+ }
+ QCOMPARE(asSecsSinceEpoch, datetime.toMSecsSinceEpoch() / 1000);
+
+ if ( asTime_t != (uint) -1 ) {
+ QDateTime datetime2 = QDateTime::fromTime_t( asTime_t );
+ QCOMPARE(datetime, datetime2);
+ }
+ QDateTime datetime2 = QDateTime::fromSecsSinceEpoch(asSecsSinceEpoch);
+ QCOMPARE(datetime, datetime2);
+}
+
+void tst_QDateTime::daylightSavingsTimeChange_data()
+{
+ QTest::addColumn<QDate>("inDST");
+ QTest::addColumn<QDate>("outDST");
+ QTest::addColumn<int>("days"); // from in to out; -ve if reversed
+ QTest::addColumn<int>("months");
+
+ QTest::newRow("Autumn") << QDate(2006, 8, 1) << QDate(2006, 12, 1)
+ << 122 << 4;
+
+ QTest::newRow("Spring") << QDate(2006, 5, 1) << QDate(2006, 2, 1)
+ << -89 << -3;
+}
+
+void tst_QDateTime::daylightSavingsTimeChange()
+{
+ // This has grown from a regression test for an old bug where starting with
+ // a date in DST and then moving to a date outside it (or vice-versa) caused
+ // 1-hour jumps in time when addSecs() was called.
+ //
+ // The bug was caused by QDateTime knowing more than it lets show.
+ // Internally, if it knows, QDateTime stores a flag indicating if the time is
+ // DST or not. If it doesn't, it sets to "LocalUnknown". The problem happened
+ // because some functions did not reset the flag when moving in or out of DST.
+
+ // WARNING: This only tests anything if there's a Daylight Savings Time change
+ // in the current locale between inDST and outDST.
+ // This is true for Central European Time and may be elsewhere.
+
+ QFETCH(QDate, inDST);
+ QFETCH(QDate, outDST);
+ QFETCH(int, days);
+ QFETCH(int, months);
+
+ // First with simple construction
+ QDateTime dt = QDateTime(outDST, QTime(0, 0, 0), Qt::LocalTime);
+ int outDSTsecs = dt.toSecsSinceEpoch();
+
+ dt.setDate(inDST);
+ dt = dt.addSecs(1);
+ QCOMPARE(dt, QDateTime(inDST, QTime(0, 0, 1)));
+
+ // now using addDays:
+ dt = dt.addDays(days).addSecs(1);
+ QCOMPARE(dt, QDateTime(outDST, QTime(0, 0, 2)));
+
+ // ... and back again:
+ dt = dt.addDays(-days).addSecs(1);
+ QCOMPARE(dt, QDateTime(inDST, QTime(0, 0, 3)));
+
+ // now using addMonths:
+ dt = dt.addMonths(months).addSecs(1);
+ QCOMPARE(dt, QDateTime(outDST, QTime(0, 0, 4)));
+
+ // ... and back again:
+ dt = dt.addMonths(-months).addSecs(1);
+ QCOMPARE(dt, QDateTime(inDST, QTime(0, 0, 5)));
+
+ // now using fromSecsSinceEpoch
+ dt = QDateTime::fromSecsSinceEpoch(outDSTsecs);
+ QCOMPARE(dt, QDateTime(outDST, QTime(0, 0, 0)));
+
+ dt.setDate(inDST);
+ dt = dt.addSecs(60);
+ QCOMPARE(dt, QDateTime(inDST, QTime(0, 1, 0)));
+
+ // using addMonths:
+ dt = dt.addMonths(months).addSecs(60);
+ QCOMPARE(dt, QDateTime(outDST, QTime(0, 2, 0)));
+ // back again:
+ dt = dt.addMonths(-months).addSecs(60);
+ QCOMPARE(dt, QDateTime(inDST, QTime(0, 3, 0)));
+
+ // using addDays:
+ dt = dt.addDays(days).addSecs(60);
+ QCOMPARE(dt, QDateTime(outDST, QTime(0, 4, 0)));
+ // back again:
+ dt = dt.addDays(-days).addSecs(60);
+ QCOMPARE(dt, QDateTime(inDST, QTime(0, 5, 0)));
+
+ // Now use the result of a UTC -> LocalTime conversion
+ dt = QDateTime(outDST, QTime(0, 0, 0), Qt::LocalTime).toUTC();
+ dt = QDateTime(dt.date(), dt.time(), Qt::UTC).toLocalTime();
+ QCOMPARE(dt, QDateTime(outDST, QTime(0, 0, 0)));
+
+ // using addDays:
+ dt = dt.addDays(-days).addSecs(3600);
+ QCOMPARE(dt, QDateTime(inDST, QTime(1, 0, 0)));
+ // back again
+ dt = dt.addDays(days).addSecs(3600);
+ QCOMPARE(dt, QDateTime(outDST, QTime(2, 0, 0)));
+
+ // using addMonths:
+ dt = dt.addMonths(-months).addSecs(3600);
+ QCOMPARE(dt, QDateTime(inDST, QTime(3, 0, 0)));
+ // back again:
+ dt = dt.addMonths(months).addSecs(3600);
+ QCOMPARE(dt, QDateTime(outDST, QTime(4, 0, 0)));
+
+ // using setDate:
+ dt.setDate(inDST);
+ dt = dt.addSecs(3600);
+ QCOMPARE(dt, QDateTime(inDST, QTime(5, 0, 0)));
+}
+
+void tst_QDateTime::springForward_data()
+{
+ QTest::addColumn<QDate>("day"); // day of DST transition
+ QTest::addColumn<QTime>("time"); // in the "missing hour"
+ QTest::addColumn<int>("step"); // days to step; +ve from before, -ve from after
+ QTest::addColumn<int>("adjust"); // minutes ahead of UTC on day stepped from
+
+ /*
+ Zone tests compare a summer and winter moment's SecsSinceEpoch to known values.
+ This could in principle be flawed (two DST-using zones in the same
+ hemisphere with the same DST and standard times but different transition
+ times) but no actual example is known where this is a problem. Please
+ document any such conflicts, if discovered.
+
+ See http://www.timeanddate.com/time/zones/ for data on more candidates to
+ test.
+ */
+
+ uint winter = QDateTime(QDate(2015, 1, 1), QTime()).toSecsSinceEpoch();
+ uint summer = QDateTime(QDate(2015, 7, 1), QTime()).toSecsSinceEpoch();
+
+ if (winter == 1420066800 && summer == 1435701600) {
+ QTest::newRow("CET from day before") << QDate(2015, 3, 29) << QTime(2, 30, 0) << 1 << 60;
+ QTest::newRow("CET from day after") << QDate(2015, 3, 29) << QTime(2, 30, 0) << -1 << 120;
+ } else if (winter == 1420063200 && summer == 1435698000) {
+ // e.g. Finland, where our CI runs ...
+ QTest::newRow("EET from day before") << QDate(2015, 3, 29) << QTime(3, 30, 0) << 1 << 120;
+ QTest::newRow("EET from day after") << QDate(2015, 3, 29) << QTime(3, 30, 0) << -1 << 180;
+ } else if (winter == 1420070400 && summer == 1435705200) {
+ // Western European Time, WET/WEST; a.k.a. GMT/BST
+ QTest::newRow("WET from day before") << QDate(2015, 3, 29) << QTime(1, 30, 0) << 1 << 0;
+ QTest::newRow("WET from day after") << QDate(2015, 3, 29) << QTime(1, 30, 0) << -1 << 60;
+ } else if (winter == 1420099200 && summer == 1435734000) {
+ // Western USA, Canada: Pacific Time (e.g. US/Pacific)
+ QTest::newRow("PT from day before") << QDate(2015, 3, 8) << QTime(2, 30, 0) << 1 << -480;
+ QTest::newRow("PT from day after") << QDate(2015, 3, 8) << QTime(2, 30, 0) << -1 << -420;
+ } else if (winter == 1420088400 && summer == 1435723200) {
+ // Eastern USA, Canada: Eastern Time (e.g. US/Eastern)
+ QTest::newRow("ET from day before") << QDate(2015, 3, 8) << QTime(2, 30, 0) << 1 << -300;
+ QTest::newRow("ET from day after") << QDate(2015, 3, 8) << QTime(2, 30, 0) << -1 << -240;
+ } else {
+ // Includes the numbers you need to test for your zone, as above:
+ QString msg(QString::fromLatin1("No spring forward test data for this TZ (%1, %2)"
+ ).arg(winter).arg(summer));
+ QSKIP(qPrintable(msg));
+ }
+}
+
+void tst_QDateTime::springForward()
+{
+ QFETCH(QDate, day);
+ QFETCH(QTime, time);
+ QFETCH(int, step);
+ QFETCH(int, adjust);
+
+ QDateTime direct = QDateTime(day.addDays(-step), time, Qt::LocalTime).addDays(step);
+ if (direct.isValid()) { // mktime() may deem a time in the gap invalid
+ QCOMPARE(direct.date(), day);
+ QCOMPARE(direct.time().minute(), time.minute());
+ QCOMPARE(direct.time().second(), time.second());
+ int off = direct.time().hour() - time.hour();
+ QVERIFY(off == 1 || off == -1);
+ // Note: function doc claims always +1, but this should be reviewed !
+ }
+
+ // Repeat, but getting there via .toLocalTime():
+ QDateTime detour = QDateTime(day.addDays(-step),
+ time.addSecs(-60 * adjust),
+ Qt::UTC).toLocalTime();
+ QCOMPARE(detour.time(), time);
+ detour = detour.addDays(step);
+ // Insist on consistency:
+ if (direct.isValid())
+ QCOMPARE(detour, direct);
+ else
+ QVERIFY(!detour.isValid());
+}
+
+void tst_QDateTime::operator_eqeq_data()
+{
+ QTest::addColumn<QDateTime>("dt1");
+ QTest::addColumn<QDateTime>("dt2");
+ QTest::addColumn<bool>("expectEqual");
+ QTest::addColumn<bool>("checkEuro");
+
+ QDateTime dateTime1(QDate(2012, 6, 20), QTime(14, 33, 2, 500));
+ QDateTime dateTime1a = dateTime1.addMSecs(1);
+ QDateTime dateTime2(QDate(2012, 20, 6), QTime(14, 33, 2, 500));
+ QDateTime dateTime2a = dateTime2.addMSecs(-1);
+ QDateTime dateTime3(QDate(1970, 1, 1), QTime(0, 0, 0, 0), Qt::UTC); // UTC epoch
+ QDateTime dateTime3a = dateTime3.addDays(1);
+ QDateTime dateTime3b = dateTime3.addDays(-1);
+ // Ensure that different times may be equal when considering timezone.
+ QDateTime dateTime3c(dateTime3.addSecs(3600));
+ dateTime3c.setOffsetFromUtc(3600);
+ QDateTime dateTime3d(dateTime3.addSecs(-3600));
+ dateTime3d.setOffsetFromUtc(-3600);
+ QDateTime dateTime3e(dateTime3.date(), dateTime3.time()); // Local time's epoch
+
+ QTest::newRow("data0") << dateTime1 << dateTime1 << true << false;
+ QTest::newRow("data1") << dateTime2 << dateTime2 << true << false;
+ QTest::newRow("data2") << dateTime1a << dateTime1a << true << false;
+ QTest::newRow("data3") << dateTime1 << dateTime2 << false << false;
+ QTest::newRow("data4") << dateTime1 << dateTime1a << false << false;
+ QTest::newRow("data5") << dateTime2 << dateTime2a << false << false;
+ QTest::newRow("data6") << dateTime2 << dateTime3 << false << false;
+ QTest::newRow("data7") << dateTime3 << dateTime3a << false << false;
+ QTest::newRow("data8") << dateTime3 << dateTime3b << false << false;
+ QTest::newRow("data9") << dateTime3a << dateTime3b << false << false;
+ QTest::newRow("data10") << dateTime3 << dateTime3c << true << false;
+ QTest::newRow("data11") << dateTime3 << dateTime3d << true << false;
+ QTest::newRow("data12") << dateTime3c << dateTime3d << true << false;
+ if (localTimeType == LocalTimeIsUtc)
+ QTest::newRow("data13") << dateTime3 << dateTime3e << true << false;
+ // ... but a zone (sometimes) ahead of or behind UTC (e.g. Europe/London)
+ // might agree with UTC about the epoch, all the same.
+
+ QTest::newRow("invalid == invalid") << invalidDateTime() << invalidDateTime() << true << false;
+ QTest::newRow("invalid == valid #1") << invalidDateTime() << dateTime1 << false << false;
+
+ if (zoneIsCET) {
+ QTest::newRow("data14") << QDateTime(QDate(2004, 1, 2), QTime(2, 2, 3), Qt::LocalTime)
+ << QDateTime(QDate(2004, 1, 2), QTime(1, 2, 3), Qt::UTC) << true << true;
+ }
+}
+
+void tst_QDateTime::operator_eqeq()
+{
+ QFETCH(QDateTime, dt1);
+ QFETCH(QDateTime, dt2);
+ QFETCH(bool, expectEqual);
+ QFETCH(bool, checkEuro);
+
+ QVERIFY(dt1 == dt1);
+ QVERIFY(!(dt1 != dt1));
+
+ QVERIFY(dt2 == dt2);
+ QVERIFY(!(dt2 != dt2));
+
+ QVERIFY(dt1 != QDateTime::currentDateTime());
+ QVERIFY(dt2 != QDateTime::currentDateTime());
+
+ QVERIFY(dt1.toUTC() == dt1.toUTC());
+
+ bool equal = dt1 == dt2;
+ QCOMPARE(equal, expectEqual);
+ bool notEqual = dt1 != dt2;
+ QCOMPARE(notEqual, !expectEqual);
+
+ if (equal)
+ QVERIFY(qHash(dt1) == qHash(dt2));
+
+ if (checkEuro && zoneIsCET) {
+ QVERIFY(dt1.toUTC() == dt2);
+ QVERIFY(dt1 == dt2.toLocalTime());
+ }
+}
+
+Q_DECLARE_METATYPE(QDataStream::Version)
+
+void tst_QDateTime::operator_insert_extract_data()
+{
+ QTest::addColumn<QDateTime>("dateTime");
+ QTest::addColumn<QString>("serialiseAs");
+ QTest::addColumn<QString>("deserialiseAs");
+ QTest::addColumn<QDataStream::Version>("dataStreamVersion");
+
+ const QDateTime positiveYear(QDateTime(QDate(2012, 8, 14), QTime(8, 0, 0), Qt::LocalTime));
+ const QDateTime negativeYear(QDateTime(QDate(-2012, 8, 14), QTime(8, 0, 0), Qt::LocalTime));
+
+ const QString westernAustralia(QString::fromLatin1("AWST-8AWDT-9,M10.5.0,M3.5.0/03:00:00"));
+ const QString hawaii(QString::fromLatin1("HAW10"));
+
+ const QDataStream tmpDataStream;
+ const int thisVersion = tmpDataStream.version();
+ for (int version = QDataStream::Qt_1_0; version <= thisVersion; ++version) {
+ const QDataStream::Version dataStreamVersion = static_cast<QDataStream::Version>(version);
+ const QByteArray vN = QByteArray::number(dataStreamVersion);
+ const QByteArray pY = positiveYear.toString().toLatin1();
+ QTest::newRow(('v' + vN + " WA => HAWAII " + pY).constData())
+ << positiveYear << westernAustralia << hawaii << dataStreamVersion;
+ QTest::newRow(('v' + vN + " WA => WA " + pY).constData())
+ << positiveYear << westernAustralia << westernAustralia << dataStreamVersion;
+ QTest::newRow(('v' + vN + " HAWAII => WA " + negativeYear.toString().toLatin1()).constData())
+ << negativeYear << hawaii << westernAustralia << dataStreamVersion;
+ QTest::newRow(('v' + vN + " HAWAII => HAWAII " + pY).constData())
+ << positiveYear << hawaii << hawaii << dataStreamVersion;
+ }
+}
+
+void tst_QDateTime::operator_insert_extract()
+{
+ QFETCH(QDateTime, dateTime);
+ QFETCH(QString, serialiseAs);
+ QFETCH(QString, deserialiseAs);
+ QFETCH(QDataStream::Version, dataStreamVersion);
+
+ // Start off in a certain timezone.
+ TimeZoneRollback useZone(serialiseAs.toLocal8Bit());
+ QDateTime dateTimeAsUTC(dateTime.toUTC());
+
+ QByteArray byteArray;
+ {
+ QDataStream dataStream(&byteArray, QIODevice::WriteOnly);
+ dataStream.setVersion(dataStreamVersion);
+ if (dataStreamVersion == QDataStream::Qt_5_0) {
+ // Qt 5 serialises as UTC and converts back to the stored timeSpec when
+ // deserialising; we don't need to do it ourselves...
+ dataStream << dateTime << dateTime;
+ } else {
+ // ... but other versions don't, so we have to here.
+ dataStream << dateTimeAsUTC << dateTimeAsUTC;
+ // We'll also make sure that a deserialised local datetime is the same
+ // time of day (potentially different UTC time), regardless of which
+ // timezone it was serialised in. E.g.: Tue Aug 14 08:00:00 2012
+ // serialised in WST should be deserialised as Tue Aug 14 08:00:00 2012
+ // HST.
+ dataStream << dateTime;
+ }
+ }
+
+ // Ensure that a change in timezone between serialisation and deserialisation
+ // still results in identical UTC-converted datetimes.
+ useZone.reset(deserialiseAs.toLocal8Bit());
+ QDateTime expectedLocalTime(dateTimeAsUTC.toLocalTime());
+ {
+ // Deserialise whole QDateTime at once.
+ QDataStream dataStream(&byteArray, QIODevice::ReadOnly);
+ dataStream.setVersion(dataStreamVersion);
+ QDateTime deserialised;
+ dataStream >> deserialised;
+
+ if (dataStreamVersion == QDataStream::Qt_5_0) {
+ // Ensure local time is still correct. Again, Qt 5 handles the timeSpec
+ // conversion (in this case, UTC => LocalTime) for us when deserialising.
+ QCOMPARE(deserialised, expectedLocalTime);
+ } else {
+ if (dataStreamVersion < QDataStream::Qt_4_0) {
+ // Versions lower than Qt 4 don't serialise the timeSpec, instead
+ // assuming that everything is LocalTime.
+ deserialised.setTimeSpec(Qt::UTC);
+ }
+ // Qt 4.* versions do serialise the timeSpec, so we only need to convert from UTC here.
+ deserialised = deserialised.toLocalTime();
+
+ QCOMPARE(deserialised, expectedLocalTime);
+ }
+ // Sanity check UTC times (operator== already converts its operands to UTC before comparing).
+ QCOMPARE(deserialised.toUTC(), expectedLocalTime.toUTC());
+
+ // Deserialise each component individually.
+ QDate deserialisedDate;
+ dataStream >> deserialisedDate;
+ QTime deserialisedTime;
+ dataStream >> deserialisedTime;
+ qint8 deserialisedSpec;
+ if (dataStreamVersion >= QDataStream::Qt_4_0)
+ dataStream >> deserialisedSpec;
+ deserialised = QDateTime(deserialisedDate, deserialisedTime, Qt::UTC);
+ if (dataStreamVersion >= QDataStream::Qt_4_0)
+ deserialised = deserialised.toTimeSpec(static_cast<Qt::TimeSpec>(deserialisedSpec));
+ // Ensure local time is still correct.
+ QCOMPARE(deserialised, expectedLocalTime);
+ // Sanity check UTC times.
+ QCOMPARE(deserialised.toUTC(), expectedLocalTime.toUTC());
+
+ if (dataStreamVersion != QDataStream::Qt_5_0) {
+ // Deserialised local datetime should be the same time of day,
+ // regardless of which timezone it was serialised in.
+ QDateTime localDeserialized;
+ dataStream >> localDeserialized;
+ QCOMPARE(localDeserialized, dateTime);
+ }
+ }
+}
+
+void tst_QDateTime::toString_strformat()
+{
+ // Most tests are in QLocale, just test that the api works.
+ QDate testDate(2013, 1, 1);
+ QTime testTime(1, 2, 3);
+ QDateTime testDateTime(testDate, testTime, Qt::UTC);
+ QCOMPARE(testDate.toString("yyyy-MM-dd"), QString("2013-01-01"));
+ QCOMPARE(testTime.toString("hh:mm:ss"), QString("01:02:03"));
+ QCOMPARE(testDateTime.toString("yyyy-MM-dd hh:mm:ss t"), QString("2013-01-01 01:02:03 UTC"));
+}
+
+void tst_QDateTime::fromStringDateFormat_data()
+{
+ QTest::addColumn<QString>("dateTimeStr");
+ QTest::addColumn<Qt::DateFormat>("dateFormat");
+ QTest::addColumn<QDateTime>("expected");
+
+ // Test Qt::TextDate format.
+ QTest::newRow("text date") << QString::fromLatin1("Tue Jun 17 08:00:10 2003")
+ << Qt::TextDate << QDateTime(QDate(2003, 6, 17), QTime(8, 0, 10, 0), Qt::LocalTime);
+ QTest::newRow("text date Year 0999") << QString::fromLatin1("Tue Jun 17 08:00:10 0999")
+ << Qt::TextDate << QDateTime(QDate(999, 6, 17), QTime(8, 0, 10, 0), Qt::LocalTime);
+ QTest::newRow("text date Year 999") << QString::fromLatin1("Tue Jun 17 08:00:10 999")
+ << Qt::TextDate << QDateTime(QDate(999, 6, 17), QTime(8, 0, 10, 0), Qt::LocalTime);
+ QTest::newRow("text date Year 12345") << QString::fromLatin1("Tue Jun 17 08:00:10 12345")
+ << Qt::TextDate << QDateTime(QDate(12345, 6, 17), QTime(8, 0, 10, 0), Qt::LocalTime);
+ QTest::newRow("text date Year -4712") << QString::fromLatin1("Tue Jan 1 00:01:02 -4712")
+ << Qt::TextDate << QDateTime(QDate(-4712, 1, 1), QTime(0, 1, 2, 0), Qt::LocalTime);
+ QTest::newRow("text data0") << QString::fromLatin1("Thu Jan 1 00:00:00 1970")
+ << Qt::TextDate << QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0), Qt::LocalTime);
+ QTest::newRow("text data1") << QString::fromLatin1("Thu Jan 2 12:34 1970")
+ << Qt::TextDate << QDateTime(QDate(1970, 1, 2), QTime(12, 34, 0), Qt::LocalTime);
+ QTest::newRow("text data2") << QString::fromLatin1("Thu Jan 1 00 1970")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text data3") << QString::fromLatin1("Thu Jan 1 00:00:00:00 1970")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text data4") << QString::fromLatin1("Thu 1. Jan 00:00:00 1970")
+ << Qt::TextDate << QDateTime(QDate(1970, 1, 1), QTime(0, 0), Qt::LocalTime);
+ QTest::newRow("text data5") << QString::fromLatin1(" Thu Jan 1 00:00:00 1970 ")
+ << Qt::TextDate << QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0), Qt::LocalTime);
+ QTest::newRow("text data6") << QString::fromLatin1("Thu Jan 1 00:00:00")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text data7") << QString::fromLatin1("Thu Jan 1 1970 00:00:00")
+ << Qt::TextDate << QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0), Qt::LocalTime);
+ QTest::newRow("text data8") << QString::fromLatin1("Thu Jan 1 00:12:34 1970 GMT+foo")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text data9") << QString::fromLatin1("Thu Jan 1 00:12:34 1970 GMT")
+ << Qt::TextDate << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC);
+ QTest::newRow("text data10") << QString::fromLatin1("Thu Jan 1 00:12:34 1970 GMT-0300")
+ << Qt::TextDate << QDateTime(QDate(1970, 1, 1), QTime(3, 12, 34), Qt::UTC);
+ QTest::newRow("text data11") << QString::fromLatin1("Thu Jan 1 00:12:34 1970 GMT+0300")
+ << Qt::TextDate << QDateTime(QDate(1969, 12, 31), QTime(21, 12, 34), Qt::UTC);
+ QTest::newRow("text data12") << QString::fromLatin1("Thu Jan 1 00:12:34 1970 gmt")
+ << Qt::TextDate << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC);
+ QTest::newRow("text data13") << QString::fromLatin1("Thu Jan 1 1970 00:12:34 GMT+0100")
+ << Qt::TextDate << QDateTime(QDate(1969, 12, 31), QTime(23, 12, 34), Qt::UTC);
+ QTest::newRow("text empty") << QString::fromLatin1("")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text too many parts") << QString::fromLatin1("Thu Jan 1 00:12:34 1970 gmt +0100")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text invalid month name") << QString::fromLatin1("Thu Jaz 1 1970 00:12:34")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text invalid date") << QString::fromLatin1("Thu Jan 32 1970 00:12:34")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text invalid day #1") << QString::fromLatin1("Thu Jan XX 1970 00:12:34")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text invalid day #2") << QString::fromLatin1("Thu X. Jan 00:00:00 1970")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text invalid day #3") << QString::fromLatin1("Thu 1 Jan 00:00:00 1970")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text invalid year #1") << QString::fromLatin1("Thu 1. Jan 00:00:00 19X0")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text invalid year #2") << QString::fromLatin1("Thu 1. Jan 19X0 00:00:00")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text invalid hour") << QString::fromLatin1("Thu 1. Jan 1970 0X:00:00")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text invalid minute") << QString::fromLatin1("Thu 1. Jan 1970 00:0X:00")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text invalid second") << QString::fromLatin1("Thu 1. Jan 1970 00:00:0X")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text invalid gmt specifier #1") << QString::fromLatin1("Thu 1. Jan 1970 00:00:00 DMT")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text invalid gmt specifier #2") << QString::fromLatin1("Thu 1. Jan 1970 00:00:00 GMTx0200")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text invalid gmt hour") << QString::fromLatin1("Thu 1. Jan 1970 00:00:00 GMT+0X00")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text invalid gmt minute") << QString::fromLatin1("Thu 1. Jan 1970 00:00:00 GMT+000X")
+ << Qt::TextDate << invalidDateTime();
+ QTest::newRow("text second fraction") << QString::fromLatin1("Mon 6. May 2013 01:02:03.456")
+ << Qt::TextDate << QDateTime(QDate(2013, 5, 6), QTime(1, 2, 3, 456));
+
+ // Test Qt::ISODate format.
+ QTest::newRow("ISO +01:00") << QString::fromLatin1("1987-02-13T13:24:51+01:00")
+ << Qt::ISODate << QDateTime(QDate(1987, 2, 13), QTime(12, 24, 51), Qt::UTC);
+ QTest::newRow("ISO +00:01") << QString::fromLatin1("1987-02-13T13:24:51+00:01")
+ << Qt::ISODate << QDateTime(QDate(1987, 2, 13), QTime(13, 23, 51), Qt::UTC);
+ QTest::newRow("ISO -01:00") << QString::fromLatin1("1987-02-13T13:24:51-01:00")
+ << Qt::ISODate << QDateTime(QDate(1987, 2, 13), QTime(14, 24, 51), Qt::UTC);
+ QTest::newRow("ISO -00:01") << QString::fromLatin1("1987-02-13T13:24:51-00:01")
+ << Qt::ISODate << QDateTime(QDate(1987, 2, 13), QTime(13, 25, 51), Qt::UTC);
+ QTest::newRow("ISO +0000") << QString::fromLatin1("1970-01-01T00:12:34+0000")
+ << Qt::ISODate << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC);
+ QTest::newRow("ISO +00:00") << QString::fromLatin1("1970-01-01T00:12:34+00:00")
+ << Qt::ISODate << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC);
+ QTest::newRow("ISO -03") << QString::fromLatin1("2014-12-15T12:37:09-03")
+ << Qt::ISODate << QDateTime(QDate(2014, 12, 15), QTime(15, 37, 9), Qt::UTC);
+ QTest::newRow("ISO zzz-03") << QString::fromLatin1("2014-12-15T12:37:09.745-03")
+ << Qt::ISODate << QDateTime(QDate(2014, 12, 15), QTime(15, 37, 9, 745), Qt::UTC);
+ QTest::newRow("ISO -3") << QString::fromLatin1("2014-12-15T12:37:09-3")
+ << Qt::ISODate << QDateTime(QDate(2014, 12, 15), QTime(15, 37, 9), Qt::UTC);
+ QTest::newRow("ISO zzz-3") << QString::fromLatin1("2014-12-15T12:37:09.745-3")
+ << Qt::ISODate << QDateTime(QDate(2014, 12, 15), QTime(15, 37, 9, 745), Qt::UTC);
+ // No time specified - defaults to Qt::LocalTime.
+ QTest::newRow("ISO data3") << QString::fromLatin1("2002-10-01")
+ << Qt::ISODate << QDateTime(QDate(2002, 10, 1), QTime(0, 0, 0, 0), Qt::LocalTime);
+ QTest::newRow("ISO") << QString::fromLatin1("2005-06-28T07:57:30.0010000000Z")
+ << Qt::ISODate << QDateTime(QDate(2005, 6, 28), QTime(7, 57, 30, 1), Qt::UTC);
+ QTest::newRow("ISO with comma 1") << QString::fromLatin1("2005-06-28T07:57:30,0040000000Z")
+ << Qt::ISODate << QDateTime(QDate(2005, 6, 28), QTime(7, 57, 30, 4), Qt::UTC);
+ QTest::newRow("ISO with comma 2") << QString::fromLatin1("2005-06-28T07:57:30,0015Z")
+ << Qt::ISODate << QDateTime(QDate(2005, 6, 28), QTime(7, 57, 30, 2), Qt::UTC);
+ QTest::newRow("ISO with comma 3") << QString::fromLatin1("2005-06-28T07:57:30,0014Z")
+ << Qt::ISODate << QDateTime(QDate(2005, 6, 28), QTime(7, 57, 30, 1), Qt::UTC);
+ QTest::newRow("ISO with comma 4") << QString::fromLatin1("2005-06-28T07:57:30,1Z")
+ << Qt::ISODate << QDateTime(QDate(2005, 6, 28), QTime(7, 57, 30, 100), Qt::UTC);
+ QTest::newRow("ISO with comma 5") << QString::fromLatin1("2005-06-28T07:57:30,11")
+ << Qt::ISODate << QDateTime(QDate(2005, 6, 28), QTime(7, 57, 30, 110), Qt::LocalTime);
+ // 24:00:00 Should be next day according to ISO 8601 section 4.2.3.
+ QTest::newRow("ISO 24:00") << QString::fromLatin1("2012-06-04T24:00:00")
+ << Qt::ISODate << QDateTime(QDate(2012, 6, 5), QTime(0, 0, 0, 0), Qt::LocalTime);
+ QTest::newRow("ISO 24:00 end of month") << QString::fromLatin1("2012-06-30T24:00:00")
+ << Qt::ISODate << QDateTime(QDate(2012, 7, 1), QTime(0, 0, 0, 0), Qt::LocalTime);
+ QTest::newRow("ISO 24:00 end of year") << QString::fromLatin1("2012-12-31T24:00:00")
+ << Qt::ISODate << QDateTime(QDate(2013, 1, 1), QTime(0, 0, 0, 0), Qt::LocalTime);
+ QTest::newRow("ISO 24:00, fract ms") << QString::fromLatin1("2012-01-01T24:00:00.000")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 2), QTime(0, 0, 0, 0), Qt::LocalTime);
+ QTest::newRow("ISO 24:00 end of year, fract ms") << QString::fromLatin1("2012-12-31T24:00:00.000")
+ << Qt::ISODate << QDateTime(QDate(2013, 1, 1), QTime(0, 0, 0, 0), Qt::LocalTime);
+ // Test fractional seconds.
+ QTest::newRow("ISO .0 of a second (period)") << QString::fromLatin1("2012-01-01T08:00:00.0")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 0, 0), Qt::LocalTime);
+ QTest::newRow("ISO .00 of a second (period)") << QString::fromLatin1("2012-01-01T08:00:00.00")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 0, 0), Qt::LocalTime);
+ QTest::newRow("ISO .000 of a second (period)") << QString::fromLatin1("2012-01-01T08:00:00.000")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 0, 0), Qt::LocalTime);
+ QTest::newRow("ISO .1 of a second (comma)") << QString::fromLatin1("2012-01-01T08:00:00,1")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 0, 100), Qt::LocalTime);
+ QTest::newRow("ISO .99 of a second (comma)") << QString::fromLatin1("2012-01-01T08:00:00,99")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 0, 990), Qt::LocalTime);
+ QTest::newRow("ISO .998 of a second (comma)") << QString::fromLatin1("2012-01-01T08:00:00,998")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 0, 998), Qt::LocalTime);
+ QTest::newRow("ISO .999 of a second (comma)") << QString::fromLatin1("2012-01-01T08:00:00,999")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 0, 999), Qt::LocalTime);
+ QTest::newRow("ISO .3335 of a second (comma)") << QString::fromLatin1("2012-01-01T08:00:00,3335")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 0, 334), Qt::LocalTime);
+ QTest::newRow("ISO .333333 of a second (comma)") << QString::fromLatin1("2012-01-01T08:00:00,333333")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 0, 333), Qt::LocalTime);
+ QTest::newRow("ISO .00009 of a second (period)") << QString::fromLatin1("2012-01-01T08:00:00.00009")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 0, 0), Qt::LocalTime);
+ QTest::newRow("ISO no fract specified") << QString::fromLatin1("2012-01-01T08:00:00.")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 0, 0), Qt::LocalTime);
+ // Test invalid characters (should ignore invalid characters at end of string).
+ QTest::newRow("ISO invalid character at end") << QString::fromLatin1("2012-01-01T08:00:00!")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 0, 0), Qt::LocalTime);
+ QTest::newRow("ISO invalid character at front") << QString::fromLatin1("!2012-01-01T08:00:00")
+ << Qt::ISODate << invalidDateTime();
+ QTest::newRow("ISO invalid character both ends") << QString::fromLatin1("!2012-01-01T08:00:00!")
+ << Qt::ISODate << invalidDateTime();
+ QTest::newRow("ISO invalid character at front, 2 at back") << QString::fromLatin1("!2012-01-01T08:00:00..")
+ << Qt::ISODate << invalidDateTime();
+ QTest::newRow("ISO invalid character 2 at front") << QString::fromLatin1("!!2012-01-01T08:00:00")
+ << Qt::ISODate << invalidDateTime();
+ // Test fractional minutes.
+ QTest::newRow("ISO .0 of a minute (period)") << QString::fromLatin1("2012-01-01T08:00.0")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 0, 0), Qt::LocalTime);
+ QTest::newRow("ISO .8 of a minute (period)") << QString::fromLatin1("2012-01-01T08:00.8")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 48, 0), Qt::LocalTime);
+ QTest::newRow("ISO .99999 of a minute (period)") << QString::fromLatin1("2012-01-01T08:00.99999")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 59, 999), Qt::LocalTime);
+ QTest::newRow("ISO .0 of a minute (comma)") << QString::fromLatin1("2012-01-01T08:00,0")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 0, 0), Qt::LocalTime);
+ QTest::newRow("ISO .8 of a minute (comma)") << QString::fromLatin1("2012-01-01T08:00,8")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 48, 0), Qt::LocalTime);
+ QTest::newRow("ISO .99999 of a minute (comma)") << QString::fromLatin1("2012-01-01T08:00,99999")
+ << Qt::ISODate << QDateTime(QDate(2012, 1, 1), QTime(8, 0, 59, 999), Qt::LocalTime);
+ QTest::newRow("ISO empty") << QString::fromLatin1("") << Qt::ISODate << invalidDateTime();
+ QTest::newRow("ISO short") << QString::fromLatin1("2017-07-01T") << Qt::ISODate << invalidDateTime();
+ QTest::newRow("ISO zoned date") << QString::fromLatin1("2017-07-01Z") << Qt::ISODate << invalidDateTime();
+ QTest::newRow("ISO zoned empty time") << QString::fromLatin1("2017-07-01TZ") << Qt::ISODate << invalidDateTime();
+ QTest::newRow("ISO mis-punctuated") << QString::fromLatin1("2018/01/30 ") << Qt::ISODate << invalidDateTime();
+
+ // Test Qt::RFC2822Date format (RFC 2822).
+ QTest::newRow("RFC 2822 +0100") << QString::fromLatin1("13 Feb 1987 13:24:51 +0100")
+ << Qt::RFC2822Date << QDateTime(QDate(1987, 2, 13), QTime(12, 24, 51), Qt::UTC);
+ QTest::newRow("RFC 2822 with day +0100") << QString::fromLatin1("Fri, 13 Feb 1987 13:24:51 +0100")
+ << Qt::RFC2822Date << QDateTime(QDate(1987, 2, 13), QTime(12, 24, 51), Qt::UTC);
+ QTest::newRow("RFC 2822 -0100") << QString::fromLatin1("13 Feb 1987 13:24:51 -0100")
+ << Qt::RFC2822Date << QDateTime(QDate(1987, 2, 13), QTime(14, 24, 51), Qt::UTC);
+ QTest::newRow("RFC 2822 with day -0100") << QString::fromLatin1("Fri, 13 Feb 1987 13:24:51 -0100")
+ << Qt::RFC2822Date << QDateTime(QDate(1987, 2, 13), QTime(14, 24, 51), Qt::UTC);
+ QTest::newRow("RFC 2822 +0000") << QString::fromLatin1("01 Jan 1970 00:12:34 +0000")
+ << Qt::RFC2822Date << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC);
+ QTest::newRow("RFC 2822 with day +0000") << QString::fromLatin1("Thu, 01 Jan 1970 00:12:34 +0000")
+ << Qt::RFC2822Date << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC);
+ QTest::newRow("RFC 2822 +0000") << QString::fromLatin1("01 Jan 1970 00:12:34 +0000")
+ << Qt::RFC2822Date << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC);
+ QTest::newRow("RFC 2822 with day +0000") << QString::fromLatin1("Thu, 01 Jan 1970 00:12:34 +0000")
+ << Qt::RFC2822Date << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC);
+ // No timezone assume UTC
+ QTest::newRow("RFC 2822 no timezone") << QString::fromLatin1("01 Jan 1970 00:12:34")
+ << Qt::RFC2822Date << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC);
+ // No time specified
+ QTest::newRow("RFC 2822 date only") << QString::fromLatin1("01 Nov 2002")
+ << Qt::RFC2822Date << invalidDateTime();
+ QTest::newRow("RFC 2822 with day date only") << QString::fromLatin1("Fri, 01 Nov 2002")
+ << Qt::RFC2822Date << invalidDateTime();
+ // Test invalid month, day, year
+ QTest::newRow("RFC 2822 invalid month name") << QString::fromLatin1("13 Fev 1987 13:24:51 +0100")
+ << Qt::RFC2822Date << invalidDateTime();
+ QTest::newRow("RFC 2822 invalid day") << QString::fromLatin1("36 Fev 1987 13:24:51 +0100")
+ << Qt::RFC2822Date << invalidDateTime();
+ QTest::newRow("RFC 2822 invalid year") << QString::fromLatin1("13 Fev 0000 13:24:51 +0100")
+ << Qt::RFC2822Date << invalidDateTime();
+ // Test invalid characters (should ignore invalid characters at end of string).
+ QTest::newRow("RFC 2822 invalid character at end") << QString::fromLatin1("01 Jan 2012 08:00:00 +0100!")
+ << Qt::RFC2822Date << QDateTime(QDate(2012, 1, 1), QTime(7, 0, 0, 0), Qt::UTC);
+ QTest::newRow("RFC 2822 invalid character at front") << QString::fromLatin1("!01 Jan 2012 08:00:00 +0000")
+ << Qt::RFC2822Date << invalidDateTime();
+ QTest::newRow("RFC 2822 invalid character both ends") << QString::fromLatin1("!01 Jan 2012 08:00:00 +0000!")
+ << Qt::RFC2822Date << invalidDateTime();
+ QTest::newRow("RFC 2822 invalid character at front, 2 at back") << QString::fromLatin1("!01 Jan 2012 08:00:00 +0000..")
+ << Qt::RFC2822Date << invalidDateTime();
+ QTest::newRow("RFC 2822 invalid character 2 at front") << QString::fromLatin1("!!01 Jan 2012 08:00:00 +0000")
+ << Qt::RFC2822Date << invalidDateTime();
+
+ // Test Qt::RFC2822Date format (RFC 850 and 1036).
+ QTest::newRow("RFC 850 and 1036 +0100") << QString::fromLatin1("Fri Feb 13 13:24:51 1987 +0100")
+ << Qt::RFC2822Date << QDateTime(QDate(1987, 2, 13), QTime(12, 24, 51), Qt::UTC);
+ QTest::newRow("RFC 850 and 1036 -0100") << QString::fromLatin1("Fri Feb 13 13:24:51 1987 -0100")
+ << Qt::RFC2822Date << QDateTime(QDate(1987, 2, 13), QTime(14, 24, 51), Qt::UTC);
+ QTest::newRow("RFC 850 and 1036 +0000") << QString::fromLatin1("Thu Jan 01 00:12:34 1970 +0000")
+ << Qt::RFC2822Date << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC);
+ QTest::newRow("RFC 850 and 1036 +0000") << QString::fromLatin1("Thu Jan 01 00:12:34 1970 +0000")
+ << Qt::RFC2822Date << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC);
+ // No timezone assume UTC
+ QTest::newRow("RFC 850 and 1036 no timezone") << QString::fromLatin1("Thu Jan 01 00:12:34 1970")
+ << Qt::RFC2822Date << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC);
+ // No time specified
+ QTest::newRow("RFC 850 and 1036 date only") << QString::fromLatin1("Fri Nov 01 2002")
+ << Qt::RFC2822Date << invalidDateTime();
+ // Test invalid characters (should ignore invalid characters at end of string).
+ QTest::newRow("RFC 850 and 1036 invalid character at end") << QString::fromLatin1("Sun Jan 01 08:00:00 2012 +0100!")
+ << Qt::RFC2822Date << QDateTime(QDate(2012, 1, 1), QTime(7, 0, 0, 0), Qt::UTC);
+ QTest::newRow("RFC 850 and 1036 invalid character at front") << QString::fromLatin1("!Sun Jan 01 08:00:00 2012 +0000")
+ << Qt::RFC2822Date << invalidDateTime();
+ QTest::newRow("RFC 850 and 1036 invalid character both ends") << QString::fromLatin1("!Sun Jan 01 08:00:00 2012 +0000!")
+ << Qt::RFC2822Date << invalidDateTime();
+ QTest::newRow("RFC 850 and 1036 invalid character at front, 2 at back") << QString::fromLatin1("!Sun Jan 01 08:00:00 2012 +0000..")
+ << Qt::RFC2822Date << invalidDateTime();
+ QTest::newRow("RFC 850 and 1036 invalid character 2 at front") << QString::fromLatin1("!!Sun Jan 01 08:00:00 2012 +0000")
+ << Qt::RFC2822Date << invalidDateTime();
+
+ QTest::newRow("RFC empty") << QString::fromLatin1("") << Qt::RFC2822Date << invalidDateTime();
+}
+
+void tst_QDateTime::fromStringDateFormat()
+{
+ QFETCH(QString, dateTimeStr);
+ QFETCH(Qt::DateFormat, dateFormat);
+ QFETCH(QDateTime, expected);
+
+ QDateTime dateTime = QDateTime::fromString(dateTimeStr, dateFormat);
+ QCOMPARE(dateTime, expected);
+}
+
+void tst_QDateTime::fromStringStringFormat_data()
+{
+ QTest::addColumn<QString>("string");
+ QTest::addColumn<QString>("format");
+ QTest::addColumn<QDateTime>("expected");
+
+ QTest::newRow("data0") << QString("101010") << QString("dMyy") << QDateTime(QDate(1910, 10, 10), QTime());
+ QTest::newRow("data1") << QString("1020") << QString("sss") << invalidDateTime();
+ QTest::newRow("data2") << QString("1010") << QString("sss") << QDateTime(defDate(), QTime(0, 0, 10));
+ QTest::newRow("data3") << QString("10hello20") << QString("ss'hello'ss") << invalidDateTime();
+ QTest::newRow("data4") << QString("10") << QString("''") << invalidDateTime();
+ QTest::newRow("data5") << QString("10") << QString("'") << invalidDateTime();
+ QTest::newRow("data6") << QString("pm") << QString("ap") << QDateTime(defDate(), QTime(12, 0, 0));
+ QTest::newRow("data7") << QString("foo") << QString("ap") << invalidDateTime();
+ // Day non-conflict should not hide earlier year conflict (1963-03-01 was a
+ // Friday; asking for Thursday moves this, without conflict, to the 7th):
+ QTest::newRow("data8") << QString("77 03 1963 Thu") << QString("yy MM yyyy ddd") << invalidDateTime();
+ QTest::newRow("data9") << QString("101010") << QString("dMyy") << QDateTime(QDate(1910, 10, 10), QTime());
+ QTest::newRow("data10") << QString("101010") << QString("dMyy") << QDateTime(QDate(1910, 10, 10), QTime());
+ QTest::newRow("data11") << QString("10 Oct 10") << QString("dd MMM yy") << QDateTime(QDate(1910, 10, 10), QTime());
+ QTest::newRow("data12") << QString("Fri December 3 2004") << QString("ddd MMMM d yyyy") << QDateTime(QDate(2004, 12, 3), QTime());
+ QTest::newRow("data13") << QString("30.02.2004") << QString("dd.MM.yyyy") << invalidDateTime();
+ QTest::newRow("data14") << QString("32.01.2004") << QString("dd.MM.yyyy") << invalidDateTime();
+ QTest::newRow("data15") << QString("Thu January 2004") << QString("ddd MMMM yyyy") << QDateTime(QDate(2004, 1, 1), QTime());
+ QTest::newRow("data16") << QString("2005-06-28T07:57:30.001Z")
+ << QString("yyyy-MM-ddThh:mm:ss.zt")
+ << QDateTime(QDate(2005, 06, 28), QTime(07, 57, 30, 1), Qt::UTC);
+#if QT_CONFIG(timezone)
+ QTimeZone southBrazil("America/Sao_Paulo");
+ if (southBrazil.isValid()) {
+ QTest::newRow("spring-forward-midnight")
+ << QString("2008-10-19 23:45.678 America/Sao_Paulo") << QString("yyyy-MM-dd mm:ss.zzz t")
+ << QDateTime(QDate(2008, 10, 19), QTime(1, 23, 45, 678), southBrazil);
+ }
+#endif
+ QTest::newRow("late") << QString("9999-12-31T23:59:59.999Z")
+ << QString("yyyy-MM-ddThh:mm:ss.zZ")
+ << QDateTime(QDate(9999, 12, 31), QTime(23, 59, 59, 999));
+ // Separators match /([^aAdhHMmstyz]*)/
+ QTest::newRow("oddly-separated") // To show broken-separator's format is valid.
+ << QStringLiteral("2018 wilful long working block relief 12-19T21:09 cruel blurb encore flux")
+ << QStringLiteral("yyyy wilful long working block relief MM-ddThh:mm cruel blurb encore flux")
+ << QDateTime(QDate(2018, 12, 19), QTime(21, 9));
+ QTest::newRow("broken-separator")
+ << QStringLiteral("2018 wilful")
+ << QStringLiteral("yyyy wilful long working block relief MM-ddThh:mm cruel blurb encore flux")
+ << invalidDateTime();
+ QTest::newRow("broken-terminator")
+ << QStringLiteral("2018 wilful long working block relief 12-19T21:09 cruel")
+ << QStringLiteral("yyyy wilful long working block relief MM-ddThh:mm cruel blurb encore flux")
+ << invalidDateTime();
+}
+
+void tst_QDateTime::fromStringStringFormat()
+{
+ QFETCH(QString, string);
+ QFETCH(QString, format);
+ QFETCH(QDateTime, expected);
+
+ QDateTime dt = QDateTime::fromString(string, format);
+
+ QCOMPARE(dt, expected);
+}
+
+void tst_QDateTime::fromStringStringFormatLocale_data()
+{
+ QTest::addColumn<QString>("string");
+ QTest::addColumn<QString>("format");
+ QTest::addColumn<QLocale>("locale");
+ QTest::addColumn<QDateTime>("expected");
+
+ QLocale c = QLocale::c();
+ QDateTime dt(QDate(2017, 02, 25), QTime(17, 21, 25));
+
+ // The formats correspond to the locale formats, with the timezone removed.
+ // We hardcode them in case an update to the locale DB changes them.
+
+ QTest::newRow("C:long") << "Saturday, 25 February 2017 17:21:25" << "dddd, d MMMM yyyy HH:mm:ss" << c << dt;
+ QTest::newRow("C:short") << "25 Feb 2017 17:21:25" << "d MMM yyyy HH:mm:ss" << c << dt;
+ QTest::newRow("C:narrow") << "25 Feb 2017 17:21:25" << "d MMM yyyy HH:mm:ss" << c << dt;
+
+ QLocale fr(QLocale::French);
+ QTest::newRow("fr:long") << "Samedi 25 février 2017 17:21:25" << "dddd d MMMM yyyy HH:mm:ss" << fr << dt;
+ QTest::newRow("fr:short") << "25/02/2017 17:21" << "dd/MM/yyyy HH:mm" << fr << dt.addSecs(-25);
+
+ // In Turkish, the word for Friday ("Cuma") is a prefix for the word for
+ // Saturday ("Cumartesi")
+ QLocale tr(QLocale::Turkish);
+ QTest::newRow("tr:long") << "25 Şubat 2017 Cumartesi 17:21:25" << "d MMMM yyyy dddd HH:mm:ss" << tr << dt;
+ QTest::newRow("tr:long2") << "24 Şubat 2017 Cuma 17:21:25" << "d MMMM yyyy dddd HH:mm:ss" << tr << dt.addDays(-1);
+ QTest::newRow("tr:mashed") << "25 Şubat2017 Cumartesi17:21:25" << "d MMMMyyyy ddddHH:mm:ss" << tr << dt;
+ QTest::newRow("tr:mashed2") << "24 Şubat2017 Cuma17:21:25" << "d MMMMyyyy ddddHH:mm:ss" << tr << dt.addDays(-1);
+ QTest::newRow("tr:short") << "25.02.2017 17:21" << "d.MM.yyyy HH:mm" << tr << dt.addSecs(-25);
+}
+
+void tst_QDateTime::fromStringStringFormatLocale()
+{
+ QFETCH(QString, string);
+ QFETCH(QString, format);
+ QFETCH(QLocale, locale);
+ QFETCH(QDateTime, expected);
+
+ QDateTime parsed = locale.toDateTime(string, format);
+ QCOMPARE(parsed, expected);
+
+ parsed = locale.toDateTime(string.toLower(), format);
+ QCOMPARE(parsed, expected);
+
+ parsed = locale.toDateTime(string.toUpper(), format);
+ QCOMPARE(parsed, expected);
+}
+
+#ifdef Q_OS_WIN
+// Windows only
+void tst_QDateTime::fromString_LOCALE_ILDATE()
+{
+ QString date1 = QLatin1String("Sun 1. Dec 13:02:00 1974");
+ QString date2 = QLatin1String("Sun Dec 1 13:02:00 1974");
+
+ QDateTime ref(QDate(1974, 12, 1), QTime(13, 2));
+ QCOMPARE(ref, QDateTime::fromString(date2, Qt::TextDate));
+ QCOMPARE(ref, QDateTime::fromString(date1, Qt::TextDate));
+}
+#endif
+
+void tst_QDateTime::fromStringToStringLocale_data()
+{
+ QTest::addColumn<QDateTime>("dateTime");
+
+ QTest::newRow("data0") << QDateTime(QDate(1999, 1, 18), QTime(11, 49, 00));
+}
+
+void tst_QDateTime::fromStringToStringLocale()
+{
+ QFETCH(QDateTime, dateTime);
+
+ QLocale def;
+ QLocale::setDefault(QLocale(QLocale::French, QLocale::France));
+#define ROUNDTRIP(format) \
+ QCOMPARE(QDateTime::fromString(dateTime.toString(format), format), dateTime)
+
+ ROUNDTRIP(Qt::DefaultLocaleShortDate);
+ ROUNDTRIP(Qt::SystemLocaleShortDate);
+
+ // obsolete
+ ROUNDTRIP(Qt::SystemLocaleDate);
+ ROUNDTRIP(Qt::LocaleDate);
+
+ ROUNDTRIP(Qt::DefaultLocaleLongDate);
+ ROUNDTRIP(Qt::SystemLocaleLongDate);
+#undef ROUNDTRIP
+ QLocale::setDefault(def);
+}
+
+void tst_QDateTime::offsetFromUtc()
+{
+ /* Check default value. */
+ QCOMPARE(QDateTime().offsetFromUtc(), 0);
+
+ // Offset constructor
+ QDateTime dt1(QDate(2013, 1, 1), QTime(1, 0, 0), Qt::OffsetFromUTC, 60 * 60);
+ QCOMPARE(dt1.offsetFromUtc(), 60 * 60);
+ QVERIFY(dt1.timeZone().isValid());
+ dt1 = QDateTime(QDate(2013, 1, 1), QTime(1, 0, 0), Qt::OffsetFromUTC, -60 * 60);
+ QCOMPARE(dt1.offsetFromUtc(), -60 * 60);
+
+ // UTC should be 0 offset
+ QDateTime dt2(QDate(2013, 1, 1), QTime(0, 0, 0), Qt::UTC);
+ QCOMPARE(dt2.offsetFromUtc(), 0);
+
+ // LocalTime should vary
+ if (zoneIsCET) {
+ // Time definitely in Standard Time so 1 hour ahead
+ QDateTime dt3(QDate(2013, 1, 1), QTime(0, 0, 0), Qt::LocalTime);
+ QCOMPARE(dt3.offsetFromUtc(), 1 * 60 * 60);
+ // Time definitely in Daylight Time so 2 hours ahead
+ QDateTime dt4(QDate(2013, 6, 1), QTime(0, 0, 0), Qt::LocalTime);
+ QCOMPARE(dt4.offsetFromUtc(), 2 * 60 * 60);
+ } else {
+ QSKIP("You must test using Central European (CET/CEST) time zone, e.g. TZ=Europe/Oslo");
+ }
+
+ QDateTime dt5(QDate(2013, 1, 1), QTime(0, 0, 0), QTimeZone("Pacific/Auckland"));
+ QCOMPARE(dt5.offsetFromUtc(), 46800);
+
+ QDateTime dt6(QDate(2013, 6, 1), QTime(0, 0, 0), QTimeZone("Pacific/Auckland"));
+ QCOMPARE(dt6.offsetFromUtc(), 43200);
+}
+
+void tst_QDateTime::setOffsetFromUtc()
+{
+ /* Basic tests. */
+ {
+ QDateTime dt(QDateTime::currentDateTime());
+ dt.setTimeSpec(Qt::LocalTime);
+
+ dt.setOffsetFromUtc(0);
+ QCOMPARE(dt.offsetFromUtc(), 0);
+ QCOMPARE(dt.timeSpec(), Qt::UTC);
+
+ dt.setOffsetFromUtc(-100);
+ QCOMPARE(dt.offsetFromUtc(), -100);
+ QCOMPARE(dt.timeSpec(), Qt::OffsetFromUTC);
+ }
+
+ /* Test detaching. */
+ {
+ QDateTime dt(QDateTime::currentDateTime());
+ QDateTime dt2(dt);
+ int offset2 = dt2.offsetFromUtc();
+
+ dt.setOffsetFromUtc(501);
+
+ QCOMPARE(dt.offsetFromUtc(), 501);
+ QCOMPARE(dt2.offsetFromUtc(), offset2);
+ }
+
+ /* Check copying. */
+ {
+ QDateTime dt(QDateTime::currentDateTime());
+ dt.setOffsetFromUtc(502);
+ QCOMPARE(dt.offsetFromUtc(), 502);
+
+ QDateTime dt2(dt);
+ QCOMPARE(dt2.offsetFromUtc(), 502);
+ }
+
+ /* Check assignment. */
+ {
+ QDateTime dt(QDateTime::currentDateTime());
+ dt.setOffsetFromUtc(502);
+ QDateTime dt2;
+ dt2 = dt;
+
+ QCOMPARE(dt2.offsetFromUtc(), 502);
+ }
+
+ // Check spec persists
+ QDateTime dt1(QDate(2013, 1, 1), QTime(0, 0, 0), Qt::OffsetFromUTC, 60 * 60);
+ dt1.setMSecsSinceEpoch(123456789);
+ QCOMPARE(dt1.timeSpec(), Qt::OffsetFromUTC);
+ QCOMPARE(dt1.offsetFromUtc(), 60 * 60);
+ dt1.setSecsSinceEpoch(123456789);
+ QCOMPARE(dt1.timeSpec(), Qt::OffsetFromUTC);
+ QCOMPARE(dt1.offsetFromUtc(), 60 * 60);
+
+ // Check datastream serialises the offset seconds
+ QByteArray tmp;
+ {
+ QDataStream ds(&tmp, QIODevice::WriteOnly);
+ ds << dt1;
+ }
+ QDateTime dt2;
+ {
+ QDataStream ds(&tmp, QIODevice::ReadOnly);
+ ds >> dt2;
+ }
+ QCOMPARE(dt2, dt1);
+ QCOMPARE(dt2.timeSpec(), Qt::OffsetFromUTC);
+ QCOMPARE(dt2.offsetFromUtc(), 60 * 60);
+}
+
+void tst_QDateTime::toOffsetFromUtc()
+{
+ QDateTime dt1(QDate(2013, 1, 1), QTime(0, 0, 0), Qt::UTC);
+
+ QDateTime dt2 = dt1.toOffsetFromUtc(60 * 60);
+ QCOMPARE(dt2, dt1);
+ QCOMPARE(dt2.timeSpec(), Qt::OffsetFromUTC);
+ QCOMPARE(dt2.date(), QDate(2013, 1, 1));
+ QCOMPARE(dt2.time(), QTime(1, 0, 0));
+
+ dt2 = dt1.toOffsetFromUtc(0);
+ QCOMPARE(dt2, dt1);
+ QCOMPARE(dt2.timeSpec(), Qt::UTC);
+ QCOMPARE(dt2.date(), QDate(2013, 1, 1));
+ QCOMPARE(dt2.time(), QTime(0, 0, 0));
+
+ dt2 = dt1.toTimeSpec(Qt::OffsetFromUTC);
+ QCOMPARE(dt2, dt1);
+ QCOMPARE(dt2.timeSpec(), Qt::UTC);
+ QCOMPARE(dt2.date(), QDate(2013, 1, 1));
+ QCOMPARE(dt2.time(), QTime(0, 0, 0));
+}
+
+void tst_QDateTime::zoneAtTime_data()
+{
+ QTest::addColumn<QByteArray>("ianaID");
+ QTest::addColumn<QDate>("date");
+ QTest::addColumn<int>("offset");
+#define ADDROW(name, zone, date, offset) \
+ QTest::newRow(name) << QByteArray(zone) << (date) << (offset)
+
+ // Check DST handling around epoch:
+ {
+ QDate epoch(1970, 1, 1);
+ ADDROW("epoch:UTC", "UTC", epoch, 0);
+ // Paris and Berlin skipped DST around 1970; but Rome used it.
+ ADDROW("epoch:CET", "Europe/Rome", epoch, 3600);
+ ADDROW("epoch:PST", "America/Vancouver", epoch, -8 * 3600);
+ ADDROW("epoch:EST", "America/New_York", epoch, -5 * 3600);
+ }
+ {
+ // QDateTime deliberately ignores DST before the epoch.
+ QDate summer69(1969, 8, 15); // Woodstock started
+ ADDROW("summer69:UTC", "UTC", summer69, 0);
+ ADDROW("summer69:CET", "Europe/Rome", summer69, 3600);
+ ADDROW("summer69:PST", "America/Vancouver", summer69, -8 * 3600);
+ ADDROW("summer69:EST", "America/New_York", summer69, -5 * 3600);
+ }
+ {
+ // ... but takes it into account after:
+ QDate summer70(1970, 8, 26); // Isle of Wight festival
+ ADDROW("summer70:UTC", "UTC", summer70, 0);
+ ADDROW("summer70:CET", "Europe/Rome", summer70, 2 * 3600);
+ ADDROW("summer70:PST", "America/Vancouver", summer70, -7 * 3600);
+ ADDROW("summer70:EST", "America/New_York", summer70, -4 * 3600);
+ }
+
+#ifdef Q_OS_ANDROID // QTBUG-68835; gets offset 0 for the affected tests.
+# define NONANDROIDROW(name, zone, date, offset)
+#else
+# define NONANDROIDROW(name, zone, date, offset) ADDROW(name, zone, date, offset)
+#endif
+
+#ifndef Q_OS_WIN
+ // Bracket a few noteworthy transitions:
+ ADDROW("before:ACWST", "Australia/Eucla", QDate(1974, 10, 26), 31500); // 8:45
+ NONANDROIDROW("after:ACWST", "Australia/Eucla", QDate(1974, 10, 27), 35100); // 9:45
+ NONANDROIDROW("before:NPT", "Asia/Kathmandu", QDate(1985, 12, 31), 19800); // 5:30
+ ADDROW("after:NPT", "Asia/Kathmandu", QDate(1986, 1, 1), 20700); // 5:45
+ // The two that have skipped a day (each):
+ NONANDROIDROW("before:LINT", "Pacific/Kiritimati", QDate(1994, 12, 30), -36000);
+ ADDROW("after:LINT", "Pacific/Kiritimati", QDate(1995, 1, 2), 14 * 3600);
+ ADDROW("after:WST", "Pacific/Apia", QDate(2011, 12, 31), 14 * 3600);
+#endif // MS lacks ACWST, NPT; doesn't grok date-line crossings; and Windows 7 lacks LINT.
+ ADDROW("before:WST", "Pacific/Apia", QDate(2011, 12, 29), -36000);
+#undef ADDROW
+}
+
+void tst_QDateTime::zoneAtTime()
+{
+ QFETCH(QByteArray, ianaID);
+ QFETCH(QDate, date);
+ QFETCH(int, offset);
+ const QTime noon(12, 0);
+
+ QTimeZone zone(ianaID);
+ QVERIFY(zone.isValid());
+ QCOMPARE(QDateTime(date, noon, zone).offsetFromUtc(), offset);
+ if (date.year() < 1970)
+ QCOMPARE(zone.standardTimeOffset(QDateTime(date, noon, zone)), offset);
+ else // zone.offsetFromUtc *does* include DST, even before epoch
+ QCOMPARE(zone.offsetFromUtc(QDateTime(date, noon, zone)), offset);
+}
+
+void tst_QDateTime::timeZoneAbbreviation()
+{
+ QDateTime dt1(QDate(2013, 1, 1), QTime(1, 0, 0), Qt::OffsetFromUTC, 60 * 60);
+ QCOMPARE(dt1.timeZoneAbbreviation(), QString("UTC+01:00"));
+ QDateTime dt2(QDate(2013, 1, 1), QTime(1, 0, 0), Qt::OffsetFromUTC, -60 * 60);
+ QCOMPARE(dt2.timeZoneAbbreviation(), QString("UTC-01:00"));
+
+ QDateTime dt3(QDate(2013, 1, 1), QTime(0, 0, 0), Qt::UTC);
+ QCOMPARE(dt3.timeZoneAbbreviation(), QString("UTC"));
+
+ // LocalTime should vary
+ if (zoneIsCET) {
+ // Time definitely in Standard Time
+ QDateTime dt4(QDate(2013, 1, 1), QTime(0, 0, 0), Qt::LocalTime);
+#ifdef Q_OS_WIN
+ QEXPECT_FAIL("", "Windows only reports long name (QTBUG-32759)", Continue);
+#endif
+ QCOMPARE(dt4.timeZoneAbbreviation(), QStringLiteral("CET"));
+ // Time definitely in Daylight Time
+ QDateTime dt5(QDate(2013, 6, 1), QTime(0, 0, 0), Qt::LocalTime);
+#ifdef Q_OS_WIN
+ QEXPECT_FAIL("", "Windows only reports long name (QTBUG-32759)", Continue);
+#endif
+ QCOMPARE(dt5.timeZoneAbbreviation(), QStringLiteral("CEST"));
+ } else {
+ qDebug("(Skipped some CET-only tests)");
+ }
+
+#ifdef Q_OS_ANDROID // Only reports (general) zones as offsets (QTBUG-68837)
+ const QString cet(QStringLiteral("GMT+01:00"));
+ const QString cest(QStringLiteral("GMT+02:00"));
+#elif defined Q_OS_DARWIN
+ const QString cet(QStringLiteral("GMT+1"));
+ const QString cest(QStringLiteral("GMT+2"));
+#else
+ const QString cet(QStringLiteral("CET"));
+ const QString cest(QStringLiteral("CEST"));
+#endif
+
+ QDateTime dt5(QDate(2013, 1, 1), QTime(0, 0, 0), QTimeZone("Europe/Berlin"));
+#ifdef Q_OS_WIN
+ QEXPECT_FAIL("", "Windows only reports long names (QTBUG-32759)", Continue);
+#endif
+ QCOMPARE(dt5.timeZoneAbbreviation(), cet);
+ QDateTime dt6(QDate(2013, 6, 1), QTime(0, 0, 0), QTimeZone("Europe/Berlin"));
+#ifdef Q_OS_WIN
+ QEXPECT_FAIL("", "Windows only reports long names (QTBUG-32759)", Continue);
+#endif
+ QCOMPARE(dt6.timeZoneAbbreviation(), cest);
+}
+
+void tst_QDateTime::getDate()
+{
+ {
+ int y = -33, m = -44, d = -55;
+ QDate date;
+ date.getDate(&y, &m, &d);
+ QCOMPARE(date.year(), y);
+ QCOMPARE(date.month(), m);
+ QCOMPARE(date.day(), d);
+
+ date.getDate(0, 0, 0);
+ }
+
+ {
+ int y = -33, m = -44, d = -55;
+ QDate date(1998, 5, 24);
+ date.getDate(0, &m, 0);
+ date.getDate(&y, 0, 0);
+ date.getDate(0, 0, &d);
+
+ QCOMPARE(date.year(), y);
+ QCOMPARE(date.month(), m);
+ QCOMPARE(date.day(), d);
+ }
+}
+
+void tst_QDateTime::fewDigitsInYear() const
+{
+ const QDateTime three(QDate(300, 10, 11), QTime());
+ QCOMPARE(three.toString(QLatin1String("yyyy-MM-dd")), QString::fromLatin1("0300-10-11"));
+
+ const QDateTime two(QDate(20, 10, 11), QTime());
+ QCOMPARE(two.toString(QLatin1String("yyyy-MM-dd")), QString::fromLatin1("0020-10-11"));
+
+ const QDateTime yyTwo(QDate(30, 10, 11), QTime());
+ QCOMPARE(yyTwo.toString(QLatin1String("yy-MM-dd")), QString::fromLatin1("30-10-11"));
+
+ const QDateTime yyOne(QDate(4, 10, 11), QTime());
+ QCOMPARE(yyOne.toString(QLatin1String("yy-MM-dd")), QString::fromLatin1("04-10-11"));
+}
+
+void tst_QDateTime::printNegativeYear() const
+{
+ {
+ QDateTime date(QDate(-20, 10, 11));
+ QVERIFY(date.isValid());
+ QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0020"));
+ }
+
+ {
+ QDateTime date(QDate(-3, 10, 11));
+ QVERIFY(date.isValid());
+ QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0003"));
+ }
+
+ {
+ QDateTime date(QDate(-400, 10, 11));
+ QVERIFY(date.isValid());
+ QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0400"));
+ }
+}
+
+void tst_QDateTime::roundtripGermanLocale() const
+{
+ /* This code path should not result in warnings. */
+ const QDateTime theDateTime(QDateTime::currentDateTime());
+ theDateTime.fromString(theDateTime.toString(Qt::TextDate), Qt::TextDate);
+}
+
+void tst_QDateTime::utcOffsetLessThan() const
+{
+ QDateTime dt1(QDate(2002, 10, 10), QTime(0, 0, 0));
+ QDateTime dt2(dt1);
+
+ dt1.setOffsetFromUtc(-(2 * 60 * 60)); // Minus two hours.
+ dt2.setOffsetFromUtc(-(3 * 60 * 60)); // Minus three hours.
+
+ QVERIFY(dt1 != dt2);
+ QVERIFY(!(dt1 == dt2));
+ QVERIFY(dt1 < dt2);
+ QVERIFY(!(dt2 < dt1));
+}
+
+void tst_QDateTime::isDaylightTime() const
+{
+ QDateTime utc1(QDate(2012, 1, 1), QTime(0, 0, 0), Qt::UTC);
+ QVERIFY(!utc1.isDaylightTime());
+ QDateTime utc2(QDate(2012, 6, 1), QTime(0, 0, 0), Qt::UTC);
+ QVERIFY(!utc2.isDaylightTime());
+
+ QDateTime offset1(QDate(2012, 1, 1), QTime(0, 0, 0), Qt::OffsetFromUTC, 1 * 60 * 60);
+ QVERIFY(!offset1.isDaylightTime());
+ QDateTime offset2(QDate(2012, 6, 1), QTime(0, 0, 0), Qt::OffsetFromUTC, 1 * 60 * 60);
+ QVERIFY(!offset2.isDaylightTime());
+
+ if (zoneIsCET) {
+ QDateTime cet1(QDate(2012, 1, 1), QTime(0, 0, 0));
+ QVERIFY(!cet1.isDaylightTime());
+ QDateTime cet2(QDate(2012, 6, 1), QTime(0, 0, 0));
+ QVERIFY(cet2.isDaylightTime());
+ } else {
+ QSKIP("You must test using Central European (CET/CEST) time zone, e.g. TZ=Europe/Oslo");
+ }
+}
+
+void tst_QDateTime::daylightTransitions() const
+{
+ if (zoneIsCET) {
+ // CET transitions occur at 01:00:00 UTC on last Sunday in March and October
+ // 2011-03-27 02:00:00 CET became 03:00:00 CEST at msecs = 1301187600000
+ // 2011-10-30 03:00:00 CEST became 02:00:00 CET at msecs = 1319936400000
+ // 2012-03-25 02:00:00 CET became 03:00:00 CEST at msecs = 1332637200000
+ // 2012-10-28 03:00:00 CEST became 02:00:00 CET at msecs = 1351386000000
+ const qint64 daylight2012 = 1332637200000;
+ const qint64 standard2012 = 1351386000000;
+ const qint64 msecsOneHour = 3600000;
+
+ // Test for correct behviour for StandardTime -> DaylightTime transition, i.e. missing hour
+
+ // Test setting date, time in missing hour will be invalid
+
+ QDateTime before(QDate(2012, 3, 25), QTime(1, 59, 59, 999));
+ QVERIFY(before.isValid());
+ QCOMPARE(before.date(), QDate(2012, 3, 25));
+ QCOMPARE(before.time(), QTime(1, 59, 59, 999));
+ QCOMPARE(before.toMSecsSinceEpoch(), daylight2012 - 1);
+
+ QDateTime missing(QDate(2012, 3, 25), QTime(2, 0, 0));
+ QVERIFY(!missing.isValid());
+ QCOMPARE(missing.date(), QDate(2012, 3, 25));
+ QCOMPARE(missing.time(), QTime(2, 0, 0));
+
+ QDateTime after(QDate(2012, 3, 25), QTime(3, 0, 0));
+ QVERIFY(after.isValid());
+ QCOMPARE(after.date(), QDate(2012, 3, 25));
+ QCOMPARE(after.time(), QTime(3, 0, 0));
+ QCOMPARE(after.toMSecsSinceEpoch(), daylight2012);
+
+ // Test round-tripping of msecs
+
+ before.setMSecsSinceEpoch(daylight2012 - 1);
+ QVERIFY(before.isValid());
+ QCOMPARE(before.date(), QDate(2012, 3, 25));
+ QCOMPARE(before.time(), QTime(1, 59, 59, 999));
+ QCOMPARE(before.toMSecsSinceEpoch(), daylight2012 -1);
+
+ after.setMSecsSinceEpoch(daylight2012);
+ QVERIFY(after.isValid());
+ QCOMPARE(after.date(), QDate(2012, 3, 25));
+ QCOMPARE(after.time(), QTime(3, 0, 0));
+ QCOMPARE(after.toMSecsSinceEpoch(), daylight2012);
+
+ // Test changing time spec re-validates the date/time
+
+ QDateTime utc(QDate(2012, 3, 25), QTime(2, 00, 0), Qt::UTC);
+ QVERIFY(utc.isValid());
+ QCOMPARE(utc.date(), QDate(2012, 3, 25));
+ QCOMPARE(utc.time(), QTime(2, 0, 0));
+ utc.setTimeSpec(Qt::LocalTime);
+ QVERIFY(!utc.isValid());
+ QCOMPARE(utc.date(), QDate(2012, 3, 25));
+ QCOMPARE(utc.time(), QTime(2, 0, 0));
+ utc.setTimeSpec(Qt::UTC);
+ QVERIFY(utc.isValid());
+ QCOMPARE(utc.date(), QDate(2012, 3, 25));
+ QCOMPARE(utc.time(), QTime(2, 0, 0));
+
+ // Test date maths, if result falls in missing hour then becomes next
+ // hour (or is always invalid; mktime() may reject gap-times).
+
+ QDateTime test(QDate(2011, 3, 25), QTime(2, 0, 0));
+ QVERIFY(test.isValid());
+ test = test.addYears(1);
+ const bool handled = test.isValid();
+#define CHECK_SPRING_FORWARD(test) \
+ if (test.isValid()) { \
+ QCOMPARE(test.date(), QDate(2012, 3, 25)); \
+ QCOMPARE(test.time(), QTime(3, 0, 0)); \
+ } else { \
+ QVERIFY(!handled); \
+ }
+ CHECK_SPRING_FORWARD(test);
+
+ test = QDateTime(QDate(2012, 2, 25), QTime(2, 0, 0));
+ QVERIFY(test.isValid());
+ test = test.addMonths(1);
+ CHECK_SPRING_FORWARD(test);
+
+ test = QDateTime(QDate(2012, 3, 24), QTime(2, 0, 0));
+ QVERIFY(test.isValid());
+ test = test.addDays(1);
+ CHECK_SPRING_FORWARD(test);
+
+ test = QDateTime(QDate(2012, 3, 25), QTime(1, 0, 0));
+ QVERIFY(test.isValid());
+ QCOMPARE(test.toMSecsSinceEpoch(), daylight2012 - msecsOneHour);
+ test = test.addMSecs(msecsOneHour);
+ CHECK_SPRING_FORWARD(test);
+ if (handled)
+ QCOMPARE(test.toMSecsSinceEpoch(), daylight2012);
+#undef CHECK_SPRING_FORWARD
+
+ // Test for correct behviour for DaylightTime -> StandardTime transition, i.e. second occurrence
+
+ // Test setting date and time in first and second occurrence will be valid
+
+ // 1 hour before transition is 2:00:00 FirstOccurrence
+ QDateTime hourBefore(QDate(2012, 10, 28), QTime(2, 0, 0));
+ QVERIFY(hourBefore.isValid());
+ QCOMPARE(hourBefore.date(), QDate(2012, 10, 28));
+ QCOMPARE(hourBefore.time(), QTime(2, 0, 0));
+#ifdef Q_OS_WIN
+ // Windows uses SecondOccurrence
+ QEXPECT_FAIL("", "QDateTime doesn't properly support Daylight Transitions", Continue);
+#endif // Q_OS_WIN
+ QCOMPARE(hourBefore.toMSecsSinceEpoch(), standard2012 - msecsOneHour);
+
+ // 1 msec before transition is 2:59:59.999 FirstOccurrence
+ QDateTime msecBefore(QDate(2012, 10, 28), QTime(2, 59, 59, 999));
+ QVERIFY(msecBefore.isValid());
+ QCOMPARE(msecBefore.date(), QDate(2012, 10, 28));
+ QCOMPARE(msecBefore.time(), QTime(2, 59, 59, 999));
+#if defined(Q_OS_DARWIN) || defined(Q_OS_WIN) || defined(Q_OS_QNX) || defined(Q_OS_ANDROID)
+ // Win and Mac uses SecondOccurrence here
+ QEXPECT_FAIL("", "QDateTime doesn't properly support Daylight Transitions", Continue);
+#endif // Q_OS_MAC
+ QCOMPARE(msecBefore.toMSecsSinceEpoch(), standard2012 - 1);
+
+ // At transition is 2:00:00 SecondOccurrence
+ QDateTime atTran(QDate(2012, 10, 28), QTime(2, 0, 0));
+ QVERIFY(atTran.isValid());
+ QCOMPARE(atTran.date(), QDate(2012, 10, 28));
+ QCOMPARE(atTran.time(), QTime(2, 0, 0));
+#ifndef Q_OS_WIN
+ // Windows uses SecondOccurrence
+ QEXPECT_FAIL("", "QDateTime doesn't properly support Daylight Transitions", Continue);
+#endif // Q_OS_WIN
+ QCOMPARE(atTran.toMSecsSinceEpoch(), standard2012);
+
+ // 59:59.999 after transition is 2:59:59.999 SecondOccurrence
+ QDateTime afterTran(QDate(2012, 10, 28), QTime(2, 59, 59, 999));
+ QVERIFY(afterTran.isValid());
+ QCOMPARE(afterTran.date(), QDate(2012, 10, 28));
+ QCOMPARE(afterTran.time(), QTime(2, 59, 59, 999));
+#ifdef __GLIBCXX__
+ // Linux (i.e. glibc) mktime bug reuses last calculation
+ QEXPECT_FAIL("", "QDateTime doesn't properly support Daylight Transitions", Continue);
+#endif // Q_OS_UNIX
+ QCOMPARE(afterTran.toMSecsSinceEpoch(), standard2012 + msecsOneHour - 1);
+
+ // 1 hour after transition is 3:00:00 FirstOccurrence
+ QDateTime hourAfter(QDate(2012, 10, 28), QTime(3, 0, 0));
+ QVERIFY(hourAfter.isValid());
+ QCOMPARE(hourAfter.date(), QDate(2012, 10, 28));
+ QCOMPARE(hourAfter.time(), QTime(3, 0, 0));
+ QCOMPARE(hourAfter.toMSecsSinceEpoch(), standard2012 + msecsOneHour);
+
+ // Test round-tripping of msecs
+
+ // 1 hour before transition is 2:00:00 FirstOccurrence
+ hourBefore.setMSecsSinceEpoch(standard2012 - msecsOneHour);
+ QVERIFY(hourBefore.isValid());
+ QCOMPARE(hourBefore.date(), QDate(2012, 10, 28));
+ QCOMPARE(hourBefore.time(), QTime(2, 0, 0));
+ QCOMPARE(hourBefore.toMSecsSinceEpoch(), standard2012 - msecsOneHour);
+
+ // 1 msec before transition is 2:59:59.999 FirstOccurrence
+ msecBefore.setMSecsSinceEpoch(standard2012 - 1);
+ QVERIFY(msecBefore.isValid());
+ QCOMPARE(msecBefore.date(), QDate(2012, 10, 28));
+ QCOMPARE(msecBefore.time(), QTime(2, 59, 59, 999));
+ QCOMPARE(msecBefore.toMSecsSinceEpoch(), standard2012 - 1);
+
+ // At transition is 2:00:00 SecondOccurrence
+ atTran.setMSecsSinceEpoch(standard2012);
+ QVERIFY(atTran.isValid());
+ QCOMPARE(atTran.date(), QDate(2012, 10, 28));
+ QCOMPARE(atTran.time(), QTime(2, 0, 0));
+ QCOMPARE(atTran.toMSecsSinceEpoch(), standard2012);
+
+ // 59:59.999 after transition is 2:59:59.999 SecondOccurrence
+ afterTran.setMSecsSinceEpoch(standard2012 + msecsOneHour - 1);
+ QVERIFY(afterTran.isValid());
+ QCOMPARE(afterTran.date(), QDate(2012, 10, 28));
+ QCOMPARE(afterTran.time(), QTime(2, 59, 59, 999));
+ QCOMPARE(afterTran.toMSecsSinceEpoch(), standard2012 + msecsOneHour - 1);
+
+ // 1 hour after transition is 3:00:00 FirstOccurrence
+ hourAfter.setMSecsSinceEpoch(standard2012 + msecsOneHour);
+ QVERIFY(hourAfter.isValid());
+ QCOMPARE(hourAfter.date(), QDate(2012, 10, 28));
+ QCOMPARE(hourAfter.time(), QTime(3, 0, 0));
+ QCOMPARE(hourAfter.toMSecsSinceEpoch(), standard2012 + msecsOneHour);
+
+ // Test date maths, result is always FirstOccurrence
+
+ // Add year to get to tran FirstOccurrence
+ test = QDateTime(QDate(2011, 10, 28), QTime(2, 0, 0));
+ test = test.addYears(1);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2012, 10, 28));
+ QCOMPARE(test.time(), QTime(2, 0, 0));
+#ifdef Q_OS_WIN
+ // Windows uses SecondOccurrence
+ QEXPECT_FAIL("", "QDateTime doesn't properly support Daylight Transitions", Continue);
+#endif // Q_OS_WIN
+ QCOMPARE(test.toMSecsSinceEpoch(), standard2012 - msecsOneHour);
+
+ // Add year to get to after tran FirstOccurrence
+ test = QDateTime(QDate(2011, 10, 28), QTime(3, 0, 0));
+ test = test.addYears(1);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2012, 10, 28));
+ QCOMPARE(test.time(), QTime(3, 0, 0));
+ QCOMPARE(test.toMSecsSinceEpoch(), standard2012 + msecsOneHour);
+
+ // Add year to tran FirstOccurrence
+ test = QDateTime(QDate(2011, 10, 30), QTime(2, 0, 0));
+ test = test.addYears(1);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2012, 10, 30));
+ QCOMPARE(test.time(), QTime(2, 0, 0));
+
+ // Add year to tran SecondOccurrence
+ test = QDateTime(QDate(2011, 10, 30), QTime(2, 0, 0)); // TODO SecondOccurrence
+ test = test.addYears(1);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2012, 10, 30));
+ QCOMPARE(test.time(), QTime(2, 0, 0));
+
+ // Add year to after tran FirstOccurrence
+ test = QDateTime(QDate(2011, 10, 30), QTime(3, 0, 0));
+ test = test.addYears(1);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2012, 10, 30));
+ QCOMPARE(test.time(), QTime(3, 0, 0));
+
+
+ // Add month to get to tran FirstOccurrence
+ test = QDateTime(QDate(2012, 9, 28), QTime(2, 0, 0));
+ test = test.addMonths(1);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2012, 10, 28));
+ QCOMPARE(test.time(), QTime(2, 0, 0));
+#ifdef Q_OS_WIN
+ // Windows uses SecondOccurrence
+ QEXPECT_FAIL("", "QDateTime doesn't properly support Daylight Transitions", Continue);
+#endif // Q_OS_WIN
+ QCOMPARE(test.toMSecsSinceEpoch(), standard2012 - msecsOneHour);
+
+ // Add month to get to after tran FirstOccurrence
+ test = QDateTime(QDate(2012, 9, 28), QTime(3, 0, 0));
+ test = test.addMonths(1);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2012, 10, 28));
+ QCOMPARE(test.time(), QTime(3, 0, 0));
+ QCOMPARE(test.toMSecsSinceEpoch(), standard2012 + msecsOneHour);
+
+ // Add month to tran FirstOccurrence
+ test = QDateTime(QDate(2011, 10, 30), QTime(2, 0, 0));
+ test = test.addMonths(1);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2011, 11, 30));
+ QCOMPARE(test.time(), QTime(2, 0, 0));
+
+ // Add month to tran SecondOccurrence
+ test = QDateTime(QDate(2011, 10, 30), QTime(2, 0, 0)); // TODO SecondOccurrence
+ test = test.addMonths(1);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2011, 11, 30));
+ QCOMPARE(test.time(), QTime(2, 0, 0));
+
+ // Add month to after tran FirstOccurrence
+ test = QDateTime(QDate(2011, 10, 30), QTime(3, 0, 0));
+ test = test.addMonths(1);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2011, 11, 30));
+ QCOMPARE(test.time(), QTime(3, 0, 0));
+
+
+ // Add day to get to tran FirstOccurrence
+ test = QDateTime(QDate(2012, 10, 27), QTime(2, 0, 0));
+ test = test.addDays(1);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2012, 10, 28));
+ QCOMPARE(test.time(), QTime(2, 0, 0));
+#ifdef Q_OS_WIN
+ // Windows uses SecondOccurrence
+ QEXPECT_FAIL("", "QDateTime doesn't properly support Daylight Transitions", Continue);
+#endif // Q_OS_WIN
+ QCOMPARE(test.toMSecsSinceEpoch(), standard2012 - msecsOneHour);
+
+ // Add day to get to after tran FirstOccurrence
+ test = QDateTime(QDate(2012, 10, 27), QTime(3, 0, 0));
+ test = test.addDays(1);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2012, 10, 28));
+ QCOMPARE(test.time(), QTime(3, 0, 0));
+ QCOMPARE(test.toMSecsSinceEpoch(), standard2012 + msecsOneHour);
+
+ // Add day to tran FirstOccurrence
+ test = QDateTime(QDate(2011, 10, 30), QTime(2, 0, 0));
+ test = test.addDays(1);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2011, 10, 31));
+ QCOMPARE(test.time(), QTime(2, 0, 0));
+
+ // Add day to tran SecondOccurrence
+ test = QDateTime(QDate(2011, 10, 30), QTime(2, 0, 0)); // TODO SecondOccurrence
+ test = test.addDays(1);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2011, 10, 31));
+ QCOMPARE(test.time(), QTime(2, 0, 0));
+
+ // Add day to after tran FirstOccurrence
+ test = QDateTime(QDate(2011, 10, 30), QTime(3, 0, 0));
+ test = test.addDays(1);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2011, 10, 31));
+ QCOMPARE(test.time(), QTime(3, 0, 0));
+
+
+ // Add hour to get to tran FirstOccurrence
+ test = QDateTime(QDate(2012, 10, 28), QTime(1, 0, 0));
+ test = test.addMSecs(msecsOneHour);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2012, 10, 28));
+ QCOMPARE(test.time(), QTime(2, 0, 0));
+ QCOMPARE(test.toMSecsSinceEpoch(), standard2012 - msecsOneHour);
+
+ // Add hour to tran FirstOccurrence to get to tran SecondOccurrence
+ test = QDateTime(QDate(2012, 10, 28), QTime(2, 0, 0));
+ test = test.addMSecs(msecsOneHour);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2012, 10, 28));
+#ifdef Q_OS_WIN
+ // Windows uses SecondOccurrence
+ QEXPECT_FAIL("", "QDateTime doesn't properly support Daylight Transitions", Continue);
+#endif // Q_OS_WIN
+ QCOMPARE(test.time(), QTime(2, 0, 0));
+#ifdef Q_OS_WIN
+ // Windows uses SecondOccurrence
+ QEXPECT_FAIL("", "QDateTime doesn't properly support Daylight Transitions", Continue);
+#endif // Q_OS_WIN
+ QCOMPARE(test.toMSecsSinceEpoch(), standard2012);
+
+ // Add hour to tran SecondOccurrence to get to after tran FirstOccurrence
+ test = QDateTime(QDate(2012, 10, 28), QTime(2, 0, 0)); // TODO SecondOccurrence
+ test = test.addMSecs(msecsOneHour);
+ QVERIFY(test.isValid());
+ QCOMPARE(test.date(), QDate(2012, 10, 28));
+#if defined(Q_OS_DARWIN) || defined(Q_OS_QNX) || defined(Q_OS_ANDROID)
+ // Mac uses FirstOccurrence, Windows uses SecondOccurrence, Linux uses last calculation
+ QEXPECT_FAIL("", "QDateTime doesn't properly support Daylight Transitions", Continue);
+#endif // Q_OS_WIN
+ QCOMPARE(test.time(), QTime(3, 0, 0));
+#if defined(Q_OS_DARWIN) || defined(Q_OS_QNX) || defined(Q_OS_ANDROID)
+ // Mac uses FirstOccurrence, Windows uses SecondOccurrence, Linux uses last calculation
+ QEXPECT_FAIL("", "QDateTime doesn't properly support Daylight Transitions", Continue);
+#endif // Q_OS_WIN
+ QCOMPARE(test.toMSecsSinceEpoch(), standard2012 + msecsOneHour);
+
+ } else {
+ QSKIP("You must test using Central European (CET/CEST) time zone, e.g. TZ=Europe/Oslo");
+ }
+}
+
+void tst_QDateTime::timeZones() const
+{
+ QTimeZone invalidTz = QTimeZone("Vulcan/ShiKahr");
+ QCOMPARE(invalidTz.isValid(), false);
+ QDateTime invalidDateTime = QDateTime(QDate(2000, 1, 1), QTime(0, 0, 0), invalidTz);
+ QCOMPARE(invalidDateTime.isValid(), false);
+ QCOMPARE(invalidDateTime.date(), QDate(2000, 1, 1));
+ QCOMPARE(invalidDateTime.time(), QTime(0, 0, 0));
+
+ QTimeZone nzTz = QTimeZone("Pacific/Auckland");
+ QTimeZone nzTzOffset = QTimeZone(12 * 3600);
+
+ // During Standard Time NZ is +12:00
+ QDateTime utcStd(QDate(2012, 6, 1), QTime(0, 0, 0), Qt::UTC);
+ QDateTime nzStd(QDate(2012, 6, 1), QTime(12, 0, 0), nzTz);
+ QDateTime nzStdOffset(QDate(2012, 6, 1), QTime(12, 0, 0), nzTzOffset);
+
+ QCOMPARE(nzStd.isValid(), true);
+ QCOMPARE(nzStd.timeSpec(), Qt::TimeZone);
+ QCOMPARE(nzStd.date(), QDate(2012, 6, 1));
+ QCOMPARE(nzStd.time(), QTime(12, 0, 0));
+ QVERIFY(nzStd.timeZone() == nzTz);
+ QCOMPARE(nzStd.timeZone().id(), QByteArray("Pacific/Auckland"));
+ QCOMPARE(nzStd.offsetFromUtc(), 43200);
+ QCOMPARE(nzStd.isDaylightTime(), false);
+ QCOMPARE(nzStd.toMSecsSinceEpoch(), utcStd.toMSecsSinceEpoch());
+
+ QCOMPARE(nzStdOffset.isValid(), true);
+ QCOMPARE(nzStdOffset.timeSpec(), Qt::TimeZone);
+ QCOMPARE(nzStdOffset.date(), QDate(2012, 6, 1));
+ QCOMPARE(nzStdOffset.time(), QTime(12, 0, 0));
+ QVERIFY(nzStdOffset.timeZone() == nzTzOffset);
+ QCOMPARE(nzStdOffset.timeZone().id(), QByteArray("UTC+12:00"));
+ QCOMPARE(nzStdOffset.offsetFromUtc(), 43200);
+ QCOMPARE(nzStdOffset.isDaylightTime(), false);
+ QCOMPARE(nzStdOffset.toMSecsSinceEpoch(), utcStd.toMSecsSinceEpoch());
+
+ // During Daylight Time NZ is +13:00
+ QDateTime utcDst(QDate(2012, 1, 1), QTime(0, 0, 0), Qt::UTC);
+ QDateTime nzDst(QDate(2012, 1, 1), QTime(13, 0, 0), nzTz);
+
+ QCOMPARE(nzDst.isValid(), true);
+ QCOMPARE(nzDst.date(), QDate(2012, 1, 1));
+ QCOMPARE(nzDst.time(), QTime(13, 0, 0));
+ QCOMPARE(nzDst.offsetFromUtc(), 46800);
+ QCOMPARE(nzDst.isDaylightTime(), true);
+ QCOMPARE(nzDst.toMSecsSinceEpoch(), utcDst.toMSecsSinceEpoch());
+
+ QDateTime utc = nzStd.toUTC();
+ QCOMPARE(utc.date(), utcStd.date());
+ QCOMPARE(utc.time(), utcStd.time());
+
+ utc = nzDst.toUTC();
+ QCOMPARE(utc.date(), utcDst.date());
+ QCOMPARE(utc.time(), utcDst.time());
+
+ // Sydney is 2 hours behind New Zealand
+ QTimeZone ausTz = QTimeZone("Australia/Sydney");
+ QDateTime aus = nzStd.toTimeZone(ausTz);
+ QCOMPARE(aus.date(), QDate(2012, 6, 1));
+ QCOMPARE(aus.time(), QTime(10, 0, 0));
+
+ QDateTime dt1(QDate(2012, 6, 1), QTime(0, 0, 0), Qt::UTC);
+ QCOMPARE(dt1.timeSpec(), Qt::UTC);
+ dt1.setTimeZone(nzTz);
+ QCOMPARE(dt1.timeSpec(), Qt::TimeZone);
+ QCOMPARE(dt1.date(), QDate(2012, 6, 1));
+ QCOMPARE(dt1.time(), QTime(0, 0, 0));
+ QCOMPARE(dt1.timeZone(), nzTz);
+
+ QDateTime dt2 = QDateTime::fromSecsSinceEpoch(1338465600, nzTz);
+ QCOMPARE(dt2.date(), dt1.date());
+ QCOMPARE(dt2.time(), dt1.time());
+ QCOMPARE(dt2.timeSpec(), dt1.timeSpec());
+ QCOMPARE(dt2.timeZone(), dt1.timeZone());
+
+ QDateTime dt3 = QDateTime::fromMSecsSinceEpoch(1338465600000, nzTz);
+ QCOMPARE(dt3.date(), dt1.date());
+ QCOMPARE(dt3.time(), dt1.time());
+ QCOMPARE(dt3.timeSpec(), dt1.timeSpec());
+ QCOMPARE(dt3.timeZone(), dt1.timeZone());
+
+ // Check datastream serialises the time zone
+ QByteArray tmp;
+ {
+ QDataStream ds(&tmp, QIODevice::WriteOnly);
+ ds << dt1;
+ }
+ QDateTime dt4;
+ {
+ QDataStream ds(&tmp, QIODevice::ReadOnly);
+ ds >> dt4;
+ }
+ QCOMPARE(dt4, dt1);
+ QCOMPARE(dt4.timeSpec(), Qt::TimeZone);
+ QCOMPARE(dt4.timeZone(), nzTz);
+
+ // Check handling of transition times
+ QTimeZone cet("Europe/Oslo");
+
+ // Standard Time to Daylight Time 2013 on 2013-03-31 is 2:00 local time / 1:00 UTC
+ qint64 stdToDstMSecs = 1364691600000;
+
+ // Test MSecs to local
+ // - Test 1 msec before tran = 01:59:59.999
+ QDateTime beforeDst = QDateTime::fromMSecsSinceEpoch(stdToDstMSecs - 1, cet);
+ QCOMPARE(beforeDst.date(), QDate(2013, 3, 31));
+ QCOMPARE(beforeDst.time(), QTime(1, 59, 59, 999));
+ // - Test at tran = 03:00:00
+ QDateTime atDst = QDateTime::fromMSecsSinceEpoch(stdToDstMSecs, cet);
+ QCOMPARE(atDst.date(), QDate(2013, 3, 31));
+ QCOMPARE(atDst.time(), QTime(3, 0, 0));
+
+ // Test local to MSecs
+ // - Test 1 msec before tran = 01:59:59.999
+ beforeDst = QDateTime(QDate(2013, 3, 31), QTime(1, 59, 59, 999), cet);
+ QCOMPARE(beforeDst.toMSecsSinceEpoch(), stdToDstMSecs - 1);
+ // - Test at tran = 03:00:00
+ atDst = QDateTime(QDate(2013, 3, 31), QTime(3, 0, 0), cet);
+ QCOMPARE(atDst.toMSecsSinceEpoch(), stdToDstMSecs);
+ // - Test transition hole, setting 03:00:00 is valid
+ atDst = QDateTime(QDate(2013, 3, 31), QTime(3, 0, 0), cet);
+ QVERIFY(atDst.isValid());
+ QCOMPARE(atDst.date(), QDate(2013, 3, 31));
+ QCOMPARE(atDst.time(), QTime(3, 0, 0));
+ QCOMPARE(atDst.toMSecsSinceEpoch(), stdToDstMSecs);
+ // - Test transition hole, setting 02:00:00 is invalid
+ atDst = QDateTime(QDate(2013, 3, 31), QTime(2, 0, 0), cet);
+ QVERIFY(!atDst.isValid());
+ QCOMPARE(atDst.date(), QDate(2013, 3, 31));
+ QCOMPARE(atDst.time(), QTime(2, 0, 0));
+ // - Test transition hole, setting 02:59:59.999 is invalid
+ atDst = QDateTime(QDate(2013, 3, 31), QTime(2, 59, 59, 999), cet);
+ QVERIFY(!atDst.isValid());
+ QCOMPARE(atDst.date(), QDate(2013, 3, 31));
+ QCOMPARE(atDst.time(), QTime(2, 59, 59, 999));
+
+ // Standard Time to Daylight Time 2013 on 2013-10-27 is 3:00 local time / 1:00 UTC
+ qint64 dstToStdMSecs = 1382835600000;
+
+ // Test MSecs to local
+ // - Test 1 hour before tran = 02:00:00 local first occurrence
+ QDateTime hourBeforeStd = QDateTime::fromMSecsSinceEpoch(dstToStdMSecs - 3600000, cet);
+ QCOMPARE(hourBeforeStd.date(), QDate(2013, 10, 27));
+ QCOMPARE(hourBeforeStd.time(), QTime(2, 0, 0));
+ // - Test 1 msec before tran = 02:59:59.999 local first occurrence
+ QDateTime msecBeforeStd = QDateTime::fromMSecsSinceEpoch(dstToStdMSecs - 1, cet);
+ QCOMPARE(msecBeforeStd.date(), QDate(2013, 10, 27));
+ QCOMPARE(msecBeforeStd.time(), QTime(2, 59, 59, 999));
+ // - Test at tran = 03:00:00 local becomes 02:00:00 local second occurrence
+ QDateTime atStd = QDateTime::fromMSecsSinceEpoch(dstToStdMSecs, cet);
+ QCOMPARE(atStd.date(), QDate(2013, 10, 27));
+ QCOMPARE(atStd.time(), QTime(2, 0, 0));
+ // - Test 59 mins after tran = 02:59:59.999 local second occurrence
+ QDateTime afterStd = QDateTime::fromMSecsSinceEpoch(dstToStdMSecs + 3600000 -1, cet);
+ QCOMPARE(afterStd.date(), QDate(2013, 10, 27));
+ QCOMPARE(afterStd.time(), QTime(2, 59, 59, 999));
+ // - Test 1 hour after tran = 03:00:00 local
+ QDateTime hourAfterStd = QDateTime::fromMSecsSinceEpoch(dstToStdMSecs + 3600000, cet);
+ QCOMPARE(hourAfterStd.date(), QDate(2013, 10, 27));
+ QCOMPARE(hourAfterStd.time(), QTime(3, 00, 00));
+
+ // Test local to MSecs
+ // - Test first occurrence 02:00:00 = 1 hour before tran
+ hourBeforeStd = QDateTime(QDate(2013, 10, 27), QTime(2, 0, 0), cet);
+ QEXPECT_FAIL("", "QDateTime doesn't properly support Daylight Transitions", Continue);
+ QCOMPARE(hourBeforeStd.toMSecsSinceEpoch(), dstToStdMSecs - 3600000);
+ // - Test first occurrence 02:59:59.999 = 1 msec before tran
+ msecBeforeStd = QDateTime(QDate(2013, 10, 27), QTime(2, 59, 59, 999), cet);
+ QEXPECT_FAIL("", "QDateTime doesn't properly support Daylight Transitions", Continue);
+ QCOMPARE(msecBeforeStd.toMSecsSinceEpoch(), dstToStdMSecs - 1);
+ // - Test second occurrence 02:00:00 = at tran
+ atStd = QDateTime(QDate(2013, 10, 27), QTime(2, 0, 0), cet);
+ QCOMPARE(atStd.toMSecsSinceEpoch(), dstToStdMSecs);
+ // - Test second occurrence 03:00:00 = 59 mins after tran
+ afterStd = QDateTime(QDate(2013, 10, 27), QTime(2, 59, 59, 999), cet);
+ QCOMPARE(afterStd.toMSecsSinceEpoch(), dstToStdMSecs + 3600000 - 1);
+ // - Test 03:00:00 = 1 hour after tran
+ hourAfterStd = QDateTime(QDate(2013, 10, 27), QTime(3, 0, 0), cet);
+ QCOMPARE(hourAfterStd.toMSecsSinceEpoch(), dstToStdMSecs + 3600000);
+
+ // Test Time Zone that has transitions but no future transitions afer a given date
+ QTimeZone sgt("Asia/Singapore");
+ QDateTime future(QDate(2015, 1, 1), QTime(0, 0, 0), sgt);
+ QVERIFY(future.isValid());
+ QCOMPARE(future.offsetFromUtc(), 28800);
+}
+
+void tst_QDateTime::systemTimeZoneChange() const
+{
+ // Set the timezone to Brisbane time
+ TimeZoneRollback useZone(QByteArray("AEST-10:00"));
+
+ QDateTime localDate = QDateTime(QDate(2012, 6, 1), QTime(2, 15, 30), Qt::LocalTime);
+ QDateTime utcDate = QDateTime(QDate(2012, 6, 1), QTime(2, 15, 30), Qt::UTC);
+ QDateTime tzDate = QDateTime(QDate(2012, 6, 1), QTime(2, 15, 30), QTimeZone("Australia/Brisbane"));
+ qint64 localMsecs = localDate.toMSecsSinceEpoch();
+ qint64 utcMsecs = utcDate.toMSecsSinceEpoch();
+ qint64 tzMsecs = tzDate.toMSecsSinceEpoch();
+
+ // check that Australia/Brisbane is known
+ QVERIFY(tzDate.timeZone().isValid());
+
+ // Change to Indian time
+ useZone.reset(QByteArray("IST-05:30"));
+
+ QCOMPARE(localDate, QDateTime(QDate(2012, 6, 1), QTime(2, 15, 30), Qt::LocalTime));
+#ifdef Q_OS_WINRT
+ QEXPECT_FAIL("", "WinRT gets this wrong, QTBUG-71185", Continue);
+#endif
+ QVERIFY(localMsecs != localDate.toMSecsSinceEpoch());
+ QCOMPARE(utcDate, QDateTime(QDate(2012, 6, 1), QTime(2, 15, 30), Qt::UTC));
+ QCOMPARE(utcDate.toMSecsSinceEpoch(), utcMsecs);
+ QCOMPARE(tzDate, QDateTime(QDate(2012, 6, 1), QTime(2, 15, 30), QTimeZone("Australia/Brisbane")));
+ QCOMPARE(tzDate.toMSecsSinceEpoch(), tzMsecs);
+}
+
+void tst_QDateTime::invalid() const
+{
+ QDateTime invalidDate = QDateTime(QDate(0, 0, 0), QTime(-1, -1, -1));
+ QCOMPARE(invalidDate.isValid(), false);
+ QCOMPARE(invalidDate.timeSpec(), Qt::LocalTime);
+
+ QDateTime utcDate = invalidDate.toUTC();
+ QCOMPARE(utcDate.isValid(), false);
+ QCOMPARE(utcDate.timeSpec(), Qt::UTC);
+
+ QDateTime offsetDate = invalidDate.toOffsetFromUtc(3600);
+ QCOMPARE(offsetDate.isValid(), false);
+ QCOMPARE(offsetDate.timeSpec(), Qt::OffsetFromUTC);
+
+ QDateTime tzDate = invalidDate.toTimeZone(QTimeZone("Europe/Oslo"));
+ QCOMPARE(tzDate.isValid(), false);
+ QCOMPARE(tzDate.timeSpec(), Qt::TimeZone);
+}
+
+void tst_QDateTime::macTypes()
+{
+#ifndef Q_OS_MAC
+ QSKIP("This is a Apple-only test");
+#else
+ extern void tst_QDateTime_macTypes(); // in qdatetime_mac.mm
+ tst_QDateTime_macTypes();
+#endif
+}
+
+QTEST_APPLESS_MAIN(tst_QDateTime)
+#include "tst_qdatetime.moc"
diff --git a/tests/auto/corelib/time/qdatetime/tst_qdatetime_mac.mm b/tests/auto/corelib/time/qdatetime/tst_qdatetime_mac.mm
new file mode 100644
index 0000000000..f73c7b9d5d
--- /dev/null
+++ b/tests/auto/corelib/time/qdatetime/tst_qdatetime_mac.mm
@@ -0,0 +1,70 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2014 Petroules Corporation.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtCore/QDateTime>
+#include <QtTest/QtTest>
+
+#include <CoreFoundation/CoreFoundation.h>
+#include <Foundation/Foundation.h>
+
+void tst_QDateTime_macTypes()
+{
+ // QDateTime <-> CFDate
+
+ static const int kMsPerSecond = 1000;
+
+ for (int i = 0; i < kMsPerSecond; ++i) {
+ QDateTime qtDateTime = QDateTime::fromMSecsSinceEpoch(i);
+ const CFDateRef cfDate = qtDateTime.toCFDate();
+ QCOMPARE(QDateTime::fromCFDate(cfDate), qtDateTime);
+ CFRelease(cfDate);
+ }
+ {
+ QDateTime qtDateTime = QDateTime::fromMSecsSinceEpoch(0);
+ const CFDateRef cfDate = qtDateTime.toCFDate();
+ QDateTime qtDateTimeCopy(qtDateTime);
+ qtDateTime.setTime_t(10000); // modify
+ QCOMPARE(QDateTime::fromCFDate(cfDate), qtDateTimeCopy);
+ }
+ // QDateTime <-> NSDate
+ for (int i = 0; i < kMsPerSecond; ++i) {
+ QMacAutoReleasePool pool;
+ QDateTime qtDateTime = QDateTime::fromMSecsSinceEpoch(i);
+ const NSDate *nsDate = qtDateTime.toNSDate();
+ QCOMPARE(QDateTime::fromNSDate(nsDate), qtDateTime);
+ }
+ {
+ QMacAutoReleasePool pool;
+ QDateTime qtDateTime = QDateTime::fromMSecsSinceEpoch(0);
+ const NSDate *nsDate = qtDateTime.toNSDate();
+ QDateTime qtDateTimeCopy(qtDateTime);
+ qtDateTime.setTime_t(10000); // modify
+ QCOMPARE(QDateTime::fromNSDate(nsDate), qtDateTimeCopy);
+ }
+}
diff --git a/tests/auto/corelib/time/qtime/.gitignore b/tests/auto/corelib/time/qtime/.gitignore
new file mode 100644
index 0000000000..26a4c65cc2
--- /dev/null
+++ b/tests/auto/corelib/time/qtime/.gitignore
@@ -0,0 +1 @@
+tst_qtime
diff --git a/tests/auto/corelib/time/qtime/CMakeLists.txt b/tests/auto/corelib/time/qtime/CMakeLists.txt
new file mode 100644
index 0000000000..0b8b4c81f3
--- /dev/null
+++ b/tests/auto/corelib/time/qtime/CMakeLists.txt
@@ -0,0 +1 @@
+add_qt_test(tst_qtime SOURCES tst_qtime.cpp)
diff --git a/tests/auto/corelib/time/qtime/qtime.pro b/tests/auto/corelib/time/qtime/qtime.pro
new file mode 100644
index 0000000000..0973b7a9ef
--- /dev/null
+++ b/tests/auto/corelib/time/qtime/qtime.pro
@@ -0,0 +1,4 @@
+CONFIG += testcase
+TARGET = tst_qtime
+QT = core testlib
+SOURCES = tst_qtime.cpp
diff --git a/tests/auto/corelib/time/qtime/tst_qtime.cpp b/tests/auto/corelib/time/qtime/tst_qtime.cpp
new file mode 100644
index 0000000000..3403c5bf7f
--- /dev/null
+++ b/tests/auto/corelib/time/qtime/tst_qtime.cpp
@@ -0,0 +1,803 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtTest/QtTest>
+#include "qdatetime.h"
+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
+# include <locale.h>
+#endif
+
+class tst_QTime : public QObject
+{
+ Q_OBJECT
+
+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
+public:
+ tst_QTime()
+ {
+ // Some tests depend on C locale - BF&I it with belt *and* braces:
+ qputenv("LC_ALL", "C");
+ setlocale(LC_ALL, "C");
+ // Need to instantiate as early as possible, before anything accesses
+ // the QSystemLocale singleton; once it exists, there's no changing it.
+ }
+#endif // remove for ### Qt 6
+
+private slots:
+ void msecsTo_data();
+ void msecsTo();
+ void secsTo_data();
+ void secsTo();
+ void setHMS_data();
+ void setHMS();
+ void hour_data();
+ void hour();
+ void isValid();
+ void isNull();
+ void addMSecs_data();
+ void addMSecs();
+ void addSecs_data();
+ void addSecs();
+ void operator_eq_eq_data();
+ void operator_eq_eq();
+ void operator_lt();
+ void operator_gt();
+ void operator_lt_eq();
+ void operator_gt_eq();
+ void fromStringFormat_data();
+ void fromStringFormat();
+ void fromStringDateFormat_data();
+ void fromStringDateFormat();
+ void toStringDateFormat_data();
+ void toStringDateFormat();
+ void toStringFormat_data();
+ void toStringFormat();
+ void toStringLocale();
+ void msecsSinceStartOfDay_data();
+ void msecsSinceStartOfDay();
+
+private:
+ QTime invalidTime() { return QTime(-1, -1, -1); }
+};
+
+Q_DECLARE_METATYPE(Qt::DateFormat)
+
+void tst_QTime::addSecs_data()
+{
+ QTest::addColumn<QTime>("t1");
+ QTest::addColumn<int>("i");
+ QTest::addColumn<QTime>("exp");
+
+ QTest::newRow("Data0") << QTime(0,0,0) << 200 << QTime(0,3,20);
+ QTest::newRow("Data1") << QTime(0,0,0) << 20 << QTime(0,0,20);
+ QTest::newRow("overflow")
+ << QTime(0,0,0) << (INT_MAX / 1000 + 1)
+ << QTime::fromMSecsSinceStartOfDay(((INT_MAX / 1000 + 1) % 86400) * 1000);
+}
+
+void tst_QTime::addSecs()
+{
+ QFETCH( QTime, t1 );
+ QFETCH( int, i );
+ QTime t2;
+ t2 = t1.addSecs( i );
+ QFETCH( QTime, exp );
+ QCOMPARE( t2, exp );
+}
+
+void tst_QTime::addMSecs_data()
+{
+ QTest::addColumn<QTime>("t1");
+ QTest::addColumn<int>("i");
+ QTest::addColumn<QTime>("exp");
+
+ // start with testing positive values
+ QTest::newRow( "Data1_0") << QTime(0,0,0,0) << 2000 << QTime(0,0,2,0);
+ QTest::newRow( "Data1_1") << QTime(0,0,0,0) << 200 << QTime(0,0,0,200);
+ QTest::newRow( "Data1_2") << QTime(0,0,0,0) << 20 << QTime(0,0,0,20);
+ QTest::newRow( "Data1_3") << QTime(0,0,0,1) << 1 << QTime(0,0,0,2);
+ QTest::newRow( "Data1_4") << QTime(0,0,0,0) << 0 << QTime(0,0,0,0);
+
+ QTest::newRow( "Data2_0") << QTime(0,0,0,98) << 0 << QTime(0,0,0,98);
+ QTest::newRow( "Data2_1") << QTime(0,0,0,98) << 1 << QTime(0,0,0,99);
+ QTest::newRow( "Data2_2") << QTime(0,0,0,98) << 2 << QTime(0,0,0,100);
+ QTest::newRow( "Data2_3") << QTime(0,0,0,98) << 3 << QTime(0,0,0,101);
+
+ QTest::newRow( "Data3_0") << QTime(0,0,0,998) << 0 << QTime(0,0,0,998);
+ QTest::newRow( "Data3_1") << QTime(0,0,0,998) << 1 << QTime(0,0,0,999);
+ QTest::newRow( "Data3_2") << QTime(0,0,0,998) << 2 << QTime(0,0,1,0);
+ QTest::newRow( "Data3_3") << QTime(0,0,0,998) << 3 << QTime(0,0,1,1);
+
+ QTest::newRow( "Data4_0") << QTime(0,0,1,995) << 4 << QTime(0,0,1,999);
+ QTest::newRow( "Data4_1") << QTime(0,0,1,995) << 5 << QTime(0,0,2,0);
+ QTest::newRow( "Data4_2") << QTime(0,0,1,995) << 6 << QTime(0,0,2,1);
+ QTest::newRow( "Data4_3") << QTime(0,0,1,995) << 100 << QTime(0,0,2,95);
+ QTest::newRow( "Data4_4") << QTime(0,0,1,995) << 105 << QTime(0,0,2,100);
+
+ QTest::newRow( "Data5_0") << QTime(0,0,59,995) << 4 << QTime(0,0,59,999);
+ QTest::newRow( "Data5_1") << QTime(0,0,59,995) << 5 << QTime(0,1,0,0);
+ QTest::newRow( "Data5_2") << QTime(0,0,59,995) << 6 << QTime(0,1,0,1);
+ QTest::newRow( "Data5_3") << QTime(0,0,59,995) << 1006 << QTime(0,1,1,1);
+
+ QTest::newRow( "Data6_0") << QTime(0,59,59,995) << 4 << QTime(0,59,59,999);
+ QTest::newRow( "Data6_1") << QTime(0,59,59,995) << 5 << QTime(1,0,0,0);
+ QTest::newRow( "Data6_2") << QTime(0,59,59,995) << 6 << QTime(1,0,0,1);
+ QTest::newRow( "Data6_3") << QTime(0,59,59,995) << 106 << QTime(1,0,0,101);
+ QTest::newRow( "Data6_4") << QTime(0,59,59,995) << 1004 << QTime(1,0,0,999);
+ QTest::newRow( "Data6_5") << QTime(0,59,59,995) << 1005 << QTime(1,0,1,0);
+ QTest::newRow( "Data6_6") << QTime(0,59,59,995) << 61006 << QTime(1,1,1,1);
+
+ QTest::newRow( "Data7_0") << QTime(23,59,59,995) << 0 << QTime(23,59,59,995);
+ QTest::newRow( "Data7_1") << QTime(23,59,59,995) << 4 << QTime(23,59,59,999);
+ QTest::newRow( "Data7_2") << QTime(23,59,59,995) << 5 << QTime(0,0,0,0);
+ QTest::newRow( "Data7_3") << QTime(23,59,59,995) << 6 << QTime(0,0,0,1);
+ QTest::newRow( "Data7_4") << QTime(23,59,59,995) << 7 << QTime(0,0,0,2);
+
+ // must test negative values too...
+ QTest::newRow( "Data11_0") << QTime(0,0,2,0) << -2000 << QTime(0,0,0,0);
+ QTest::newRow( "Data11_1") << QTime(0,0,0,200) << -200 << QTime(0,0,0,0);
+ QTest::newRow( "Data11_2") << QTime(0,0,0,20) << -20 << QTime(0,0,0,0);
+ QTest::newRow( "Data11_3") << QTime(0,0,0,2) << -1 << QTime(0,0,0,1);
+ QTest::newRow( "Data11_4") << QTime(0,0,0,0) << -0 << QTime(0,0,0,0);
+
+ QTest::newRow( "Data12_0") << QTime(0,0,0,98) << -0 << QTime(0,0,0,98);
+ QTest::newRow( "Data12_1") << QTime(0,0,0,99) << -1 << QTime(0,0,0,98);
+ QTest::newRow( "Data12_2") << QTime(0,0,0,100) << -2 << QTime(0,0,0,98);
+ QTest::newRow( "Data12_3") << QTime(0,0,0,101) << -3 << QTime(0,0,0,98);
+
+ QTest::newRow( "Data13_0") << QTime(0,0,0,998) << -0 << QTime(0,0,0,998);
+ QTest::newRow( "Data13_1") << QTime(0,0,0,999) << -1 << QTime(0,0,0,998);
+ QTest::newRow( "Data13_2") << QTime(0,0,1,0) << -2 << QTime(0,0,0,998);
+ QTest::newRow( "Data13_3") << QTime(0,0,1,1) << -3 << QTime(0,0,0,998);
+
+ QTest::newRow( "Data14_0") << QTime(0,0,1,999) << -4 << QTime(0,0,1,995);
+ QTest::newRow( "Data14_1") << QTime(0,0,2,0) << -5 << QTime(0,0,1,995);
+ QTest::newRow( "Data14_2") << QTime(0,0,2,1) << -6 << QTime(0,0,1,995);
+ QTest::newRow( "Data14_3") << QTime(0,0,2,95) << -100 << QTime(0,0,1,995);
+ QTest::newRow( "Data14_4") << QTime(0,0,2,100) << -105 << QTime(0,0,1,995);
+
+ QTest::newRow( "Data15_0") << QTime(0,0,59,999) << -4 << QTime(0,0,59,995);
+ QTest::newRow( "Data15_1") << QTime(0,1,0,0) << -5 << QTime(0,0,59,995);
+ QTest::newRow( "Data15_2") << QTime(0,1,0,1) << -6 << QTime(0,0,59,995);
+ QTest::newRow( "Data15_3") << QTime(0,1,1,1) << -1006 << QTime(0,0,59,995);
+
+ QTest::newRow( "Data16_0") << QTime(0,59,59,999) << -4 << QTime(0,59,59,995);
+ QTest::newRow( "Data16_1") << QTime(1,0,0,0) << -5 << QTime(0,59,59,995);
+ QTest::newRow( "Data16_2") << QTime(1,0,0,1) << -6 << QTime(0,59,59,995);
+ QTest::newRow( "Data16_3") << QTime(1,0,0,101) << -106 << QTime(0,59,59,995);
+ QTest::newRow( "Data16_4") << QTime(1,0,0,999) << -1004 << QTime(0,59,59,995);
+ QTest::newRow( "Data16_5") << QTime(1,0,1,0) << -1005 << QTime(0,59,59,995);
+ QTest::newRow( "Data16_6") << QTime(1,1,1,1) << -61006 << QTime(0,59,59,995);
+
+ QTest::newRow( "Data17_0") << QTime(23,59,59,995) << -0 << QTime(23,59,59,995);
+ QTest::newRow( "Data17_1") << QTime(23,59,59,999) << -4 << QTime(23,59,59,995);
+ QTest::newRow( "Data17_2") << QTime(0,0,0,0) << -5 << QTime(23,59,59,995);
+ QTest::newRow( "Data17_3") << QTime(0,0,0,1) << -6 << QTime(23,59,59,995);
+ QTest::newRow( "Data17_4") << QTime(0,0,0,2) << -7 << QTime(23,59,59,995);
+
+ QTest::newRow( "Data18_0" ) << invalidTime() << 1 << invalidTime();
+}
+
+void tst_QTime::addMSecs()
+{
+ QFETCH( QTime, t1 );
+ QFETCH( int, i );
+ QTime t2;
+ t2 = t1.addMSecs( i );
+ QFETCH( QTime, exp );
+ QCOMPARE( t2, exp );
+}
+
+void tst_QTime::isNull()
+{
+ QTime t1;
+ QVERIFY( t1.isNull() );
+ QTime t2(0,0,0);
+ QVERIFY( !t2.isNull() );
+ QTime t3(0,0,1);
+ QVERIFY( !t3.isNull() );
+ QTime t4(0,0,0,1);
+ QVERIFY( !t4.isNull() );
+ QTime t5(23,59,59);
+ QVERIFY( !t5.isNull() );
+}
+
+void tst_QTime::isValid()
+{
+ QTime t1;
+ QVERIFY( !t1.isValid() );
+ QTime t2(24,0,0,0);
+ QVERIFY( !t2.isValid() );
+ QTime t3(23,60,0,0);
+ QVERIFY( !t3.isValid() );
+ QTime t4(23,0,-1,0);
+ QVERIFY( !t4.isValid() );
+ QTime t5(23,0,60,0);
+ QVERIFY( !t5.isValid() );
+ QTime t6(23,0,0,1000);
+ QVERIFY( !t6.isValid() );
+}
+
+void tst_QTime::hour_data()
+{
+ QTest::addColumn<int>("hour");
+ QTest::addColumn<int>("minute");
+ QTest::addColumn<int>("sec");
+ QTest::addColumn<int>("msec");
+
+ QTest::newRow( "data0" ) << 0 << 0 << 0 << 0;
+ QTest::newRow( "data1" ) << 0 << 0 << 0 << 1;
+ QTest::newRow( "data2" ) << 1 << 2 << 3 << 4;
+ QTest::newRow( "data3" ) << 2 << 12 << 13 << 65;
+ QTest::newRow( "data4" ) << 23 << 59 << 59 << 999;
+ QTest::newRow( "data5" ) << -1 << -1 << -1 << -1;
+}
+
+void tst_QTime::hour()
+{
+ QFETCH( int, hour );
+ QFETCH( int, minute );
+ QFETCH( int, sec );
+ QFETCH( int, msec );
+
+ QTime t1( hour, minute, sec, msec );
+ QCOMPARE( t1.hour(), hour );
+ QCOMPARE( t1.minute(), minute );
+ QCOMPARE( t1.second(), sec );
+ QCOMPARE( t1.msec(), msec );
+}
+
+void tst_QTime::setHMS_data()
+{
+ QTest::addColumn<int>("hour");
+ QTest::addColumn<int>("minute");
+ QTest::addColumn<int>("sec");
+
+ QTest::newRow( "data0" ) << 0 << 0 << 0;
+ QTest::newRow( "data1" ) << 1 << 2 << 3;
+ QTest::newRow( "data2" ) << 0 << 59 << 0;
+ QTest::newRow( "data3" ) << 0 << 59 << 59;
+ QTest::newRow( "data4" ) << 23 << 0 << 0;
+ QTest::newRow( "data5" ) << 23 << 59 << 0;
+ QTest::newRow( "data6" ) << 23 << 59 << 59;
+ QTest::newRow( "data7" ) << -1 << -1 << -1;
+}
+
+void tst_QTime::setHMS()
+{
+ QFETCH( int, hour );
+ QFETCH( int, minute );
+ QFETCH( int, sec );
+
+ QTime t(3,4,5);
+ t.setHMS( hour, minute, sec );
+ QCOMPARE( t.hour(), hour );
+ QCOMPARE( t.minute(), minute );
+ QCOMPARE( t.second(), sec );
+}
+
+void tst_QTime::secsTo_data()
+{
+ QTest::addColumn<QTime>("t1");
+ QTest::addColumn<QTime>("t2");
+ QTest::addColumn<int>("delta");
+
+ QTest::newRow( "data0" ) << QTime(0,0,0) << QTime(0,0,59) << 59;
+ QTest::newRow( "data1" ) << QTime(0,0,0) << QTime(0,1,0) << 60;
+ QTest::newRow( "data2" ) << QTime(0,0,0) << QTime(0,10,0) << 600;
+ QTest::newRow( "data3" ) << QTime(0,0,0) << QTime(23,59,59) << 86399;
+ QTest::newRow( "data4" ) << QTime(-1, -1, -1) << QTime(0, 0, 0) << 0;
+ QTest::newRow( "data5" ) << QTime(0, 0, 0) << QTime(-1, -1, -1) << 0;
+ QTest::newRow( "data6" ) << QTime(-1, -1, -1) << QTime(-1, -1, -1) << 0;
+ QTest::newRow("disregard msec (1s)") << QTime(12, 30, 1, 500) << QTime(12, 30, 2, 400) << 1;
+ QTest::newRow("disregard msec (0s)") << QTime(12, 30, 1, 500) << QTime(12, 30, 1, 900) << 0;
+ QTest::newRow("disregard msec (-1s)") << QTime(12, 30, 2, 400) << QTime(12, 30, 1, 500) << -1;
+ QTest::newRow("disregard msec (0s)") << QTime(12, 30, 1, 900) << QTime(12, 30, 1, 500) << 0;
+}
+
+void tst_QTime::secsTo()
+{
+ QFETCH( QTime, t1 );
+ QFETCH( QTime, t2 );
+ QFETCH( int, delta );
+
+ QCOMPARE( t1.secsTo( t2 ), delta );
+}
+
+void tst_QTime::msecsTo_data()
+{
+ QTest::addColumn<QTime>("t1");
+ QTest::addColumn<QTime>("t2");
+ QTest::addColumn<int>("delta");
+
+ QTest::newRow( "data0" ) << QTime(0,0,0,0) << QTime(0,0,0,0) << 0;
+ QTest::newRow( "data1" ) << QTime(0,0,0,0) << QTime(0,0,1,0) << 1000;
+ QTest::newRow( "data2" ) << QTime(0,0,0,0) << QTime(0,0,10,0) << 10000;
+ QTest::newRow( "data3" ) << QTime(0,0,0,0) << QTime(23,59,59,0) << 86399000;
+ QTest::newRow( "data4" ) << QTime(-1, -1, -1, -1) << QTime(0, 0, 0, 0) << 0;
+ QTest::newRow( "data5" ) << QTime(0, 0, 0, 0) << QTime(-1, -1, -1, -1) << 0;
+ QTest::newRow( "data6" ) << QTime(-1, -1, -1, -1) << QTime(-1, -1, -1, -1) << 0;
+}
+
+void tst_QTime::msecsTo()
+{
+ QFETCH( QTime, t1 );
+ QFETCH( QTime, t2 );
+ QFETCH( int, delta );
+
+ QCOMPARE( t1.msecsTo( t2 ), delta );
+}
+
+void tst_QTime::operator_eq_eq_data()
+{
+ QTest::addColumn<QTime>("t1");
+ QTest::addColumn<QTime>("t2");
+ QTest::addColumn<bool>("expectEqual");
+
+ QTime time1(0, 0, 0, 0);
+ QTime time2 = time1.addMSecs(1);
+ QTime time3 = time1.addMSecs(-1);
+ QTime time4(23, 59, 59, 999);
+
+ QTest::newRow("data0") << time1 << time2 << false;
+ QTest::newRow("data1") << time2 << time3 << false;
+ QTest::newRow("data2") << time4 << time1 << false;
+ QTest::newRow("data3") << time1 << time1 << true;
+ QTest::newRow("data4") << QTime(12,34,56,20) << QTime(12,34,56,20) << true;
+ QTest::newRow("data5") << QTime(01,34,56,20) << QTime(13,34,56,20) << false;
+}
+
+void tst_QTime::operator_eq_eq()
+{
+ QFETCH(QTime, t1);
+ QFETCH(QTime, t2);
+ QFETCH(bool, expectEqual);
+
+ bool equal = t1 == t2;
+ QCOMPARE(equal, expectEqual);
+ bool notEqual = t1 != t2;
+ QCOMPARE(notEqual, !expectEqual);
+
+ if (equal)
+ QVERIFY(qHash(t1) == qHash(t2));
+}
+
+void tst_QTime::operator_lt()
+{
+ QTime t1(0,0,0,0);
+ QTime t2(0,0,0,0);
+ QVERIFY( !(t1 < t2) );
+
+ t1 = QTime(12,34,56,20);
+ t2 = QTime(12,34,56,30);
+ QVERIFY( t1 < t2 );
+
+ t1 = QTime(13,34,46,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( t1 < t2 );
+
+ t1 = QTime(13,24,56,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( t1 < t2 );
+
+ t1 = QTime(12,34,56,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( t1 < t2 );
+
+ t1 = QTime(14,34,56,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( !(t1 < t2) );
+
+ t1 = QTime(13,44,56,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( !(t1 < t2) );
+
+ t1 = QTime(13,34,56,20);
+ t2 = QTime(13,34,46,20);
+ QVERIFY( !(t1 < t2) );
+
+ t1 = QTime(13,44,56,30);
+ t2 = QTime(13,44,56,20);
+ QVERIFY( !(t1 < t2) );
+}
+
+void tst_QTime::operator_gt()
+{
+ QTime t1(0,0,0,0);
+ QTime t2(0,0,0,0);
+ QVERIFY( !(t1 > t2) );
+
+ t1 = QTime(12,34,56,20);
+ t2 = QTime(12,34,56,30);
+ QVERIFY( !(t1 > t2) );
+
+ t1 = QTime(13,34,46,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( !(t1 > t2) );
+
+ t1 = QTime(13,24,56,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( !(t1 > t2) );
+
+ t1 = QTime(12,34,56,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( !(t1 > t2) );
+
+ t1 = QTime(14,34,56,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( t1 > t2 );
+
+ t1 = QTime(13,44,56,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( t1 > t2 );
+
+ t1 = QTime(13,34,56,20);
+ t2 = QTime(13,34,46,20);
+ QVERIFY( t1 > t2 );
+
+ t1 = QTime(13,44,56,30);
+ t2 = QTime(13,44,56,20);
+ QVERIFY( t1 > t2 );
+}
+
+void tst_QTime::operator_lt_eq()
+{
+ QTime t1(0,0,0,0);
+ QTime t2(0,0,0,0);
+ QVERIFY( t1 <= t2 );
+
+ t1 = QTime(12,34,56,20);
+ t2 = QTime(12,34,56,30);
+ QVERIFY( t1 <= t2 );
+
+ t1 = QTime(13,34,46,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( t1 <= t2 );
+
+ t1 = QTime(13,24,56,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( t1 <= t2 );
+
+ t1 = QTime(12,34,56,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( t1 <= t2 );
+
+ t1 = QTime(14,34,56,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( !(t1 <= t2) );
+
+ t1 = QTime(13,44,56,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( !(t1 <= t2) );
+
+ t1 = QTime(13,34,56,20);
+ t2 = QTime(13,34,46,20);
+ QVERIFY( !(t1 <= t2) );
+
+ t1 = QTime(13,44,56,30);
+ t2 = QTime(13,44,56,20);
+ QVERIFY( !(t1 <= t2) );
+}
+
+void tst_QTime::operator_gt_eq()
+{
+ QTime t1(0,0,0,0);
+ QTime t2(0,0,0,0);
+ QVERIFY( t1 >= t2 );
+
+ t1 = QTime(12,34,56,20);
+ t2 = QTime(12,34,56,30);
+ QVERIFY( !(t1 >= t2) );
+
+ t1 = QTime(13,34,46,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( !(t1 >= t2) );
+
+ t1 = QTime(13,24,56,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( !(t1 >= t2) );
+
+ t1 = QTime(12,34,56,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( !(t1 >= t2) );
+
+ t1 = QTime(14,34,56,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( t1 >= t2 );
+
+ t1 = QTime(13,44,56,20);
+ t2 = QTime(13,34,56,20);
+ QVERIFY( t1 >= t2 );
+
+ t1 = QTime(13,34,56,20);
+ t2 = QTime(13,34,46,20);
+ QVERIFY( t1 >= t2 );
+
+ t1 = QTime(13,44,56,30);
+ t2 = QTime(13,44,56,20);
+ QVERIFY( t1 >= t2 );
+}
+
+void tst_QTime::fromStringFormat_data()
+{
+ QTest::addColumn<QString>("string");
+ QTest::addColumn<QString>("format");
+ QTest::addColumn<QTime>("expected");
+
+ QTest::newRow("data0") << QString("1010") << QString("mmm") << QTime(0, 10, 0);
+ QTest::newRow("data1") << QString("00") << QString("hm") << invalidTime();
+ QTest::newRow("data2") << QString("10am") << QString("hap") << QTime(10, 0, 0);
+ QTest::newRow("data3") << QString("10pm") << QString("hap") << QTime(22, 0, 0);
+ QTest::newRow("data4") << QString("10pmam") << QString("hapap") << invalidTime();
+ QTest::newRow("data5") << QString("1070") << QString("hhm") << invalidTime();
+ QTest::newRow("data6") << QString("1011") << QString("hh") << invalidTime();
+ QTest::newRow("data7") << QString("25") << QString("hh") << invalidTime();
+ QTest::newRow("data8") << QString("22pm") << QString("Hap") << QTime(22, 0, 0);
+ QTest::newRow("data9") << QString("2221") << QString("hhhh") << invalidTime();
+ QTest::newRow("data10") << QString("02:23PM") << QString("hh:mmAP") << QTime(14,23,0,0);
+ QTest::newRow("data11") << QString("02:23pm") << QString("hh:mmap") << QTime(14,23,0,0);
+ QTest::newRow("short-msecs-lt100") << QString("10:12:34:045") << QString("hh:m:ss:z") << QTime(10,12,34,45);
+ QTest::newRow("short-msecs-gt100") << QString("10:12:34:45") << QString("hh:m:ss:z") << QTime(10,12,34,450);
+ QTest::newRow("late") << QString("23:59:59.999") << QString("hh:mm:ss.z") << QTime(23, 59, 59, 999);
+}
+
+void tst_QTime::fromStringFormat()
+{
+ QFETCH(QString, string);
+ QFETCH(QString, format);
+ QFETCH(QTime, expected);
+
+ QTime dt = QTime::fromString(string, format);
+ QCOMPARE(dt, expected);
+}
+
+void tst_QTime::fromStringDateFormat_data()
+{
+ QTest::addColumn<QString>("string");
+ QTest::addColumn<Qt::DateFormat>("format");
+ QTest::addColumn<QTime>("expected");
+
+ QTest::newRow("TextDate - data0") << QString("00:00:00") << Qt::TextDate << QTime(0,0,0,0);
+ QTest::newRow("TextDate - data1") << QString("10:12:34") << Qt::TextDate << QTime(10,12,34,0);
+ QTest::newRow("TextDate - data2") << QString("19:03:54.998601") << Qt::TextDate << QTime(19, 3, 54, 999);
+ QTest::newRow("TextDate - data3") << QString("19:03:54.999601") << Qt::TextDate << QTime(19, 3, 54, 999);
+ QTest::newRow("TextDate - data4") << QString("10:12") << Qt::TextDate << QTime(10, 12, 0, 0);
+ QTest::newRow("TextDate - invalid, minutes") << QString::fromLatin1("23:XX:00") << Qt::TextDate << invalidTime();
+ QTest::newRow("TextDate - invalid, minute fraction") << QString::fromLatin1("23:00.123456") << Qt::TextDate << invalidTime();
+ QTest::newRow("TextDate - invalid, seconds") << QString::fromLatin1("23:00:XX") << Qt::TextDate << invalidTime();
+ QTest::newRow("TextDate - invalid, milliseconds") << QString::fromLatin1("23:01:01:XXXX") << Qt::TextDate << QTime(23, 1, 1, 0);
+ QTest::newRow("TextDate - midnight 24") << QString("24:00:00") << Qt::TextDate << QTime();
+
+ QTest::newRow("IsoDate - valid, start of day, omit seconds") << QString::fromLatin1("00:00") << Qt::ISODate << QTime(0, 0, 0);
+ QTest::newRow("IsoDate - valid, omit seconds") << QString::fromLatin1("22:21") << Qt::ISODate << QTime(22, 21, 0);
+ QTest::newRow("IsoDate - valid, omit seconds (2)") << QString::fromLatin1("23:59") << Qt::ISODate << QTime(23, 59, 0);
+ QTest::newRow("IsoDate - valid, end of day") << QString::fromLatin1("23:59:59") << Qt::ISODate << QTime(23, 59, 59);
+
+ QTest::newRow("IsoDate - invalid, empty string") << QString::fromLatin1("") << Qt::ISODate << invalidTime();
+ QTest::newRow("IsoDate - invalid, too many hours") << QString::fromLatin1("25:00") << Qt::ISODate << invalidTime();
+ QTest::newRow("IsoDate - invalid, too many minutes") << QString::fromLatin1("10:70") << Qt::ISODate << invalidTime();
+ // This is a valid time if it happens on June 30 or December 31 (leap seconds).
+ QTest::newRow("IsoDate - invalid, too many seconds") << QString::fromLatin1("23:59:60") << Qt::ISODate << invalidTime();
+ QTest::newRow("IsoDate - invalid, minutes") << QString::fromLatin1("23:XX:00") << Qt::ISODate << invalidTime();
+ QTest::newRow("IsoDate - invalid, not enough minutes") << QString::fromLatin1("23:0") << Qt::ISODate << invalidTime();
+ QTest::newRow("IsoDate - invalid, minute fraction") << QString::fromLatin1("23:00,XX") << Qt::ISODate << invalidTime();
+ QTest::newRow("IsoDate - invalid, seconds") << QString::fromLatin1("23:00:XX") << Qt::ISODate << invalidTime();
+ QTest::newRow("IsoDate - invalid, milliseconds") << QString::fromLatin1("23:01:01:XXXX") << Qt::ISODate << QTime(23, 1, 1, 0);
+
+ QTest::newRow("IsoDate - data0") << QString("00:00:00") << Qt::ISODate << QTime(0,0,0,0);
+ QTest::newRow("IsoDate - data1") << QString("10:12:34") << Qt::ISODate << QTime(10,12,34,0);
+ QTest::newRow("IsoDate - data2") << QString("19:03:54.998601") << Qt::ISODate << QTime(19, 3, 54, 999);
+ QTest::newRow("IsoDate - data3") << QString("19:03:54.999601") << Qt::ISODate << QTime(19, 3, 54, 999);
+ QTest::newRow("IsoDate - midnight 24") << QString("24:00:00") << Qt::ISODate << QTime(0, 0, 0, 0);
+ QTest::newRow("IsoDate - minute fraction midnight") << QString("24:00,0") << Qt::ISODate << QTime(0, 0, 0, 0);
+
+ // Test Qt::RFC2822Date format (RFC 2822).
+ QTest::newRow("RFC 2822") << QString::fromLatin1("13 Feb 1987 13:24:51 +0100")
+ << Qt::RFC2822Date << QTime(13, 24, 51);
+ QTest::newRow("RFC 2822 with day") << QString::fromLatin1("Thu, 01 Jan 1970 00:12:34 +0000")
+ << Qt::RFC2822Date << QTime(0, 12, 34);
+ // No timezone
+ QTest::newRow("RFC 2822 no timezone") << QString::fromLatin1("01 Jan 1970 00:12:34")
+ << Qt::RFC2822Date << QTime(0, 12, 34);
+ // No time specified
+ QTest::newRow("RFC 2822 date only") << QString::fromLatin1("01 Nov 2002")
+ << Qt::RFC2822Date << invalidTime();
+ QTest::newRow("RFC 2822 with day date only") << QString::fromLatin1("Fri, 01 Nov 2002")
+ << Qt::RFC2822Date << invalidTime();
+ // Test invalid month, day, year
+ QTest::newRow("RFC 2822 invalid month name") << QString::fromLatin1("13 Fev 1987 13:24:51 +0100")
+ << Qt::RFC2822Date << QTime(13, 24, 51);
+ QTest::newRow("RFC 2822 invalid day") << QString::fromLatin1("36 Fev 1987 13:24:51 +0100")
+ << Qt::RFC2822Date << QTime(13, 24, 51);
+ QTest::newRow("RFC 2822 invalid year") << QString::fromLatin1("13 Fev 0000 13:24:51 +0100")
+ << Qt::RFC2822Date << QTime(13, 24, 51);
+ // Test invalid characters (should ignore invalid characters at end of string).
+ QTest::newRow("RFC 2822 invalid character at end") << QString::fromLatin1("01 Jan 2012 08:00:00 +0100!")
+ << Qt::RFC2822Date << QTime(8, 0, 0);
+ QTest::newRow("RFC 2822 invalid character at front") << QString::fromLatin1("!01 Jan 2012 08:00:00 +0000")
+ << Qt::RFC2822Date << invalidTime();
+ QTest::newRow("RFC 2822 invalid character both ends") << QString::fromLatin1("!01 Jan 2012 08:00:00 +0000!")
+ << Qt::RFC2822Date << invalidTime();
+ QTest::newRow("RFC 2822 invalid character at front, 2 at back") << QString::fromLatin1("!01 Jan 2012 08:00:00 +0000..")
+ << Qt::RFC2822Date << invalidTime();
+ QTest::newRow("RFC 2822 invalid character 2 at front") << QString::fromLatin1("!!01 Jan 2012 08:00:00 +0000")
+ << Qt::RFC2822Date << invalidTime();
+
+ // Test Qt::RFC2822Date format (RFC 850 and 1036).
+ QTest::newRow("RFC 850 and 1036") << QString::fromLatin1("Fri Feb 13 13:24:51 1987 +0100")
+ << Qt::RFC2822Date << QTime(13, 24, 51);
+ // No timezone
+ QTest::newRow("RFC 850 and 1036 no timezone") << QString::fromLatin1("Thu Jan 01 00:12:34 1970")
+ << Qt::RFC2822Date << QTime(0, 12, 34);
+ // No time specified
+ QTest::newRow("RFC 850 and 1036 date only") << QString::fromLatin1("Fri Nov 01 2002")
+ << Qt::RFC2822Date << invalidTime();
+ // Test invalid characters (should ignore invalid characters at end of string).
+ QTest::newRow("RFC 850 and 1036 invalid character at end") << QString::fromLatin1("Sun Jan 01 08:00:00 2012 +0100!")
+ << Qt::RFC2822Date << QTime(8, 0, 0);
+ QTest::newRow("RFC 850 and 1036 invalid character at front") << QString::fromLatin1("!Sun Jan 01 08:00:00 2012 +0000")
+ << Qt::RFC2822Date << invalidTime();
+ QTest::newRow("RFC 850 and 1036 invalid character both ends") << QString::fromLatin1("!Sun Jan 01 08:00:00 2012 +0000!")
+ << Qt::RFC2822Date << invalidTime();
+ QTest::newRow("RFC 850 and 1036 invalid character at front, 2 at back") << QString::fromLatin1("!Sun Jan 01 08:00:00 2012 +0000..")
+ << Qt::RFC2822Date << invalidTime();
+
+ QTest::newRow("RFC empty") << QString::fromLatin1("") << Qt::RFC2822Date << invalidTime();
+}
+
+void tst_QTime::fromStringDateFormat()
+{
+ QFETCH(QString, string);
+ QFETCH(Qt::DateFormat, format);
+ QFETCH(QTime, expected);
+
+ QTime dt = QTime::fromString(string, format);
+ QCOMPARE(dt, expected);
+}
+
+void tst_QTime::toStringDateFormat_data()
+{
+ QTest::addColumn<QTime>("time");
+ QTest::addColumn<Qt::DateFormat>("format");
+ QTest::addColumn<QString>("expected");
+
+ QTest::newRow("00:00:00.000") << QTime(0, 0, 0, 0) << Qt::TextDate << QString("00:00:00");
+ QTest::newRow("ISO 00:00:00.000") << QTime(0, 0, 0, 0) << Qt::ISODate << QString("00:00:00");
+ QTest::newRow("Text 10:12:34.000") << QTime(10, 12, 34, 0) << Qt::TextDate << QString("10:12:34");
+ QTest::newRow("ISO 10:12:34.000") << QTime(10, 12, 34, 0) << Qt::ISODate << QString("10:12:34");
+ QTest::newRow("Text 10:12:34.001") << QTime(10, 12, 34, 001) << Qt::TextDate << QString("10:12:34");
+ QTest::newRow("ISO 10:12:34.001") << QTime(10, 12, 34, 001) << Qt::ISODate << QString("10:12:34");
+ QTest::newRow("Text 10:12:34.999") << QTime(10, 12, 34, 999) << Qt::TextDate << QString("10:12:34");
+ QTest::newRow("ISO 10:12:34.999") << QTime(10, 12, 34, 999) << Qt::ISODate << QString("10:12:34");
+ QTest::newRow("RFC2822Date") << QTime(10, 12, 34, 999) << Qt::RFC2822Date << QString("10:12:34");
+ QTest::newRow("ISOWithMs 10:12:34.000") << QTime(10, 12, 34, 0) << Qt::ISODateWithMs << QString("10:12:34.000");
+ QTest::newRow("ISOWithMs 10:12:34.020") << QTime(10, 12, 34, 20) << Qt::ISODateWithMs << QString("10:12:34.020");
+ QTest::newRow("ISOWithMs 10:12:34.999") << QTime(10, 12, 34, 999) << Qt::ISODateWithMs << QString("10:12:34.999");
+}
+
+void tst_QTime::toStringDateFormat()
+{
+ QFETCH(QTime, time);
+ QFETCH(Qt::DateFormat, format);
+ QFETCH(QString, expected);
+
+ QCOMPARE(time.toString(format), expected);
+}
+
+void tst_QTime::toStringFormat_data()
+{
+ QTest::addColumn<QTime>("t");
+ QTest::addColumn<QString>("format");
+ QTest::addColumn<QString>("str");
+
+ QTest::newRow( "midnight" ) << QTime(0,0,0,0) << QString("h:m:s:z") << QString("0:0:0:0");
+ QTest::newRow( "full" ) << QTime(10,12,34,53) << QString("hh:mm:ss:zzz") << QString("10:12:34:053");
+ QTest::newRow( "short-msecs-lt100" ) << QTime(10,12,34,45) << QString("hh:m:ss:z") << QString("10:12:34:045");
+ QTest::newRow( "short-msecs-gt100" ) << QTime(10,12,34,450) << QString("hh:m:ss:z") << QString("10:12:34:45");
+ QTest::newRow( "am-pm" ) << QTime(10,12,34,45) << QString("hh:ss ap") << QString("10:34 am");
+ QTest::newRow( "AM-PM" ) << QTime(22,12,34,45) << QString("hh:zzz AP") << QString("10:045 PM");
+ QTest::newRow( "invalid" ) << QTime(230,230,230,230) << QString("hh:mm:ss") << QString();
+}
+
+void tst_QTime::toStringFormat()
+{
+ QFETCH( QTime, t );
+ QFETCH( QString, format );
+ QFETCH( QString, str );
+
+ QCOMPARE( t.toString( format ), str );
+}
+
+void tst_QTime::toStringLocale()
+{
+ QTime time(18, 30);
+ QCOMPARE(time.toString(Qt::SystemLocaleShortDate),
+ QLocale::system().toString(time, QLocale::ShortFormat));
+ QCOMPARE(time.toString(Qt::DefaultLocaleShortDate),
+ QLocale().toString(time, QLocale::ShortFormat));
+ QCOMPARE(time.toString(Qt::SystemLocaleLongDate),
+ QLocale::system().toString(time, QLocale::LongFormat));
+ QCOMPARE(time.toString(Qt::DefaultLocaleLongDate),
+ QLocale().toString(time, QLocale::LongFormat));
+ QLocale::setDefault(QLocale::German);
+ QCOMPARE(time.toString(Qt::SystemLocaleShortDate),
+ QLocale::system().toString(time, QLocale::ShortFormat));
+ QCOMPARE(time.toString(Qt::DefaultLocaleShortDate),
+ QLocale().toString(time, QLocale::ShortFormat));
+ QCOMPARE(time.toString(Qt::SystemLocaleLongDate),
+ QLocale::system().toString(time, QLocale::LongFormat));
+ QCOMPARE(time.toString(Qt::DefaultLocaleLongDate),
+ QLocale().toString(time, QLocale::LongFormat));
+}
+
+void tst_QTime::msecsSinceStartOfDay_data()
+{
+ QTest::addColumn<int>("msecs");
+ QTest::addColumn<bool>("isValid");
+ QTest::addColumn<int>("hour");
+ QTest::addColumn<int>("minute");
+ QTest::addColumn<int>("second");
+ QTest::addColumn<int>("msec");
+
+ QTest::newRow("00:00:00.000") << 0 << true
+ << 0 << 0 << 0 << 0;
+ QTest::newRow("01:00:00.001") << ((1 * 3600 * 1000) + 1) << true
+ << 1 << 0 << 0 << 1;
+ QTest::newRow("03:04:05.678") << ((3 * 3600 + 4 * 60 + 5) * 1000 + 678) << true
+ << 3 << 4 << 5 << 678;
+ QTest::newRow("23:59:59.999") << ((23 * 3600 + 59 * 60 + 59) * 1000 + 999) << true
+ << 23 << 59 << 59 << 999;
+ QTest::newRow("24:00:00.000") << ((24 * 3600) * 1000) << false
+ << -1 << -1 << -1 << -1;
+ QTest::newRow("-1 invalid") << -1 << false
+ << -1 << -1 << -1 << -1;
+}
+
+void tst_QTime::msecsSinceStartOfDay()
+{
+ QFETCH(int, msecs);
+ QFETCH(bool, isValid);
+ QFETCH(int, hour);
+ QFETCH(int, minute);
+ QFETCH(int, second);
+ QFETCH(int, msec);
+
+ QTime time = QTime::fromMSecsSinceStartOfDay(msecs);
+ QCOMPARE(time.isValid(), isValid);
+ if (msecs >= 0)
+ QCOMPARE(time.msecsSinceStartOfDay(), msecs);
+ else
+ QCOMPARE(time.msecsSinceStartOfDay(), 0);
+ QCOMPARE(time.hour(), hour);
+ QCOMPARE(time.minute(), minute);
+ QCOMPARE(time.second(), second);
+ QCOMPARE(time.msec(), msec);
+}
+
+QTEST_APPLESS_MAIN(tst_QTime)
+#include "tst_qtime.moc"
diff --git a/tests/auto/corelib/time/qtimezone/BLACKLIST b/tests/auto/corelib/time/qtimezone/BLACKLIST
new file mode 100644
index 0000000000..840c3b1181
--- /dev/null
+++ b/tests/auto/corelib/time/qtimezone/BLACKLIST
@@ -0,0 +1,171 @@
+# QTBUG-69122
+[dataStreamTest]
+android
+
+# QTBUG-69128
+[isTimeZoneIdAvailable]
+android
+
+# QTBUG-69129
+[specificTransition]
+android
+
+# QTBUG-69131
+[transitionEachZone:America/Cancun@2010]
+android
+[transitionEachZone:America/Eirunepe@2010]
+android
+[transitionEachZone:America/Montevideo@2010]
+android
+[transitionEachZone:America/Porto_Acre@2010]
+android
+[transitionEachZone:America/Rio_Branco@2010]
+android
+[transitionEachZone:Asia/Anadyr@2010]
+android
+[transitionEachZone:Asia/Chita@2010]
+android
+[transitionEachZone:Asia/Kamchatka@2010]
+android
+[transitionEachZone:Asia/Khandyga@2010]
+android
+[transitionEachZone:Asia/Magadan@2010]
+android
+[transitionEachZone:Asia/Novokuznetsk@2010]
+android
+[transitionEachZone:Asia/Pyongyang@2010]
+android
+[transitionEachZone:Asia/Ust-Nera@2010]
+android
+[transitionEachZone:Asia/Yerevan@2010]
+android
+[transitionEachZone:Europe/Kaliningrad@2010]
+android
+[transitionEachZone:Europe/Minsk@2010]
+android
+[transitionEachZone:Europe/Moscow@2010]
+android
+[transitionEachZone:Europe/Samara@2010]
+android
+[transitionEachZone:Europe/Simferopol@2010]
+android
+[transitionEachZone:Europe/Volgograd@2010]
+android
+[transitionEachZone:W-SU@2010]
+android
+[transitionEachZone:Africa/Bissau@1970]
+android
+[transitionEachZone:Africa/Juba@1970]
+android
+[transitionEachZone:Africa/Khartoum@1970]
+android
+[transitionEachZone:America/Metlakatla@1970]
+android
+[transitionEachZone:America/Montevideo@1970]
+android
+[transitionEachZone:America/Paramaribo@1970]
+android
+[transitionEachZone:America/Santarem@1970]
+android
+[transitionEachZone:America/Santo_Domingo@1970]
+android
+[transitionEachZone:Asia/Anadyr@1970]
+android
+[transitionEachZone:Asia/Bahrain@1970]
+android
+[transitionEachZone:Asia/Chita@1970]
+android
+[transitionEachZone:Asia/Dushanbe@1970]
+android
+[transitionEachZone:Asia/Ho_Chi_Minh@1970]
+android
+[transitionEachZone:Asia/Kathmandu@1970]
+android
+[transitionEachZone:Asia/Katmandu@1970]
+android
+[transitionEachZone:Asia/Kuala_Lumpur@1970]
+android
+[transitionEachZone:Asia/Magadan@1970]
+android
+[transitionEachZone:Asia/Novosibirsk@1970]
+android
+[transitionEachZone:Asia/Pontianak@1970]
+android
+[transitionEachZone:Asia/Pyongyang@1970]
+android
+[transitionEachZone:Asia/Qatar@1970]
+android
+[transitionEachZone:Asia/Qyzylorda@1970]
+android
+[transitionEachZone:Asia/Saigon@1970]
+android
+[transitionEachZone:Asia/Sakhalin@1970]
+android
+[transitionEachZone:Asia/Singapore@1970]
+android
+[transitionEachZone:Asia/Tashkent@1970]
+android
+[transitionEachZone:Asia/Thimbu@1970]
+android
+[transitionEachZone:Asia/Thimphu@1970]
+android
+[transitionEachZone:Asia/Ust-Nera@1970]
+android
+[transitionEachZone:Atlantic/Cape_Verde@1970]
+android
+[transitionEachZone:Chile/EasterIsland@1970]
+android
+[transitionEachZone:Europe/Kaliningrad@1970]
+android
+[transitionEachZone:Pacific/Bougainville@1970]
+android
+[transitionEachZone:Pacific/Easter@1970]
+android
+[transitionEachZone:Pacific/Enderbury@1970]
+android
+[transitionEachZone:Pacific/Galapagos@1970]
+android
+[transitionEachZone:Pacific/Kiritimati@1970]
+android
+[transitionEachZone:Pacific/Kosrae@1970]
+android
+[transitionEachZone:Pacific/Kwajalein@1970]
+android
+[transitionEachZone:Pacific/Nauru@1970]
+android
+[transitionEachZone:Pacific/Niue@1970]
+android
+[transitionEachZone:Singapore@1970]
+android
+[transitionEachZone:Brazil/Acre@2010]
+android
+[transitionEachZone:Pacific/Bougainville@2010]
+android
+[transitionEachZone:Africa/Algiers@1970]
+android
+[transitionEachZone:Africa/Monrovia@1970]
+android
+[transitionEachZone:Kwajalein@1970]
+android
+[transitionEachZone:Indian/Chagos@1970]
+android
+[transitionEachZone:Europe/Volgograd@1970]
+android
+[transitionEachZone:Atlantic/Stanley@1970]
+android
+[transitionEachZone:Antarctica/Mawson@1970]
+android
+[transitionEachZone:America/Swift_Current@1970]
+android
+[transitionEachZone:America/Guyana@1970]
+android
+[transitionEachZone:America/Grand_Turk@1970]
+android
+[transitionEachZone:America/Dawson_Creek@1970]
+android
+[transitionEachZone:America/Cancun@1970]
+android
+[transitionEachZone:America/Caracas@1970]
+android
+[transitionEachZone:America/Danmarkshavn@1970]
+android
diff --git a/tests/auto/corelib/time/qtimezone/qtimezone.pro b/tests/auto/corelib/time/qtimezone/qtimezone.pro
new file mode 100644
index 0000000000..5ec8d008e7
--- /dev/null
+++ b/tests/auto/corelib/time/qtimezone/qtimezone.pro
@@ -0,0 +1,12 @@
+CONFIG += testcase
+TARGET = tst_qtimezone
+QT = core-private testlib
+SOURCES = tst_qtimezone.cpp
+qtConfig(icu) {
+ QMAKE_USE_PRIVATE += icu
+}
+
+darwin {
+ OBJECTIVE_SOURCES += tst_qtimezone_darwin.mm
+ LIBS += -framework Foundation
+}
diff --git a/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp b/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp
new file mode 100644
index 0000000000..9904719f7c
--- /dev/null
+++ b/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp
@@ -0,0 +1,1340 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtTest/QtTest>
+#include <qtimezone.h>
+#include <private/qtimezoneprivate_p.h>
+#include <qlocale.h>
+
+#if defined(Q_OS_WIN) && !QT_CONFIG(icu)
+# define USING_WIN_TZ
+#endif
+
+class tst_QTimeZone : public QObject
+{
+ Q_OBJECT
+
+public:
+ tst_QTimeZone();
+
+private slots:
+ // Public class default system tests
+ void createTest();
+ void nullTest();
+ void dataStreamTest();
+ void isTimeZoneIdAvailable();
+ void availableTimeZoneIds();
+ void specificTransition_data();
+ void specificTransition();
+ void transitionEachZone_data();
+ void transitionEachZone();
+ void checkOffset_data();
+ void checkOffset();
+ void stressTest();
+ void windowsId();
+ void isValidId_data();
+ void isValidId();
+ // Backend tests
+ void utcTest();
+ void icuTest();
+ void tzTest();
+ void macTest();
+ void darwinTypes();
+ void winTest();
+
+private:
+ void printTimeZone(const QTimeZone &tz);
+#ifdef QT_BUILD_INTERNAL
+ // Generic tests of privates, called by implementation-specific private tests:
+ void testCetPrivate(const QTimeZonePrivate &tzp);
+ void testEpochTranPrivate(const QTimeZonePrivate &tzp);
+#endif // QT_BUILD_INTERNAL
+ const bool debug;
+};
+
+tst_QTimeZone::tst_QTimeZone()
+ // Set to true to print debug output, test Display Names and run long stress tests
+ : debug(false)
+{
+}
+
+void tst_QTimeZone::printTimeZone(const QTimeZone &tz)
+{
+ QDateTime now = QDateTime::currentDateTime();
+ QDateTime jan = QDateTime(QDate(2012, 1, 1), QTime(0, 0, 0), Qt::UTC);
+ QDateTime jun = QDateTime(QDate(2012, 6, 1), QTime(0, 0, 0), Qt::UTC);
+ qDebug() << "";
+ qDebug() << "Time Zone = " << tz;
+ qDebug() << "";
+ qDebug() << "Is Valid = " << tz.isValid();
+ qDebug() << "";
+ qDebug() << "Zone ID = " << tz.id();
+ qDebug() << "Country = " << QLocale::countryToString(tz.country());
+ qDebug() << "Comment = " << tz.comment();
+ qDebug() << "";
+ qDebug() << "Locale = " << QLocale().name();
+ qDebug() << "Name Long = " << tz.displayName(QTimeZone::StandardTime, QTimeZone::LongName);
+ qDebug() << "Name Short = " << tz.displayName(QTimeZone::StandardTime, QTimeZone::ShortName);
+ qDebug() << "Name Offset = " << tz.displayName(QTimeZone::StandardTime, QTimeZone::OffsetName);
+ qDebug() << "Name Long DST = " << tz.displayName(QTimeZone::DaylightTime, QTimeZone::LongName);
+ qDebug() << "Name Short DST = " << tz.displayName(QTimeZone::DaylightTime, QTimeZone::ShortName);
+ qDebug() << "Name Offset DST = " << tz.displayName(QTimeZone::DaylightTime, QTimeZone::OffsetName);
+ qDebug() << "Name Long Generic = " << tz.displayName(QTimeZone::GenericTime, QTimeZone::LongName);
+ qDebug() << "Name Short Generic = " << tz.displayName(QTimeZone::GenericTime, QTimeZone::ShortName);
+ qDebug() << "Name Offset Generic = " << tz.displayName(QTimeZone::GenericTime, QTimeZone::OffsetName);
+ qDebug() << "";
+ QLocale locale = QLocale(QStringLiteral("de_DE"));
+ qDebug() << "Locale = " << locale.name();
+ qDebug() << "Name Long = " << tz.displayName(QTimeZone::StandardTime, QTimeZone::LongName, locale);
+ qDebug() << "Name Short = " << tz.displayName(QTimeZone::StandardTime, QTimeZone::ShortName, locale);
+ qDebug() << "Name Offset = " << tz.displayName(QTimeZone::StandardTime, QTimeZone::OffsetName, locale);
+ qDebug() << "Name Long DST = " << tz.displayName(QTimeZone::DaylightTime, QTimeZone::LongName,locale);
+ qDebug() << "Name Short DST = " << tz.displayName(QTimeZone::DaylightTime, QTimeZone::ShortName, locale);
+ qDebug() << "Name Offset DST = " << tz.displayName(QTimeZone::DaylightTime, QTimeZone::OffsetName, locale);
+ qDebug() << "Name Long Generic = " << tz.displayName(QTimeZone::GenericTime, QTimeZone::LongName, locale);
+ qDebug() << "Name Short Generic = " << tz.displayName(QTimeZone::GenericTime, QTimeZone::ShortName, locale);
+ qDebug() << "Name Offset Generic = " << tz.displayName(QTimeZone::GenericTime, QTimeZone::OffsetName, locale);
+ qDebug() << "";
+ qDebug() << "Abbreviation Now = " << tz.abbreviation(now);
+ qDebug() << "Abbreviation on 1 Jan = " << tz.abbreviation(jan);
+ qDebug() << "Abbreviation on 1 June = " << tz.abbreviation(jun);
+ qDebug() << "";
+ qDebug() << "Offset on 1 January = " << tz.offsetFromUtc(jan);
+ qDebug() << "Offset on 1 June = " << tz.offsetFromUtc(jun);
+ qDebug() << "Offset Now = " << tz.offsetFromUtc(now);
+ qDebug() << "";
+ qDebug() << "UTC Offset Now = " << tz.standardTimeOffset(now);
+ qDebug() << "UTC Offset on 1 January = " << tz.standardTimeOffset(jan);
+ qDebug() << "UTC Offset on 1 June = " << tz.standardTimeOffset(jun);
+ qDebug() << "";
+ qDebug() << "DST Offset on 1 January = " << tz.daylightTimeOffset(jan);
+ qDebug() << "DST Offset on 1 June = " << tz.daylightTimeOffset(jun);
+ qDebug() << "DST Offset Now = " << tz.daylightTimeOffset(now);
+ qDebug() << "";
+ qDebug() << "Has DST = " << tz.hasDaylightTime();
+ qDebug() << "Is DST Now = " << tz.isDaylightTime(now);
+ qDebug() << "Is DST on 1 January = " << tz.isDaylightTime(jan);
+ qDebug() << "Is DST on 1 June = " << tz.isDaylightTime(jun);
+ qDebug() << "";
+ qDebug() << "Has Transitions = " << tz.hasTransitions();
+ qDebug() << "Transition after 1 Jan = " << tz.nextTransition(jan).atUtc;
+ qDebug() << "Transition after 1 Jun = " << tz.nextTransition(jun).atUtc;
+ qDebug() << "Transition before 1 Jan = " << tz.previousTransition(jan).atUtc;
+ qDebug() << "Transition before 1 Jun = " << tz.previousTransition(jun).atUtc;
+ qDebug() << "";
+}
+
+void tst_QTimeZone::createTest()
+{
+ QTimeZone tz("Pacific/Auckland");
+
+ if (debug)
+ printTimeZone(tz);
+
+ // If the tz is not valid then skip as is probably using the UTC backend which is tested later
+ if (!tz.isValid())
+ return;
+
+ // Validity tests
+ QCOMPARE(tz.isValid(), true);
+
+ // Comparison tests
+ QTimeZone tz2("Pacific/Auckland");
+ QTimeZone tz3("Australia/Sydney");
+ QCOMPARE((tz == tz2), true);
+ QCOMPARE((tz != tz2), false);
+ QCOMPARE((tz == tz3), false);
+ QCOMPARE((tz != tz3), true);
+
+ QCOMPARE(tz.country(), QLocale::NewZealand);
+
+ QDateTime jan = QDateTime(QDate(2012, 1, 1), QTime(0, 0, 0), Qt::UTC);
+ QDateTime jun = QDateTime(QDate(2012, 6, 1), QTime(0, 0, 0), Qt::UTC);
+ QDateTime janPrev = QDateTime(QDate(2011, 1, 1), QTime(0, 0, 0), Qt::UTC);
+
+ QCOMPARE(tz.offsetFromUtc(jan), 13 * 3600);
+ QCOMPARE(tz.offsetFromUtc(jun), 12 * 3600);
+
+ QCOMPARE(tz.standardTimeOffset(jan), 12 * 3600);
+ QCOMPARE(tz.standardTimeOffset(jun), 12 * 3600);
+
+ QCOMPARE(tz.daylightTimeOffset(jan), 3600);
+ QCOMPARE(tz.daylightTimeOffset(jun), 0);
+
+ QCOMPARE(tz.hasDaylightTime(), true);
+ QCOMPARE(tz.isDaylightTime(jan), true);
+ QCOMPARE(tz.isDaylightTime(jun), false);
+
+ // Only test transitions if host system supports them
+ if (tz.hasTransitions()) {
+ QTimeZone::OffsetData tran = tz.nextTransition(jan);
+ // 2012-04-01 03:00 NZDT, +13 -> +12
+ QCOMPARE(tran.atUtc,
+ QDateTime(QDate(2012, 4, 1), QTime(3, 0), Qt::OffsetFromUTC, 13 * 3600));
+ QCOMPARE(tran.offsetFromUtc, 12 * 3600);
+ QCOMPARE(tran.standardTimeOffset, 12 * 3600);
+ QCOMPARE(tran.daylightTimeOffset, 0);
+
+ tran = tz.nextTransition(jun);
+ // 2012-09-30 02:00 NZST, +12 -> +13
+ QCOMPARE(tran.atUtc,
+ QDateTime(QDate(2012, 9, 30), QTime(2, 0), Qt::OffsetFromUTC, 12 * 3600));
+ QCOMPARE(tran.offsetFromUtc, 13 * 3600);
+ QCOMPARE(tran.standardTimeOffset, 12 * 3600);
+ QCOMPARE(tran.daylightTimeOffset, 3600);
+
+ tran = tz.previousTransition(jan);
+ // 2011-09-25 02:00 NZST, +12 -> +13
+ QCOMPARE(tran.atUtc,
+ QDateTime(QDate(2011, 9, 25), QTime(2, 0), Qt::OffsetFromUTC, 12 * 3600));
+ QCOMPARE(tran.offsetFromUtc, 13 * 3600);
+ QCOMPARE(tran.standardTimeOffset, 12 * 3600);
+ QCOMPARE(tran.daylightTimeOffset, 3600);
+
+ tran = tz.previousTransition(jun);
+ // 2012-04-01 03:00 NZDT, +13 -> +12 (again)
+ QCOMPARE(tran.atUtc,
+ QDateTime(QDate(2012, 4, 1), QTime(3, 0), Qt::OffsetFromUTC, 13 * 3600));
+ QCOMPARE(tran.offsetFromUtc, 12 * 3600);
+ QCOMPARE(tran.standardTimeOffset, 12 * 3600);
+ QCOMPARE(tran.daylightTimeOffset, 0);
+
+ QTimeZone::OffsetDataList expected;
+ tran.atUtc = QDateTime(QDate(2011, 4, 3), QTime(2, 0), Qt::OffsetFromUTC, 13 * 3600);
+ tran.offsetFromUtc = 13 * 3600;
+ tran.standardTimeOffset = 12 * 3600;
+ tran.daylightTimeOffset = 3600;
+ expected << tran;
+ tran.atUtc = QDateTime(QDate(2011, 9, 25), QTime(2, 0), Qt::OffsetFromUTC, 12 * 3600);
+ tran.offsetFromUtc = 12 * 3600;
+ tran.standardTimeOffset = 12 * 3600;
+ tran.daylightTimeOffset = 0;
+ expected << tran;
+ QTimeZone::OffsetDataList result = tz.transitions(janPrev, jan);
+ QCOMPARE(result.count(), expected.count());
+ for (int i = 0; i > expected.count(); ++i) {
+ QCOMPARE(result.at(i).atUtc, expected.at(i).atUtc);
+ QCOMPARE(result.at(i).offsetFromUtc, expected.at(i).offsetFromUtc);
+ QCOMPARE(result.at(i).standardTimeOffset, expected.at(i).standardTimeOffset);
+ QCOMPARE(result.at(i).daylightTimeOffset, expected.at(i).daylightTimeOffset);
+ }
+ }
+}
+
+void tst_QTimeZone::nullTest()
+{
+ QTimeZone nullTz1;
+ QTimeZone nullTz2;
+ QTimeZone utc("UTC");
+
+ // Validity tests
+ QCOMPARE(nullTz1.isValid(), false);
+ QCOMPARE(nullTz2.isValid(), false);
+ QCOMPARE(utc.isValid(), true);
+
+ // Comparison tests
+ QCOMPARE((nullTz1 == nullTz2), true);
+ QCOMPARE((nullTz1 != nullTz2), false);
+ QCOMPARE((nullTz1 == utc), false);
+ QCOMPARE((nullTz1 != utc), true);
+
+ // Assignment tests
+ nullTz2 = utc;
+ QCOMPARE(nullTz2.isValid(), true);
+ utc = nullTz1;
+ QCOMPARE(utc.isValid(), false);
+
+ QCOMPARE(nullTz1.id(), QByteArray());
+ QCOMPARE(nullTz1.country(), QLocale::AnyCountry);
+ QCOMPARE(nullTz1.comment(), QString());
+
+ QDateTime jan = QDateTime(QDate(2012, 1, 1), QTime(0, 0, 0), Qt::UTC);
+ QDateTime jun = QDateTime(QDate(2012, 6, 1), QTime(0, 0, 0), Qt::UTC);
+ QDateTime janPrev = QDateTime(QDate(2011, 1, 1), QTime(0, 0, 0), Qt::UTC);
+
+ QCOMPARE(nullTz1.abbreviation(jan), QString());
+ QCOMPARE(nullTz1.displayName(jan), QString());
+ QCOMPARE(nullTz1.displayName(QTimeZone::StandardTime), QString());
+
+ QCOMPARE(nullTz1.offsetFromUtc(jan), 0);
+ QCOMPARE(nullTz1.offsetFromUtc(jun), 0);
+
+ QCOMPARE(nullTz1.standardTimeOffset(jan), 0);
+ QCOMPARE(nullTz1.standardTimeOffset(jun), 0);
+
+ QCOMPARE(nullTz1.daylightTimeOffset(jan), 0);
+ QCOMPARE(nullTz1.daylightTimeOffset(jun), 0);
+
+ QCOMPARE(nullTz1.hasDaylightTime(), false);
+ QCOMPARE(nullTz1.isDaylightTime(jan), false);
+ QCOMPARE(nullTz1.isDaylightTime(jun), false);
+
+ QTimeZone::OffsetData data = nullTz1.offsetData(jan);
+ QCOMPARE(data.atUtc, QDateTime());
+ QCOMPARE(data.offsetFromUtc, std::numeric_limits<int>::min());
+ QCOMPARE(data.standardTimeOffset, std::numeric_limits<int>::min());
+ QCOMPARE(data.daylightTimeOffset, std::numeric_limits<int>::min());
+
+ QCOMPARE(nullTz1.hasTransitions(), false);
+
+ data = nullTz1.nextTransition(jan);
+ QCOMPARE(data.atUtc, QDateTime());
+ QCOMPARE(data.offsetFromUtc, std::numeric_limits<int>::min());
+ QCOMPARE(data.standardTimeOffset, std::numeric_limits<int>::min());
+ QCOMPARE(data.daylightTimeOffset, std::numeric_limits<int>::min());
+
+ data = nullTz1.previousTransition(jan);
+ QCOMPARE(data.atUtc, QDateTime());
+ QCOMPARE(data.offsetFromUtc, std::numeric_limits<int>::min());
+ QCOMPARE(data.standardTimeOffset, std::numeric_limits<int>::min());
+ QCOMPARE(data.daylightTimeOffset, std::numeric_limits<int>::min());
+}
+
+void tst_QTimeZone::dataStreamTest()
+{
+ // Test the OffsetFromUtc backend serialization. First with a custom timezone:
+ QTimeZone tz1("QST", 123456, "Qt Standard Time", "QST", QLocale::Norway, "Qt Testing");
+ QByteArray tmp;
+ {
+ QDataStream ds(&tmp, QIODevice::WriteOnly);
+ ds << tz1;
+ }
+ QTimeZone tz2("UTC");
+ {
+ QDataStream ds(&tmp, QIODevice::ReadOnly);
+ ds >> tz2;
+ }
+ QCOMPARE(tz2.id(), QByteArray("QST"));
+ QCOMPARE(tz2.comment(), QString("Qt Testing"));
+ QCOMPARE(tz2.country(), QLocale::Norway);
+ QCOMPARE(tz2.abbreviation(QDateTime::currentDateTime()), QString("QST"));
+ QCOMPARE(tz2.displayName(QTimeZone::StandardTime, QTimeZone::LongName, QString()),
+ QString("Qt Standard Time"));
+ QCOMPARE(tz2.displayName(QTimeZone::DaylightTime, QTimeZone::LongName, QString()),
+ QString("Qt Standard Time"));
+ QCOMPARE(tz2.offsetFromUtc(QDateTime::currentDateTime()), 123456);
+
+ // And then with a standard IANA timezone (QTBUG-60595):
+ tz1 = QTimeZone("UTC");
+ QCOMPARE(tz1.isValid(), true);
+ {
+ QDataStream ds(&tmp, QIODevice::WriteOnly);
+ ds << tz1;
+ }
+ {
+ QDataStream ds(&tmp, QIODevice::ReadOnly);
+ ds >> tz2;
+ }
+ QCOMPARE(tz2.isValid(), true);
+ QCOMPARE(tz2.id(), tz1.id());
+
+ // Test the system backend serialization
+ tz1 = QTimeZone("Pacific/Auckland");
+
+ // If not valid then probably using the UTC system backend so skip
+ if (!tz1.isValid())
+ return;
+
+ {
+ QDataStream ds(&tmp, QIODevice::WriteOnly);
+ ds << tz1;
+ }
+ tz2 = QTimeZone("UTC");
+ {
+ QDataStream ds(&tmp, QIODevice::ReadOnly);
+ ds >> tz2;
+ }
+ QCOMPARE(tz2.id(), tz1.id());
+}
+
+void tst_QTimeZone::isTimeZoneIdAvailable()
+{
+ QList<QByteArray> available = QTimeZone::availableTimeZoneIds();
+ foreach (const QByteArray &id, available)
+ QVERIFY(QTimeZone::isTimeZoneIdAvailable(id));
+
+#ifdef QT_BUILD_INTERNAL
+ // a-z, A-Z, 0-9, '.', '-', '_' are valid chars
+ // Can't start with '-'
+ // Parts separated by '/', each part min 1 and max of 14 chars
+ QCOMPARE(QTimeZonePrivate::isValidId("az"), true);
+ QCOMPARE(QTimeZonePrivate::isValidId("AZ"), true);
+ QCOMPARE(QTimeZonePrivate::isValidId("09"), true);
+ QCOMPARE(QTimeZonePrivate::isValidId("a/z"), true);
+ QCOMPARE(QTimeZonePrivate::isValidId("a.z"), true);
+ QCOMPARE(QTimeZonePrivate::isValidId("a-z"), true);
+ QCOMPARE(QTimeZonePrivate::isValidId("a_z"), true);
+ QCOMPARE(QTimeZonePrivate::isValidId(".z"), true);
+ QCOMPARE(QTimeZonePrivate::isValidId("_z"), true);
+ QCOMPARE(QTimeZonePrivate::isValidId("12345678901234"), true);
+ QCOMPARE(QTimeZonePrivate::isValidId("12345678901234/12345678901234"), true);
+ QCOMPARE(QTimeZonePrivate::isValidId("a z"), false);
+ QCOMPARE(QTimeZonePrivate::isValidId("a\\z"), false);
+ QCOMPARE(QTimeZonePrivate::isValidId("a,z"), false);
+ QCOMPARE(QTimeZonePrivate::isValidId("/z"), false);
+ QCOMPARE(QTimeZonePrivate::isValidId("-z"), false);
+ QCOMPARE(QTimeZonePrivate::isValidId("123456789012345"), false);
+ QCOMPARE(QTimeZonePrivate::isValidId("123456789012345/12345678901234"), false);
+ QCOMPARE(QTimeZonePrivate::isValidId("12345678901234/123456789012345"), false);
+#endif // QT_BUILD_INTERNAL
+}
+
+void tst_QTimeZone::specificTransition_data()
+{
+ QTest::addColumn<QByteArray>("zone");
+ QTest::addColumn<QDate>("start");
+ QTest::addColumn<QDate>("stop");
+ QTest::addColumn<int>("count");
+ QTest::addColumn<QDateTime>("atUtc");
+ // In minutes:
+ QTest::addColumn<int>("offset");
+ QTest::addColumn<int>("stdoff");
+ QTest::addColumn<int>("dstoff");
+
+ // Moscow ditched DST on 2010-10-31 but has since changed standard offset twice.
+#ifdef USING_WIN_TZ
+ // Win7 is too old to know about this transition:
+ if (QOperatingSystemVersion::current() > QOperatingSystemVersion::Windows7)
+#endif
+ {
+ QTest::newRow("Moscow/2014") // From original bug-report
+ << QByteArray("Europe/Moscow")
+ << QDate(2011, 4, 1) << QDate(2017, 12,31) << 1
+ << QDateTime(QDate(2014, 10, 26), QTime(2, 0, 0),
+ Qt::OffsetFromUTC, 4 * 3600).toUTC()
+ << 3 * 3600 << 3 * 3600 << 0;
+ }
+ QTest::newRow("Moscow/2011") // Transition on 2011-03-27
+ << QByteArray("Europe/Moscow")
+ << QDate(2010, 11, 1) << QDate(2014, 10, 25) << 1
+ << QDateTime(QDate(2011, 3, 27), QTime(2, 0, 0),
+ Qt::OffsetFromUTC, 3 * 3600).toUTC()
+ << 4 * 3600 << 4 * 3600 << 0;
+}
+
+void tst_QTimeZone::specificTransition()
+{
+ // Regression test for QTBUG-42021 (on MS-Win)
+ QFETCH(QByteArray, zone);
+ QFETCH(QDate, start);
+ QFETCH(QDate, stop);
+ QFETCH(int, count);
+ // No attempt to check abbreviations; to much cross-platform variation.
+ QFETCH(QDateTime, atUtc);
+ QFETCH(int, offset);
+ QFETCH(int, stdoff);
+ QFETCH(int, dstoff);
+
+ QTimeZone timeZone(zone);
+ if (!timeZone.isValid())
+ QSKIP("Missing time-zone data");
+ QTimeZone::OffsetDataList transits =
+ timeZone.transitions(QDateTime(start, QTime(0, 0), timeZone),
+ QDateTime(stop, QTime(23, 59), timeZone));
+ QCOMPARE(transits.length(), count);
+ const QTimeZone::OffsetData &transition = transits.at(0);
+ QCOMPARE(transition.offsetFromUtc, offset);
+ QCOMPARE(transition.standardTimeOffset, stdoff);
+ QCOMPARE(transition.daylightTimeOffset, dstoff);
+ QCOMPARE(transition.atUtc, atUtc);
+}
+
+void tst_QTimeZone::transitionEachZone_data()
+{
+ QTest::addColumn<QByteArray>("zone");
+ QTest::addColumn<qint64>("secs");
+ QTest::addColumn<int>("start");
+ QTest::addColumn<int>("stop");
+
+ struct {
+ qint64 baseSecs;
+ int start, stop;
+ int year;
+ } table[] = {
+ { 25666200, 3, 12, 1970 }, // 1970-10-25 01:30 UTC; North America
+ { 1288488600, -4, 8, 2010 } // 2010-10-31 01:30 UTC; Europe, Russia
+ };
+
+ const auto zones = QTimeZone::availableTimeZoneIds();
+ for (int k = sizeof(table) / sizeof(table[0]); k-- > 0; ) {
+ for (const QByteArray &zone : zones) {
+ const QString name = QString::asprintf("%s@%d", zone.constData(), table[k].year);
+ QTest::newRow(name.toUtf8().constData())
+ << zone
+ << table[k].baseSecs
+ << table[k].start
+ << table[k].stop;
+ }
+ }
+}
+
+void tst_QTimeZone::transitionEachZone()
+{
+ // Regression test: round-trip fromMsecs/toMSecs should be idempotent; but
+ // various zones failed during fall-back transitions.
+ QFETCH(QByteArray, zone);
+ QFETCH(qint64, secs);
+ QFETCH(int, start);
+ QFETCH(int, stop);
+ QTimeZone named(zone);
+
+ for (int i = start; i < stop; i++) {
+#ifdef USING_WIN_TZ
+ // See QTBUG-64985: MS's TZ APIs' misdescription of Europe/Samara leads
+ // to mis-disambiguation of its fall-back here.
+ if (zone == "Europe/Samara" && i == -3) {
+ continue;
+ }
+#endif
+#ifdef Q_OS_ANDROID
+ if (zone == "America/Mazatlan" || zone == "Mexico/BajaSur")
+ QSKIP("Crashes on Android, see QTBUG-69132");
+#endif
+ qint64 here = secs + i * 3600;
+ QDateTime when = QDateTime::fromMSecsSinceEpoch(here * 1000, named);
+ qint64 stamp = when.toMSecsSinceEpoch();
+ if (here * 1000 != stamp) // (The +1 is due to using *1*:30 as baseSecs.)
+ qDebug() << "Failing for" << zone << "at half past" << (i + 1) << "UTC";
+ QCOMPARE(stamp % 1000, 0);
+ QCOMPARE(here - stamp / 1000, 0);
+ }
+}
+
+void tst_QTimeZone::checkOffset_data()
+{
+ QTest::addColumn<QByteArray>("zoneName");
+ QTest::addColumn<QDateTime>("when");
+ QTest::addColumn<int>("netOffset");
+ QTest::addColumn<int>("stdOffset");
+ QTest::addColumn<int>("dstOffset");
+
+ struct {
+ const char *zone, *nick;
+ int year, month, day, hour, min, sec;
+ int std, dst;
+ } table[] = {
+ // Zone with no transitions (QTBUG-74614, QTBUG-74666, when TZ backend uses minimal data)
+ { "Etc/UTC", "epoch", 1970, 1, 1, 0, 0, 0, 0, 0 },
+ { "Etc/UTC", "pre_int32", 1901, 12, 13, 20, 45, 51, 0, 0 },
+ { "Etc/UTC", "post_int32", 2038, 1, 19, 3, 14, 9, 0, 0 },
+ { "Etc/UTC", "post_uint32", 2106, 2, 7, 6, 28, 17, 0, 0 },
+ { "Etc/UTC", "initial", -292275056, 5, 16, 16, 47, 5, 0, 0 },
+ { "Etc/UTC", "final", 292278994, 8, 17, 7, 12, 55, 0, 0 },
+ // Kiev: regression test for QTBUG-64122 (on MS):
+ { "Europe/Kiev", "summer", 2017, 10, 27, 12, 0, 0, 2 * 3600, 3600 },
+ { "Europe/Kiev", "winter", 2017, 10, 29, 12, 0, 0, 2 * 3600, 0 }
+ };
+ for (const auto &entry : table) {
+ QTimeZone zone(entry.zone);
+ if (zone.isValid()) {
+ QTest::addRow("%s@%s", entry.zone, entry.nick)
+ << QByteArray(entry.zone)
+ << QDateTime(QDate(entry.year, entry.month, entry.day),
+ QTime(entry.hour, entry.min, entry.sec), zone)
+ << entry.dst + entry.std << entry.std << entry.dst;
+ } else {
+ qWarning("Skipping %s@%s test as zone is invalid", entry.zone, entry.nick);
+ }
+ }
+}
+
+void tst_QTimeZone::checkOffset()
+{
+ QFETCH(QByteArray, zoneName);
+ QFETCH(QDateTime, when);
+ QFETCH(int, netOffset);
+ QFETCH(int, stdOffset);
+ QFETCH(int, dstOffset);
+
+ QTimeZone zone(zoneName);
+ QVERIFY(zone.isValid()); // It was when _data() added the row !
+ QCOMPARE(zone.offsetFromUtc(when), netOffset);
+ QCOMPARE(zone.standardTimeOffset(when), stdOffset);
+ QCOMPARE(zone.daylightTimeOffset(when), dstOffset);
+ QCOMPARE(zone.isDaylightTime(when), dstOffset != 0);
+}
+
+void tst_QTimeZone::availableTimeZoneIds()
+{
+ if (debug) {
+ qDebug() << "";
+ qDebug() << "Available Time Zones" ;
+ qDebug() << QTimeZone::availableTimeZoneIds();
+ qDebug() << "";
+ qDebug() << "Available Time Zones in the US";
+ qDebug() << QTimeZone::availableTimeZoneIds(QLocale::UnitedStates);
+ qDebug() << "";
+ qDebug() << "Available Time Zones with UTC Offset 0";
+ qDebug() << QTimeZone::availableTimeZoneIds(0);
+ qDebug() << "";
+ } else {
+ //Just test the calls work, we cannot know what any test machine has available
+ QList<QByteArray> listAll = QTimeZone::availableTimeZoneIds();
+ QList<QByteArray> listUs = QTimeZone::availableTimeZoneIds(QLocale::UnitedStates);
+ QList<QByteArray> listZero = QTimeZone::availableTimeZoneIds(0);
+ }
+}
+
+void tst_QTimeZone::stressTest()
+{
+ QList<QByteArray> idList = QTimeZone::availableTimeZoneIds();
+ foreach (const QByteArray &id, idList) {
+ QTimeZone testZone = QTimeZone(id);
+ QCOMPARE(testZone.isValid(), true);
+ QCOMPARE(testZone.id(), id);
+ QDateTime testDate = QDateTime(QDate(2015, 1, 1), QTime(0, 0, 0), Qt::UTC);
+ testZone.country();
+ testZone.comment();
+ testZone.displayName(testDate);
+ testZone.displayName(QTimeZone::DaylightTime);
+ testZone.displayName(QTimeZone::StandardTime);
+ testZone.abbreviation(testDate);
+ testZone.offsetFromUtc(testDate);
+ testZone.standardTimeOffset(testDate);
+ testZone.daylightTimeOffset(testDate);
+ testZone.hasDaylightTime();
+ testZone.isDaylightTime(testDate);
+ testZone.offsetData(testDate);
+ testZone.hasTransitions();
+ testZone.nextTransition(testDate);
+ testZone.previousTransition(testDate);
+ // Dates known to be outside possible tz file pre-calculated rules range
+ QDateTime lowDate1 = QDateTime(QDate(1800, 1, 1), QTime(0, 0, 0), Qt::UTC);
+ QDateTime lowDate2 = QDateTime(QDate(1800, 6, 1), QTime(0, 0, 0), Qt::UTC);
+ QDateTime highDate1 = QDateTime(QDate(2200, 1, 1), QTime(0, 0, 0), Qt::UTC);
+ QDateTime highDate2 = QDateTime(QDate(2200, 6, 1), QTime(0, 0, 0), Qt::UTC);
+ testZone.nextTransition(lowDate1);
+ testZone.nextTransition(lowDate2);
+ testZone.previousTransition(lowDate2);
+ testZone.previousTransition(lowDate2);
+ testZone.nextTransition(highDate1);
+ testZone.nextTransition(highDate2);
+ testZone.previousTransition(highDate1);
+ testZone.previousTransition(highDate2);
+ if (debug) {
+ // This could take a long time, depending on platform and database
+ qDebug() << "Stress test calculating transistions for" << testZone.id();
+ testZone.transitions(lowDate1, highDate1);
+ }
+ testDate.setTimeZone(testZone);
+ testDate.isValid();
+ testDate.offsetFromUtc();
+ testDate.timeZoneAbbreviation();
+ }
+}
+
+void tst_QTimeZone::windowsId()
+{
+/*
+ Current Windows zones for "Central Standard Time":
+ Region IANA Id(s)
+ Default "America/Chicago"
+ Canada "America/Winnipeg America/Rainy_River America/Rankin_Inlet America/Resolute"
+ Mexico "America/Matamoros"
+ USA "America/Chicago America/Indiana/Knox America/Indiana/Tell_City America/Menominee"
+ "America/North_Dakota/Beulah America/North_Dakota/Center"
+ "America/North_Dakota/New_Salem"
+ AnyCountry "CST6CDT"
+*/
+ QCOMPARE(QTimeZone::ianaIdToWindowsId("America/Chicago"),
+ QByteArray("Central Standard Time"));
+ QCOMPARE(QTimeZone::ianaIdToWindowsId("America/Resolute"),
+ QByteArray("Central Standard Time"));
+
+ // Partials shouldn't match
+ QCOMPARE(QTimeZone::ianaIdToWindowsId("America/Chi"), QByteArray());
+ QCOMPARE(QTimeZone::ianaIdToWindowsId("InvalidZone"), QByteArray());
+ QCOMPARE(QTimeZone::ianaIdToWindowsId(QByteArray()), QByteArray());
+
+ // Check default value
+ QCOMPARE(QTimeZone::windowsIdToDefaultIanaId("Central Standard Time"),
+ QByteArray("America/Chicago"));
+ QCOMPARE(QTimeZone::windowsIdToDefaultIanaId("Central Standard Time", QLocale::Canada),
+ QByteArray("America/Winnipeg"));
+ QCOMPARE(QTimeZone::windowsIdToDefaultIanaId("Central Standard Time", QLocale::AnyCountry),
+ QByteArray("CST6CDT"));
+ QCOMPARE(QTimeZone::windowsIdToDefaultIanaId(QByteArray()), QByteArray());
+
+ // No country is sorted list of all zones
+ QList<QByteArray> list;
+ list << "America/Chicago" << "America/Indiana/Knox" << "America/Indiana/Tell_City"
+ << "America/Matamoros" << "America/Menominee" << "America/North_Dakota/Beulah"
+ << "America/North_Dakota/Center" << "America/North_Dakota/New_Salem"
+ << "America/Rainy_River" << "America/Rankin_Inlet" << "America/Resolute"
+ << "America/Winnipeg" << "CST6CDT";
+ QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time"), list);
+
+ // Check country with no match returns empty list
+ list.clear();
+ QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time", QLocale::NewZealand),
+ list);
+
+ // Check valid country returns list in preference order
+ list.clear();
+ list << "America/Winnipeg" << "America/Rainy_River" << "America/Rankin_Inlet"
+ << "America/Resolute";
+ QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time", QLocale::Canada), list);
+
+ list.clear();
+ list << "America/Matamoros";
+ QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time", QLocale::Mexico), list);
+
+ list.clear();
+ list << "America/Chicago" << "America/Indiana/Knox" << "America/Indiana/Tell_City"
+ << "America/Menominee" << "America/North_Dakota/Beulah" << "America/North_Dakota/Center"
+ << "America/North_Dakota/New_Salem";
+ QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time", QLocale::UnitedStates),
+ list);
+
+ list.clear();
+ list << "CST6CDT";
+ QCOMPARE(QTimeZone::windowsIdToIanaIds("Central Standard Time", QLocale::AnyCountry),
+ list);
+
+ // Check no windowsId return empty
+ list.clear();
+ QCOMPARE(QTimeZone::windowsIdToIanaIds(QByteArray()), list);
+ QCOMPARE(QTimeZone::windowsIdToIanaIds(QByteArray(), QLocale::AnyCountry), list);
+}
+
+void tst_QTimeZone::isValidId_data()
+{
+#ifdef QT_BUILD_INTERNAL
+ QTest::addColumn<QByteArray>("input");
+ QTest::addColumn<bool>("valid");
+
+#define TESTSET(name, section, valid) \
+ QTest::newRow(name " front") << QByteArray(section "/xyz/xyz") << valid; \
+ QTest::newRow(name " middle") << QByteArray("xyz/" section "/xyz") << valid; \
+ QTest::newRow(name " back") << QByteArray("xyz/xyz/" section) << valid
+
+ TESTSET("empty", "", false);
+ TESTSET("minimal", "m", true);
+ TESTSET("maximal", "12345678901234", true);
+ TESTSET("too long", "123456789012345", false);
+
+ TESTSET("bad hyphen", "-hyphen", false);
+ TESTSET("good hyphen", "hy-phen", true);
+
+ TESTSET("valid char _", "_", true);
+ TESTSET("valid char .", ".", true);
+ TESTSET("valid char :", ":", true);
+ TESTSET("valid char +", "+", true);
+ TESTSET("valid char A", "A", true);
+ TESTSET("valid char Z", "Z", true);
+ TESTSET("valid char a", "a", true);
+ TESTSET("valid char z", "z", true);
+ TESTSET("valid char 0", "0", true);
+ TESTSET("valid char 9", "9", true);
+
+ TESTSET("invalid char ^", "^", false);
+ TESTSET("invalid char \"", "\"", false);
+ TESTSET("invalid char $", "$", false);
+ TESTSET("invalid char %", "%", false);
+ TESTSET("invalid char &", "&", false);
+ TESTSET("invalid char (", "(", false);
+ TESTSET("invalid char )", ")", false);
+ TESTSET("invalid char =", "=", false);
+ TESTSET("invalid char ?", "?", false);
+ TESTSET("invalid char ß", "ß", false);
+ TESTSET("invalid char \\x01", "\x01", false);
+ TESTSET("invalid char ' '", " ", false);
+
+#undef TESTSET
+#endif // QT_BUILD_INTERNAL
+}
+
+void tst_QTimeZone::isValidId()
+{
+#ifdef QT_BUILD_INTERNAL
+ QFETCH(QByteArray, input);
+ QFETCH(bool, valid);
+
+ QCOMPARE(QTimeZonePrivate::isValidId(input), valid);
+#else
+ QSKIP("This test requires a Qt -developer-build.");
+#endif
+}
+
+void tst_QTimeZone::utcTest()
+{
+#ifdef QT_BUILD_INTERNAL
+ // Test default UTC constructor
+ QUtcTimeZonePrivate tzp;
+ QCOMPARE(tzp.isValid(), true);
+ QCOMPARE(tzp.id(), QByteArray("UTC"));
+ QCOMPARE(tzp.country(), QLocale::AnyCountry);
+ QCOMPARE(tzp.abbreviation(0), QString("UTC"));
+ QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::LongName, QString()), QString("UTC"));
+ QCOMPARE(tzp.offsetFromUtc(0), 0);
+ QCOMPARE(tzp.standardTimeOffset(0), 0);
+ QCOMPARE(tzp.daylightTimeOffset(0), 0);
+ QCOMPARE(tzp.hasDaylightTime(), false);
+ QCOMPARE(tzp.hasTransitions(), false);
+
+ // Test create from UTC Offset
+ QDateTime now = QDateTime::currentDateTime();
+ QTimeZone tz(36000);
+ QCOMPARE(tz.isValid(), true);
+ QCOMPARE(tz.id(), QByteArray("UTC+10:00"));
+ QCOMPARE(tz.offsetFromUtc(now), 36000);
+ QCOMPARE(tz.standardTimeOffset(now), 36000);
+ QCOMPARE(tz.daylightTimeOffset(now), 0);
+
+ // Test invalid UTC offset, must be in range -14 to +14 hours
+ int min = -14*60*60;
+ int max = 14*60*60;
+ QCOMPARE(QTimeZone(min - 1).isValid(), false);
+ QCOMPARE(QTimeZone(min).isValid(), true);
+ QCOMPARE(QTimeZone(min + 1).isValid(), true);
+ QCOMPARE(QTimeZone(max - 1).isValid(), true);
+ QCOMPARE(QTimeZone(max).isValid(), true);
+ QCOMPARE(QTimeZone(max + 1).isValid(), false);
+
+ // Test create from standard name
+ tz = QTimeZone("UTC+10:00");
+ QCOMPARE(tz.isValid(), true);
+ QCOMPARE(tz.id(), QByteArray("UTC+10:00"));
+ QCOMPARE(tz.offsetFromUtc(now), 36000);
+ QCOMPARE(tz.standardTimeOffset(now), 36000);
+ QCOMPARE(tz.daylightTimeOffset(now), 0);
+
+ // Test invalid UTC ID, must be in available list
+ tz = QTimeZone("UTC+00:01");
+ QCOMPARE(tz.isValid(), false);
+
+ // Test create custom zone
+ tz = QTimeZone("QST", 123456, "Qt Standard Time", "QST", QLocale::Norway, "Qt Testing");
+ QCOMPARE(tz.isValid(), true);
+ QCOMPARE(tz.id(), QByteArray("QST"));
+ QCOMPARE(tz.comment(), QString("Qt Testing"));
+ QCOMPARE(tz.country(), QLocale::Norway);
+ QCOMPARE(tz.abbreviation(now), QString("QST"));
+ QCOMPARE(tz.displayName(QTimeZone::StandardTime, QTimeZone::LongName, QString()),
+ QString("Qt Standard Time"));
+ QCOMPARE(tz.offsetFromUtc(now), 123456);
+ QCOMPARE(tz.standardTimeOffset(now), 123456);
+ QCOMPARE(tz.daylightTimeOffset(now), 0);
+#endif // QT_BUILD_INTERNAL
+}
+
+void tst_QTimeZone::icuTest()
+{
+#if defined(QT_BUILD_INTERNAL) && QT_CONFIG(icu)
+ // Known datetimes
+ qint64 std = QDateTime(QDate(2012, 1, 1), QTime(0, 0, 0), Qt::UTC).toMSecsSinceEpoch();
+ qint64 dst = QDateTime(QDate(2012, 6, 1), QTime(0, 0, 0), Qt::UTC).toMSecsSinceEpoch();
+
+ // Test default constructor
+ QIcuTimeZonePrivate tzpd;
+ QVERIFY(tzpd.isValid());
+
+ // Test invalid constructor
+ QIcuTimeZonePrivate tzpi("Gondwana/Erewhon");
+ QCOMPARE(tzpi.isValid(), false);
+
+ // Test named constructor
+ QIcuTimeZonePrivate tzp("Europe/Berlin");
+ QVERIFY(tzp.isValid());
+
+ // Only test names in debug mode, names used can vary by ICU version installed
+ if (debug) {
+ // Test display names by type
+ QLocale enUS("en_US");
+ QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::LongName, enUS),
+ QString("Central European Standard Time"));
+ QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::ShortName, enUS),
+ QString("GMT+01:00"));
+ QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::OffsetName, enUS),
+ QString("UTC+01:00"));
+ QCOMPARE(tzp.displayName(QTimeZone::DaylightTime, QTimeZone::LongName, enUS),
+ QString("Central European Summer Time"));
+ QCOMPARE(tzp.displayName(QTimeZone::DaylightTime, QTimeZone::ShortName, enUS),
+ QString("GMT+02:00"));
+ QCOMPARE(tzp.displayName(QTimeZone::DaylightTime, QTimeZone::OffsetName, enUS),
+ QString("UTC+02:00"));
+ // ICU C api does not support Generic Time yet, C++ api does
+ QCOMPARE(tzp.displayName(QTimeZone::GenericTime, QTimeZone::LongName, enUS),
+ QString("Central European Standard Time"));
+ QCOMPARE(tzp.displayName(QTimeZone::GenericTime, QTimeZone::ShortName, enUS),
+ QString("GMT+01:00"));
+ QCOMPARE(tzp.displayName(QTimeZone::GenericTime, QTimeZone::OffsetName, enUS),
+ QString("UTC+01:00"));
+
+ // Test Abbreviations
+ QCOMPARE(tzp.abbreviation(std), QString("CET"));
+ QCOMPARE(tzp.abbreviation(dst), QString("CEST"));
+ }
+
+ testCetPrivate(tzp);
+ testEpochTranPrivate(QIcuTimeZonePrivate("America/Toronto"));
+#endif // icu
+}
+
+void tst_QTimeZone::tzTest()
+{
+#if defined QT_BUILD_INTERNAL && defined Q_OS_UNIX && !defined Q_OS_DARWIN && !defined Q_OS_ANDROID
+ // Known datetimes
+ qint64 std = QDateTime(QDate(2012, 1, 1), QTime(0, 0, 0), Qt::UTC).toMSecsSinceEpoch();
+ qint64 dst = QDateTime(QDate(2012, 6, 1), QTime(0, 0, 0), Qt::UTC).toMSecsSinceEpoch();
+
+ // Test default constructor
+ QTzTimeZonePrivate tzpd;
+ QVERIFY(tzpd.isValid());
+
+ // Test invalid constructor
+ QTzTimeZonePrivate tzpi("Gondwana/Erewhon");
+ QCOMPARE(tzpi.isValid(), false);
+
+ // Test named constructor
+ QTzTimeZonePrivate tzp("Europe/Berlin");
+ QVERIFY(tzp.isValid());
+
+ // Test POSIX-format value for $TZ:
+ QTzTimeZonePrivate tzposix("MET-1METDST-2,M3.5.0/02:00:00,M10.5.0/03:00:00");
+ QVERIFY(tzposix.isValid());
+
+ QTimeZone tzBrazil("BRT+3"); // parts of Northern Brazil, as a POSIX rule
+ QVERIFY(tzBrazil.isValid());
+ QCOMPARE(tzBrazil.offsetFromUtc(QDateTime(QDate(1111, 11, 11).startOfDay())), -10800);
+
+ // Test display names by type, either ICU or abbreviation only
+ QLocale enUS("en_US");
+ // Only test names in debug mode, names used can vary by ICU version installed
+ if (debug) {
+#if QT_CONFIG(icu)
+ QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::LongName, enUS),
+ QString("Central European Standard Time"));
+ QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::ShortName, enUS),
+ QString("GMT+01:00"));
+ QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::OffsetName, enUS),
+ QString("UTC+01:00"));
+ QCOMPARE(tzp.displayName(QTimeZone::DaylightTime, QTimeZone::LongName, enUS),
+ QString("Central European Summer Time"));
+ QCOMPARE(tzp.displayName(QTimeZone::DaylightTime, QTimeZone::ShortName, enUS),
+ QString("GMT+02:00"));
+ QCOMPARE(tzp.displayName(QTimeZone::DaylightTime, QTimeZone::OffsetName, enUS),
+ QString("UTC+02:00"));
+ // ICU C api does not support Generic Time yet, C++ api does
+ QCOMPARE(tzp.displayName(QTimeZone::GenericTime, QTimeZone::LongName, enUS),
+ QString("Central European Standard Time"));
+ QCOMPARE(tzp.displayName(QTimeZone::GenericTime, QTimeZone::ShortName, enUS),
+ QString("GMT+01:00"));
+ QCOMPARE(tzp.displayName(QTimeZone::GenericTime, QTimeZone::OffsetName, enUS),
+ QString("UTC+01:00"));
+#else
+ QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::LongName, enUS),
+ QString("CET"));
+ QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::ShortName, enUS),
+ QString("CET"));
+ QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::OffsetName, enUS),
+ QString("CET"));
+ QCOMPARE(tzp.displayName(QTimeZone::DaylightTime, QTimeZone::LongName, enUS),
+ QString("CEST"));
+ QCOMPARE(tzp.displayName(QTimeZone::DaylightTime, QTimeZone::ShortName, enUS),
+ QString("CEST"));
+ QCOMPARE(tzp.displayName(QTimeZone::DaylightTime, QTimeZone::OffsetName, enUS),
+ QString("CEST"));
+ QCOMPARE(tzp.displayName(QTimeZone::GenericTime, QTimeZone::LongName, enUS),
+ QString("CET"));
+ QCOMPARE(tzp.displayName(QTimeZone::GenericTime, QTimeZone::ShortName, enUS),
+ QString("CET"));
+ QCOMPARE(tzp.displayName(QTimeZone::GenericTime, QTimeZone::OffsetName, enUS),
+ QString("CET"));
+#endif // icu
+
+ // Test Abbreviations
+ QCOMPARE(tzp.abbreviation(std), QString("CET"));
+ QCOMPARE(tzp.abbreviation(dst), QString("CEST"));
+ }
+
+ testCetPrivate(tzp);
+ testEpochTranPrivate(QTzTimeZonePrivate("America/Toronto"));
+
+ // Test first and last transition rule
+ // Warning: This could vary depending on age of TZ file!
+
+ // Test low date uses first rule found
+ // Note: Depending on the OS in question, the database may be carrying the
+ // Local Mean Time. which for Berlin is 0:53:28
+ QTimeZonePrivate::Data dat = tzp.data(-9999999999999);
+ QCOMPARE(dat.atMSecsSinceEpoch, (qint64)-9999999999999);
+ QCOMPARE(dat.daylightTimeOffset, 0);
+ if (dat.abbreviation == "LMT") {
+ QCOMPARE(dat.standardTimeOffset, 3208);
+ } else {
+ QCOMPARE(dat.standardTimeOffset, 3600);
+
+ // Test previous to low value is invalid
+ dat = tzp.previousTransition(-9999999999999);
+ QCOMPARE(dat.atMSecsSinceEpoch, std::numeric_limits<qint64>::min());
+ QCOMPARE(dat.standardTimeOffset, std::numeric_limits<int>::min());
+ QCOMPARE(dat.daylightTimeOffset, std::numeric_limits<int>::min());
+ }
+
+ dat = tzp.nextTransition(-9999999999999);
+ QCOMPARE(QDateTime::fromMSecsSinceEpoch(dat.atMSecsSinceEpoch, Qt::OffsetFromUTC, 3600),
+ QDateTime(QDate(1893, 4, 1), QTime(0, 6, 32), Qt::OffsetFromUTC, 3600));
+ QCOMPARE(dat.standardTimeOffset, 3600);
+ QCOMPARE(dat.daylightTimeOffset, 0);
+
+ // Known high datetimes
+ qint64 stdHi = QDateTime(QDate(2100, 1, 1), QTime(0, 0, 0), Qt::UTC).toMSecsSinceEpoch();
+ qint64 dstHi = QDateTime(QDate(2100, 6, 1), QTime(0, 0, 0), Qt::UTC).toMSecsSinceEpoch();
+
+ // Tets high dates use the POSIX rule
+ dat = tzp.data(stdHi);
+ QCOMPARE(dat.atMSecsSinceEpoch - stdHi, (qint64)0);
+ QCOMPARE(dat.offsetFromUtc, 3600);
+ QCOMPARE(dat.standardTimeOffset, 3600);
+ QCOMPARE(dat.daylightTimeOffset, 0);
+
+ dat = tzp.data(dstHi);
+ QCOMPARE(dat.atMSecsSinceEpoch - dstHi, (qint64)0);
+ QCOMPARE(dat.offsetFromUtc, 7200);
+ QCOMPARE(dat.standardTimeOffset, 3600);
+ QCOMPARE(dat.daylightTimeOffset, 3600);
+
+ dat = tzp.previousTransition(stdHi);
+ QCOMPARE(QDateTime::fromMSecsSinceEpoch(dat.atMSecsSinceEpoch, Qt::OffsetFromUTC, 3600),
+ QDateTime(QDate(2099, 10, 26), QTime(2, 0), Qt::OffsetFromUTC, 3600));
+ QCOMPARE(dat.offsetFromUtc, 3600);
+ QCOMPARE(dat.standardTimeOffset, 3600);
+ QCOMPARE(dat.daylightTimeOffset, 0);
+
+ dat = tzp.previousTransition(dstHi);
+ QCOMPARE(QDateTime::fromMSecsSinceEpoch(dat.atMSecsSinceEpoch, Qt::OffsetFromUTC, 3600),
+ QDateTime(QDate(2100, 3, 29), QTime(2, 0), Qt::OffsetFromUTC, 3600));
+ QCOMPARE(dat.offsetFromUtc, 7200);
+ QCOMPARE(dat.standardTimeOffset, 3600);
+ QCOMPARE(dat.daylightTimeOffset, 3600);
+
+ dat = tzp.nextTransition(stdHi);
+ QCOMPARE(QDateTime::fromMSecsSinceEpoch(dat.atMSecsSinceEpoch, Qt::OffsetFromUTC, 3600),
+ QDateTime(QDate(2100, 3, 29), QTime(2, 0), Qt::OffsetFromUTC, 3600));
+ QCOMPARE(dat.offsetFromUtc, 7200);
+ QCOMPARE(dat.standardTimeOffset, 3600);
+ QCOMPARE(dat.daylightTimeOffset, 3600);
+
+ dat = tzp.nextTransition(dstHi);
+ QCOMPARE(QDateTime::fromMSecsSinceEpoch(dat.atMSecsSinceEpoch, Qt::OffsetFromUTC, 3600),
+ QDateTime(QDate(2100, 10, 25), QTime(2, 0), Qt::OffsetFromUTC, 3600));
+ QCOMPARE(dat.offsetFromUtc, 3600);
+ QCOMPARE(dat.standardTimeOffset, 3600);
+ QCOMPARE(dat.daylightTimeOffset, 0);
+
+ // Test TZ timezone vs UTC timezone for fractionary negative offset
+ QTzTimeZonePrivate tztz1("America/Caracas");
+ QUtcTimeZonePrivate tzutc1("UTC-04:30");
+ QVERIFY(tztz1.isValid());
+ QVERIFY(tzutc1.isValid());
+ QTzTimeZonePrivate::Data datatz1 = tztz1.data(std);
+ QTzTimeZonePrivate::Data datautc1 = tzutc1.data(std);
+ QCOMPARE(datatz1.offsetFromUtc, datautc1.offsetFromUtc);
+
+ // Test TZ timezone vs UTC timezone for fractionary positive offset
+ QTzTimeZonePrivate tztz2("Asia/Calcutta");
+ QUtcTimeZonePrivate tzutc2("UTC+05:30");
+ QVERIFY(tztz2.isValid());
+ QVERIFY(tzutc2.isValid());
+ QTzTimeZonePrivate::Data datatz2 = tztz2.data(std);
+ QTzTimeZonePrivate::Data datautc2 = tzutc2.data(std);
+ QCOMPARE(datatz2.offsetFromUtc, datautc2.offsetFromUtc);
+
+ // Test a timezone with a name that isn't all letters
+ QTzTimeZonePrivate tzBarnaul("Asia/Barnaul");
+ if (tzBarnaul.isValid()) {
+ QCOMPARE(tzBarnaul.data(std).abbreviation, QString("+07"));
+
+ // first full day of the new rule (tzdata2016b)
+ QDateTime dt(QDate(2016, 3, 28), QTime(0, 0, 0), Qt::UTC);
+ QCOMPARE(tzBarnaul.data(dt.toMSecsSinceEpoch()).abbreviation, QString("+07"));
+ }
+#endif // QT_BUILD_INTERNAL && Q_OS_UNIX && !Q_OS_DARWIN
+}
+
+void tst_QTimeZone::macTest()
+{
+#if defined(QT_BUILD_INTERNAL) && defined(Q_OS_DARWIN)
+ // Known datetimes
+ qint64 std = QDateTime(QDate(2012, 1, 1), QTime(0, 0, 0), Qt::UTC).toMSecsSinceEpoch();
+ qint64 dst = QDateTime(QDate(2012, 6, 1), QTime(0, 0, 0), Qt::UTC).toMSecsSinceEpoch();
+
+ // Test default constructor
+ QMacTimeZonePrivate tzpd;
+ QVERIFY(tzpd.isValid());
+
+ // Test invalid constructor
+ QMacTimeZonePrivate tzpi("Gondwana/Erewhon");
+ QCOMPARE(tzpi.isValid(), false);
+
+ // Test named constructor
+ QMacTimeZonePrivate tzp("Europe/Berlin");
+ QVERIFY(tzp.isValid());
+
+ // Only test names in debug mode, names used can vary by version
+ if (debug) {
+ // Test display names by type
+ QLocale enUS("en_US");
+ QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::LongName, enUS),
+ QString("Central European Standard Time"));
+ QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::ShortName, enUS),
+ QString("GMT+01:00"));
+ QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::OffsetName, enUS),
+ QString("UTC+01:00"));
+ QCOMPARE(tzp.displayName(QTimeZone::DaylightTime, QTimeZone::LongName, enUS),
+ QString("Central European Summer Time"));
+ QCOMPARE(tzp.displayName(QTimeZone::DaylightTime, QTimeZone::ShortName, enUS),
+ QString("GMT+02:00"));
+ QCOMPARE(tzp.displayName(QTimeZone::DaylightTime, QTimeZone::OffsetName, enUS),
+ QString("UTC+02:00"));
+ // ICU C api does not support Generic Time yet, C++ api does
+ QCOMPARE(tzp.displayName(QTimeZone::GenericTime, QTimeZone::LongName, enUS),
+ QString("Central European Time"));
+ QCOMPARE(tzp.displayName(QTimeZone::GenericTime, QTimeZone::ShortName, enUS),
+ QString("Germany Time"));
+ QCOMPARE(tzp.displayName(QTimeZone::GenericTime, QTimeZone::OffsetName, enUS),
+ QString("UTC+01:00"));
+
+ // Test Abbreviations
+ QCOMPARE(tzp.abbreviation(std), QString("CET"));
+ QCOMPARE(tzp.abbreviation(dst), QString("CEST"));
+ }
+
+ testCetPrivate(tzp);
+ testEpochTranPrivate(QMacTimeZonePrivate("America/Toronto"));
+#endif // QT_BUILD_INTERNAL && Q_OS_DARWIN
+}
+
+void tst_QTimeZone::darwinTypes()
+{
+#ifndef Q_OS_DARWIN
+ QSKIP("This is an Apple-only test");
+#else
+ extern void tst_QTimeZone_darwinTypes(); // in tst_qtimezone_darwin.mm
+ tst_QTimeZone_darwinTypes();
+#endif
+}
+
+void tst_QTimeZone::winTest()
+{
+#if defined(QT_BUILD_INTERNAL) && defined(USING_WIN_TZ)
+ // Known datetimes
+ qint64 std = QDateTime(QDate(2012, 1, 1), QTime(0, 0, 0), Qt::UTC).toMSecsSinceEpoch();
+ qint64 dst = QDateTime(QDate(2012, 6, 1), QTime(0, 0, 0), Qt::UTC).toMSecsSinceEpoch();
+
+ // Test default constructor
+ QWinTimeZonePrivate tzpd;
+ if (debug)
+ qDebug() << "System ID = " << tzpd.id()
+ << tzpd.displayName(QTimeZone::StandardTime, QTimeZone::LongName, QLocale())
+ << tzpd.displayName(QTimeZone::GenericTime, QTimeZone::LongName, QLocale());
+ QVERIFY(tzpd.isValid());
+
+ // Test invalid constructor
+ QWinTimeZonePrivate tzpi("Gondwana/Erewhon");
+ QCOMPARE(tzpi.isValid(), false);
+
+ // Test named constructor
+ QWinTimeZonePrivate tzp("Europe/Berlin");
+ QVERIFY(tzp.isValid());
+
+ // Only test names in debug mode, names used can vary by version
+ if (debug) {
+ // Test display names by type
+ QLocale enUS("en_US");
+ QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::LongName, enUS),
+ QString("W. Europe Standard Time"));
+ QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::ShortName, enUS),
+ QString("W. Europe Standard Time"));
+ QCOMPARE(tzp.displayName(QTimeZone::StandardTime, QTimeZone::OffsetName, enUS),
+ QString("UTC+01:00"));
+ QCOMPARE(tzp.displayName(QTimeZone::DaylightTime, QTimeZone::LongName, enUS),
+ QString("W. Europe Daylight Time"));
+ QCOMPARE(tzp.displayName(QTimeZone::DaylightTime, QTimeZone::ShortName, enUS),
+ QString("W. Europe Daylight Time"));
+ QCOMPARE(tzp.displayName(QTimeZone::DaylightTime, QTimeZone::OffsetName, enUS),
+ QString("UTC+02:00"));
+ QCOMPARE(tzp.displayName(QTimeZone::GenericTime, QTimeZone::LongName, enUS),
+ QString("(UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna"));
+ QCOMPARE(tzp.displayName(QTimeZone::GenericTime, QTimeZone::ShortName, enUS),
+ QString("(UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna"));
+ QCOMPARE(tzp.displayName(QTimeZone::GenericTime, QTimeZone::OffsetName, enUS),
+ QString("UTC+01:00"));
+
+ // Test Abbreviations
+ QCOMPARE(tzp.abbreviation(std), QString("W. Europe Standard Time"));
+ QCOMPARE(tzp.abbreviation(dst), QString("W. Europe Daylight Time"));
+ }
+
+ testCetPrivate(tzp);
+ testEpochTranPrivate(QWinTimeZonePrivate("America/Toronto"));
+#endif // QT_BUILD_INTERNAL && USING_WIN_TZ
+}
+
+#ifdef QT_BUILD_INTERNAL
+// Test each private produces the same basic results for CET
+void tst_QTimeZone::testCetPrivate(const QTimeZonePrivate &tzp)
+{
+ // Known datetimes
+ qint64 std = QDateTime(QDate(2012, 1, 1), QTime(0, 0, 0), Qt::UTC).toMSecsSinceEpoch();
+ qint64 dst = QDateTime(QDate(2012, 6, 1), QTime(0, 0, 0), Qt::UTC).toMSecsSinceEpoch();
+ qint64 prev = QDateTime(QDate(2011, 1, 1), QTime(0, 0, 0), Qt::UTC).toMSecsSinceEpoch();
+
+ QCOMPARE(tzp.offsetFromUtc(std), 3600);
+ QCOMPARE(tzp.offsetFromUtc(dst), 7200);
+
+ QCOMPARE(tzp.standardTimeOffset(std), 3600);
+ QCOMPARE(tzp.standardTimeOffset(dst), 3600);
+
+ QCOMPARE(tzp.daylightTimeOffset(std), 0);
+ QCOMPARE(tzp.daylightTimeOffset(dst), 3600);
+
+ QCOMPARE(tzp.hasDaylightTime(), true);
+ QCOMPARE(tzp.isDaylightTime(std), false);
+ QCOMPARE(tzp.isDaylightTime(dst), true);
+
+ QTimeZonePrivate::Data dat = tzp.data(std);
+ QCOMPARE(dat.atMSecsSinceEpoch, std);
+ QCOMPARE(dat.offsetFromUtc, 3600);
+ QCOMPARE(dat.standardTimeOffset, 3600);
+ QCOMPARE(dat.daylightTimeOffset, 0);
+ QCOMPARE(dat.abbreviation, tzp.abbreviation(std));
+
+ dat = tzp.data(dst);
+ QCOMPARE(dat.atMSecsSinceEpoch, dst);
+ QCOMPARE(dat.offsetFromUtc, 7200);
+ QCOMPARE(dat.standardTimeOffset, 3600);
+ QCOMPARE(dat.daylightTimeOffset, 3600);
+ QCOMPARE(dat.abbreviation, tzp.abbreviation(dst));
+
+ // Only test transitions if host system supports them
+ if (tzp.hasTransitions()) {
+ QTimeZonePrivate::Data tran = tzp.nextTransition(std);
+ // 2012-03-25 02:00 CET, +1 -> +2
+ QCOMPARE(QDateTime::fromMSecsSinceEpoch(tran.atMSecsSinceEpoch, Qt::UTC),
+ QDateTime(QDate(2012, 3, 25), QTime(2, 0), Qt::OffsetFromUTC, 3600));
+ QCOMPARE(tran.offsetFromUtc, 7200);
+ QCOMPARE(tran.standardTimeOffset, 3600);
+ QCOMPARE(tran.daylightTimeOffset, 3600);
+
+ tran = tzp.nextTransition(dst);
+ // 2012-10-28 03:00 CEST, +2 -> +1
+ QCOMPARE(QDateTime::fromMSecsSinceEpoch(tran.atMSecsSinceEpoch, Qt::UTC),
+ QDateTime(QDate(2012, 10, 28), QTime(3, 0), Qt::OffsetFromUTC, 2 * 3600));
+ QCOMPARE(tran.offsetFromUtc, 3600);
+ QCOMPARE(tran.standardTimeOffset, 3600);
+ QCOMPARE(tran.daylightTimeOffset, 0);
+
+ tran = tzp.previousTransition(std);
+ // 2011-10-30 03:00 CEST, +2 -> +1
+ QCOMPARE(QDateTime::fromMSecsSinceEpoch(tran.atMSecsSinceEpoch, Qt::UTC),
+ QDateTime(QDate(2011, 10, 30), QTime(3, 0), Qt::OffsetFromUTC, 2 * 3600));
+ QCOMPARE(tran.offsetFromUtc, 3600);
+ QCOMPARE(tran.standardTimeOffset, 3600);
+ QCOMPARE(tran.daylightTimeOffset, 0);
+
+ tran = tzp.previousTransition(dst);
+ // 2012-03-25 02:00 CET, +1 -> +2 (again)
+ QCOMPARE(QDateTime::fromMSecsSinceEpoch(tran.atMSecsSinceEpoch, Qt::UTC),
+ QDateTime(QDate(2012, 3, 25), QTime(2, 0), Qt::OffsetFromUTC, 3600));
+ QCOMPARE(tran.offsetFromUtc, 7200);
+ QCOMPARE(tran.standardTimeOffset, 3600);
+ QCOMPARE(tran.daylightTimeOffset, 3600);
+
+ QTimeZonePrivate::DataList expected;
+ // 2011-03-27 02:00 CET, +1 -> +2
+ tran.atMSecsSinceEpoch = QDateTime(QDate(2011, 3, 27), QTime(2, 0),
+ Qt::OffsetFromUTC, 3600).toMSecsSinceEpoch();
+ tran.offsetFromUtc = 7200;
+ tran.standardTimeOffset = 3600;
+ tran.daylightTimeOffset = 3600;
+ expected << tran;
+ // 2011-10-30 03:00 CEST, +2 -> +1
+ tran.atMSecsSinceEpoch = QDateTime(QDate(2011, 10, 30), QTime(3, 0),
+ Qt::OffsetFromUTC, 2 * 3600).toMSecsSinceEpoch();
+ tran.offsetFromUtc = 3600;
+ tran.standardTimeOffset = 3600;
+ tran.daylightTimeOffset = 0;
+ expected << tran;
+ QTimeZonePrivate::DataList result = tzp.transitions(prev, std);
+ QCOMPARE(result.count(), expected.count());
+ for (int i = 0; i < expected.count(); ++i) {
+ QCOMPARE(QDateTime::fromMSecsSinceEpoch(result.at(i).atMSecsSinceEpoch,
+ Qt::OffsetFromUTC, 3600),
+ QDateTime::fromMSecsSinceEpoch(expected.at(i).atMSecsSinceEpoch,
+ Qt::OffsetFromUTC, 3600));
+ QCOMPARE(result.at(i).offsetFromUtc, expected.at(i).offsetFromUtc);
+ QCOMPARE(result.at(i).standardTimeOffset, expected.at(i).standardTimeOffset);
+ QCOMPARE(result.at(i).daylightTimeOffset, expected.at(i).daylightTimeOffset);
+ }
+ }
+}
+
+// Needs a zone with DST around the epoch; currently America/Toronto (EST5EDT)
+void tst_QTimeZone::testEpochTranPrivate(const QTimeZonePrivate &tzp)
+{
+ if (!tzp.hasTransitions())
+ return; // test only viable for transitions
+
+ QTimeZonePrivate::Data tran = tzp.nextTransition(0); // i.e. first after epoch
+ // 1970-04-26 02:00 EST, -5 -> -4
+ const QDateTime after = QDateTime(QDate(1970, 4, 26), QTime(2, 0), Qt::OffsetFromUTC, -5 * 3600);
+ const QDateTime found = QDateTime::fromMSecsSinceEpoch(tran.atMSecsSinceEpoch, Qt::UTC);
+#ifdef USING_WIN_TZ // MS gets the date wrong: 5th April instead of 26th.
+ QCOMPARE(found.toOffsetFromUtc(-5 * 3600).time(), after.time());
+#else
+ QCOMPARE(found, after);
+#endif
+ QCOMPARE(tran.offsetFromUtc, -4 * 3600);
+ QCOMPARE(tran.standardTimeOffset, -5 * 3600);
+ QCOMPARE(tran.daylightTimeOffset, 3600);
+
+ // Pre-epoch time-zones might not be supported at all:
+ tran = tzp.nextTransition(QDateTime(QDate(1601, 1, 1), QTime(0, 0),
+ Qt::UTC).toMSecsSinceEpoch());
+ if (tran.atMSecsSinceEpoch != QTimeZonePrivate::invalidMSecs()
+ // Toronto *did* have a transition before 1970 (DST since 1918):
+ && tran.atMSecsSinceEpoch < 0) {
+ // ... but, if they are, we should be able to search back to them:
+ tran = tzp.previousTransition(0); // i.e. last before epoch
+ // 1969-10-26 02:00 EDT, -4 -> -5
+ QCOMPARE(QDateTime::fromMSecsSinceEpoch(tran.atMSecsSinceEpoch, Qt::UTC),
+ QDateTime(QDate(1969, 10, 26), QTime(2, 0), Qt::OffsetFromUTC, -4 * 3600));
+ QCOMPARE(tran.offsetFromUtc, -5 * 3600);
+ QCOMPARE(tran.standardTimeOffset, -5 * 3600);
+ QCOMPARE(tran.daylightTimeOffset, 0);
+ } else {
+ // Do not use QSKIP(): that would discard the rest of this sub-test's caller.
+ qDebug() << "No support for pre-epoch time-zone transitions";
+ }
+}
+#endif // QT_BUILD_INTERNAL
+
+QTEST_APPLESS_MAIN(tst_QTimeZone)
+#include "tst_qtimezone.moc"
diff --git a/tests/auto/corelib/time/qtimezone/tst_qtimezone_darwin.mm b/tests/auto/corelib/time/qtimezone/tst_qtimezone_darwin.mm
new file mode 100644
index 0000000000..de801e55d0
--- /dev/null
+++ b/tests/auto/corelib/time/qtimezone/tst_qtimezone_darwin.mm
@@ -0,0 +1,68 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtCore/QTimeZone>
+#include <QtTest/QtTest>
+
+#include <CoreFoundation/CoreFoundation.h>
+#include <Foundation/Foundation.h>
+
+void tst_QTimeZone_darwinTypes()
+{
+#if !defined(QT_NO_SYSTEMLOCALE)
+ // QTimeZone <-> CFTimeZone
+ {
+ QTimeZone qtTimeZone("America/Los_Angeles");
+ const CFTimeZoneRef cfTimeZone = qtTimeZone.toCFTimeZone();
+ QCOMPARE(QTimeZone::fromCFTimeZone(cfTimeZone), qtTimeZone);
+ CFRelease(cfTimeZone);
+ }
+ {
+ CFTimeZoneRef cfTimeZone = CFTimeZoneCreateWithName(kCFAllocatorDefault,
+ CFSTR("America/Los_Angeles"), false);
+ const QTimeZone qtTimeZone = QTimeZone::fromCFTimeZone(cfTimeZone);
+ QVERIFY(CFEqual(qtTimeZone.toCFTimeZone(), cfTimeZone));
+ CFRelease(cfTimeZone);
+ }
+ // QTimeZone <-> NSTimeZone
+ {
+ NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
+ QTimeZone qtTimeZone("America/Los_Angeles");
+ const NSTimeZone *nsTimeZone = qtTimeZone.toNSTimeZone();
+ QCOMPARE(QTimeZone::fromNSTimeZone(nsTimeZone), qtTimeZone);
+ [autoreleasepool release];
+ }
+ {
+ NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
+ NSTimeZone *nsTimeZone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
+ const QTimeZone qtTimeZone = QTimeZone::fromNSTimeZone(nsTimeZone);
+ QVERIFY([qtTimeZone.toNSTimeZone() isEqual:nsTimeZone]);
+ [autoreleasepool release];
+ }
+#endif
+}
diff --git a/tests/auto/corelib/time/time.pro b/tests/auto/corelib/time/time.pro
new file mode 100644
index 0000000000..6f9ff038db
--- /dev/null
+++ b/tests/auto/corelib/time/time.pro
@@ -0,0 +1,6 @@
+TEMPLATE = subdirs
+SUBDIRS = \
+ qdate \
+ qdatetime \
+ qtime \
+ qtimezone