timespec.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* timespec -- System time interface
  2. Copyright (C) 2000, 2002, 2004-2005, 2007, 2009-2020 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 <https://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. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include "arg-nonnull.h"
  28. /* Inverse resolution of timespec timestamps (in units per second),
  29. and log base 10 of the inverse resolution. */
  30. enum { TIMESPEC_HZ = 1000000000 };
  31. enum { LOG10_TIMESPEC_HZ = 9 };
  32. /* Obsolescent names for backward compatibility.
  33. They are misnomers, because TIMESPEC_RESOLUTION is not a resolution. */
  34. enum { TIMESPEC_RESOLUTION = TIMESPEC_HZ };
  35. enum { LOG10_TIMESPEC_RESOLUTION = LOG10_TIMESPEC_HZ };
  36. /* Return a timespec with seconds S and nanoseconds NS. */
  37. _GL_TIMESPEC_INLINE struct timespec
  38. make_timespec (time_t s, long int ns)
  39. {
  40. struct timespec r;
  41. r.tv_sec = s;
  42. r.tv_nsec = ns;
  43. return r;
  44. }
  45. /* Return negative, zero, positive if A < B, A == B, A > B, respectively. */
  46. _GL_TIMESPEC_INLINE int _GL_ATTRIBUTE_PURE
  47. timespec_cmp (struct timespec a, struct timespec b)
  48. {
  49. return 2 * _GL_CMP (a.tv_sec, b.tv_sec) + _GL_CMP (a.tv_nsec, b.tv_nsec);
  50. }
  51. /* Return -1, 0, 1, depending on the sign of A. A.tv_nsec must be
  52. nonnegative. */
  53. _GL_TIMESPEC_INLINE int _GL_ATTRIBUTE_PURE
  54. timespec_sign (struct timespec a)
  55. {
  56. return _GL_CMP (a.tv_sec, 0) + (!a.tv_sec & !!a.tv_nsec);
  57. }
  58. struct timespec timespec_add (struct timespec, struct timespec)
  59. _GL_ATTRIBUTE_CONST;
  60. struct timespec timespec_sub (struct timespec, struct timespec)
  61. _GL_ATTRIBUTE_CONST;
  62. struct timespec dtotimespec (double)
  63. _GL_ATTRIBUTE_CONST;
  64. /* Return an approximation to A, of type 'double'. */
  65. _GL_TIMESPEC_INLINE double
  66. timespectod (struct timespec a)
  67. {
  68. return a.tv_sec + a.tv_nsec / 1e9;
  69. }
  70. struct timespec current_timespec (void);
  71. void gettime (struct timespec *) _GL_ARG_NONNULL ((1));
  72. int settime (struct timespec const *) _GL_ARG_NONNULL ((1));
  73. #ifdef __cplusplus
  74. }
  75. #endif
  76. _GL_INLINE_HEADER_END
  77. #endif