Using ANSI-C on Windows platform can I get time of system upto milliseconds accuracy?
Asked Answered
G

3

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.

Gwenora answered 29/6, 2010 at 6:15 Comment(2)
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.Ivett
Does this answer your question? How can I get the Windows system time with millisecond resolution?Lesslie
G
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

Gearwheel answered 29/6, 2010 at 6:23 Comment(0)
H
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.

Hyperesthesia answered 29/6, 2010 at 6:55 Comment(0)
R
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;
Reserve answered 29/6, 2010 at 6:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.