time.h 10 KB

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