1

I need to get the millisecond accuracy. I take a look on this question but I am working on Windows: it gives linking errors for POSIX functions.

It will be very good if I can get UTC time since 1970 with milliseconds precision.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Badr
  • 10,384
  • 15
  • 70
  • 104
  • 1
    This is a really good question. You should restrict answers to providing at least millisecond resolution in the unix epoch using only ANSI-C or Windows API. – Matt Joiner Jun 29 '10 at 06:24
  • Does this answer your question? [How can I get the Windows system time with millisecond resolution?](https://stackoverflow.com/questions/3729169/how-can-i-get-the-windows-system-time-with-millisecond-resolution) – phuclv Apr 30 '22 at 15:09

3 Answers3

3

Not in ANSI C, but the Windows API provides a GetSystemTime function as illustrated here: https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-systemtime

phuclv
  • 37,963
  • 15
  • 156
  • 475
user85509
  • 36,612
  • 7
  • 33
  • 26
1

Sorry, but you can't do that using neither ANSI C nor the Windows API.

You can get the system time with a millisecond resolution using GetSystemTime or with a 100-nanosecond resolution using GetSystemTimeAsFileTime, but the accuracy will not be that good. The system time is only updated at each clock interval, which is somewhere around 10-15 milliseconds depending on the underlying architecture (SMP, Uniprocessor, ...).

There are ways to extrapolate the system time using different algorithms of varying complexity, but without the support of the operating system you'll never be guaranteed a correct high-resolution clock time.

rjnilsson
  • 2,343
  • 15
  • 20
0

In Windows API there is a SYSTEMTIME structure and some system calls to get system time and local time of your machine, you can implement it in this way:

SYSTEMTIME systime;
GetSystemTime(&systime);
unsigned int millisec = systime.wMilliseconds;
phuclv
  • 37,963
  • 15
  • 156
  • 475
Kumar Alok
  • 2,512
  • 8
  • 26
  • 36