source: trunk/third/xntp/include/ntp_unixtime.h @ 10832

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