简体   繁体   English

在Windows平台上使用ansi-c我可以获得系统时间精确​​到毫秒级吗?

[英]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. 我需要获得毫秒级的精度我看看这个问题,但我正在研究Windows:它给出了POSIX函数的链接错误。

it will be very good if i can get UTC time since 1970 with milliseconds precision. 如果我能以1970毫秒的精度获得UTC时间,那将是非常好的。

不是在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. 抱歉,但您不能同时使用ANSI C和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. 可以使用GetSystemTime或使用GetSystemTimeAsFileTime以100纳秒的分辨率获得毫秒分辨率的系统时间,但精度不会那么好。 The system time is only updated at each clock interval, which is somewhere around 10-15 milliseconds depending on the underlying architecture (SMP, Uniprocessor, ...). 系统时间仅在每个时钟间隔更新,大约10-15毫秒,具体取决于底层架构(SMP,单处理器......)。

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: 在windows api中有一个SYSTEMTIME结构和一些sys调用来获取你机器的系统时间和本地时间,你可以用这种方式实现它:

SYSTEMTIME systime; SYSTEMTIME系统时间;

GetSystemTime(&systime); GetSystemTime(&SYSTIME);

unsigned int millisec = systime.wMilliseconds; unsigned int millisec = systime.wMilliseconds;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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