/contrib/ntp/include/ntp_unixtime.h

https://bitbucket.org/freebsd/freebsd-head/ · C++ Header · 141 lines · 85 code · 17 blank · 39 comment · 12 complexity · 567e041525d3fe921b7d74f674f81983 MD5 · raw file

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