Disregard milliseconds in QTime::secsTo().
authorMitch Curtis <mitch.curtis@nokia.com>
Tue, 31 Jul 2012 09:34:43 +0000 (11:34 +0200)
committerQt by Nokia <qt-info@nokia.com>
Tue, 21 Aug 2012 09:05:20 +0000 (11:05 +0200)
"The documentation states that "secsTo() does not take into account any
milliseconds", however, this is not the case. Given times 12:30:01.500
and 12:30:02.400 secsTo returns 0. If milliseconds are not taken into
account, I would expect this to return 1 (i.e. interprets the times as
12:30:01 and 12:30:02 thus truncating the milliseconds)."

Note that tests were also written for QDateTime::secsTo(), as it uses
QTime::secsTo internally. This addresses Javier's issue in the
comments of QTBUG-9304.

Task-number: QTBUG-9304
Change-Id: I9efe0c8f710db859c1d086d67ba3e5b349a56c4e
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
src/corelib/tools/qdatetime.cpp
tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp
tests/auto/corelib/tools/qtime/tst_qtime.cpp

index 3324408..fe53895 100644 (file)
@@ -1655,7 +1655,10 @@ int QTime::secsTo(const QTime &t) const
     if (!isValid() || !t.isValid())
         return 0;
 
-    return (t.ds() - ds()) / 1000;
+    // Truncate milliseconds as we do not want to consider them.
+    int ourSeconds = ds() / 1000;
+    int theirSeconds = t.ds() / 1000;
+    return theirSeconds - ourSeconds;
 }
 
 /*!
index 70b761f..0c018d7 100644 (file)
@@ -895,6 +895,14 @@ void tst_QDateTime::daysTo()
 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()
index 9a49dac..a747b1b 100644 (file)
@@ -305,6 +305,10 @@ void tst_QTime::secsTo_data()
     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()