diff future.c @ 81:be814b934eca

Implement gettimeofday()
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 18 Mar 2013 12:36:34 +0100
parents 9d57d1fcbcd5
children 4102fe614df2
line wrap: on
line diff
--- a/future.c	Mon Mar 18 12:34:05 2013 +0100
+++ b/future.c	Mon Mar 18 12:36:34 2013 +0100
@@ -3,6 +3,12 @@
 /* required by CVI */
 #include <ansi_c.h>
 
+/* time functions */
+#include <windows.h>
+
+#include "future.h"
+
+
 /* an implementation of the C99 `round` function based on `floor` and `ceil` */
 double round(double x)
 {
@@ -18,3 +24,48 @@
 		return y;
 	}
 }
+
+
+/* implement high resolution time service */
+int gettimeofday(struct timeval *tp, struct timezone *tzp)
+{
+	/* difference between 1970-01-01 and 1601-01-01
+	   in 100-nanoseconds intervals */
+	const unsigned __int64 shift = 116444736000000000ULL;
+	union {
+		FILETIME ft;
+		unsigned __int64 integer; /* 100-nanoseconds since 1601-01-01 */
+	} now;
+
+	/* retrive system time in UTC. the resolution is limited to the resolution
+	   of the system timer, which is typically in the range of 10 milliseconds
+	   to 16 milliseconds */
+	GetSystemTimeAsFileTime(&now.ft);
+	
+	/* shift to epoch and truncate to microseconds */
+	now.integer = (now.integer - shift) / 10LL;
+	
+	tp->tv_usec = (long)(now.integer % 1000000LL);
+	tp->tv_sec = (long)(now.integer / 1000000LL);
+	
+	/* FIXME CVI redefines the epoch as seconds since 1900-01-01 ! */
+	tp->tv_sec += 2208988800;
+	
+	return 0;
+}
+
+
+static struct tm * copy_tm_result(struct tm *dest, struct tm const *src)
+{
+	if (! src)
+		return NULL;
+	*dest = *src;
+	return dest;
+}
+
+
+struct tm * gmtime_r(time_t const *t, struct tm *tp)
+{
+	return copy_tm_result(tp, gmtime(t));
+}
+