timespec.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* timespec -- System time interface
  2. Copyright (C) 2000, 2002, 2004-2005, 2007, 2009-2015 Free Software
  3. Foundation, Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #if ! defined TIMESPEC_H
  15. # define TIMESPEC_H
  16. # include <time.h>
  17. #ifndef _GL_INLINE_HEADER_BEGIN
  18. #error "Please include config.h first."
  19. #endif
  20. _GL_INLINE_HEADER_BEGIN
  21. #ifndef _GL_TIMESPEC_INLINE
  22. # define _GL_TIMESPEC_INLINE _GL_INLINE
  23. #endif
  24. /* Resolution of timespec time stamps (in units per second), and log
  25. base 10 of the resolution. */
  26. enum { TIMESPEC_RESOLUTION = 1000000000 };
  27. enum { LOG10_TIMESPEC_RESOLUTION = 9 };
  28. /* Return a timespec with seconds S and nanoseconds NS. */
  29. _GL_TIMESPEC_INLINE struct timespec
  30. make_timespec (time_t s, long int ns)
  31. {
  32. struct timespec r;
  33. r.tv_sec = s;
  34. r.tv_nsec = ns;
  35. return r;
  36. }
  37. /* Return negative, zero, positive if A < B, A == B, A > B, respectively.
  38. For each time stamp T, this code assumes that either:
  39. * T.tv_nsec is in the range 0..999999999; or
  40. * T.tv_sec corresponds to a valid leap second on a host that supports
  41. leap seconds, and T.tv_nsec is in the range 1000000000..1999999999; or
  42. * T.tv_sec is the minimum time_t value and T.tv_nsec is -1; or
  43. T.tv_sec is the maximum time_t value and T.tv_nsec is 2000000000.
  44. This allows for special struct timespec values that are less or
  45. greater than all possible valid time stamps.
  46. In all these cases, it is safe to subtract two tv_nsec values and
  47. convert the result to integer without worrying about overflow on
  48. any platform of interest to the GNU project, since all such
  49. platforms have 32-bit int or wider.
  50. Replacing "(int) (a.tv_nsec - b.tv_nsec)" with something like
  51. "a.tv_nsec < b.tv_nsec ? -1 : a.tv_nsec > b.tv_nsec" would cause
  52. this function to work in some cases where the above assumption is
  53. violated, but not in all cases (e.g., a.tv_sec==1, a.tv_nsec==-2,
  54. b.tv_sec==0, b.tv_nsec==999999999) and is arguably not worth the
  55. extra instructions. Using a subtraction has the advantage of
  56. detecting some invalid cases on platforms that detect integer
  57. overflow.
  58. The (int) cast avoids a gcc -Wconversion warning. */
  59. _GL_TIMESPEC_INLINE int _GL_ATTRIBUTE_PURE
  60. timespec_cmp (struct timespec a, struct timespec b)
  61. {
  62. return (a.tv_sec < b.tv_sec ? -1
  63. : a.tv_sec > b.tv_sec ? 1
  64. : (int) (a.tv_nsec - b.tv_nsec));
  65. }
  66. /* Return -1, 0, 1, depending on the sign of A. A.tv_nsec must be
  67. nonnegative. */
  68. _GL_TIMESPEC_INLINE int _GL_ATTRIBUTE_PURE
  69. timespec_sign (struct timespec a)
  70. {
  71. return a.tv_sec < 0 ? -1 : a.tv_sec || a.tv_nsec;
  72. }
  73. struct timespec timespec_add (struct timespec, struct timespec)
  74. _GL_ATTRIBUTE_CONST;
  75. struct timespec timespec_sub (struct timespec, struct timespec)
  76. _GL_ATTRIBUTE_CONST;
  77. struct timespec dtotimespec (double)
  78. _GL_ATTRIBUTE_CONST;
  79. /* Return an approximation to A, of type 'double'. */
  80. _GL_TIMESPEC_INLINE double
  81. timespectod (struct timespec a)
  82. {
  83. return a.tv_sec + a.tv_nsec / 1e9;
  84. }
  85. void gettime (struct timespec *);
  86. int settime (struct timespec const *);
  87. _GL_INLINE_HEADER_END
  88. #endif