1
0

time.h 8.9 KB

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