`Timer` global property comes handy for measuring elapsed time or for logging time-stamps. It basically returns number of seconds since midnight with 2 digits precision.
Usually to measure elapsed timer in seconds one can do something like this:
Unfortunately this suffers from `Timer`'s midnight rollover and is not milliseconds precise.
Here is a naive fix for the rollover and a complete fix for the precision too:
The naive version just multiplies `Date` with number of seconds in a day and adds `Timer` which equals to number of seconds elapsed since `CDate(0)` = #1899-12-30#
The completely fixed `DateTimer` return value has the same semantics but is precise to 5 digits after the floating point i.e. 1/100 of a millisecond precise. Of course it all depends on OS and hardware support but the API call is easy and convenient -- the "hacked" parameter type is the trick here.
Here is how we log current date/time with milliseconds precision in our error reporting code:
Enjoy!
cheers,
</wqw>
Usually to measure elapsed timer in seconds one can do something like this:
Code:
dblTimer = Timer
...
' Code here
...
Debug.Print Timer - dblTimer
Here is a naive fix for the rollover and a complete fix for the precision too:
Code:
Option Explicit
Private Declare Function GetSystemTimeAsFileTime Lib "kernel32.dll" (lpSystemTimeAsFileTime As Currency) As Long
Private Sub Form_Load()
Debug.Print Timer, NaiveDateTimer, DateTimer
End Sub
Public Property Get NaiveDateTimer() As Double
NaiveDateTimer = CLng(Date) * 86400# + CDbl(CStr(Timer))
End Property
Public Property Get DateTimer() As Double
Dim cDateTime As Currency
Call GetSystemTimeAsFileTime(cDateTime)
DateTimer = CDbl(cDateTime - 9435304800000@) / 1000#
End Property
The completely fixed `DateTimer` return value has the same semantics but is precise to 5 digits after the floating point i.e. 1/100 of a millisecond precise. Of course it all depends on OS and hardware support but the API call is easy and convenient -- the "hacked" parameter type is the trick here.
Here is how we log current date/time with milliseconds precision in our error reporting code:
Code:
Debug.Print Format$(Now, "yyyy.mm.dd hh:mm:ss") & Right$(Format$(DateTimer, "0.000"), 4)
> 2015.01.29 20:17:20.771
cheers,
</wqw>