time.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* Copyright (C) 1991-2003,2006,2009 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. #if (! defined __need_time_t && !defined __need_clock_t && \
  19. ! defined __need_timespec)
  20. # define _TIME_H 1
  21. # include <features.h>
  22. __BEGIN_DECLS
  23. #endif
  24. #ifdef _TIME_H
  25. /* Get size_t and NULL from <stddef.h>. */
  26. # define __need_size_t
  27. # define __need_NULL
  28. # include <stddef.h>
  29. /* This defines CLOCKS_PER_SEC, which is the number of processor clock
  30. ticks per second. */
  31. # include <bits/time.h>
  32. /* This is the obsolete POSIX.1-1988 name for the same constant. */
  33. # if !defined __STRICT_ANSI__ && !defined __USE_XOPEN2K
  34. # ifndef CLK_TCK
  35. # define CLK_TCK CLOCKS_PER_SEC
  36. # endif
  37. # endif
  38. #endif /* <time.h> included. */
  39. #if !defined __clock_t_defined && (defined _TIME_H || defined __need_clock_t)
  40. # define __clock_t_defined 1
  41. # include <bits/types.h>
  42. __BEGIN_NAMESPACE_STD
  43. /* Returned by `clock'. */
  44. typedef __clock_t clock_t;
  45. __END_NAMESPACE_STD
  46. #if defined __USE_XOPEN || defined __USE_POSIX || defined __USE_MISC
  47. __USING_NAMESPACE_STD(clock_t)
  48. #endif
  49. #endif /* clock_t not defined and <time.h> or need clock_t. */
  50. #undef __need_clock_t
  51. #if !defined __time_t_defined && (defined _TIME_H || defined __need_time_t)
  52. # define __time_t_defined 1
  53. # include <bits/types.h>
  54. __BEGIN_NAMESPACE_STD
  55. /* Returned by `time'. */
  56. typedef __time_t time_t;
  57. __END_NAMESPACE_STD
  58. #if defined __USE_POSIX || defined __USE_MISC || defined __USE_SVID
  59. __USING_NAMESPACE_STD(time_t)
  60. #endif
  61. #endif /* time_t not defined and <time.h> or need time_t. */
  62. #undef __need_time_t
  63. #if !defined __clockid_t_defined && \
  64. ((defined _TIME_H && defined __USE_POSIX199309) || defined __need_clockid_t)
  65. # define __clockid_t_defined 1
  66. # include <bits/types.h>
  67. /* Clock ID used in clock and timer functions. */
  68. typedef __clockid_t clockid_t;
  69. #endif /* clockid_t not defined and <time.h> or need clockid_t. */
  70. #undef __clockid_time_t
  71. #if !defined __timer_t_defined && \
  72. ((defined _TIME_H && defined __USE_POSIX199309) || defined __need_timer_t)
  73. # define __timer_t_defined 1
  74. # include <bits/types.h>
  75. /* Timer ID returned by `timer_create'. */
  76. typedef __timer_t timer_t;
  77. #endif /* timer_t not defined and <time.h> or need timer_t. */
  78. #undef __need_timer_t
  79. #if !defined __timespec_defined && \
  80. ((defined _TIME_H && \
  81. (defined __USE_POSIX199309 || defined __USE_MISC)) || \
  82. defined __need_timespec)
  83. # define __timespec_defined 1
  84. # include <bits/types.h> /* This defines __time_t for us. */
  85. /* POSIX.1b structure for a time value. This is like a `struct timeval' but
  86. has nanoseconds instead of microseconds. */
  87. struct timespec
  88. {
  89. __time_t tv_sec; /* Seconds. */
  90. long int tv_nsec; /* Nanoseconds. */
  91. };
  92. #endif /* timespec not defined and <time.h> or need timespec. */
  93. #undef __need_timespec
  94. #ifdef _TIME_H
  95. __BEGIN_NAMESPACE_STD
  96. /* Used by other time functions. */
  97. struct tm
  98. {
  99. int tm_sec; /* Seconds. [0-60] (1 leap second) */
  100. int tm_min; /* Minutes. [0-59] */
  101. int tm_hour; /* Hours. [0-23] */
  102. int tm_mday; /* Day. [1-31] */
  103. int tm_mon; /* Month. [0-11] */
  104. int tm_year; /* Year - 1900. */
  105. int tm_wday; /* Day of week. [0-6] */
  106. int tm_yday; /* Days in year.[0-365] */
  107. int tm_isdst; /* DST. [-1/0/1]*/
  108. #ifdef __UCLIBC_HAS_TM_EXTENSIONS__
  109. #ifdef __USE_BSD
  110. long int tm_gmtoff; /* Seconds east of UTC. */
  111. const char *tm_zone; /* Timezone abbreviation. */
  112. #else
  113. long int __tm_gmtoff; /* Seconds east of UTC. */
  114. const char *__tm_zone; /* Timezone abbreviation. */
  115. #endif
  116. #endif /* __UCLIBC_HAS_TM_EXTENSIONS__ */
  117. };
  118. __END_NAMESPACE_STD
  119. #if defined __USE_XOPEN || defined __USE_POSIX || defined __USE_MISC
  120. __USING_NAMESPACE_STD(tm)
  121. #endif
  122. #ifdef __USE_POSIX199309
  123. /* POSIX.1b structure for timer start values and intervals. */
  124. struct itimerspec
  125. {
  126. struct timespec it_interval;
  127. struct timespec it_value;
  128. };
  129. /* We can use a simple forward declaration. */
  130. struct sigevent;
  131. #endif /* POSIX.1b */
  132. #ifdef __USE_XOPEN2K
  133. # ifndef __pid_t_defined
  134. typedef __pid_t pid_t;
  135. # define __pid_t_defined
  136. # endif
  137. #endif
  138. __BEGIN_NAMESPACE_STD
  139. /* Time used by the program so far (user time + system time).
  140. The result / CLOCKS_PER_SECOND is program time in seconds. */
  141. extern clock_t clock (void) __THROW;
  142. /* Return the current time and put it in *TIMER if TIMER is not NULL. */
  143. extern time_t time (time_t *__timer) __THROW;
  144. libc_hidden_proto(time)
  145. #ifdef __UCLIBC_HAS_FLOATS__
  146. /* Return the difference between TIME1 and TIME0. */
  147. extern double difftime (time_t __time1, time_t __time0)
  148. __THROW __attribute__ ((__const__));
  149. #endif /* __UCLIBC_HAS_FLOATS__ */
  150. #ifdef _LIBC
  151. # define CLOCK_IDFIELD_SIZE 3
  152. #endif
  153. /* Return the `time_t' representation of TP and normalize TP. */
  154. extern time_t mktime (struct tm *__tp) __THROW;
  155. /* Format TP into S according to FORMAT.
  156. Write no more than MAXSIZE characters and return the number
  157. of characters written, or 0 if it would exceed MAXSIZE. */
  158. extern size_t strftime (char *__restrict __s, size_t __maxsize,
  159. const char *__restrict __format,
  160. const struct tm *__restrict __tp) __THROW;
  161. __END_NAMESPACE_STD
  162. # ifdef __USE_XOPEN
  163. /* Parse S according to FORMAT and store binary time information in TP.
  164. The return value is a pointer to the first unparsed character in S. */
  165. extern char *strptime (const char *__restrict __s,
  166. const char *__restrict __fmt, struct tm *__tp)
  167. __THROW;
  168. # endif
  169. #ifdef __UCLIBC_HAS_XLOCALE__
  170. # ifdef __USE_XOPEN2K8
  171. /* Similar to the two functions above but take the information from
  172. the provided locale and not the global locale. */
  173. # include <xlocale.h>
  174. extern size_t strftime_l (char *__restrict __s, size_t __maxsize,
  175. const char *__restrict __format,
  176. const struct tm *__restrict __tp,
  177. __locale_t __loc) __THROW;
  178. libc_hidden_proto(strftime_l)
  179. # endif
  180. # ifdef __USE_GNU
  181. extern char *strptime_l (const char *__restrict __s,
  182. const char *__restrict __fmt, struct tm *__tp,
  183. __locale_t __loc) __THROW;
  184. libc_hidden_proto(strptime_l)
  185. # endif
  186. #endif
  187. __BEGIN_NAMESPACE_STD
  188. /* Return the `struct tm' representation of *TIMER
  189. in Universal Coordinated Time (aka Greenwich Mean Time). */
  190. extern struct tm *gmtime (const time_t *__timer) __THROW;
  191. /* Return the `struct tm' representation
  192. of *TIMER in the local timezone. */
  193. extern struct tm *localtime (const time_t *__timer) __THROW;
  194. libc_hidden_proto(localtime)
  195. __END_NAMESPACE_STD
  196. # if defined __USE_POSIX || defined __USE_MISC
  197. /* Return the `struct tm' representation of *TIMER in UTC,
  198. using *TP to store the result. */
  199. extern struct tm *gmtime_r (const time_t *__restrict __timer,
  200. struct tm *__restrict __tp) __THROW;
  201. /* Return the `struct tm' representation of *TIMER in local time,
  202. using *TP to store the result. */
  203. extern struct tm *localtime_r (const time_t *__restrict __timer,
  204. struct tm *__restrict __tp) __THROW;
  205. libc_hidden_proto(localtime_r)
  206. # endif /* POSIX or misc */
  207. __BEGIN_NAMESPACE_STD
  208. /* Return a string of the form "Day Mon dd hh:mm:ss yyyy\n"
  209. that is the representation of TP in this format. */
  210. extern char *asctime (const struct tm *__tp) __THROW;
  211. libc_hidden_proto(asctime)
  212. /* Equivalent to `asctime (localtime (timer))'. */
  213. extern char *ctime (const time_t *__timer) __THROW;
  214. libc_hidden_proto(ctime)
  215. __END_NAMESPACE_STD
  216. # if defined __USE_POSIX || defined __USE_MISC
  217. /* Reentrant versions of the above functions. */
  218. /* Return in BUF a string of the form "Day Mon dd hh:mm:ss yyyy\n"
  219. that is the representation of TP in this format. */
  220. extern char *asctime_r (const struct tm *__restrict __tp,
  221. char *__restrict __buf) __THROW;
  222. libc_hidden_proto(asctime_r)
  223. /* Equivalent to `asctime_r (localtime_r (timer, *TMP*), buf)'. */
  224. extern char *ctime_r (const time_t *__restrict __timer,
  225. char *__restrict __buf) __THROW;
  226. # endif /* POSIX or misc */
  227. # ifdef __USE_POSIX
  228. /* Same as above. */
  229. extern char *tzname[2];
  230. /* Set time conversion information from the TZ environment variable.
  231. If TZ is not defined, a locale-dependent default is used. */
  232. extern void tzset (void) __THROW;
  233. libc_hidden_proto(tzset)
  234. # endif
  235. # if defined __USE_SVID || defined __USE_XOPEN
  236. extern int daylight;
  237. extern long int timezone;
  238. # endif
  239. # ifdef __USE_SVID
  240. /* Set the system time to *WHEN.
  241. This call is restricted to the superuser. */
  242. extern int stime (const time_t *__when) __THROW;
  243. libc_hidden_proto(stime)
  244. # endif
  245. /* Nonzero if YEAR is a leap year (every 4 years,
  246. except every 100th isn't, and every 400th is). */
  247. # define __isleap(year) \
  248. ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
  249. # ifdef __USE_MISC
  250. /* Miscellaneous functions many Unices inherited from the public domain
  251. localtime package. These are included only for compatibility. */
  252. /* Like `mktime', but for TP represents Universal Time, not local time. */
  253. extern time_t timegm (struct tm *__tp) __THROW;
  254. /* Another name for `mktime'. */
  255. extern time_t timelocal (struct tm *__tp) __THROW;
  256. /* Return the number of days in YEAR. */
  257. extern int dysize (int __year) __THROW __attribute__ ((__const__));
  258. # endif
  259. # ifdef __USE_POSIX199309
  260. # if defined __UCLIBC_HAS_REALTIME__
  261. /* Pause execution for a number of nanoseconds.
  262. This function is a cancellation point and therefore not marked with
  263. __THROW. */
  264. extern int nanosleep (const struct timespec *__requested_time,
  265. struct timespec *__remaining);
  266. libc_hidden_proto(nanosleep)
  267. /* Get resolution of clock CLOCK_ID. */
  268. extern int clock_getres (clockid_t __clock_id, struct timespec *__res) __THROW;
  269. libc_hidden_proto(clock_getres)
  270. /* Get current value of clock CLOCK_ID and store it in TP. */
  271. extern int clock_gettime (clockid_t __clock_id, struct timespec *__tp) __THROW;
  272. /* Set clock CLOCK_ID to value TP. */
  273. extern int clock_settime (clockid_t __clock_id, const struct timespec *__tp)
  274. __THROW;
  275. # endif /* __UCLIBC_HAS_REALTIME__ */
  276. # if defined __USE_XOPEN2K && defined __UCLIBC_HAS_ADVANCED_REALTIME__
  277. # ifdef __UCLIBC_HAS_THREADS_NATIVE__
  278. /* High-resolution sleep with the specified clock.
  279. This function is a cancellation point and therefore not marked with
  280. __THROW. */
  281. extern int clock_nanosleep (clockid_t __clock_id, int __flags,
  282. const struct timespec *__req,
  283. struct timespec *__rem);
  284. /* Return clock ID for CPU-time clock. */
  285. extern int clock_getcpuclockid (pid_t __pid, clockid_t *__clock_id) __THROW;
  286. # endif /* __UCLIBC_HAS_THREADS_NATIVE__ */
  287. # endif
  288. # if defined __UCLIBC_HAS_REALTIME__
  289. /* Create new per-process timer using CLOCK_ID. */
  290. extern int timer_create (clockid_t __clock_id,
  291. struct sigevent *__restrict __evp,
  292. timer_t *__restrict __timerid) __THROW;
  293. /* Delete timer TIMERID. */
  294. extern int timer_delete (timer_t __timerid) __THROW;
  295. /* Set timer TIMERID to VALUE, returning old value in OVLAUE. */
  296. extern int timer_settime (timer_t __timerid, int __flags,
  297. const struct itimerspec *__restrict __value,
  298. struct itimerspec *__restrict __ovalue) __THROW;
  299. /* Get current value of timer TIMERID and store it in VLAUE. */
  300. extern int timer_gettime (timer_t __timerid, struct itimerspec *__value)
  301. __THROW;
  302. /* Get expiration overrun for timer TIMERID. */
  303. extern int timer_getoverrun (timer_t __timerid) __THROW;
  304. # endif /* __UCLIBC_HAS_REALTIME__ */
  305. # endif /* __USE_POSIX199309 */
  306. __END_DECLS
  307. #endif /* <time.h> included. */
  308. #endif /* <time.h> not already included. */