time.h 2.9 KB

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