xref: /freebsd/contrib/ntp/include/ntp_unixtime.h (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
1 /*
2  * ntp_unixtime.h - contains constants and macros for converting between
3  *		    NTP time stamps (l_fp) and Unix times (struct timeval)
4  */
5 
6 #include "ntp_types.h"
7 
8 #include <sys/time.h>
9 
10 /* gettimeofday() takes two args in BSD and only one in SYSV */
11 # if defined(HAVE_SYS_TIMERS_H) && defined(HAVE_GETCLOCK)
12 #  include <sys/timers.h>
13 int getclock (int clock_type, struct timespec *tp);
14 /* Don't #define GETTIMEOFDAY because we shouldn't be using it in this case. */
15 #   define SETTIMEOFDAY(a, b) (settimeofday(a, b))
16 # else /* not (HAVE_SYS_TIMERS_H && HAVE_GETCLOCK) */
17 #  ifdef SYSV_TIMEOFDAY
18 #   define GETTIMEOFDAY(a, b) (gettimeofday(a))
19 #   define SETTIMEOFDAY(a, b) (settimeofday(a))
20 #  else /* ! SYSV_TIMEOFDAY */
21 #if defined SYS_CYGWIN32
22 #   define GETTIMEOFDAY(a, b) (gettimeofday(a, b))
23 #   define SETTIMEOFDAY(a, b) (settimeofday_NT(a))
24 #else
25 #   define GETTIMEOFDAY(a, b) (gettimeofday(a, b))
26 #   define SETTIMEOFDAY(a, b) (settimeofday(a, b))
27 #endif
28 #  endif /* SYSV_TIMEOFDAY */
29 # endif /* not (HAVE_SYS_TIMERS_H && HAVE_GETCLOCK) */
30 
31 /*
32  * Time of day conversion constant.  Ntp's time scale starts in 1900,
33  * Unix in 1970.
34  */
35 #define	JAN_1970	0x83aa7e80	/* 2208988800 1970 - 1900 in seconds */
36 
37 /*
38  * These constants are used to round the time stamps computed from
39  * a struct timeval to the microsecond (more or less).  This keeps
40  * things neat.
41  */
42 #define	TS_MASK		0xfffff000	/* mask to usec, for time stamps */
43 #define	TS_ROUNDBIT	0x00000800	/* round at this bit */
44 
45 
46 /*
47  * Convert usec to a time stamp fraction.  If you use this the program
48  * must include the following declarations:
49  */
50 extern u_long ustotslo[];
51 extern u_long ustotsmid[];
52 extern u_long ustotshi[];
53 
54 #define	TVUTOTSF(tvu, tsf) \
55 	(tsf) = ustotslo[(tvu) & 0xff] \
56 	    + ustotsmid[((tvu) >> 8) & 0xff] \
57 	    + ustotshi[((tvu) >> 16) & 0xf]
58 
59 /*
60  * Convert a struct timeval to a time stamp.
61  */
62 #define TVTOTS(tv, ts) \
63 	do { \
64 		(ts)->l_ui = (u_long)(tv)->tv_sec; \
65 		TVUTOTSF((tv)->tv_usec, (ts)->l_uf); \
66 	} while(0)
67 
68 #define sTVTOTS(tv, ts) \
69 	do { \
70 		int isneg = 0; \
71 		long usec; \
72 		(ts)->l_ui = (tv)->tv_sec; \
73 		usec = (tv)->tv_usec; \
74 		if (((tv)->tv_sec < 0) || ((tv)->tv_usec < 0)) { \
75 			usec = -usec; \
76 			(ts)->l_ui = -(ts)->l_ui; \
77 			isneg = 1; \
78 		} \
79 		TVUTOTSF(usec, (ts)->l_uf); \
80 		if (isneg) { \
81 			L_NEG((ts)); \
82 		} \
83 	} while(0)
84 
85 /*
86  * TV_SHIFT is used to turn the table result into a usec value.  To round,
87  * add in TV_ROUNDBIT before shifting
88  */
89 #define	TV_SHIFT	3
90 #define	TV_ROUNDBIT	0x4
91 
92 
93 /*
94  * Convert a time stamp fraction to microseconds.  The time stamp
95  * fraction is assumed to be unsigned.  To use this in a program, declare:
96  */
97 extern long tstouslo[];
98 extern long tstousmid[];
99 extern long tstoushi[];
100 
101 #define	TSFTOTVU(tsf, tvu) \
102 	(tvu) = (tstoushi[((tsf) >> 24) & 0xff] \
103 	    + tstousmid[((tsf) >> 16) & 0xff] \
104 	    + tstouslo[((tsf) >> 9) & 0x7f] \
105 	    + TV_ROUNDBIT) >> TV_SHIFT
106 /*
107  * Convert a time stamp to a struct timeval.  The time stamp
108  * has to be positive.
109  */
110 #define	TSTOTV(ts, tv) \
111 	do { \
112 		(tv)->tv_sec = (ts)->l_ui; \
113 		TSFTOTVU((ts)->l_uf, (tv)->tv_usec); \
114 		if ((tv)->tv_usec == 1000000) { \
115 			(tv)->tv_sec++; \
116 			(tv)->tv_usec = 0; \
117 		} \
118 	} while (0)
119 
120 /*
121  * Convert milliseconds to a time stamp fraction.  This shouldn't be
122  * here, but it is convenient since the guys who use the definition will
123  * often be including this file anyway.
124  */
125 extern u_long msutotsflo[];
126 extern u_long msutotsfhi[];
127 
128 #define	MSUTOTSF(msu, tsf) \
129 	(tsf) = msutotsfhi[((msu) >> 5) & 0x1f] + msutotsflo[(msu) & 0x1f]
130 
131 extern	char *	tvtoa		P((const struct timeval *));
132 extern	char *	utvtoa		P((const struct timeval *));
133