time.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <sys/cdefs.h>
  8. #include <sys/types.h>
  9. #include <time.h>
  10. __BEGIN_DECLS
  11. struct timeval {
  12. time_t tv_sec;
  13. suseconds_t tv_usec;
  14. };
  15. struct timezone {
  16. int tz_minuteswest;
  17. int tz_dsttime;
  18. };
  19. int adjtime(const struct timeval* delta, struct timeval* old_delta);
  20. int gettimeofday(struct timeval* __restrict__, void* __restrict__) __attribute__((nonnull(1)));
  21. int settimeofday(struct timeval* __restrict__, void* __restrict__) __attribute__((nonnull(1)));
  22. int utimes(const char* pathname, const struct timeval[2]);
  23. static inline void timeradd(const struct timeval* a, const struct timeval* b, struct timeval* out)
  24. {
  25. out->tv_sec = a->tv_sec + b->tv_sec;
  26. out->tv_usec = a->tv_usec + b->tv_usec;
  27. if (out->tv_usec >= 1000 * 1000) {
  28. out->tv_sec++;
  29. out->tv_usec -= 1000 * 1000;
  30. }
  31. }
  32. static inline void timersub(const struct timeval* a, const struct timeval* b, struct timeval* out)
  33. {
  34. out->tv_sec = a->tv_sec - b->tv_sec;
  35. out->tv_usec = a->tv_usec - b->tv_usec;
  36. if (out->tv_usec < 0) {
  37. out->tv_sec--;
  38. out->tv_usec += 1000 * 1000;
  39. }
  40. }
  41. static inline void timerclear(struct timeval* out)
  42. {
  43. out->tv_sec = out->tv_usec = 0;
  44. }
  45. static inline int timerisset(const struct timeval* tv)
  46. {
  47. return tv->tv_sec || tv->tv_usec;
  48. }
  49. #define timeradd timeradd
  50. #define timersub timersub
  51. #define timerclear timerclear
  52. #define timerisset timerisset
  53. #define timercmp(tvp, uvp, cmp) \
  54. (((tvp)->tv_sec == (uvp)->tv_sec) ? ((tvp)->tv_usec cmp(uvp)->tv_usec) : ((tvp)->tv_sec cmp(uvp)->tv_sec))
  55. static inline void timespecadd(const struct timespec* a, const struct timespec* b, struct timespec* out)
  56. {
  57. out->tv_sec = a->tv_sec + b->tv_sec;
  58. out->tv_nsec = a->tv_nsec + b->tv_nsec;
  59. if (out->tv_nsec >= 1000 * 1000 * 1000) {
  60. out->tv_sec++;
  61. out->tv_nsec -= 1000 * 1000 * 1000;
  62. }
  63. }
  64. static inline void timespecsub(const struct timespec* a, const struct timespec* b, struct timespec* out)
  65. {
  66. out->tv_sec = a->tv_sec - b->tv_sec;
  67. out->tv_nsec = a->tv_nsec - b->tv_nsec;
  68. if (out->tv_nsec < 0) {
  69. out->tv_sec--;
  70. out->tv_nsec += 1000 * 1000 * 1000;
  71. }
  72. }
  73. static inline void timespecclear(struct timespec* out)
  74. {
  75. out->tv_sec = out->tv_nsec = 0;
  76. }
  77. static inline int timespecisset(const struct timespec* ts)
  78. {
  79. return ts->tv_sec || ts->tv_nsec;
  80. }
  81. static inline void TIMEVAL_TO_TIMESPEC(const struct timeval* tv, struct timespec* ts)
  82. {
  83. ts->tv_sec = tv->tv_sec;
  84. ts->tv_nsec = tv->tv_usec * 1000;
  85. }
  86. static inline void TIMESPEC_TO_TIMEVAL(struct timeval* tv, const struct timespec* ts)
  87. {
  88. tv->tv_sec = ts->tv_sec;
  89. tv->tv_usec = ts->tv_nsec / 1000;
  90. }
  91. #define timespecadd timespecadd
  92. #define timespecsub timespecsub
  93. #define timespecclear timespecclear
  94. #define timespecisset timespecisset
  95. #define timespeccmp(ts, us, cmp) \
  96. (((ts)->tv_sec == (us)->tv_sec) ? ((ts)->tv_nsec cmp(us)->tv_nsec) : ((ts)->tv_sec cmp(us)->tv_sec))
  97. __END_DECLS