1timeval(3bsd)                        LOCAL                       timeval(3bsd)
2

NAME

4     timeval, timespec — time structures
5

LIBRARY

7     Utility functions from BSD systems (libbsd, -lbsd)
8

SYNOPSIS

10     #include <sys/time.h>
11     (See libbsd(7) for include usage.)
12
13     void
14     TIMEVAL_TO_TIMESPEC(struct timeval *tv, struct timespec *ts);
15
16     void
17     TIMESPEC_TO_TIMEVAL(struct timeval *tv, struct timespec *ts);
18

DESCRIPTION

20     The <sys/time.h> header, included by <time.h>, defines various structures
21     related to time and timers.
22
23      1.   The following structure is used by gettimeofday(2), among others:
24
25                 struct timeval {
26                         time_t          tv_sec;
27                         suseconds_t     tv_usec;
28                 };
29
30           The tv_sec member represents the elapsed time, in whole seconds.
31           The tv_usec member captures rest of the elapsed time, represented
32           as the number of microseconds.
33
34      2.   The following structure is used by nanosleep(2), among others:
35
36                 struct timespec {
37                         time_t          tv_sec;
38                         long            tv_nsec;
39                 };
40
41           The tv_sec member is again the elapsed time in whole seconds.  The
42           tv_nsec member represents the rest of the elapsed time in nanosec‐
43           onds.
44
45           A microsecond is equal to one millionth of a second, 1000 nanosec‐
46           onds, or 1/1000 milliseconds.  To ease the conversions, the macros
47           TIMEVAL_TO_TIMESPEC() and TIMESPEC_TO_TIMEVAL() can be used to con‐
48           vert between struct timeval and struct timespec.
49

EXAMPLES

51     It can be stressed that the traditional UNIX timeval and timespec struc‐
52     tures represent elapsed time, measured by the system clock.  The follow‐
53     ing sketch implements a function suitable for use in a context where the
54     timespec structure is required for a conditional timeout:
55
56           static void
57           example(struct timespec *spec, time_t minutes)
58           {
59                   struct timeval elapsed;
60
61                   (void)gettimeofday(&elapsed, NULL);
62
63                   _DIAGASSERT(spec != NULL);
64                   TIMEVAL_TO_TIMESPEC(&elapsed, spec);
65
66                   /* Add the offset for timeout in minutes. */
67                   spec->tv_sec = spec->tv_sec + minutes * 60;
68           }
69
70     A better alternative would use the more precise clock_gettime(2).
71

SEE ALSO

73     timeradd(3bsd)
74
75BSD                             April 12, 2011                             BSD
Impressum