timespec_get(3) Library Functions Manual timespec_get(3)

timespec_get, timespec_getres - ISO C interface to clock and time functions

Standard C library (libc-lc),

#include <time.h>
int timespec_get(struct timespec *res, int base);
int timespec_getres(struct timespec *tp, int base);

timespec_get(tp, TIME_UTC) is defined as clock_gettime(CLOCK_REALTIME, tp).

timespec_getres(res, TIME_UTC) is equivalent to clock_getres(CLOCK_REALTIME, res).

TIME_UTC is universally guaranteed to be a valid base, and is the only one supported under Linux. Some other systems support different time bases.

On success, base is returned. On error, -1 is returned.

Some C libraries may set errno to the same value as would be set by clock_gettime(2) or clock_getres(2). Neither C nor POSIX specify this, but they don't really indicate it shouldn't happen, either. Don't rely on this.

For an explanation of the terms used in this section, see attributes(7).

Interface Attribute Value
timespec_get (), timespec_getres () Thread safety MT-Safe

C23 (though ISO C doesn't specify the time epoch), POSIX.1-2024.
C23.

C11, POSIX.1-2024, glibc 2.16, musl 1.1.10.
C23, glibc 2.34.

clock_gettime(2), clock_getres(2)

2024-09-08 Linux man-pages 6.10