获取系统时间(以毫秒为单位)作为int/double

Get System Time In MilliSeconds as an int/double

本文关键字:作为 int double 为单位 时间 系统 获取      更新时间:2023-10-16

我是c++的新手,但我根本无法让它工作。我试图获得系统的当前时间(毫秒),并用它做一些事情,但它不会像我尝试的那样工作。

Qt

 QDateTime qt = new QDateTime();
  int x = qt.currentDateTimeUtc();
    if(x%5 ==0){
        //something
     }

c++

     double sysTime = time(0);
if(sysTime%5.00 ==0.00){

}

我得到类型为double的无效操作数到二进制运算符错误。我不知道为什么?任何人都可以指向正确的方向

对于QT,请尝试使用函数QDateTime::toMSecsSinceEpoch()

http://doc.qt.io/qt-5/qdatetime.html#toMSecsSinceEpoch

这将返回一个qint64http://doc.qt.io/qt-5/qtglobal.html#qint64-typedef

如果您试图在C中获得以毫秒为单位的unix时间戳,您可以尝试以下代码:

include "time.h"
...
time_t seconds;
time(&seconds);
unsigned long long millis = (unsigned long long)seconds * 1000;

尽管请注意,这是乘以1000的——看起来像毫秒,但准确度是秒——如果你试图每5秒做一件事,根据你的x % 5代码判断,这可能就足够了,所以以下应该足够了:

time_t seconds; time(&seconds);
相关文章: