time.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* Copyright (C) 1991, 92, 93, 94, 96, 97, 98 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 Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. 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. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If not,
  13. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA. */
  15. #ifndef _SYS_TIME_H
  16. #define _SYS_TIME_H 1
  17. #include <features.h>
  18. /* POSIX.4 structure for a time value. This is like a `struct timeval' but
  19. has nanoseconds instead of microseconds. */
  20. struct timespec
  21. {
  22. long int tv_sec; /* Seconds. */
  23. long int tv_nsec; /* Nanoseconds. */
  24. };
  25. #include <time.h>
  26. #include <sys/select.h>
  27. #define __need_timeval
  28. #include <bits/time.h>
  29. __BEGIN_DECLS
  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. /* Structure crudely representing a timezone.
  40. This is obsolete and should never be used. */
  41. struct timezone
  42. {
  43. int tz_minuteswest; /* Minutes west of GMT. */
  44. int tz_dsttime; /* Nonzero if DST is ever in effect. */
  45. };
  46. /* Get the current time of day and timezone information,
  47. putting it into *TV and *TZ. If TZ is NULL, *TZ is not filled.
  48. Returns 0 on success, -1 on errors.
  49. NOTE: This form of timezone information is obsolete.
  50. Use the functions and variables declared in <time.h> instead. */
  51. extern int gettimeofday __P ((struct timeval *__tv,
  52. struct timezone *__tz));
  53. /* Set the current time of day and timezone information.
  54. This call is restricted to the super-user. */
  55. extern int settimeofday __P ((__const struct timeval *__tv,
  56. __const struct timezone *__tz));
  57. /* Adjust the current time of day by the amount in DELTA.
  58. If OLDDELTA is not NULL, it is filled in with the amount
  59. of time adjustment remaining to be done from the last `adjtime' call.
  60. This call is restricted to the super-user. */
  61. extern int adjtime __P ((__const struct timeval *__delta,
  62. struct timeval *__olddelta));
  63. /* Values for the first argument to `getitimer' and `setitimer'. */
  64. enum __itimer_which
  65. {
  66. /* Timers run in real time. */
  67. ITIMER_REAL = 0,
  68. #define ITIMER_REAL ITIMER_REAL
  69. /* Timers run only when the process is executing. */
  70. ITIMER_VIRTUAL = 1,
  71. #define ITIMER_VIRTUAL ITIMER_VIRTUAL
  72. /* Timers run when the process is executing and when
  73. the system is executing on behalf of the process. */
  74. ITIMER_PROF = 2
  75. #define ITIMER_PROF ITIMER_PROF
  76. };
  77. /* Type of the second argument to `getitimer' and
  78. the second and third arguments `setitimer'. */
  79. struct itimerval
  80. {
  81. /* Value to put into `it_value' when the timer expires. */
  82. struct timeval it_interval;
  83. /* Time to the next timer expiration. */
  84. struct timeval it_value;
  85. };
  86. /* Set *VALUE to the current setting of timer WHICH.
  87. Return 0 on success, -1 on errors. */
  88. extern int getitimer __P ((enum __itimer_which __which,
  89. struct itimerval *__value));
  90. /* Set the timer WHICH to *NEW. If OLD is not NULL,
  91. set *OLD to the old value of timer WHICH.
  92. Returns 0 on success, -1 on errors. */
  93. extern int setitimer __P ((enum __itimer_which __which,
  94. __const struct itimerval *__new,
  95. struct itimerval *__old));
  96. /* Change the access time of FILE to TVP[0] and
  97. the modification time of FILE to TVP[1]. */
  98. extern int utimes __P ((__const char *__file, struct timeval __tvp[2]));
  99. /* Convenience macros for operations on timevals.
  100. NOTE: `timercmp' does not work for >= or <=. */
  101. #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
  102. #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
  103. #define timercmp(a, b, CMP) \
  104. (((a)->tv_sec == (b)->tv_sec) ? \
  105. ((a)->tv_usec CMP (b)->tv_usec) : \
  106. ((a)->tv_sec CMP (b)->tv_sec))
  107. #define timeradd(a, b, result) \
  108. do { \
  109. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  110. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  111. if ((result)->tv_usec >= 1000000) \
  112. { \
  113. ++(result)->tv_sec; \
  114. (result)->tv_usec -= 1000000; \
  115. } \
  116. } while (0)
  117. #define timersub(a, b, result) \
  118. do { \
  119. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  120. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  121. if ((result)->tv_usec < 0) { \
  122. --(result)->tv_sec; \
  123. (result)->tv_usec += 1000000; \
  124. } \
  125. } while (0)
  126. __END_DECLS
  127. #endif /* sys/time.h */