NAME
timeval, timespec — time structuresLIBRARY
library “libbsd”SYNOPSIS
#include <sys/time.h> (See libbsd(7) for include usage.)void
TIMEVAL_TO_TIMESPEC(struct timeval *tv, struct timespec *ts); void
TIMESPEC_TO_TIMEVAL(struct timeval *tv, struct timespec *ts);
DESCRIPTION
The <sys/time.h> header, included by <time.h>, defines various structures related to time and timers.- The following structure is used by
gettimeofday(2), among others:
struct timeval { time_t tv_sec; suseconds_t tv_usec; };
- The following structure is used by
nanosleep(2), among others:
struct timespec { time_t tv_sec; long tv_nsec; };
EXAMPLES
It can be stressed that the traditional UNIX timeval and timespec structures represent elapsed time, measured by the system clock. The following sketch implements a function suitable for use in a context where the timespec structure is required for a conditional timeout:static void example(struct timespec *spec, time_t minutes) { struct timeval elapsed; (void)gettimeofday(&elapsed, NULL); _DIAGASSERT(spec != NULL); TIMEVAL_TO_TIMESPEC(&elapsed, spec); /* Add the offset for timeout in minutes. */ spec->tv_sec = spec->tv_sec + minutes * 60; }
SEE ALSO
timeradd(3bsd)April 12, 2011 | Debian |