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

你说的曾经没有我的故事 提交于 2019-12-06 14:27:54

问题


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.


回答1:


Not in ANSI C, but the Windows API provides a GetSystemTime function as illustrated here: http://msdn.microsoft.com/en-us/library/ms724950(v=VS.85).aspx




回答2:


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.




回答3:


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;



来源:https://stackoverflow.com/questions/3138275/using-ansi-c-on-windows-platform-can-i-get-time-of-system-upto-milliseconds-accu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!