PDA

View Full Version : Current Time with microsecond precision on windows



Dwarf007
3rd April 2006, 12:41
Hello,

Does anyone know how, under Windows, I can get the exact current microsecond so that I can display a time with the microsecond precision ?

Thanks!

Zatraz
3rd April 2006, 13:39
Is it that you are searching?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getfiletime.asp

Dwarf007
3rd April 2006, 13:50
Not at all... It has nothing to do with files and I need microsecond.
Sorry.

Kapil
3rd April 2006, 14:05
Not at all... It has nothing to do with files and I need microsecond.
Sorry.


There can be many ways thru which u can get time..

The include files are :

ctime.h, time.h, windows.h... they help you in extracting the current system time...

u can get time in milliseconds.. once you have that, you can extract to microseconds...

I hope this is what you wanted...

Dwarf007
3rd April 2006, 14:37
u can get time in milliseconds.. once you have that, you can extract to microseconds...

I need microseconds, milliseconds are not precise enough.
What do you mean with extract to microseconds ?

wysota
3rd April 2006, 16:40
Does Windows handle microseconds at all?

Dwarf007
3rd April 2006, 16:53
Does Windows handle microseconds at all?

I heared and read a bit, that using the following Windows API functions : QueryPerformanceFrequency() and QueryPerformanceCounter() it should do...

but I didn't manage to display the current microsecond under windows, therefore I was wondering if anybody here had an example or an easy way to do that.

brcain
3rd April 2006, 17:06
Are you trying to handle something with a hard real-time requirement? I've heard of extensions (patches to kernel) for Windows to do this.

Most timers (at least standard APIs) that I've used only have a resolution of 1 millisecond ... even though they may indicate a higher resolution value.

wysota
3rd April 2006, 17:30
It would be hard to get higher resolutions with standard system as the internal system clock would have to tick with frequency higher than 1kHz, meaning there would need to be more than 1000 clock interrupts per second.

Dwarf007
3rd April 2006, 17:35
Linux can.
gettimeofday() is accurate to the microsecond

wysota
3rd April 2006, 18:28
Are you sure it doesn't only display in microseconds? I believe microseconds can be obtained when using hardware alarms from the real time clock, but I doubt system clock can be that accurate on its own. AFAIR modern Linux distros on i386 architecture have the clock interrupts set at 1kHz, so it would mean they can measure time with accuracy of 0.001s = 1ms. This is only my logic explanation, if someone knows better, please say so.

Dwarf007
3rd April 2006, 18:36
you mean it just gives a millisecond accurate time and the microseconds are a kind of rand() % 999 ?

wysota
3rd April 2006, 18:37
you mean it just gives a millisecond accurate time and the microseconds are a kind of rand() % 999 ?

Or set to 0. But generaly yes, it might be so.

Dwarf007
3rd April 2006, 18:39
:eek: *grumpfl*

Ok thanks.. I think I will do something like that then.. (rand() % 999)

wysota
3rd April 2006, 19:14
Why do you need microseconds anyway?

Dwarf007
4th April 2006, 12:50
wysota: I want to do a kind of portable and accurate gettimeofday();

Here is the solution, in case someone wants to do the same one day.
No garantie on it but it seems to work.

Windows part for a portable gettimeofday():


#ifdef _WIN32_

#include <windows.h>

int gettimeofday( struct timeval *tv, struct timezone *tz )
{
time_t rawtime;

time(&rawtime);
tv->tv_sec = (long)rawtime;

// here starts the microsecond resolution:

LARGE_INTEGER tickPerSecond;
LARGE_INTEGER tick; // a point in time

// get the high resolution counter's accuracy
QueryPerformanceFrequency(&tickPerSecond);

// what time is it ?
QueryPerformanceCounter(&tick);

// and here we get the current microsecond! \o/
tv->tv_usec = (tick.QuadPart % tickPerSecond.QuadPart);

return 0;
}
#endif // _WIN32_

TheKedge
5th April 2006, 11:42
.... and here's a class using those funcs (if it's any help)

K