timespec.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * This file is Copyright (c) 2015 by the GPSD project
  3. * SPDX-License-Identifier: BSD-2-clause
  4. */
  5. #ifndef GPSD_TIMESPEC_H
  6. #define GPSD_TIMESPEC_H
  7. #include <math.h> /* for modf() */
  8. #include <stdbool.h> /* for bool */
  9. /* normalize a timespec
  10. *
  11. * three cases to note
  12. * if tv_sec is positve, then tv_nsec must be positive
  13. * if tv_sec is negative, then tv_nsec must be negative
  14. * if tv_sec is zero, then tv_nsec may be positive or negative.
  15. *
  16. * this only handles the case where two normalized timespecs
  17. * are added or subracted. (e.g. only a one needs to be borrowed/carried
  18. *
  19. * NOTE: this normalization is not the same as ntpd uses
  20. */
  21. #define NS_IN_SEC 1000000000LL /* nanoseconds in a second */
  22. #define US_IN_SEC 1000000LL /* microseconds in a second */
  23. #define MS_IN_SEC 1000LL /* milliseconds in a second */
  24. /* return the difference between timespecs in nanoseconds
  25. * int may be too small, 32 bit long is too small, floats are too imprecise,
  26. * doubles are not quite precise enough
  27. * MUST be at least int64_t to maintain precision on 32 bit code */
  28. #define timespec_diff_ns(x, y) \
  29. (int64_t)((((x).tv_sec-(y).tv_sec)*NS_IN_SEC)+(x).tv_nsec-(y).tv_nsec)
  30. static inline void TS_NORM( struct timespec *ts)
  31. {
  32. if ( ( 1 <= ts->tv_sec ) ||
  33. ( (0 == ts->tv_sec ) && (0 <= ts->tv_nsec ) ) ) {
  34. /* result is positive */
  35. if ( NS_IN_SEC <= ts->tv_nsec ) {
  36. /* borrow from tv_sec */
  37. ts->tv_nsec -= NS_IN_SEC;
  38. ts->tv_sec++;
  39. } else if ( 0 > (ts)->tv_nsec ) {
  40. /* carry to tv_sec */
  41. ts->tv_nsec += NS_IN_SEC;
  42. ts->tv_sec--;
  43. }
  44. } else {
  45. /* result is negative */
  46. if ( -NS_IN_SEC >= ts->tv_nsec ) {
  47. /* carry to tv_sec */
  48. ts->tv_nsec += NS_IN_SEC;
  49. ts->tv_sec--;
  50. } else if ( 0 < ts->tv_nsec ) {
  51. /* borrow from tv_sec */
  52. ts->tv_nsec -= NS_IN_SEC;
  53. ts->tv_sec++;
  54. }
  55. }
  56. }
  57. /* normalize a timeval */
  58. #define TV_NORM(tv) \
  59. do { \
  60. if ( US_IN_SEC <= (tv)->tv_usec ) { \
  61. (tv)->tv_usec -= US_IN_SEC; \
  62. (tv)->tv_sec++; \
  63. } else if ( 0 > (tv)->tv_usec ) { \
  64. (tv)->tv_usec += US_IN_SEC; \
  65. (tv)->tv_sec--; \
  66. } \
  67. } while (0)
  68. /* convert timespec to timeval, with rounding */
  69. #define TSTOTV(tv, ts) \
  70. do { \
  71. (tv)->tv_sec = (ts)->tv_sec; \
  72. (tv)->tv_usec = ((ts)->tv_nsec + 500)/1000; \
  73. TV_NORM( tv ); \
  74. } while (0)
  75. /* convert timeval to timespec */
  76. #define TVTOTS(ts, tv) \
  77. do { \
  78. (ts)->tv_sec = (tv)->tv_sec; \
  79. (ts)->tv_nsec = (tv)->tv_usec*1000; \
  80. TS_NORM( ts ); \
  81. } while (0)
  82. /* subtract two timespec */
  83. #define TS_SUB(r, ts1, ts2) \
  84. do { \
  85. (r)->tv_sec = (ts1)->tv_sec - (ts2)->tv_sec; \
  86. (r)->tv_nsec = (ts1)->tv_nsec - (ts2)->tv_nsec; \
  87. TS_NORM( r ); \
  88. } while (0)
  89. /* subtract two timespec, return a double */
  90. #define TS_SUB_D(ts1, ts2) \
  91. ((double)((ts1)->tv_sec - (ts2)->tv_sec) + \
  92. ((double)((ts1)->tv_nsec - (ts2)->tv_nsec) * 1e-9))
  93. // true if normalized timespec is non zero
  94. #define TS_NZ(ts) (0 != (ts)->tv_sec || 0 != (ts)->tv_nsec)
  95. // true if normalized timespec equal or greater than zero
  96. #define TS_GEZ(ts) (0 <= (ts)->tv_sec && 0 <= (ts)->tv_nsec)
  97. // true if normalized timespec greater than zero
  98. #define TS_GZ(ts) (0 < (ts)->tv_sec || 0 < (ts)->tv_nsec)
  99. // true if normalized timespec1 greater than timespec2
  100. #define TS_GT(ts1, ts2) ((ts1)->tv_sec > (ts2)->tv_sec || \
  101. ((ts1)->tv_sec == (ts2)->tv_sec && \
  102. (ts1)->tv_nsec > (ts2)->tv_nsec))
  103. // true if normalized timespec1 greater or equal to timespec2
  104. #define TS_GE(ts1, ts2) ((ts1)->tv_sec > (ts2)->tv_sec || \
  105. ((ts1)->tv_sec == (ts2)->tv_sec && \
  106. (ts1)->tv_nsec >= (ts2)->tv_nsec))
  107. // true if normalized timespec1 equal to timespec2
  108. #define TS_EQ(ts1, ts2) ((ts1)->tv_sec == (ts2)->tv_sec && \
  109. (ts1)->tv_nsec == (ts2)->tv_nsec)
  110. /* convert a timespec to a double.
  111. * if tv_sec > 2, then inevitable loss of precision in tv_nsec
  112. * so best to NEVER use TSTONS()
  113. * WARNING replacing 1e9 with NS_IN_SEC causes loss of precision */
  114. #define TSTONS(ts) ((double)((ts)->tv_sec + ((ts)->tv_nsec / 1e9)))
  115. /* convert a double to a timespec_t
  116. * if D > 2, then inevitable loss of precision in nanoseconds
  117. */
  118. #define DTOTS(ts, d) \
  119. do { \
  120. double int_part; \
  121. (ts)->tv_nsec = (long)(modf(d, &int_part) * 1e9); \
  122. (ts)->tv_sec = (time_t)int_part; \
  123. } while (0)
  124. /* convert integer (64 bit for full range) ms to a timespec_t */
  125. #define MSTOTS(ts, ms) \
  126. do { \
  127. (ts)->tv_sec = (time_t)(ms / 1000); \
  128. (ts)->tv_nsec = (long)((ms % 1000) * 1000000L); \
  129. } while (0)
  130. #define TIMESPEC_LEN 22 /* required length of a timespec buffer */
  131. extern const char *timespec_str(const struct timespec *, char *, size_t);
  132. bool nanowait(int, int);
  133. #endif /* GPSD_TIMESPEC_H */
  134. /* end */