-1

For Windows C++, I am trying something like -

unsigned long long int Device::getCurrentUtcTimeinMiliSecond() {
    time_t ltime;
    time(&ltime);
    std::tm* newtime = gmtime(&ltime);
    newtime->tm_hour = 0;
    newtime->tm_min = 0;
    newtime->tm_sec = 0;
    time_t timex = mktime(newtime);
    // Want to convert tm* to total miliseconds since Midnight 1970
    return  (long long)timex * 1000;
}

Is there any other way or am I going in right direction? If yes, then how to convert tm* to total time millisecond since 1970 Midnight?

Or someone can suggest simpler way of doing it.

Bit_Pulse
  • 336
  • 1
  • 2
  • 16

1 Answers1

-1

You can use clock_gettime; This is not available by default under windows, but there is an an example on stackoverflow

Once you have clock_gettime, you can do the following:

#define MS_PER_SEC 1000
#define NS_PER_MS  1000000
timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
unsigned long long msecs = (((unsigned long long) ts.tv_sec) * MS_PER_SEC + (ts.tv_nsec / NS_PER_MS);
return msecs;

You could clean that up a bit, but once you have clock_gettime() it should be fairly easy.

Community
  • 1
  • 1
Signal Eleven
  • 231
  • 1
  • 6