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