Back to index

glibc  2.9
time.h
Go to the documentation of this file.
00001 /* Copyright (C) 1991-1994,1996-2002,2003,2005,2006
00002        Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #ifndef _SYS_TIME_H
00021 #define _SYS_TIME_H  1
00022 
00023 #include <features.h>
00024 
00025 #include <bits/types.h>
00026 #define __need_time_t
00027 #include <time.h>
00028 #define __need_timeval
00029 #include <bits/time.h>
00030 
00031 #include <sys/select.h>
00032 
00033 #ifndef __suseconds_t_defined
00034 typedef __suseconds_t suseconds_t;
00035 # define __suseconds_t_defined
00036 #endif
00037 
00038 
00039 __BEGIN_DECLS
00040 
00041 #ifdef __USE_GNU
00042 /* Macros for converting between `struct timeval' and `struct timespec'.  */
00043 # define TIMEVAL_TO_TIMESPEC(tv, ts) {                                   \
00044         (ts)->tv_sec = (tv)->tv_sec;                                    \
00045         (ts)->tv_nsec = (tv)->tv_usec * 1000;                           \
00046 }
00047 # define TIMESPEC_TO_TIMEVAL(tv, ts) {                                   \
00048         (tv)->tv_sec = (ts)->tv_sec;                                    \
00049         (tv)->tv_usec = (ts)->tv_nsec / 1000;                           \
00050 }
00051 #endif
00052 
00053 
00054 #ifdef __USE_BSD
00055 /* Structure crudely representing a timezone.
00056    This is obsolete and should never be used.  */
00057 struct timezone
00058   {
00059     int tz_minuteswest;            /* Minutes west of GMT.  */
00060     int tz_dsttime;         /* Nonzero if DST is ever in effect.  */
00061   };
00062 
00063 typedef struct timezone *__restrict __timezone_ptr_t;
00064 #else
00065 typedef void *__restrict __timezone_ptr_t;
00066 #endif
00067 
00068 /* Get the current time of day and timezone information,
00069    putting it into *TV and *TZ.  If TZ is NULL, *TZ is not filled.
00070    Returns 0 on success, -1 on errors.
00071    NOTE: This form of timezone information is obsolete.
00072    Use the functions and variables declared in <time.h> instead.  */
00073 extern int gettimeofday (struct timeval *__restrict __tv,
00074                       __timezone_ptr_t __tz) __THROW __nonnull ((1));
00075 
00076 #ifdef __USE_BSD
00077 /* Set the current time of day and timezone information.
00078    This call is restricted to the super-user.  */
00079 extern int settimeofday (__const struct timeval *__tv,
00080                       __const struct timezone *__tz)
00081      __THROW __nonnull ((1));
00082 
00083 /* Adjust the current time of day by the amount in DELTA.
00084    If OLDDELTA is not NULL, it is filled in with the amount
00085    of time adjustment remaining to be done from the last `adjtime' call.
00086    This call is restricted to the super-user.  */
00087 extern int adjtime (__const struct timeval *__delta,
00088                   struct timeval *__olddelta) __THROW;
00089 #endif
00090 
00091 
00092 /* Values for the first argument to `getitimer' and `setitimer'.  */
00093 enum __itimer_which
00094   {
00095     /* Timers run in real time.  */
00096     ITIMER_REAL = 0,
00097 #define ITIMER_REAL ITIMER_REAL
00098     /* Timers run only when the process is executing.  */
00099     ITIMER_VIRTUAL = 1,
00100 #define ITIMER_VIRTUAL ITIMER_VIRTUAL
00101     /* Timers run when the process is executing and when
00102        the system is executing on behalf of the process.  */
00103     ITIMER_PROF = 2
00104 #define ITIMER_PROF ITIMER_PROF
00105   };
00106 
00107 /* Type of the second argument to `getitimer' and
00108    the second and third arguments `setitimer'.  */
00109 struct itimerval
00110   {
00111     /* Value to put into `it_value' when the timer expires.  */
00112     struct timeval it_interval;
00113     /* Time to the next timer expiration.  */
00114     struct timeval it_value;
00115   };
00116 
00117 #if defined __USE_GNU && !defined __cplusplus
00118 /* Use the nicer parameter type only in GNU mode and not for C++ since the
00119    strict C++ rules prevent the automatic promotion.  */
00120 typedef enum __itimer_which __itimer_which_t;
00121 #else
00122 typedef int __itimer_which_t;
00123 #endif
00124 
00125 /* Set *VALUE to the current setting of timer WHICH.
00126    Return 0 on success, -1 on errors.  */
00127 extern int getitimer (__itimer_which_t __which,
00128                     struct itimerval *__value) __THROW;
00129 
00130 /* Set the timer WHICH to *NEW.  If OLD is not NULL,
00131    set *OLD to the old value of timer WHICH.
00132    Returns 0 on success, -1 on errors.  */
00133 extern int setitimer (__itimer_which_t __which,
00134                     __const struct itimerval *__restrict __new,
00135                     struct itimerval *__restrict __old) __THROW;
00136 
00137 /* Change the access time of FILE to TVP[0] and the modification time of
00138    FILE to TVP[1].  If TVP is a null pointer, use the current time instead.
00139    Returns 0 on success, -1 on errors.  */
00140 extern int utimes (__const char *__file, __const struct timeval __tvp[2])
00141      __THROW __nonnull ((1));
00142 
00143 #ifdef __USE_BSD
00144 /* Same as `utimes', but does not follow symbolic links.  */
00145 extern int lutimes (__const char *__file, __const struct timeval __tvp[2])
00146      __THROW __nonnull ((1));
00147 
00148 /* Same as `utimes', but takes an open file descriptor instead of a name.  */
00149 extern int futimes (int __fd, __const struct timeval __tvp[2]) __THROW;
00150 #endif
00151 
00152 #ifdef __USE_ATFILE
00153 /* Change the access time of FILE relative to FD to TVP[0] and the
00154    modification time of FILE to TVP[1].  If TVP is a null pointer, use
00155    the current time instead.  Returns 0 on success, -1 on errors.  */
00156 extern int futimesat (int __fd, __const char *__file,
00157                     __const struct timeval __tvp[2]) __THROW;
00158 #endif
00159 
00160 
00161 #ifdef __USE_BSD
00162 /* Convenience macros for operations on timevals.
00163    NOTE: `timercmp' does not work for >= or <=.  */
00164 # define timerisset(tvp)    ((tvp)->tv_sec || (tvp)->tv_usec)
00165 # define timerclear(tvp)    ((tvp)->tv_sec = (tvp)->tv_usec = 0)
00166 # define timercmp(a, b, CMP)                                                \
00167   (((a)->tv_sec == (b)->tv_sec) ?                                    \
00168    ((a)->tv_usec CMP (b)->tv_usec) :                                        \
00169    ((a)->tv_sec CMP (b)->tv_sec))
00170 # define timeradd(a, b, result)                                             \
00171   do {                                                               \
00172     (result)->tv_sec = (a)->tv_sec + (b)->tv_sec;                           \
00173     (result)->tv_usec = (a)->tv_usec + (b)->tv_usec;                        \
00174     if ((result)->tv_usec >= 1000000)                                       \
00175       {                                                                     \
00176        ++(result)->tv_sec;                                           \
00177        (result)->tv_usec -= 1000000;                                        \
00178       }                                                                     \
00179   } while (0)
00180 # define timersub(a, b, result)                                             \
00181   do {                                                               \
00182     (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;                           \
00183     (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;                        \
00184     if ((result)->tv_usec < 0) {                                     \
00185       --(result)->tv_sec;                                            \
00186       (result)->tv_usec += 1000000;                                         \
00187     }                                                                \
00188   } while (0)
00189 #endif /* BSD */
00190 
00191 __END_DECLS
00192 
00193 #endif /* sys/time.h */