Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get the system time with millisecond precision? 2

Status
Not open for further replies.

Devin

Programmer
Dec 17, 2001
42
US
I need to get the system time, but Time() only returns seconds, and VB help offers no other options. Can anyone point me in the right direction?

Thanks much.

Devin

 
There are a number of API which you may find useful.

Among them are

GetLocalTime and
GetSystemTime

here is an example of GetSystemTime

Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)

Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type

Private Sub Form_Load()

Dim SysTime As SYSTEMTIME

GetSystemTime SysTime

Debug.Print "The System Date is:" & SysTime.wMonth & "-" & SysTime.wDay & "-" & SysTime.wYear

End Sub

and you can reference any value in the SYSTEMTIME structure
Good Luck
------------
Select * from Users where Clue > 0
0 rows returned
 
Be warned, however, thatthe system time is 'only' accurate to...erm...55ms
 
"Only???" That is an eternity! Is there no way to do better than 55ms precision? That would be a major disappointment. Oh why couldn't my predecessor have created this app in VC++?
 
Even in C++, the system timer has the same restriction...
 
CajunC, you may remember me from last week. I am still trying to create a more accurate timer, hence this thread. I plan, as I think you suggested, to get the system time at the beginning and end of my timer event code and then to subtract the event execution time from the subsequent timer interval. So, in other words, the interval would be adaptively set depending on how long it takes to execute the timer event.
 
The basic problem revolves around two things which are essetially outside of the programmer's control. The first is the CMOS clock itself, which generally cycles 18.2 times per second - that is approx 54.9 ms. Add to that the overhead that Windows needs to process and deliver the appropriate messages as strongm has already indicated, your precision can't get much better than that.

If you dropped into assembler, you may be able to circumvent some of the Windows overhead, but unless you have some special purpose timer hardware, I don't know of a way to substantially decrease the timer interval.

Good Luck
------------
Select * from Users where Clue > 0
0 rows returned
 
Ok - now we've established that you really do want a higher resolution timer than the system time, try the following:
[tt]
Option Explicit
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long

Private Sub Timed_Procedure()
Dim startticks As Currency
Dim endticks As Currency
Dim frequency As Currency

QueryPerformanceFrequency frequency ' ticks per second. Guaranteed not to change once machine is booted, but may differ slightly with each reboot. Don't hardcode
QueryPerformanceCounter startticks
' stuff you want to time
QueryPerformanceCounter endticks
MsgBox (endticks - startticks) / frequency ' elapsed time in seconds...
End Sub
 
Awesome, y'all. Thanks. You both get a star from me.

Devin

ps- I'll try that, strongm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top