time.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* Copyright (C) 1991-1999,2000,2001,2002,2003 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. /* 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. #ifdef __UCLIBC_HAS_FLOATS__
  145. /* Return the difference between TIME1 and TIME0. */
  146. extern double difftime (time_t __time1, time_t __time0)
  147. __THROW __attribute__ ((__const__));
  148. #endif /* __UCLIBC_HAS_FLOATS__ */
  149. /* Return the `time_t' representation of TP and normalize TP. */
  150. extern time_t mktime (struct tm *__tp) __THROW;
  151. /* Format TP into S according to FORMAT.
  152. Write no more than MAXSIZE characters and return the number
  153. of characters written, or 0 if it would exceed MAXSIZE. */
  154. extern size_t strftime (char *__restrict __s, size_t __maxsize,
  155. __const char *__restrict __format,
  156. __const struct tm *__restrict __tp) __THROW;
  157. __END_NAMESPACE_STD
  158. # ifdef __USE_XOPEN
  159. /* Parse S according to FORMAT and store binary time information in TP.
  160. The return value is a pointer to the first unparsed character in S. */
  161. extern char *strptime (__const char *__restrict __s,
  162. __const char *__restrict __fmt, struct tm *__tp)
  163. __THROW;
  164. # endif
  165. #ifdef __UCLIBC_HAS_XLOCALE__
  166. # ifdef __USE_GNU
  167. /* Similar to the two functions above but take the information from
  168. the provided locale and not the global locale. */
  169. # include <xlocale.h>
  170. extern size_t strftime_l (char *__restrict __s, size_t __maxsize,
  171. __const char *__restrict __format,
  172. __const struct tm *__restrict __tp,
  173. __locale_t __loc) __THROW;
  174. extern char *strptime_l (__const char *__restrict __s,
  175. __const char *__restrict __fmt, struct tm *__tp,
  176. __locale_t __loc) __THROW;
  177. extern size_t __strftime_l (char *__restrict __s, size_t __maxsize,
  178. __const char *__restrict __format,
  179. __const struct tm *__restrict __tp,
  180. __locale_t __loc) __THROW;
  181. extern char *__strptime_l (__const char *__restrict __s,
  182. __const char *__restrict __fmt, struct tm *__tp,
  183. __locale_t __loc) __THROW;
  184. # endif
  185. #endif
  186. __BEGIN_NAMESPACE_STD
  187. /* Return the `struct tm' representation of *TIMER
  188. in Universal Coordinated Time (aka Greenwich Mean Time). */
  189. extern struct tm *gmtime (__const time_t *__timer) __THROW;
  190. /* Return the `struct tm' representation
  191. of *TIMER in the local timezone. */
  192. extern struct tm *localtime (__const time_t *__timer) __THROW;
  193. __END_NAMESPACE_STD
  194. # if defined __USE_POSIX || defined __USE_MISC
  195. /* Return the `struct tm' representation of *TIMER in UTC,
  196. using *TP to store the result. */
  197. extern struct tm *gmtime_r (__const time_t *__restrict __timer,
  198. struct tm *__restrict __tp) __THROW;
  199. /* Return the `struct tm' representation of *TIMER in local time,
  200. using *TP to store the result. */
  201. extern struct tm *localtime_r (__const time_t *__restrict __timer,
  202. struct tm *__restrict __tp) __THROW;
  203. # endif /* POSIX or misc */
  204. __BEGIN_NAMESPACE_STD
  205. /* Return a string of the form "Day Mon dd hh:mm:ss yyyy\n"
  206. that is the representation of TP in this format. */
  207. extern char *asctime (__const struct tm *__tp) __THROW;
  208. /* Equivalent to `asctime (localtime (timer))'. */
  209. extern char *ctime (__const time_t *__timer) __THROW;
  210. __END_NAMESPACE_STD
  211. # if defined __USE_POSIX || defined __USE_MISC
  212. /* Reentrant versions of the above functions. */
  213. /* Return in BUF a string of the form "Day Mon dd hh:mm:ss yyyy\n"
  214. that is the representation of TP in this format. */
  215. extern char *asctime_r (__const struct tm *__restrict __tp,
  216. char *__restrict __buf) __THROW;
  217. /* Equivalent to `asctime_r (localtime_r (timer, *TMP*), buf)'. */
  218. extern char *ctime_r (__const time_t *__restrict __timer,
  219. char *__restrict __buf) __THROW;
  220. # endif /* POSIX or misc */
  221. /* Defined in localtime.c. */
  222. extern char *__tzname[2]; /* Current timezone names. */
  223. extern int __daylight; /* If daylight-saving time is ever in use. */
  224. extern long int __timezone; /* Seconds west of UTC. */
  225. # ifdef __USE_POSIX
  226. /* Same as above. */
  227. extern char *tzname[2];
  228. /* Set time conversion information from the TZ environment variable.
  229. If TZ is not defined, a locale-dependent default is used. */
  230. extern void tzset (void) __THROW;
  231. # endif
  232. # if defined __USE_SVID || defined __USE_XOPEN
  233. extern int daylight;
  234. extern long int timezone;
  235. # endif
  236. # ifdef __USE_SVID
  237. /* Set the system time to *WHEN.
  238. This call is restricted to the superuser. */
  239. extern int stime (__const time_t *__when) __THROW;
  240. # endif
  241. /* Nonzero if YEAR is a leap year (every 4 years,
  242. except every 100th isn't, and every 400th is). */
  243. # define __isleap(year) \
  244. ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
  245. # ifdef __USE_MISC
  246. /* Miscellaneous functions many Unices inherited from the public domain
  247. localtime package. These are included only for compatibility. */
  248. /* Like `mktime', but for TP represents Universal Time, not local time. */
  249. extern time_t timegm (struct tm *__tp) __THROW;
  250. /* Another name for `mktime'. */
  251. extern time_t timelocal (struct tm *__tp) __THROW;
  252. /* Return the number of days in YEAR. */
  253. extern int dysize (int __year) __THROW __attribute__ ((__const__));
  254. # endif
  255. # ifdef __USE_POSIX199309
  256. /* Pause execution for a number of nanoseconds.
  257. This function is a cancellation point and therefore not marked with
  258. __THROW. */
  259. extern int nanosleep (__const struct timespec *__requested_time,
  260. struct timespec *__remaining);
  261. /* Get resolution of clock CLOCK_ID. */
  262. extern int clock_getres (clockid_t __clock_id, struct timespec *__res) __THROW;
  263. /* Get current value of clock CLOCK_ID and store it in TP. */
  264. extern int clock_gettime (clockid_t __clock_id, struct timespec *__tp) __THROW;
  265. /* Set clock CLOCK_ID to value TP. */
  266. extern int clock_settime (clockid_t __clock_id, __const struct timespec *__tp)
  267. __THROW;
  268. # ifdef __USE_XOPEN2K
  269. /* High-resolution sleep with the specified clock.
  270. This function is a cancellation point and therefore not marked with
  271. __THROW. */
  272. extern int clock_nanosleep (clockid_t __clock_id, int __flags,
  273. __const struct timespec *__req,
  274. struct timespec *__rem);
  275. /* Return clock ID for CPU-time clock. */
  276. extern int clock_getcpuclockid (pid_t __pid, clockid_t *__clock_id) __THROW;
  277. # endif
  278. /* Create new per-process timer using CLOCK_ID. */
  279. extern int timer_create (clockid_t __clock_id,
  280. struct sigevent *__restrict __evp,
  281. timer_t *__restrict __timerid) __THROW;
  282. /* Delete timer TIMERID. */
  283. extern int timer_delete (timer_t __timerid) __THROW;
  284. /* Set timer TIMERID to VALUE, returning old value in OVLAUE. */
  285. extern int timer_settime (timer_t __timerid, int __flags,
  286. __const struct itimerspec *__restrict __value,
  287. struct itimerspec *__restrict __ovalue) __THROW;
  288. /* Get current value of timer TIMERID and store it in VLAUE. */
  289. extern int timer_gettime (timer_t __timerid, struct itimerspec *__value)
  290. __THROW;
  291. /* Get expiration overrun for timer TIMERID. */
  292. extern int timer_getoverrun (timer_t __timerid) __THROW;
  293. # endif
  294. # ifdef __USE_XOPEN_EXTENDED
  295. /* Set to one of the following values to indicate an error.
  296. 1 the DATEMSK environment variable is null or undefined,
  297. 2 the template file cannot be opened for reading,
  298. 3 failed to get file status information,
  299. 4 the template file is not a regular file,
  300. 5 an error is encountered while reading the template file,
  301. 6 memory allication failed (not enough memory available),
  302. 7 there is no line in the template that matches the input,
  303. 8 invalid input specification Example: February 31 or a time is
  304. specified that can not be represented in a time_t (representing
  305. the time in seconds since 00:00:00 UTC, January 1, 1970) */
  306. extern int getdate_err;
  307. /* Parse the given string as a date specification and return a value
  308. representing the value. The templates from the file identified by
  309. the environment variable DATEMSK are used. In case of an error
  310. `getdate_err' is set.
  311. This function is a possible cancellation points and therefore not
  312. marked with __THROW. */
  313. extern struct tm *getdate (__const char *__string);
  314. # endif
  315. # ifdef __USE_GNU
  316. /* Since `getdate' is not reentrant because of the use of `getdate_err'
  317. and the static buffer to return the result in, we provide a thread-safe
  318. variant. The functionality is the same. The result is returned in
  319. the buffer pointed to by RESBUFP and in case of an error the return
  320. value is != 0 with the same values as given above for `getdate_err'.
  321. This function is not part of POSIX and therefore no official
  322. cancellation point. But due to similarity with an POSIX interface
  323. or due to the implementation it is a cancellation point and
  324. therefore not marked with __THROW. */
  325. extern int getdate_r (__const char *__restrict __string,
  326. struct tm *__restrict __resbufp);
  327. # endif
  328. __END_DECLS
  329. #endif /* <time.h> included. */
  330. #endif /* <time.h> not already included. */