xref: /minix/minix/lib/libsys/clock_time.c (revision 0a6a1f1d)
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 	struct minix_kerninfo *minix_kerninfo;
16 	uint32_t system_hz;
17 	clock_t realtime;
18 	time_t boottime, sec;
19 
20 	minix_kerninfo = get_minix_kerninfo();
21 
22 	/* We assume atomic 32-bit field retrieval.  TODO: 64-bit support. */
23 	boottime = minix_kerninfo->kclockinfo->boottime;
24 	realtime = minix_kerninfo->kclockinfo->realtime;
25 	system_hz = minix_kerninfo->kclockinfo->hz;
26 
27 	sec = boottime + realtime / system_hz;
28 
29 	if (tv != NULL) {
30 		tv->tv_sec = sec;
31 
32 		/*
33 		 * We do not want to overflow, and system_hz can be as high as
34 		 * 50kHz.
35 		 */
36 		if (system_hz < LONG_MAX / 40000)
37 			tv->tv_nsec = (realtime % system_hz) * 40000 /
38 			    system_hz * 25000;
39 		else
40 			tv->tv_nsec = 0;	/* bad, but what's better? */
41 	}
42 
43 	return sec;
44 }
45