xref: /plan9/sys/src/ape/lib/bsd/gettimeofday.c (revision 282e677fa45fb578cdb8bc2c412ac084c367776e)
1 #include <sys/types.h>
2 #include <time.h>
3 #include <sys/time.h>
4 #include <string.h>
5 #include "sys9.h"
6 
7 typedef unsigned long long uvlong;
8 typedef long long vlong;
9 typedef unsigned char uchar;
10 
11 static uvlong order = 0x0001020304050607ULL;
12 
13 static void
14 be2vlong(vlong *to, uchar *f)
15 {
16 	uchar *t, *o;
17 	int i;
18 
19 	t = (uchar*)to;
20 	o = (uchar*)&order;
21 	for(i = 0; i < 8; i++)
22 		t[o[i]] = f[i];
23 }
24 
25 int
26 gettimeofday(struct timeval *tp, struct timezone *tzp)
27 {
28 	int f;
29 	uchar b[8];
30 	vlong t;
31 
32 	memset(b, 0, sizeof b);
33 	f = _OPEN("/dev/bintime", 0);
34 	if(f >= 0) {
35 		_PREAD(f, b, sizeof(b), 0);
36 		_CLOSE(f);
37 	}
38 	be2vlong(&t, b);
39 
40 	tp->tv_sec = t/1000000000;
41 	tp->tv_usec = (t/1000)%1000000;
42 
43 	if(tzp) {
44 		tzp->tz_minuteswest = 240;
45 		tzp->tz_dsttime = 1;
46 	}
47 
48 	return 0;
49 }
50