diff options
Diffstat (limited to 'tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp')
-rw-r--r-- | tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp | 83 |
1 files changed, 73 insertions, 10 deletions
diff --git a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp index 4604e664b0..5f9f5fbb6f 100644 --- a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp @@ -77,8 +77,10 @@ private slots: void fromMSecsSinceEpoch(); void toString_isoDate_data(); void toString_isoDate(); + void toString_isoDate_extra(); void toString_textDate_data(); void toString_textDate(); + void toString_textDate_extra(); void toString_rfcDate_data(); void toString_rfcDate(); void toString_enumformat(); @@ -153,8 +155,6 @@ private: QDateTime invalidDateTime() const { return QDateTime(invalidDate(), invalidTime()); } QDate invalidDate() const { return QDate(); } QTime invalidTime() const { return QTime(-1, -1, -1); } - qint64 minJd() const { return QDateTimePrivate::minJd(); } - qint64 maxJd() const { return QDateTimePrivate::maxJd(); } }; Q_DECLARE_METATYPE(Qt::TimeSpec) @@ -606,10 +606,8 @@ void tst_QDateTime::setMSecsSinceEpoch_data() << QDateTime(QDate::fromJulianDay(0x7fffffff), QTime(21, 59, 59, 999), Qt::UTC) << QDateTime(QDate::fromJulianDay(0x7fffffff), QTime(23, 59, 59, 999)); QTest::newRow("min") - // + 1 because, in the reference check below, calling addMSecs(qint64min) - // will internally apply unary minus to -qint64min, resulting in a - // positive value 1 too big for qint64max, causing an overflow. - << std::numeric_limits<qint64>::min() + 1 + // 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") @@ -687,8 +685,8 @@ void tst_QDateTime::fromMSecsSinceEpoch() QDateTime dtUtc = QDateTime::fromMSecsSinceEpoch(msecs, Qt::UTC); QDateTime dtOffset = QDateTime::fromMSecsSinceEpoch(msecs, Qt::OffsetFromUTC, 60*60); - // LocalTime will overflow for min or max, depending on whether you're - // East or West of Greenwich. The test passes at GMT. + // 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); @@ -794,6 +792,28 @@ void tst_QDateTime::toString_isoDate() 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 +} + void tst_QDateTime::toString_textDate_data() { QTest::addColumn<QDateTime>("datetime"); @@ -831,6 +851,49 @@ void tst_QDateTime::toString_textDate() QCOMPARE(resultDatetime.utcOffset(), datetime.utcOffset()); } +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) + QTimeZone PST("America/Vancouver"); + if (PST.isValid()) { + dt = QDateTime::fromMSecsSinceEpoch(0, PST); +# if defined Q_OS_UNIX && !defined Q_OS_DARWIN + QCOMPARE(dt.toString(), QLatin1String("Wed Dec 31 16:00:00 1969 PST")); +# else // QTBUG-57320, QTBUG-57298 + 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); +# if defined Q_OS_UNIX && !defined Q_OS_DARWIN + QCOMPARE(dt.toString(), QLatin1String("Thu Jan 1 01:00:00 1970 CET")); +# else // QTBUG-57320, QTBUG-57298 + 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)); +} + void tst_QDateTime::toString_rfcDate_data() { QTest::addColumn<QDateTime>("dt"); @@ -3145,17 +3208,17 @@ void tst_QDateTime::timeZones() const // 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); - QEXPECT_FAIL("", "QDateTime doesn't properly support Daylight Transitions", Continue); 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); - QEXPECT_FAIL("", "QDateTime doesn't properly support Daylight Transitions", Continue); 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); |