Solved

Better timer than WIN32 GetLocalTime()

Posted on 2006-06-13
5
2,227 Views
Last Modified: 2013-12-03
I'm logging some incoming data that needs to be time correlated and was using GetLocalTime() to get a SYSTEMTIME value for each incoming packet.  Unfortunately at times multiple (usually 2 or 3) packets come in and get the same SYSTEMTIME value.  I'm sure this is due to the resolution of the timer.  The specs on GetLocalTime() say it resolved to mS but it's pretty clear that it doesn't really provide 1mS resolution.

I think I read somewhere about a better timer mechanism on Windows.  I did find some stuff on using the CPU counter register but this is more useful for providing an accurate RELATIVE time.  I need an absolute time.

Any ideas?
0
Comment
Question by:jhance
5 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 100 total points
Comment Utility
I know two ways to calculate exact time:
1) Using QueryPerformanceFrequency and QueryPerformanceCounter - this can be used to calculate time intervals.
2) Multimedia timer - see timeGetTime and other functions.

Both ways don't provide absolute time. However, you can get absolute start time in the beginning of the program, and add time interval to it for every packet. Pseudo code:
absoluteStartTime = GetLocalTime();
windowsStartTime =  timeGetTime();

For every packet:
packetTime = absoluteStartTime + (timeGetTime() - windowsStartTime);
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 50 total points
Comment Utility
If you actually don't need a better precision but simply a unique and increasing filetime, I would recommend to save the last time stored and add a millisecond if there is no difference when taking the next time. I don't think that any other filetimes you have are much more precise.

Regards, Alex


0
 
LVL 7

Assisted Solution

by:aib_42
aib_42 earned 50 total points
Comment Utility
If you use any kind of system timer, you are bound to get duplicate times due to their resolution. To get unique values, you want a timer with resolution equal to or a small multiple of your clock speed, for which the performance counter should suffice (if the target computer has one).

If your algorithm needs absolute time, though, how come duplicate values are inacceptable?
I'd look for another solution such as adding a "sequence number" of sorts to distinguish among identical SYSTEMTIME values, or do what Alex said.
0
 
LVL 32

Author Comment

by:jhance
Comment Utility
Actually I'm trying to correlate network packets that are being logged and timestamped by a tool called Analyzer, which is based on the WinPCap driver.  It timestamps the packets with what appears to be uS resolution where I'm not even getting mS with GetLocalTime().

Since it's open source I've been looking through the code and it uses a thing called wxDateTime from the wxWidgets library.  That's also open source but I've been unable to figure out how/where they get their time on a Windows system.  I don't know where open source code writers learn how to write convoluted code but I'm never able to follow what they do.

Anyway, if anyone has any experience with wxWidgets and can point me to where or how they do the wxDateTime it would help (I think...)
0
 
LVL 8

Assisted Solution

by:craigwardman
craigwardman earned 50 total points
Comment Utility
For an x86 specific solution, you could try the mnemonic instruction 'RDTSC' which reads the time stamp from the CPU..

A c++ inline example:


unsigned long hTime, lTime;

  __asm{
      RDTSC
      mov hTime, EDX
      mov lTime, EAX
}


Some info on this:
The time-stamp counter is contained in a 64-bit MSR. The high-order 32 bits of the MSR are loaded into the EDX register, and the low-order 32 bits are loaded into the EAX register. The processor increments the time-stamp counter MSR every clock cycle and resets it to 0 whenever the processor is reset.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now