view future.c @ 90:c9aec93005a4

Cleanup log file data provider
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Fri, 22 Mar 2013 18:49:58 +0100
parents be814b934eca
children 4102fe614df2
line wrap: on
line source

/* welcome in the 2012 */

/* 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)
{
	if (x >= 0.0) {
		double y = floor(x);
		if (x - y >= 0.5)
			y += 1.0;
		return y;
	} else {
		double y = ceil(x);
		if (x - y >= 0.5)
			y -= 1.0;
		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));
}