如何以毫秒为单位获得两个QDateTimes之间的差异?

时间:2022-07-16 21:29:31

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.

我希望QDateTime超过了操作符,并返回一个QTimeSpan,表示两个QDateTimes之间的差异(就像. net的TimeSpan)。因为这在Qt中不存在,所以我决定实现它。

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

不幸的是,QDateTime没有类似msecsto的函数。什么是最干净的方法来获得两个QDateTimes精确到毫秒之间的差异?

3 个解决方案

#1


7  

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.

我可能会使用a。daysto (b)*1000*60*60*24 + a.time(). msecsto (b.time())。注意,您需要密切关注您的数据类型,因为您将很快地溢出数据类型。

#2


6  

I realize that this question is from 2010, and that Qt 4.7 didn't exist back then (it actually came out about a week after this question was originally asked--September 21 2010), but for people who are looking for how to do this now:

我意识到这个问题是从2010年开始的,Qt 4.7在那时并不存在(它实际上是在这个问题最初被问及的一个星期后才出现的),但是对于那些正在寻找如何做到这一点的人来说:

As of Qt 4.7, QDateTime has a "msecsTo" method. See Qt 4.8 documentation at http://doc.qt.io/qt-4.8/qdatetime.html#msecsTo.

在Qt 4.7中,QDateTime有一个“msecsTo”方法。查看Qt 4.8文档,网址是http://doc.qt.io/qt-4.8/qdatetime.html#msecsTo。

QDateTime dateTime1 = QDateTime::currentDateTime();
// let's say exactly 5 seconds pass here...
QDateTime dateTime2 = QDateTime::currentDateTime();
qint64 millisecondsDiff = dateTime1.msecsTo(dateTime2);
// millisecondsDiff is equal to 5000

#3


1  

how about this:

这个怎么样:

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

Source

#1


7  

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.

我可能会使用a。daysto (b)*1000*60*60*24 + a.time(). msecsto (b.time())。注意,您需要密切关注您的数据类型,因为您将很快地溢出数据类型。

#2


6  

I realize that this question is from 2010, and that Qt 4.7 didn't exist back then (it actually came out about a week after this question was originally asked--September 21 2010), but for people who are looking for how to do this now:

我意识到这个问题是从2010年开始的,Qt 4.7在那时并不存在(它实际上是在这个问题最初被问及的一个星期后才出现的),但是对于那些正在寻找如何做到这一点的人来说:

As of Qt 4.7, QDateTime has a "msecsTo" method. See Qt 4.8 documentation at http://doc.qt.io/qt-4.8/qdatetime.html#msecsTo.

在Qt 4.7中,QDateTime有一个“msecsTo”方法。查看Qt 4.8文档,网址是http://doc.qt.io/qt-4.8/qdatetime.html#msecsTo。

QDateTime dateTime1 = QDateTime::currentDateTime();
// let's say exactly 5 seconds pass here...
QDateTime dateTime2 = QDateTime::currentDateTime();
qint64 millisecondsDiff = dateTime1.msecsTo(dateTime2);
// millisecondsDiff is equal to 5000

#3


1  

how about this:

这个怎么样:

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

Source