time.h 14 KB

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