summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qdatetime.cpp
diff options
context:
space:
mode:
authorJason Hollingsworth <jhworth.developer@gmail.com>2010-05-11 14:26:42 (GMT)
committerBenjamin Poulain <benjamin.poulain@nokia.com>2010-05-11 14:29:51 (GMT)
commita1cda97c4002195f98c18c460d1f30682bb5a82e (patch)
treecb0424081bbd6c34d6b7a010a2d3e992c1c7e9e0 /src/corelib/tools/qdatetime.cpp
parent949f49c9ea4dadfc6e7f3bf7d822830b7ef8a8e6 (diff)
downloadQt-a1cda97c4002195f98c18c460d1f30682bb5a82e.zip
Qt-a1cda97c4002195f98c18c460d1f30682bb5a82e.tar.gz
Qt-a1cda97c4002195f98c18c460d1f30682bb5a82e.tar.bz2
Added QDateTime::msecsTo()
This adds a QDateTime::msecsTo() function which is similar to QDateTime::secsTo(). This refers to QTBUG-8790 and task 147685. According to the task this functionality should have been added in version 4.5.0 but it never was. Task-number: 147685 Task-number: QTBUG-8790 Merge-request: 501 Reviewed-by: Benjamin Poulain <benjamin.poulain@nokia.com> Reviewed-by: Andreas Kling <andreas.kling@nokia.com>
Diffstat (limited to 'src/corelib/tools/qdatetime.cpp')
-rw-r--r--src/corelib/tools/qdatetime.cpp44
1 files changed, 36 insertions, 8 deletions
diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp
index 9afcd80..9f5d8c6 100644
--- a/src/corelib/tools/qdatetime.cpp
+++ b/src/corelib/tools/qdatetime.cpp
@@ -1705,7 +1705,7 @@ int QTime::secsTo(const QTime &t) const
Note that the time will wrap if it passes midnight. See addSecs()
for an example.
- \sa addSecs(), msecsTo()
+ \sa addSecs(), msecsTo(), QDateTime::addMSecs()
*/
QTime QTime::addMSecs(int ms) const
@@ -1734,7 +1734,7 @@ QTime QTime::addMSecs(int ms) const
seconds in a day, the result is always between -86400000 and
86400000 ms.
- \sa secsTo(), addMSecs()
+ \sa secsTo(), addMSecs(), QDateTime::msecsTo()
*/
int QTime::msecsTo(const QTime &t) const
@@ -2042,10 +2042,11 @@ int QTime::elapsed() const
later.
You can increment (or decrement) a datetime by a given number of
- seconds using addSecs(), or days using addDays(). Similarly you can
- use addMonths() and addYears(). The daysTo() function returns the
- number of days between two datetimes, and secsTo() returns the
- number of seconds between two datetimes.
+ milliseconds using addMSecs(), seconds using addSecs(), or days
+ using addDays(). Similarly you can use addMonths() and addYears().
+ The daysTo() function returns the number of days between two datetimes,
+ secsTo() returns the number of seconds between two datetimes, and
+ msecsTo() returns the number of milliseconds between two datetimes.
QDateTime can store datetimes as \l{Qt::LocalTime}{local time} or
as \l{Qt::UTC}{UTC}. QDateTime::currentDateTime() returns a
@@ -2719,7 +2720,7 @@ QDateTime QDateTime::addSecs(int s) const
later than the datetime of this object (or earlier if \a msecs is
negative).
- \sa addSecs(), secsTo(), addDays(), addMonths(), addYears()
+ \sa addSecs(), msecsTo(), addDays(), addMonths(), addYears()
*/
QDateTime QDateTime::addMSecs(qint64 msecs) const
{
@@ -2731,7 +2732,7 @@ QDateTime QDateTime::addMSecs(qint64 msecs) const
datetime. If the \a other datetime is earlier than this datetime,
the value returned is negative.
- \sa addDays(), secsTo()
+ \sa addDays(), secsTo(), msecsTo()
*/
int QDateTime::daysTo(const QDateTime &other) const
@@ -2766,6 +2767,33 @@ int QDateTime::secsTo(const QDateTime &other) const
}
/*!
+ Returns the number of milliseconds from this datetime to the \a other
+ datetime. If the \a other datetime is earlier than this datetime,
+ the value returned is negative.
+
+ Before performing the comparison, the two datetimes are converted
+ to Qt::UTC to ensure that the result is correct if one of the two
+ datetimes has daylight saving time (DST) and the other doesn't.
+
+ \sa addMSecs(), daysTo(), QTime::msecsTo()
+*/
+
+qint64 QDateTime::msecsTo(const QDateTime &other) const
+{
+ QDate selfDate;
+ QDate otherDate;
+ QTime selfTime;
+ QTime otherTime;
+
+ d->getUTC(selfDate, selfTime);
+ other.d->getUTC(otherDate, otherTime);
+
+ return (static_cast<qint64>(selfDate.daysTo(otherDate)) * static_cast<qint64>(MSECS_PER_DAY))
+ + static_cast<qint64>(selfTime.msecsTo(otherTime));
+}
+
+
+/*!
\fn QDateTime QDateTime::toTimeSpec(Qt::TimeSpec specification) const
Returns a copy of this datetime configured to use the given time