#include <sys/time.h>
#include <dlfcn.h>


/* Sep 1, 1999, 8:00am is as good a time to MATLAB as any :-) */
#define T_TARGET	936165600


int initialized = 0;
int delta_t = 0;


int (*real_time)(time_t *);
int (*real_gettimeofday)(struct timeval *, void *);


void init() {
	real_time = dlsym(RTLD_NEXT, "time");
	real_gettimeofday = dlsym(RTLD_NEXT, "gettimeofday");

	delta_t = real_time(NULL) - T_TARGET;

	initialized = 1;
}


time_t time(time_t *tloc) {
	time_t t;

	if (!initialized)
		init();

	t = real_time(tloc);

	if (tloc != NULL)
		*tloc -= delta_t;

	return (t - delta_t);
}


int gettimeofday(struct timeval *tp, void *x) {
	int status;

	if (!initialized)
		init();

	status = real_gettimeofday(tp, x);

	if (tp != NULL)
		tp->tv_sec -= delta_t;

	return status;
}

