4 * ntp_unixtime.h - contains constants and macros for converting between
5 * NTP time stamps (l_fp) and Unix times (struct timeval)
15 # define GETTIMEOFDAY(a, b) (node_gettime(&ntp_node, a))
16 # define SETTIMEOFDAY(a, b) (node_settime(&ntp_node, a))
17 # define ADJTIMEOFDAY(a, b) (node_adjtime(&ntp_node, a, b))
19 # define ADJTIMEOFDAY(a, b) (adjtime(a, b))
20 /* gettimeofday() takes two args in BSD and only one in SYSV */
21 # if defined(HAVE_SYS_TIMERS_H) && defined(HAVE_GETCLOCK)
22 # include <sys/timers.h>
23 int getclock (int clock_type
, struct timespec
*tp
);
24 /* Don't #define GETTIMEOFDAY because we shouldn't be using it in this case. */
25 # define SETTIMEOFDAY(a, b) (settimeofday(a, b))
26 # else /* not (HAVE_SYS_TIMERS_H && HAVE_GETCLOCK) */
27 # ifdef SYSV_TIMEOFDAY
28 # define GETTIMEOFDAY(a, b) (gettimeofday(a))
29 # define SETTIMEOFDAY(a, b) (settimeofday(a))
30 # else /* ! SYSV_TIMEOFDAY */
31 #if defined SYS_CYGWIN32
32 # define GETTIMEOFDAY(a, b) (gettimeofday(a, b))
33 # define SETTIMEOFDAY(a, b) (settimeofday_NT(a))
35 # define GETTIMEOFDAY(a, b) (gettimeofday(a, b))
36 # define SETTIMEOFDAY(a, b) (settimeofday(a, b))
38 # endif /* SYSV_TIMEOFDAY */
39 # endif /* not (HAVE_SYS_TIMERS_H && HAVE_GETCLOCK) */
43 * Time of day conversion constant. Ntp's time scale starts in 1900,
46 #define JAN_1970 0x83aa7e80 /* 2208988800 1970 - 1900 in seconds */
49 * These constants are used to round the time stamps computed from
50 * a struct timeval to the microsecond (more or less). This keeps
53 #define TS_MASK 0xfffff000 /* mask to usec, for time stamps */
54 #define TS_ROUNDBIT 0x00000800 /* round at this bit */
58 * Convert usec to a time stamp fraction. If you use this the program
59 * must include the following declarations:
61 extern u_long ustotslo
[];
62 extern u_long ustotsmid
[];
63 extern u_long ustotshi
[];
65 #define TVUTOTSF(tvu, tsf) \
66 (tsf) = ustotslo[(tvu) & 0xff] \
67 + ustotsmid[((tvu) >> 8) & 0xff] \
68 + ustotshi[((tvu) >> 16) & 0xf]
71 * Convert a struct timeval to a time stamp.
73 #define TVTOTS(tv, ts) \
75 (ts)->l_ui = (u_long)(tv)->tv_sec; \
76 TVUTOTSF((tv)->tv_usec, (ts)->l_uf); \
79 #define sTVTOTS(tv, ts) \
83 (ts)->l_ui = (tv)->tv_sec; \
84 usec = (tv)->tv_usec; \
85 if (((tv)->tv_sec < 0) || ((tv)->tv_usec < 0)) { \
87 (ts)->l_ui = -(ts)->l_ui; \
90 TVUTOTSF(usec, (ts)->l_uf); \
97 * TV_SHIFT is used to turn the table result into a usec value. To round,
98 * add in TV_ROUNDBIT before shifting
101 #define TV_ROUNDBIT 0x4
105 * Convert a time stamp fraction to microseconds. The time stamp
106 * fraction is assumed to be unsigned. To use this in a program, declare:
108 extern long tstouslo
[];
109 extern long tstousmid
[];
110 extern long tstoushi
[];
112 #define TSFTOTVU(tsf, tvu) \
113 (tvu) = (tstoushi[((tsf) >> 24) & 0xff] \
114 + tstousmid[((tsf) >> 16) & 0xff] \
115 + tstouslo[((tsf) >> 9) & 0x7f] \
116 + TV_ROUNDBIT) >> TV_SHIFT
118 * Convert a time stamp to a struct timeval. The time stamp
119 * has to be positive.
121 #define TSTOTV(ts, tv) \
123 (tv)->tv_sec = (ts)->l_ui; \
124 TSFTOTVU((ts)->l_uf, (tv)->tv_usec); \
125 if ((tv)->tv_usec == 1000000) { \
132 * Convert milliseconds to a time stamp fraction. This shouldn't be
133 * here, but it is convenient since the guys who use the definition will
134 * often be including this file anyway.
136 extern u_long msutotsflo
[];
137 extern u_long msutotsfhi
[];
139 #define MSUTOTSF(msu, tsf) \
140 (tsf) = msutotsfhi[((msu) >> 5) & 0x1f] + msutotsflo[(msu) & 0x1f]
142 extern char * tvtoa
P((const struct timeval
*));
143 extern char * utvtoa
P((const struct timeval
*));