Parcourir la source

When calculating QDateTime diffs, take daylight saving into account.

Sascha Zelzer il y a 13 ans
Parent
commit
9eb70828f1
2 fichiers modifiés avec 8 ajouts et 5 suppressions
  1. 5 3
      Libs/Core/ctkUtils.cpp
  2. 3 2
      Libs/Core/ctkUtils.h

+ 5 - 3
Libs/Core/ctkUtils.cpp

@@ -318,7 +318,9 @@ QString ctk::qtHandleToString(Qt::HANDLE handle)
 //-----------------------------------------------------------------------------
 qint64 ctk::msecsTo(const QDateTime& t1, const QDateTime& t2)
 {
-  qint64 days = t1.daysTo(t2);
-  qint64 msecs = t1.time().msecsTo(t2.time());
-  return days*(1000*3600*24) + msecs;
+  QDateTime utcT1 = t1.toUTC();
+  QDateTime utcT2 = t2.toUTC();
+
+  return static_cast<qint64>(utcT1.daysTo(utcT2)) * static_cast<qint64>(1000*3600*24)
+      + static_cast<qint64>(utcT1.time().msecsTo(utcT2.time()));
 }

+ 3 - 2
Libs/Core/ctkUtils.h

@@ -126,10 +126,11 @@ QString CTK_CORE_EXPORT qtHandleToString(Qt::HANDLE handle);
 
 ///
 /// \ingroup Core
-/// \brief Compute the milli secons from one QDateTime to an other.
+/// \brief Compute the milli seconds from one QDateTime to an other.
 ///
 /// This function can be used to correctly compute the amount of milli
-/// seconds from <code>t1</code> to <code>t2</code>. This is for
+/// seconds from <code>t1</code> to <code>t2</code>. The QDateTime objects
+/// are converted to Qt::UTC to take daylight saving time into account. This is for
 /// back-wards compatibility with Qt 4.6. Since Qt 4.7 there exists
 /// a QDateTime::msecsTo() method which should be used instead, after
 /// bumping the minimum required Qt version for CTK.