Computing.Net > Forums > Programming > Precise timestamps

Precise timestamps

Reply to Message Icon

Original Message
Name: ward
Date: June 13, 2002 at 12:30:31 Pacific
Subject: Precise timestamps
Comment:

I need to get timestamps with millisecond precision with a C program off a linux pc, and I can't figure out how to do it. Could someone help me out with some source code?
thanks.


Report Offensive Message For Removal


Response Number 1
Name: Jim
Date: June 13, 2002 at 13:55:34 Pacific
Reply: (edit)

No source, but I can give you a couple of suggestions.

The easier way would be to use the RDTSC instruction. Only two problems using it with C are how to get the info, and calibrating it. It's easy enough to get at using inline assembly, but you'd still have to calibrate it to give you the millisecond resolution you're after.

The other way is to read the countdown timer on the timer chip. Just out a byte 0 to port 0x43, and then read in two bytes from port 0x40. The only problem is that it's counting down as you're reading it, and might have generated an interrupt in the middle, which might invalidate the results. Well, that and there might be issues reading from the same I/O port twice in a row too quickly, depending on your motherboard. So you might have to read it a couple of times and validate the results. The resolution is pretty accurate, though. It rolls over 65536 every tick, and it rolls over 65536 ticks every hour, so your resolution is 838 ns. Plenty of space for your millisecond requirements.


Report Offensive Follow Up For Removal

Response Number 2
Name: cup
Date: June 14, 2002 at 02:22:48 Pacific
Reply: (edit)

Try gettimeofday. This returns a timeval structure which gives seconds and milliseconds.


Report Offensive Follow Up For Removal

Response Number 3
Name: cup
Date: June 14, 2002 at 02:42:59 Pacific
Reply: (edit)

Oops - timeval gives result in seconds and microseconds not milliseconds. Just divide by 1000 to get milliseconds.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Precise timestamps

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes Today.
Discuss in The Lounge