9fans archive / 1996 / 03 / 22 /    prev next

From: Scott Schwartz schwartz@gal...
Subject: ape, gettimeofday.c
Date: Thu, 7 Mar 1996 14:57:08 -0500

Pgp is much happier if gettimeofday returns something for
the usec field.  Here's a version that tries to do that.

#include <sys/types.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

static int
ms(void) 
{
	char b[20];
	static int f = -1;

	/* XXX - should use native plan9 syscalls herein */
	memset(b, 0, sizeof(b));
	if (f < 0) {
		f = open("/dev/msec", O_RDONLY);
		fcntl(f, FD_CLOEXEC, 1);
	}
	if (f >= 0) {
		lseek(f, 0, 0);
		read(f, b, sizeof(b));
	}
	return atol(b) % 1000;
}

int
gettimeofday(struct timeval *tp, struct timezone *tzp)
{
	tp->tv_sec = time(0);
	tp->tv_usec = ms() * 1000;

	if(tzp) {
		tzp->tz_minuteswest = 240;
		tzp->tz_dsttime = 1;
	}

	return 0;
}