Return invalid datetime when calling addMSecs with invalid QDateTime.

This is in line with what QTime::addMSecs and QDate::addDays do, for
example.

Change-Id: I902112486727f74f669a97bde6c42028e0838f8d
Reviewed-by: Jon Severinsson <jon@severinsson.net>
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@digia.com>
This commit is contained in:
Mitch Curtis 2012-10-17 13:56:50 +02:00 committed by The Qt Project
parent 8310533975
commit b016597db8
2 changed files with 41 additions and 22 deletions

View File

@ -2692,6 +2692,9 @@ QDateTime QDateTime::addYears(int nyears) const
QDateTime QDateTimePrivate::addMSecs(const QDateTime &dt, qint64 msecs)
{
if (!dt.isValid())
return QDateTime();
QDate utcDate;
QTime utcTime;
dt.d->getUTC(utcDate, utcTime);
@ -2742,6 +2745,8 @@ void QDateTimePrivate::addMSecs(QDate &utcDate, QTime &utcTime, qint64 msecs)
later than the datetime of this object (or earlier if \a s is
negative).
If this datetime is invalid, an invalid datetime will be returned.
\sa addMSecs(), secsTo(), addDays(), addMonths(), addYears()
*/
@ -2755,6 +2760,8 @@ QDateTime QDateTime::addSecs(qint64 s) const
later than the datetime of this object (or earlier if \a msecs is
negative).
If this datetime is invalid, an invalid datetime will be returned.
\sa addSecs(), msecsTo(), addDays(), addMonths(), addYears()
*/
QDateTime QDateTime::addMSecs(qint64 msecs) const

View File

@ -728,6 +728,8 @@ void tst_QDateTime::addSecs_data()
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();
}
void tst_QDateTime::addSecs()
@ -915,17 +917,22 @@ void tst_QDateTime::secsTo()
QFETCH(int, nsecs);
QFETCH(QDateTime, result);
#ifdef Q_OS_IRIX
QEXPECT_FAIL("cet4", "IRIX databases say 1970 had DST", Abort);
#endif
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));
if (dt.isValid()) {
#ifdef Q_OS_IRIX
QEXPECT_FAIL("cet4", "IRIX databases say 1970 had DST", Abort);
#endif
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()
@ -939,17 +946,22 @@ void tst_QDateTime::msecsTo()
QFETCH(int, nsecs);
QFETCH(QDateTime, result);
#ifdef Q_OS_IRIX
QEXPECT_FAIL("cet4", "IRIX databases say 1970 had DST", Abort);
#endif
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)));
if (dt.isValid()) {
#ifdef Q_OS_IRIX
QEXPECT_FAIL("cet4", "IRIX databases say 1970 had DST", Abort);
#endif
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()