time.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* Copyright (C) 1991-2020 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. /*
  15. * ISO C99 Standard: 7.23 Date and time <time.h>
  16. */
  17. #ifndef _TIME_H
  18. #define _TIME_H 1
  19. #include <features.h>
  20. #define __need_size_t
  21. #define __need_NULL
  22. #include <stddef.h>
  23. /* This defines CLOCKS_PER_SEC, which is the number of processor clock
  24. ticks per second, and possibly a number of other constants. */
  25. #include <bits/time.h>
  26. /* Many of the typedefs and structs whose official home is this header
  27. may also need to be defined by other headers. */
  28. #include <bits/types/clock_t.h>
  29. #include <bits/types/time_t.h>
  30. #include <bits/types/struct_tm.h>
  31. #if defined __USE_POSIX199309 || defined __USE_ISOC11
  32. # include <bits/types/struct_timespec.h>
  33. #endif
  34. #ifdef __USE_POSIX199309
  35. # include <bits/types/clockid_t.h>
  36. # include <bits/types/timer_t.h>
  37. # include <bits/types/struct_itimerspec.h>
  38. struct sigevent;
  39. #endif
  40. #ifdef __USE_XOPEN2K
  41. # ifndef __pid_t_defined
  42. typedef __pid_t pid_t;
  43. # define __pid_t_defined
  44. # endif
  45. #endif
  46. #ifdef __USE_XOPEN2K8
  47. # include <bits/types/locale_t.h>
  48. #endif
  49. #ifdef __USE_ISOC11
  50. /* Time base values for timespec_get. */
  51. # define TIME_UTC 1
  52. #endif
  53. __BEGIN_DECLS
  54. /* Time used by the program so far (user time + system time).
  55. The result / CLOCKS_PER_SEC is program time in seconds. */
  56. extern clock_t clock (void) __THROW;
  57. /* Return the current time and put it in *TIMER if TIMER is not NULL. */
  58. extern time_t time (time_t *__timer) __THROW;
  59. /* Return the difference between TIME1 and TIME0. */
  60. extern double difftime (time_t __time1, time_t __time0)
  61. __THROW __attribute__ ((__const__));
  62. /* Return the `time_t' representation of TP and normalize TP. */
  63. extern time_t mktime (struct tm *__tp) __THROW;
  64. /* Format TP into S according to FORMAT.
  65. Write no more than MAXSIZE characters and return the number
  66. of characters written, or 0 if it would exceed MAXSIZE. */
  67. extern size_t strftime (char *__restrict __s, size_t __maxsize,
  68. const char *__restrict __format,
  69. const struct tm *__restrict __tp) __THROW;
  70. #ifdef __USE_XOPEN
  71. /* Parse S according to FORMAT and store binary time information in TP.
  72. The return value is a pointer to the first unparsed character in S. */
  73. extern char *strptime (const char *__restrict __s,
  74. const char *__restrict __fmt, struct tm *__tp)
  75. __THROW;
  76. #endif
  77. #ifdef __USE_XOPEN2K8
  78. /* Similar to the two functions above but take the information from
  79. the provided locale and not the global locale. */
  80. extern size_t strftime_l (char *__restrict __s, size_t __maxsize,
  81. const char *__restrict __format,
  82. const struct tm *__restrict __tp,
  83. locale_t __loc) __THROW;
  84. #endif
  85. #ifdef __USE_GNU
  86. extern char *strptime_l (const char *__restrict __s,
  87. const char *__restrict __fmt, struct tm *__tp,
  88. locale_t __loc) __THROW;
  89. #endif
  90. /* Return the `struct tm' representation of *TIMER
  91. in Universal Coordinated Time (aka Greenwich Mean Time). */
  92. extern struct tm *gmtime (const time_t *__timer) __THROW;
  93. /* Return the `struct tm' representation
  94. of *TIMER in the local timezone. */
  95. extern struct tm *localtime (const time_t *__timer) __THROW;
  96. #if defined __USE_POSIX || __GLIBC_USE (ISOC2X)
  97. /* Return the `struct tm' representation of *TIMER in UTC,
  98. using *TP to store the result. */
  99. extern struct tm *gmtime_r (const time_t *__restrict __timer,
  100. struct tm *__restrict __tp) __THROW;
  101. /* Return the `struct tm' representation of *TIMER in local time,
  102. using *TP to store the result. */
  103. extern struct tm *localtime_r (const time_t *__restrict __timer,
  104. struct tm *__restrict __tp) __THROW;
  105. #endif /* POSIX || C2X */
  106. /* Return a string of the form "Day Mon dd hh:mm:ss yyyy\n"
  107. that is the representation of TP in this format. */
  108. extern char *asctime (const struct tm *__tp) __THROW;
  109. /* Equivalent to `asctime (localtime (timer))'. */
  110. extern char *ctime (const time_t *__timer) __THROW;
  111. #if defined __USE_POSIX || __GLIBC_USE (ISOC2X)
  112. /* Reentrant versions of the above functions. */
  113. /* Return in BUF a string of the form "Day Mon dd hh:mm:ss yyyy\n"
  114. that is the representation of TP in this format. */
  115. extern char *asctime_r (const struct tm *__restrict __tp,
  116. char *__restrict __buf) __THROW;
  117. /* Equivalent to `asctime_r (localtime_r (timer, *TMP*), buf)'. */
  118. extern char *ctime_r (const time_t *__restrict __timer,
  119. char *__restrict __buf) __THROW;
  120. #endif /* POSIX || C2X */
  121. /* Defined in localtime.c. */
  122. extern char *__tzname[2]; /* Current timezone names. */
  123. extern int __daylight; /* If daylight-saving time is ever in use. */
  124. extern long int __timezone; /* Seconds west of UTC. */
  125. #ifdef __USE_POSIX
  126. /* Same as above. */
  127. extern char *tzname[2];
  128. /* Set time conversion information from the TZ environment variable.
  129. If TZ is not defined, a locale-dependent default is used. */
  130. extern void tzset (void) __THROW;
  131. #endif
  132. #if defined __USE_MISC || defined __USE_XOPEN
  133. extern int daylight;
  134. extern long int timezone;
  135. #endif
  136. /* Nonzero if YEAR is a leap year (every 4 years,
  137. except every 100th isn't, and every 400th is). */
  138. #define __isleap(year) \
  139. ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
  140. #ifdef __USE_MISC
  141. /* Miscellaneous functions many Unices inherited from the public domain
  142. localtime package. These are included only for compatibility. */
  143. /* Like `mktime', but for TP represents Universal Time, not local time. */
  144. extern time_t timegm (struct tm *__tp) __THROW;
  145. /* Another name for `mktime'. */
  146. extern time_t timelocal (struct tm *__tp) __THROW;
  147. /* Return the number of days in YEAR. */
  148. extern int dysize (int __year) __THROW __attribute__ ((__const__));
  149. #endif
  150. #ifdef __USE_POSIX199309
  151. /* Pause execution for a number of nanoseconds.
  152. This function is a cancellation point and therefore not marked with
  153. __THROW. */
  154. extern int nanosleep (const struct timespec *__requested_time,
  155. struct timespec *__remaining);
  156. /* Get resolution of clock CLOCK_ID. */
  157. extern int clock_getres (clockid_t __clock_id, struct timespec *__res) __THROW;
  158. /* Get current value of clock CLOCK_ID and store it in TP. */
  159. extern int clock_gettime (clockid_t __clock_id, struct timespec *__tp) __THROW;
  160. /* Set clock CLOCK_ID to value TP. */
  161. extern int clock_settime (clockid_t __clock_id, const struct timespec *__tp)
  162. __THROW;
  163. # ifdef __USE_XOPEN2K
  164. /* High-resolution sleep with the specified clock.
  165. This function is a cancellation point and therefore not marked with
  166. __THROW. */
  167. extern int clock_nanosleep (clockid_t __clock_id, int __flags,
  168. const struct timespec *__req,
  169. struct timespec *__rem);
  170. /* Return clock ID for CPU-time clock. */
  171. extern int clock_getcpuclockid (pid_t __pid, clockid_t *__clock_id) __THROW;
  172. # endif
  173. /* Create new per-process timer using CLOCK_ID. */
  174. extern int timer_create (clockid_t __clock_id,
  175. struct sigevent *__restrict __evp,
  176. timer_t *__restrict __timerid) __THROW;
  177. /* Delete timer TIMERID. */
  178. extern int timer_delete (timer_t __timerid) __THROW;
  179. /* Set timer TIMERID to VALUE, returning old value in OVALUE. */
  180. extern int timer_settime (timer_t __timerid, int __flags,
  181. const struct itimerspec *__restrict __value,
  182. struct itimerspec *__restrict __ovalue) __THROW;
  183. /* Get current value of timer TIMERID and store it in VALUE. */
  184. extern int timer_gettime (timer_t __timerid, struct itimerspec *__value)
  185. __THROW;
  186. /* Get expiration overrun for timer TIMERID. */
  187. extern int timer_getoverrun (timer_t __timerid) __THROW;
  188. #endif
  189. #ifdef __USE_ISOC11
  190. /* Set TS to calendar time based in time base BASE. */
  191. extern int timespec_get (struct timespec *__ts, int __base)
  192. __THROW __nonnull ((1));
  193. #endif
  194. #ifdef __USE_XOPEN_EXTENDED
  195. /* Set to one of the following values to indicate an error.
  196. 1 the DATEMSK environment variable is null or undefined,
  197. 2 the template file cannot be opened for reading,
  198. 3 failed to get file status information,
  199. 4 the template file is not a regular file,
  200. 5 an error is encountered while reading the template file,
  201. 6 memory allication failed (not enough memory available),
  202. 7 there is no line in the template that matches the input,
  203. 8 invalid input specification Example: February 31 or a time is
  204. specified that can not be represented in a time_t (representing
  205. the time in seconds since 00:00:00 UTC, January 1, 1970) */
  206. extern int getdate_err;
  207. /* Parse the given string as a date specification and return a value
  208. representing the value. The templates from the file identified by
  209. the environment variable DATEMSK are used. In case of an error
  210. `getdate_err' is set.
  211. This function is a possible cancellation point and therefore not
  212. marked with __THROW. */
  213. extern struct tm *getdate (const char *__string);
  214. #endif
  215. #ifdef __USE_GNU
  216. /* Since `getdate' is not reentrant because of the use of `getdate_err'
  217. and the static buffer to return the result in, we provide a thread-safe
  218. variant. The functionality is the same. The result is returned in
  219. the buffer pointed to by RESBUFP and in case of an error the return
  220. value is != 0 with the same values as given above for `getdate_err'.
  221. This function is not part of POSIX and therefore no official
  222. cancellation point. But due to similarity with an POSIX interface
  223. or due to the implementation it is a cancellation point and
  224. therefore not marked with __THROW. */
  225. extern int getdate_r (const char *__restrict __string,
  226. struct tm *__restrict __resbufp);
  227. #endif
  228. __END_DECLS
  229. #endif /* time.h. */