从Windows的系统时钟获取当前时间(以毫秒为单位)? [英] Getting the current time (in milliseconds) from the system clock in Windows?
问题描述
如何在C ++中获取系统时钟的当前时间(以毫秒为单位)?这是一个Windows特定的应用程序。
How can you obtain the system clock's current time of day (in milliseconds) in C++? This is a windows specific app.
推荐答案
要获取以UTC表示的时间,请使用 GetSystemTime
。
To get the time expressed as UTC, use GetSystemTime
in the Win32 API.
SYSTEMTIME st;
GetSystemTime(&st);
SYSTEMTIME
记录为具有这些相关成员:
SYSTEMTIME
is documented as having these relevant members:
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
如shf301在下面有帮助的指出, GetLocalTime
(具有相同的原型)将产生一个时间更正到用户的目前的时区。
As shf301 helpfully points out below, GetLocalTime
(with the same prototype) will yield a time corrected to the user's current timezone.
这里有几个好的答案,取决于你以后。如果你正在寻找时间,我的答案是最好的方法 - 如果你需要固体日期的算术,考虑亚历克斯。在Windows上有许多方法来修复时间猫,其中一些比其他更准确(没有人提到 QueryPerformanceCounter
)。
You have a few good answers here, depending on what you're after. If you're looking for just time of day, my answer is the best approach -- if you need solid dates for arithmetic, consider Alex's. There's a lot of ways to skin the time cat on Windows, and some of them are more accurate than others (and nobody has mentioned QueryPerformanceCounter
yet).
这篇关于从Windows的系统时钟获取当前时间(以毫秒为单位)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!