如何以毫秒分辨率获得Windows系统时间?

如何以毫秒分辨率获得Windows系统时间?

如果以上是不可能的,那我怎样才能得到操作系统的启动时间? 我想将这个值和timeGetTime()一起使用,以毫秒的分辨率计算一个系统时间。

先谢谢你。

GetTickCount不会为你完成它。

查看QueryPerformanceFrequency / QueryPerformanceCounter 。 这里唯一的问题就是CPU的缩放,你的研究也是如此。

试试从MSDN杂志这篇文章。 这实际上很复杂。

为Windows实施持续更新的高分辨率时间提供程序

这是上述评论的阐述,以解释一些原因。

首先, GetSystemTime *调用是提供系统时间的唯一Win32 API。 这一次的粒度相当粗糙,因为大多数应用程序不需要维持较高分辨率所需的开销。 时间(可能)在内部存储为64位计数的毫秒。 调用timeGetTime得到低阶32位。 调用GetSystemTime等要求Windows返回这个毫秒时间,转换成天等后包括系统启动时间。

机器中有两个时间源:CPU时钟和板载时钟(例如,实时时钟(RTC),可编程间隔定时器(PIT)和高精度事件定时器(HPET))。 第一个具有大约〜0.5ns(2GHz)的分辨率,第二个一般可编程为1ms(尽pipe较新的芯片(HPET)具有较高的分辨率)。 Windows使用这些周期性滴答执行某些操作,包括更新系统时间。

应用程序可以通过timerBeginPeriod改变这个时间段 ; 然而,这影响整个系统。 操作系统将按要求的频率检查/更新常规事件。 在CPU负载/频率较低的情况下,有闲置的节省时间。 在高频下,没有时间让处理器进入低功耗状态。 有关详细信息,请参阅计时器分辨 最后,每个tick都有一些开销,增加频率会消耗更多的CPU周期。

对于更高的分辨率的时间,系统时间不会保持这个精度,只不过是大本钟而已。 使用QueryPerformanceCounter(QPC)或CPU的滴答(rdtsc)可以提供系统时间滴答之间的分辨率。 凯文引用的MSDN杂志文章中使用了这种方法。 虽然这些方法可能会有漂移(例如,由于频率缩放)等,因此需要同步到系统时间。

从Windows 8开始,Microsoft引入了新的API命令GetSystemTimePreciseAsFileTime:

https://msdn.microsoft.com/en-us/library/windows/desktop/hh706895%28v=vs.85%29.aspx

不幸的是,如果您创build的软件也必须在较早的操作系统上运行,那么您就不能使用它。

我目前的解决scheme如下,但要注意:确定的时间不准确,只是接近实时。 结果应该总是小于或等于实际时间,但是有一个固定的错误(除非计算机进入待机状态)。 结果有一个毫秒的分辨率。 为了我的目的,这是确切的。

 void GetHighResolutionSystemTime(SYSTEMTIME* pst) { static LARGE_INTEGER uFrequency = { 0 }; static LARGE_INTEGER uInitialCount; static LARGE_INTEGER uInitialTime; static bool bNoHighResolution = false; if(!bNoHighResolution && uFrequency.QuadPart == 0) { // Initialize performance counter to system time mapping bNoHighResolution = !QueryPerformanceFrequency(&uFrequency); if(!bNoHighResolution) { FILETIME ftOld, ftInitial; GetSystemTimeAsFileTime(&ftOld); do { GetSystemTimeAsFileTime(&ftInitial); QueryPerformanceCounter(&uInitialCount); } while(ftOld.dwHighDateTime == ftInitial.dwHighDateTime && ftOld.dwLowDateTime == ftInitial.dwLowDateTime); uInitialTime.LowPart = ftInitial.dwLowDateTime; uInitialTime.HighPart = ftInitial.dwHighDateTime; } } if(bNoHighResolution) { GetSystemTime(pst); } else { LARGE_INTEGER uNow, uSystemTime; { FILETIME ftTemp; GetSystemTimeAsFileTime(&ftTemp); uSystemTime.LowPart = ftTemp.dwLowDateTime; uSystemTime.HighPart = ftTemp.dwHighDateTime; } QueryPerformanceCounter(&uNow); LARGE_INTEGER uCurrentTime; uCurrentTime.QuadPart = uInitialTime.QuadPart + (uNow.QuadPart - uInitialCount.QuadPart) * 10000000 / uFrequency.QuadPart; if(uCurrentTime.QuadPart < uSystemTime.QuadPart || abs(uSystemTime.QuadPart - uCurrentTime.QuadPart) > 1000000) { // The performance counter has been frozen (eg after standby on laptops) // -> Use current system time and determine the high performance time the next time we need it uFrequency.QuadPart = 0; uCurrentTime = uSystemTime; } FILETIME ftCurrent; ftCurrent.dwLowDateTime = uCurrentTime.LowPart; ftCurrent.dwHighDateTime = uCurrentTime.HighPart; FileTimeToSystemTime(&ftCurrent, pst); } } 

GetSystemTimeAsFileTime为绝对时间提供任何Win32函数的最佳精度。 QPF / QPC如乔尔·克拉克(Joel Clark)所build议的那样,相对时间会更长。

QueryPerformanceCounter()是为细粒度定时器parsing而构build的。

它是系统必须提供的最高分辨率的计时器,您可以在应用程序代码中使用它来识别性能瓶颈

这是一个简单的C#开发实现:

  [DllImport("kernel32.dll")] extern static short QueryPerformanceCounter(ref long x); [DllImport("kernel32.dll")] extern static short QueryPerformanceFrequency(ref long x); private long m_endTime; private long m_startTime; private long m_frequency; public Form1() { InitializeComponent(); } public void Begin() { QueryPerformanceCounter(ref m_startTime); } public void End() { QueryPerformanceCounter(ref m_endTime); } private void button1_Click(object sender, EventArgs e) { QueryPerformanceFrequency(ref m_frequency); Begin(); for (long i = 0; i < 1000; i++) ; End(); MessageBox.Show((m_endTime - m_startTime).ToString()); } 

如果您是C / C ++开发人员,请在此处查看: http : //support.microsoft.com/kb/815668

那么,这个是非常古老的,但Windows C库_ftime还有另外一个有用的函数,它返回一个结构,它的本地时间为time_t ,毫秒,时区和夏令时标志。

在Windows中,所有时间的基础是一个名为GetSystemTimeAsFiletime的函数。

  • 它返回一个能够容纳100ns资源的结构。
  • 它保存在UTC

FILETIME结构自1600年1月1日起logging了100ns的间隔时间; 意味着它的分辨率被限制在100ns。

这形成我们的第一个function:

在这里输入图像描述

自1600年1月1日以来,它的64位数字为100ns,这有点笨拙。 Windows提供了一个方便的辅助函数FileTimeToSystemTime ,可以将这个64位整数解码为有用的部分:

 record SYSTEMTIME { wYear: Word; wMonth: Word; wDayOfWeek: Word; wDay: Word; wHour: Word; wMinute: Word; wSecond: Word; wMilliseconds: Word; } 

请注意, SYSTEMTIME具有1 1ms的内置分辨率限制

现在我们有一个从FILETIMESYSTEMTIME

在这里输入图像描述

我们可以写这个函数来获取当前系统时间作为SYSTEIMTIME结构:

 SYSTEMTIME GetSystemTime() { //Get the current system time utc in it's native 100ns FILETIME structure FILETIME ftNow; GetSytemTimeAsFileTime(ref ft); //Decode the 100ns intervals into a 1ms resolution SYSTEMTIME for us SYSTEMTIME stNow; FileTimeToSystemTime(ref stNow); return stNow; } 

除了Windows已经为你写了这样一个函数: GetSystemTime

在这里输入图像描述

本地,而不是UTC

现在如果你不想要UTC的当前时间。 如果你想在你当地的时间呢? Windows提供了一个将UTC中的FILETIME转换为本地时间的FileTimeToLocalFileTimeFileTimeToLocalFileTime

在这里输入图像描述

您可以编写一个函数,在本地时间已经返回FILETIME

 FILETIME GetLocalTimeAsFileTime() { FILETIME ftNow; GetSystemTimeAsFileTime(ref ftNow); //convert to local FILETIME ftNowLocal FileTimeToLocalFileTime(ftNow, ref ftNowLocal); return ftNowLocal; } 

在这里输入图像描述

并且假设你想将本地 FILETIME解码为SYSTEMTIME。 这是没有问题的,你可以再次使用FileTimeToSystemTime

在这里输入图像描述

幸运的是,Windows已经为您提供了一个返回值的函数:

在这里输入图像描述

精确

还有一个考虑。 在Windows 8之前,时钟的分辨率约为15ms。 在Windows 8中,他们将时钟提高到了100ns(匹配FILETIME的分辨率)。

  • GetSystemTimeAsFileTime (传统,15ms分辨率)
  • GetSystemTimeAsPreciseFileTime (Windows 8,100ns分辨率)

这意味着我们应该总是喜欢新的价值:

在这里输入图像描述

你问的时间

你问时间; 但是你有一些select。

时区:

  • UTC (系统本地)
  • 本地时区

格式:

  • FILETIME (系统本地,100ns分辨率)
  • SYTEMTIME (解码,1ms分辨率)

概要

  • 100ns分辨率: FILETIME
    • UTC: GetSytemTimeAsPreciseFileTime (或GetSystemTimeAsFileTime
    • 本地:( 滚动你自己的)
  • 1ms分辨率: SYSTEMTIME
    • UTC: GetSystemTime
    • 本地: GetLocalTime

我已经写了一些关于如何以适合大多数目的的方式快速,轻松地实现这一点的信息,在我对“Windows上的微秒分辨率时间戳”问题的回答中。