Date: 2021dec30
Language: C/C++
Platform: win32
Q. Windows/C/C++: Get number of milliseconds since 1970 (the epoch)
A.
The same as Java's System.currentTimeMillis()
#include <time.h>
// This gives the clock milliseconds in 0..999
static WORD CurrentMilliseconds() {
SYSTEMTIME st;
SYSTEMTIME lst;
GetSystemTime(&st);
SystemTimeToTzSpecificLocalTime(NULL, &st, &lst);
return lst.wMilliseconds;
}
LONGLONG MillisecondsOfEpoch() {
return (LONGLONG)time(NULL) * 1000LL + (LONGLONG)CurrentMilliseconds();
}
Bonus:
LONGLONG NanosecondsOfEpoch() {
return MillisecondsOfEpoch() * 1000000LL;
}