
如何以毫秒分辨率获得windows系统时间?
如果以上是不可能的,那我怎样才能得到 *** 作系统的启动时间? 我想将这个值和timeGetTime()一起使用,以毫秒的分辨率计算一个系统时间。
先谢谢你。
获取屏幕分辨率作为cmd中的variables
windows 10下Java应用程序的放大问题
我怎样才能得到R的屏幕分辨率
以编程方式更改屏幕分辨率
图标分辨率:像素与DPI
如何在windows中查询主监视器的NATIVE硬件分辨率?
GetTickCount不会为你完成它。
查看queryPerformanceFrequency / queryPerformanceCounter 。 这里唯一的问题就是cpu的缩放,你的研究也是如此。
试试从MSDN杂志这篇文章。 这实际上很复杂。
为windows实施持续更新的高分辨率时间提供程序
这是上述评论的阐述,以解释一些原因。
首先, GetSystemTime *调用是提供系统时间的唯一Win32 API。 这一次的粒度相当粗糙,因为大多数应用程序不需要维持较高分辨率所需的开销。 时间(可能)在内部存储为64位计数的毫秒。 调用timeGetTime得到低阶32位。 调用GetSystemTime等要求windows返回这个毫秒时间,转换成天等后包括系统启动时间。
机器中有两个时间源:cpu时钟和板载时钟(例如,实时时钟(RTC),可编程间隔定时器(PIT)和高精度事件定时器(HPET))。 第一个具有大约〜0.5ns(2GHz)的分辨率,第二个一般可编程为1ms(尽管较新的芯片(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
不幸的是,如果您创建的软件也必须在较早的 *** 作系统上运行,那么您就不能使用它。
我目前的解决方案如下,但要注意:确定的时间不准确,只是接近实时。 结果应该总是小于或等于实际时间,但是有一个固定的错误(除非计算机进入待机状态)。 结果有一个毫秒的分辨率。 为了我的目的,这是确切的。
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)所建议的那样,相对时间会更长。
queryPerformanceCounter()是为细粒度定时器解析而构建的。
它是系统必须提供的最高分辨率的计时器,您可以在应用程序代码中使用它来识别性能瓶颈
这是一个简单的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日起记录了100ns的间隔时间; 意味着它的分辨率被限制在100ns。
这形成我们的第一个功能:
自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的内置分辨率限制
现在我们有一个从fileTIME到SYstemTIME :
我们可以编写函数来获取当前系统时间作为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转换为本地时间的fileTimetoLocalfileTime : fileTimetoLocalfileTime
您可以编写一个函数,在本地时间已经返回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分辨率)
这意味着我们应该总是喜欢新的价值:
你问的时间
你问时间; 但是你有一些选择。
时区:
UTC (系统本地)
本地时区
格式:
fileTIME (系统本地,100ns分辨率)
SYTEMTIME (解码,1ms分辨率)
概要
100ns分辨率: fileTIME
UTC: GetSytemTimeAsPrecisefileTime (或GetSystemTimeAsfileTime )
本地:( 滚动你自己的)
1ms分辨率: SYstemTIME
UTC: GetSystemTime
本地: GetLocalTime
我已经写了一些关于如何以适合大多数目的的方式快速,轻松地实现这一点的信息,在我对“windows上的微秒分辨率时间戳”问题的回答中。
总结以上是内存溢出为你收集整理的如何以毫秒分辨率获得Windows系统时间?全部内容,希望文章能够帮你解决如何以毫秒分辨率获得Windows系统时间?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)