tags:

views:

55

answers:

2

I wish QDateTime overrode the - operator and returned a QTimeSpan representing the difference between two QDateTimes (just like .NET's TimeSpan). Since this doesn't exist in Qt, I decided to implement it.

Unfortunately, QDateTime has no msecsTo-like function. What is the cleanest way to get the difference between two QDateTimes accurate to the millisecond?

+4  A: 

I would probably use a.daysTo(b)*1000*60*60*24 + a.time().msecsTo(b.time()). Note that you need to watch how close you can be, since you're going to overflow your data type rather quickly.

jkerian
+1 Perfect, thanks. Sorry for the long delay!
Jake Petroules
A: 

how about this:

QDateTime a = QDateTime::currentDateTime();
QDateTime b = a.addMSecs( 1000 );
qDebug( "%d", a.time().msecsTo( b.time() ) );

Source

Here Be Wolves
But that will ignore the date part altogether.
Roku