time.h 2.9 KB

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