timespec(3HEAD) Headers timespec(3HEAD)

timespec, timeval, TIMESPEC_TO_TIMEVAL, TIMEVAL_TO_TIMESPEC
time structures and conversion

#include <sys/time.h>

void
TIMESPEC_TO_TIMEVAL(struct timeval *tv, const struct timespec *ts);

void
TIMEVAL_TO_TIMESPEC(const struct timeval *tv, struct timespec *ts);

The timeval and timespec structures are declared in the <time.h> and <sys/time.h> headers respectively:
typedef struct timespec {
	time_t		tv_sec;		/* seconds */
	long		tv_nsec;	/* and nanoseconds */
} timespec_t;

struct timeval {
	time_t		tv_sec;		/* seconds */
	suseconds_t	tv_usec;	/* and microseconds */
};

In both cases, the tv_sec member represents elapsed time in whole seconds. The tv_nsec and tv_usec members represent the rest of the elapsed time in nanoseconds and microseconds respectively, depending on the structure.

The TIMEVAL_TO_TIMESPEC macro can be used to convert a struct timeval structure to a struct timespec structure, while the TIMESPEC_TO_TIMEVAL macro works in the opposite direction.

When converting from a struct timespec to a struct timeval structure, the tv_nsec member is truncated, losing precision. When converting from a struct timeval to a struct timespec structure, the tv_usec member is multiplied by 1000 to reach the precision of the target structure. The tv_sec member is always preserved, no matter which conversion is performed.

Note that the TIMEVAL_TO_TIMESPEC and TIMESPEC_TO_TIMEVAL macros are non-standard but are commonly found on UNIX and UNIX-like systems.

Committed

MT-Safe

time.h(3HEAD)
September 6, 2020 illumos