time.h 6.7 KB

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