hat_to_the_back
hat_to_the_back

Reputation: 291

Get System Time In MilliSeconds as an int/double

I am new to c++ but I just cant get this to work at all. I am trying to get the system current time in ms and do something with it but it wont work what I have tried.

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){


}

I get invalid operands of type double to binary operator error. I have no idea why? Can anyone point in the right direction

Upvotes: 0

Views: 2715

Answers (2)

Sandman
Sandman

Reputation: 2745

If you're trying to get the unix timestamp in milliseconds in C you can try this code:

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

Though please note this is multiplied by 1000 - it looks like milliseconds but the accuracy is that of seconds - which judging by your x % 5 code might be enough if you're trying to do something every 5 seconds, so the following should be enough:

time_t seconds; time(&seconds);

Upvotes: 0

user700390
user700390

Reputation: 2339

For QT, try using the function QDateTime::toMSecsSinceEpoch()

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

This will return a qint64 http://doc.qt.io/qt-5/qtglobal.html#qint64-typedef

Upvotes: 2

Related Questions