Improve gettimeofday() on Windows (if it compiles).
static void
gettimeofday(struct timeval *tv, void *ignore)
{
- struct timeb tb;
-
- ftime(&tb);
- tv->tv_sec = (long)tb.time;
- tv->tv_usec = (long)(tb.millitm) * 1000L;
+ FILETIME file_time;
+ ULARGE_INTEGER ularge;
+
+ GetSystemTimeAsFileTime(&file_time);
+ ularge.LowPart = file_time.dwLowDateTime;
+ ularge.HighPart = file_time.dwHighDateTime;
+ /* Change base from Jan 1 1601 00:00:00 to Jan 1 1970 00:00:00 */
+ ularge.QuadPart -= 116444736000000000UI64;
+ /*
+ * ularge.QuadPart is now the number of 100-nanosecond intervals
+ * since Jan 1 1970 00:00:00.
+ */
+ tv->tv_sec = (long)(ularge.QuadPart / 10000000UI64);
+ tv->tv_usec = (long)((ularge.QuadPart % 10000000UI64) / 10UI64);
}
#endif
