From a87b4e23d688fd5e5ff7e332a522599b86881f68 Mon Sep 17 00:00:00 2001 From: "Asanka C. Herath" Date: Thu, 7 Oct 2010 11:31:11 -0400 Subject: [PATCH] Windows: Implement gettimeofday() using native APIs We now use GetSystemTimeAsFileTime() for gettimeofday(). This gives us a better resolution than one second. --- lib/roken/gettimeofday.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/roken/gettimeofday.c b/lib/roken/gettimeofday.c index e6e2e04a3..a53b96f4f 100644 --- a/lib/roken/gettimeofday.c +++ b/lib/roken/gettimeofday.c @@ -35,6 +35,31 @@ #include "roken.h" #ifndef HAVE_GETTIMEOFDAY +#ifdef _WIN32 + +ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL +gettimeofday (struct timeval *tp, void *ignore) +{ + FILETIME ft; + ULARGE_INTEGER li; + ULONGLONG ull; + + GetSystemTimeAsFileTime(&ft); + li.LowPart = ft.dwLowDateTime; + li.HighPart = ft.dwHighDateTime; + ull = li.QuadPart; + + ull -= 116444736000000000i64; + ull /= 10i64; /* ull is now in microseconds */ + + tp->tv_usec = (ull % 1000000i64); + tp->tv_sec = (ull / 1000000i64); + + return 0; +} + +#else + /* * Simple gettimeofday that only returns seconds. */ @@ -48,4 +73,6 @@ gettimeofday (struct timeval *tp, void *ignore) tp->tv_usec = 0; return 0; } + +#endif /* !_WIN32 */ #endif