xref: /minix/minix/lib/libsys/clock_time.c (revision 83133719)
1 
2 #include "sysutil.h"
3 #include <sys/time.h>
4 
5 /*
6  * This routine returns the time in seconds since 1.1.1970.  MINIX is an
7  * astrophysically naive system that assumes the earth rotates at a constant
8  * rate and that such things as leap seconds do not exist.  If a non-NULL
9  * pointer to a timespec structure is given, that structure is filled with
10  * the current time in subsecond precision.
11  */
12 time_t
13 clock_time(struct timespec *tv)
14 {
15 	uint32_t system_hz;
16 	clock_t uptime, realtime;
17 	time_t boottime, sec;
18 	int r;
19 
20 	if ((r = getuptime(&uptime, &realtime, &boottime)) != OK)
21 		panic("clock_time: getuptime failed: %d", r);
22 
23 	system_hz = sys_hz();	/* sys_hz() caches its return value */
24 
25 	sec = boottime + realtime / system_hz;
26 
27 	if (tv != NULL) {
28 		tv->tv_sec = sec;
29 
30 		/*
31 		 * We do not want to overflow, and system_hz can be as high as
32 		 * 50kHz.
33 		 */
34 		if (system_hz < LONG_MAX / 40000)
35 			tv->tv_nsec = (realtime % system_hz) * 40000 /
36 			    system_hz * 25000;
37 		else
38 			tv->tv_nsec = 0;	/* bad, but what's better? */
39 	}
40 
41 	return sec;
42 }
43