It can be stressed that the traditional UNIX
timeval and
timespec structures represent elapsed time, measured by the system clock (see
hz(9)). 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;
}
A better alternative would use the more precise
clock_gettime(2).