简体   繁体   中英

using ansi-c on windows platform can i get time of system upto milliseconds accuracy?

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.

不是在ANSI C中,但Windows API提供了GetSystemTime函数,如下所示: http//msdn.microsoft.com/en-us/library/ms724950( GetSystemTime .aspx

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.

In windows api there is a SYSTEMTIME structure and some sys calls to get systemtime and localtime of your machine, you can implement it in this way:

SYSTEMTIME systime;

GetSystemTime(&systime);

unsigned int millisec = systime.wMilliseconds;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM