2 * ntp_unixtime.h - contains constants and macros for converting between
3 * NTP time stamps (l_fp) and Unix times (struct timeval)
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))
25 # define GETTIMEOFDAY(a, b) (gettimeofday(a, b))
26 # define SETTIMEOFDAY(a, b) (settimeofday(a, b))
28 # endif /* SYSV_TIMEOFDAY */
29 # endif /* not (HAVE_SYS_TIMERS_H && HAVE_GETCLOCK) */
32 * Time of day conversion constant. Ntp's time scale starts in 1900,
35 #define JAN_1970 0x83aa7e80 /* 2208988800 1970 - 1900 in seconds */
38 * These constants are used to round the time stamps computed from
39 * a struct timeval to the microsecond (more or less). This keeps
42 #define TS_MASK 0xfffff000 /* mask to usec, for time stamps */
43 #define TS_ROUNDBIT 0x00000800 /* round at this bit */
47 * Convert usec to a time stamp fraction. If you use this the program
48 * must include the following declarations:
50 extern u_long ustotslo[];
51 extern u_long ustotsmid[];
52 extern u_long ustotshi[];
54 #define TVUTOTSF(tvu, tsf) \
55 (tsf) = ustotslo[(tvu) & 0xff] \
56 + ustotsmid[((tvu) >> 8) & 0xff] \
57 + ustotshi[((tvu) >> 16) & 0xf]
60 * Convert a struct timeval to a time stamp.
62 #define TVTOTS(tv, ts) \
64 (ts)->l_ui = (u_long)(tv)->tv_sec; \
65 TVUTOTSF((tv)->tv_usec, (ts)->l_uf); \
68 #define sTVTOTS(tv, ts) \
72 (ts)->l_ui = (tv)->tv_sec; \
73 usec = (tv)->tv_usec; \
74 if (((tv)->tv_sec < 0) || ((tv)->tv_usec < 0)) { \
76 (ts)->l_ui = -(ts)->l_ui; \
79 TVUTOTSF(usec, (ts)->l_uf); \
86 * TV_SHIFT is used to turn the table result into a usec value. To round,
87 * add in TV_ROUNDBIT before shifting
90 #define TV_ROUNDBIT 0x4
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:
97 extern long tstouslo[];
98 extern long tstousmid[];
99 extern long tstoushi[];
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
107 * Convert a time stamp to a struct timeval. The time stamp
108 * has to be positive.
110 #define TSTOTV(ts, tv) \
112 (tv)->tv_sec = (ts)->l_ui; \
113 TSFTOTVU((ts)->l_uf, (tv)->tv_usec); \
114 if ((tv)->tv_usec == 1000000) { \
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.
125 extern u_long msutotsflo[];
126 extern u_long msutotsfhi[];
128 #define MSUTOTSF(msu, tsf) \
129 (tsf) = msutotsfhi[((msu) >> 5) & 0x1f] + msutotsflo[(msu) & 0x1f]
131 extern char * tvtoa P((const struct timeval *));
132 extern char * utvtoa P((const struct timeval *));