time.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //
  2. // Copyright 2026 Aarav Ravindra Kharade
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  17. This file is part of the GNU C Library.
  18. The GNU C Library is free software; you can redistribute it and/or
  19. modify it under the terms of the GNU Lesser General Public
  20. License as published by the Free Software Foundation; either
  21. version 2.1 of the License, or (at your option) any later version.
  22. The GNU C Library is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. Lesser General Public License for more details.
  26. You should have received a copy of the GNU Lesser General Public
  27. License along with the GNU C Library; if not, see
  28. <https://www.gnu.org/licenses/>. */
  29. #ifndef _SYS_TIME_H
  30. #define _SYS_TIME_H 1
  31. #include <features.h>
  32. #include <bits/types.h>
  33. #include <bits/types/time_t.h>
  34. #include <bits/types/struct_timeval.h>
  35. #ifndef __suseconds_t_defined
  36. typedef __suseconds_t suseconds_t;
  37. # define __suseconds_t_defined
  38. #endif
  39. #include <sys/select.h>
  40. __BEGIN_DECLS
  41. #ifdef __USE_GNU
  42. /* Macros for converting between `struct timeval' and `struct timespec'. */
  43. # define TIMEVAL_TO_TIMESPEC(tv, ts) { \
  44. (ts)->tv_sec = (tv)->tv_sec; \
  45. (ts)->tv_nsec = (tv)->tv_usec * 1000; \
  46. }
  47. # define TIMESPEC_TO_TIMEVAL(tv, ts) { \
  48. (tv)->tv_sec = (ts)->tv_sec; \
  49. (tv)->tv_usec = (ts)->tv_nsec / 1000; \
  50. }
  51. #endif
  52. #ifdef __USE_MISC
  53. /* Structure crudely representing a timezone.
  54. This is obsolete and should never be used. */
  55. struct timezone
  56. {
  57. int tz_minuteswest; /* Minutes west of GMT. */
  58. int tz_dsttime; /* Nonzero if DST is ever in effect. */
  59. };
  60. #endif
  61. /* Get the current time of day, putting it into *TV.
  62. If TZ is not null, *TZ must be a struct timezone, and both fields
  63. will be set to zero.
  64. Calling this function with a non-null TZ is obsolete;
  65. use localtime etc. instead.
  66. This function itself is semi-obsolete;
  67. most callers should use time or clock_gettime instead. */
  68. #ifndef __USE_TIME64_REDIRECTS
  69. extern int gettimeofday (struct timeval *__restrict __tv,
  70. void *__restrict __tz) __THROW __nonnull ((1));
  71. #else
  72. # ifdef __REDIRECT_NTH
  73. extern int __REDIRECT_NTH (gettimeofday, (struct timeval *__restrict __tv,
  74. void *__restrict __tz),
  75. __gettimeofday64) __nonnull ((1));
  76. # else
  77. # define gettimeofday __gettimeofday64
  78. # endif
  79. #endif
  80. #ifdef __USE_MISC
  81. # ifndef __USE_TIME64_REDIRECTS
  82. /* Set the current time of day and timezone information.
  83. This call is restricted to the super-user.
  84. Setting the timezone in this way is obsolete, but we don't yet
  85. warn about it because it still has some uses for which there is
  86. no alternative. */
  87. extern int settimeofday (const struct timeval *__tv,
  88. const struct timezone *__tz)
  89. __THROW;
  90. /* Adjust the current time of day by the amount in DELTA.
  91. If OLDDELTA is not NULL, it is filled in with the amount
  92. of time adjustment remaining to be done from the last `adjtime' call.
  93. This call is restricted to the super-user. */
  94. extern int adjtime (const struct timeval *__delta,
  95. struct timeval *__olddelta) __THROW;
  96. # else
  97. # ifdef __REDIRECT_NTH
  98. extern int __REDIRECT_NTH (settimeofday, (const struct timeval *__tv,
  99. const struct timezone *__tz),
  100. __settimeofday64);
  101. extern int __REDIRECT_NTH (adjtime, (const struct timeval *__delta,
  102. struct timeval *__olddelta),
  103. __adjtime64);
  104. # else
  105. # define settimeofday __settimeofday64
  106. # define adjtime __adjtime64
  107. # endif
  108. # endif
  109. #endif
  110. /* Values for the first argument to `getitimer' and `setitimer'. */
  111. enum __itimer_which
  112. {
  113. /* Timers run in real time. */
  114. ITIMER_REAL = 0,
  115. #define ITIMER_REAL ITIMER_REAL
  116. /* Timers run only when the process is executing. */
  117. ITIMER_VIRTUAL = 1,
  118. #define ITIMER_VIRTUAL ITIMER_VIRTUAL
  119. /* Timers run when the process is executing and when
  120. the system is executing on behalf of the process. */
  121. ITIMER_PROF = 2
  122. #define ITIMER_PROF ITIMER_PROF
  123. };
  124. /* Type of the second argument to `getitimer' and
  125. the second and third arguments `setitimer'. */
  126. struct itimerval
  127. {
  128. /* Value to put into `it_value' when the timer expires. */
  129. struct timeval it_interval;
  130. /* Time to the next timer expiration. */
  131. struct timeval it_value;
  132. };
  133. #if defined __USE_GNU && !defined __cplusplus
  134. /* Use the nicer parameter type only in GNU mode and not for C++ since the
  135. strict C++ rules prevent the automatic promotion. */
  136. typedef enum __itimer_which __itimer_which_t;
  137. #else
  138. typedef int __itimer_which_t;
  139. #endif
  140. #ifndef __USE_TIME64_REDIRECTS
  141. /* Set *VALUE to the current setting of timer WHICH.
  142. Return 0 on success, -1 on errors. */
  143. extern int getitimer (__itimer_which_t __which,
  144. struct itimerval *__value) __THROW;
  145. /* Set the timer WHICH to *NEW. If OLD is not NULL,
  146. set *OLD to the old value of timer WHICH.
  147. Returns 0 on success, -1 on errors. */
  148. extern int setitimer (__itimer_which_t __which,
  149. const struct itimerval *__restrict __new,
  150. struct itimerval *__restrict __old) __THROW;
  151. /* Change the access time of FILE to TVP[0] and the modification time of
  152. FILE to TVP[1]. If TVP is a null pointer, use the current time instead.
  153. Returns 0 on success, -1 on errors. */
  154. extern int utimes (const char *__file, const struct timeval __tvp[2])
  155. __THROW __nonnull ((1));
  156. #else
  157. # ifdef __REDIRECT_NTH
  158. extern int __REDIRECT_NTH (getitimer, (__itimer_which_t __which,
  159. struct itimerval *__value),
  160. __getitimer64);
  161. extern int __REDIRECT_NTH (setitimer, (__itimer_which_t __which,
  162. const struct itimerval *__restrict __new,
  163. struct itimerval *__restrict __old),
  164. __setitimer64);
  165. extern int __REDIRECT_NTH (utimes, (const char *__file,
  166. const struct timeval __tvp[2]),
  167. __utimes64) __nonnull ((1));
  168. # else
  169. # define getitimer __getitimer64
  170. # define setitimer __setitimer64
  171. # define utimes __utimes64
  172. # endif
  173. #endif
  174. #ifdef __USE_MISC
  175. # ifndef __USE_TIME64_REDIRECTS
  176. /* Same as `utimes', but does not follow symbolic links. */
  177. extern int lutimes (const char *__file, const struct timeval __tvp[2])
  178. __THROW __nonnull ((1));
  179. /* Same as `utimes', but takes an open file descriptor instead of a name. */
  180. extern int futimes (int __fd, const struct timeval __tvp[2]) __THROW;
  181. # else
  182. # ifdef __REDIRECT_NTH
  183. extern int __REDIRECT_NTH (lutimes, (const char *__file,
  184. const struct timeval __tvp[2]),
  185. __lutimes64) __nonnull ((1));
  186. extern int __REDIRECT_NTH (futimes, (int __fd, const struct timeval __tvp[2]),
  187. __futimes64);
  188. # else
  189. # define lutimes __lutimes64
  190. # define futimes __futimes64
  191. # endif
  192. # endif
  193. #endif
  194. #ifdef __USE_GNU
  195. # ifndef __USE_TIME64_REDIRECTS
  196. /* Change the access time of FILE relative to FD to TVP[0] and the
  197. modification time of FILE to TVP[1]. If TVP is a null pointer, use
  198. the current time instead. Returns 0 on success, -1 on errors. */
  199. extern int futimesat (int __fd, const char *__file,
  200. const struct timeval __tvp[2]) __THROW;
  201. # else
  202. # ifdef __REDIRECT_NTH
  203. extern int __REDIRECT_NTH (futimesat, (int __fd, const char *__file,
  204. const struct timeval __tvp[2]),
  205. __futimesat64);
  206. # else
  207. # define futimesat __futimesat64
  208. # endif
  209. # endif
  210. #endif
  211. #ifdef __USE_MISC
  212. /* Convenience macros for operations on timevals.
  213. NOTE: `timercmp' does not work for >= or <=. */
  214. # define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
  215. # define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
  216. # define timercmp(a, b, CMP) \
  217. (((a)->tv_sec == (b)->tv_sec) \
  218. ? ((a)->tv_usec CMP (b)->tv_usec) \
  219. : ((a)->tv_sec CMP (b)->tv_sec))
  220. # define timeradd(a, b, result) \
  221. do { \
  222. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  223. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  224. if ((result)->tv_usec >= 1000000) \
  225. { \
  226. ++(result)->tv_sec; \
  227. (result)->tv_usec -= 1000000; \
  228. } \
  229. } while (0)
  230. # define timersub(a, b, result) \
  231. do { \
  232. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  233. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  234. if ((result)->tv_usec < 0) { \
  235. --(result)->tv_sec; \
  236. (result)->tv_usec += 1000000; \
  237. } \
  238. } while (0)
  239. #endif /* Misc. */
  240. __END_DECLS
  241. #endif /* sys/time.h */