Define gettimeofday() for systems that don't have it
authoraland <aland>
Sat, 14 Apr 2007 17:57:04 +0000 (17:57 +0000)
committeraland <aland>
Sat, 14 Apr 2007 17:57:04 +0000 (17:57 +0000)
src/include/missing.h
src/lib/missing.c

index 570eb09..5bcd0b6 100644 (file)
@@ -348,4 +348,12 @@ extern size_t strlcat(char *dst, const char *src, size_t siz);
 #define INT16SZ (2)
 #endif
 
+#ifndef HAVE_GMTIME_R
+struct tm *gmtime_r(const time_t *l_clock, struct tm *result);
+#endif
+
+#ifndef HAVE_GETTIMEOFDAY
+int gettimeofday (struct timeval *tv, void *tz);
+#endif
+
 #endif /* _FR_MISSING_H */
index 9cf93cb..653ad0c 100644 (file)
@@ -191,3 +191,45 @@ struct tm *gmtime_r(const time_t *l_clock, struct tm *result)
   return result;
 }
 #endif
+
+#ifndef HAVE_GETTIMEOFDAY
+#ifdef WIN32
+/*
+ * Number of micro-seconds between the beginning of the Windows epoch
+ * (Jan. 1, 1601) and the Unix epoch (Jan. 1, 1970).
+ *
+ * This assumes all Win32 compilers have 64-bit support.
+ */
+#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) || defined(__WATCOMC__)
+#define DELTA_EPOCH_IN_USEC  11644473600000000Ui64
+#else
+#define DELTA_EPOCH_IN_USEC  11644473600000000ULL
+#endif
+
+static uint64_t filetime_to_unix_epoch (const FILETIME *ft)
+{
+       uint64_t res = (uint64_t) ft->dwHighDateTime << 32;
+       
+       res |= ft->dwLowDateTime;
+       res /= 10;                   /* from 100 nano-sec periods to usec */
+       res -= DELTA_EPOCH_IN_USEC;  /* from Win epoch to Unix epoch */
+       return (res);
+}
+
+int gettimeofday (struct timeval *tv, UNUSED void *tz)
+{
+       FILETIME  ft;
+       uint64_t tim;
+       
+       if (!tv) {
+               errno = EINVAL;
+               return (-1);
+       }
+        GetSystemTimeAsFileTime (&ft);
+        tim = filetime_to_unix_epoch (&ft);
+        tv->tv_sec  = (long) (tim / 1000000L);
+        tv->tv_usec = (long) (tim % 1000000L);
+        return (0);
+}
+#endif
+#endif