time.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* Copyright (C) 1991-1994,1996-2003,2005,2006,2009
  2. Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifndef _SYS_TIME_H
  17. #define _SYS_TIME_H 1
  18. #include <features.h>
  19. #include <bits/types.h>
  20. #define __need_time_t
  21. #include <time.h>
  22. #define __need_timeval
  23. #include <bits/time.h>
  24. #include <sys/select.h>
  25. #ifndef __suseconds_t_defined
  26. typedef __suseconds_t suseconds_t;
  27. # define __suseconds_t_defined
  28. #endif
  29. __BEGIN_DECLS
  30. #ifdef __USE_GNU
  31. /* Macros for converting between `struct timeval' and `struct timespec'. */
  32. # define TIMEVAL_TO_TIMESPEC(tv, ts) { \
  33. (ts)->tv_sec = (tv)->tv_sec; \
  34. (ts)->tv_nsec = (tv)->tv_usec * 1000; \
  35. }
  36. # define TIMESPEC_TO_TIMEVAL(tv, ts) { \
  37. (tv)->tv_sec = (ts)->tv_sec; \
  38. (tv)->tv_usec = (ts)->tv_nsec / 1000; \
  39. }
  40. #endif
  41. #ifdef __USE_BSD
  42. /* Structure crudely representing a timezone.
  43. This is obsolete and should never be used. */
  44. struct timezone
  45. {
  46. int tz_minuteswest; /* Minutes west of GMT. */
  47. int tz_dsttime; /* Nonzero if DST is ever in effect. */
  48. };
  49. typedef struct timezone *__restrict __timezone_ptr_t;
  50. #else
  51. typedef void *__restrict __timezone_ptr_t;
  52. #endif
  53. /* Get the current time of day and timezone information,
  54. putting it into *TV and *TZ. If TZ is NULL, *TZ is not filled.
  55. Returns 0 on success, -1 on errors.
  56. NOTE: This form of timezone information is obsolete.
  57. Use the functions and variables declared in <time.h> instead. */
  58. extern int gettimeofday (struct timeval *__restrict __tv,
  59. __timezone_ptr_t __tz) __THROW __nonnull ((1));
  60. libc_hidden_proto(gettimeofday)
  61. #ifdef __USE_BSD
  62. /* Set the current time of day and timezone information.
  63. This call is restricted to the super-user. */
  64. extern int settimeofday (__const struct timeval *__tv,
  65. __const struct timezone *__tz)
  66. __THROW __nonnull ((1));
  67. libc_hidden_proto(settimeofday)
  68. /* Adjust the current time of day by the amount in DELTA.
  69. If OLDDELTA is not NULL, it is filled in with the amount
  70. of time adjustment remaining to be done from the last `adjtime' call.
  71. This call is restricted to the super-user. */
  72. extern int adjtime (__const struct timeval *__delta,
  73. struct timeval *__olddelta) __THROW;
  74. #endif
  75. /* Values for the first argument to `getitimer' and `setitimer'. */
  76. enum __itimer_which
  77. {
  78. /* Timers run in real time. */
  79. ITIMER_REAL = 0,
  80. #define ITIMER_REAL ITIMER_REAL
  81. /* Timers run only when the process is executing. */
  82. ITIMER_VIRTUAL = 1,
  83. #define ITIMER_VIRTUAL ITIMER_VIRTUAL
  84. /* Timers run when the process is executing and when
  85. the system is executing on behalf of the process. */
  86. ITIMER_PROF = 2
  87. #define ITIMER_PROF ITIMER_PROF
  88. };
  89. /* Type of the second argument to `getitimer' and
  90. the second and third arguments `setitimer'. */
  91. struct itimerval
  92. {
  93. /* Value to put into `it_value' when the timer expires. */
  94. struct timeval it_interval;
  95. /* Time to the next timer expiration. */
  96. struct timeval it_value;
  97. };
  98. #if defined __USE_GNU && !defined __cplusplus
  99. /* Use the nicer parameter type only in GNU mode and not for C++ since the
  100. strict C++ rules prevent the automatic promotion. */
  101. typedef enum __itimer_which __itimer_which_t;
  102. #else
  103. typedef int __itimer_which_t;
  104. #endif
  105. /* Set *VALUE to the current setting of timer WHICH.
  106. Return 0 on success, -1 on errors. */
  107. extern int getitimer (__itimer_which_t __which,
  108. struct itimerval *__value) __THROW;
  109. /* Set the timer WHICH to *NEW. If OLD is not NULL,
  110. set *OLD to the old value of timer WHICH.
  111. Returns 0 on success, -1 on errors. */
  112. extern int setitimer (__itimer_which_t __which,
  113. __const struct itimerval *__restrict __new,
  114. struct itimerval *__restrict __old) __THROW;
  115. libc_hidden_proto(setitimer)
  116. /* Change the access time of FILE to TVP[0] and the modification time of
  117. FILE to TVP[1]. If TVP is a null pointer, use the current time instead.
  118. Returns 0 on success, -1 on errors. */
  119. extern int utimes (__const char *__file, __const struct timeval __tvp[2])
  120. __THROW __nonnull ((1));
  121. libc_hidden_proto(utimes)
  122. #if 0 /*def __USE_BSD*/
  123. /* Same as `utimes', but does not follow symbolic links. */
  124. extern int lutimes (__const char *__file, __const struct timeval __tvp[2])
  125. __THROW __nonnull ((1));
  126. /* Same as `utimes', but takes an open file descriptor instead of a name. */
  127. extern int futimes (int __fd, __const struct timeval __tvp[2]) __THROW;
  128. #endif
  129. #ifdef __USE_GNU
  130. /* Change the access time of FILE relative to FD to TVP[0] and the
  131. modification time of FILE to TVP[1]. If TVP is a null pointer, use
  132. the current time instead. Returns 0 on success, -1 on errors. */
  133. extern int futimesat (int __fd, __const char *__file,
  134. __const struct timeval __tvp[2]) __THROW;
  135. #endif
  136. #ifdef __USE_BSD
  137. /* Convenience macros for operations on timevals.
  138. NOTE: `timercmp' does not work for >= or <=. */
  139. # define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
  140. # define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
  141. # define timercmp(a, b, CMP) \
  142. (((a)->tv_sec == (b)->tv_sec) ? \
  143. ((a)->tv_usec CMP (b)->tv_usec) : \
  144. ((a)->tv_sec CMP (b)->tv_sec))
  145. # define timeradd(a, b, result) \
  146. do { \
  147. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  148. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  149. if ((result)->tv_usec >= 1000000) \
  150. { \
  151. ++(result)->tv_sec; \
  152. (result)->tv_usec -= 1000000; \
  153. } \
  154. } while (0)
  155. # define timersub(a, b, result) \
  156. do { \
  157. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  158. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  159. if ((result)->tv_usec < 0) { \
  160. --(result)->tv_sec; \
  161. (result)->tv_usec += 1000000; \
  162. } \
  163. } while (0)
  164. #endif /* BSD */
  165. __END_DECLS
  166. #endif /* sys/time.h */