Click to See Complete Forum and Search --> : GetTimeOfDay Implementation


niraja_npi
October 8th, 2002, 06:26 AM
Hi All,

I want to implement the gettimeofday functionality in WinXP using VC++ as implemented in unix environment.

Man page description of Unix

The gettimeofday() function gets and the settimeofday()
function sets the system's notion of the current time. The
current time is expressed in elapsed seconds and
microseconds since 00:00 Universal Coordinated Time, January
1, 1970. The resolution of the system clock is hardware
dependent; the time may be updated continuously or in clock
ticks.

The tp argument points to a timeval structure, which
includes the following members:

long tv_sec; /* seconds since Jan. 1, 1970 */
long tv_usec; /* and microseconds */

If tp is a null pointer, the current time information is not
returned or set.

Best Regards,
Niraj Agarwal

cup
October 8th, 2002, 07:01 AM
If you are not too worried about the number of microseconds since 01/01/70, you can use time(0) to fill in the tv_sec bit and set the tv_usec bit to 0.

The Windows equivalent, COleDateTime and CTime class won't give you the number of microseconds and you'll have to work out the number of seconds. You can use COleDateTime::SetDateTime to set the date/time. Again, you'd have to translate the number of seconds into their date/time structure. Probably easier to use gmtime to translate it for you and stuff in the values.

niraja_npi
October 9th, 2002, 01:03 AM
//From January 1, 1601 (UTC). to January 1,1970
#define FACTOR 0x19db1ded53e8000

int gettimeofday(struct timeval *tp,void * tz)
{
FILETIME f;
ULARGE_INTEGER ifreq;
LONGLONG res;
GetSystemTimeAsFileTime(&f);
ifreq.HighPart = f.dwHighDateTime;
ifreq.LowPart = f.dwLowDateTime;

res = ifreq.QuadPart - FACTOR;
tp->tv_sec = (long)((LONGLONG)res/10000000);
tp->tv_usec = (long)((LONGLONG)res% 10000000000); // Micro Seonds

return 0;

}