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);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
timespec_get(), TIME_UTC:
_ISOC11_SOURCE
timespec_getres(), TIME_MONOTONIC, TIME_ACTIVE, TIME_THREAD_ACTIVE:
_ISOC23_SOURCE
The timespec_get() function stores the current time,
based on the specified time base, in the
timespec(3type) structure pointed to by
res.
The timespec_getres() function stores the resolution
of times retrieved by timespec_get() with the specified
time base in the timespec(3type) structure pointed to
by tp, if tp is non-NULL. For a particular time base,
the resolution is constant for the lifetime of the calling process.
The time base base is one of the following:
A system-wide time base that measures real (i.e., wall-clock) time.
The time and resolution in this time base are the same as those
retrieved by clock_gettime(CLOCK_REALTIME, res) and
clock_getres(CLOCK_REALTIME, tp), respectively.
A time base that measures time since an unspecified point in the
past, where the time within a process will not decrease even if the
system's real time clock is set or adjusted. The time and resolution in
this time base are the same as those retrieved by
clock_gettime(CLOCK_MONOTONIC, res) and
clock_getres(CLOCK_MONOTONIC, tp), respectively.
A process-specific time base that measures CPU time consumed by the
calling process. The time and resolution in this time base are the same
as those retrieved by
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, res) and
clock_getres(CLOCK_PROCESS_CPUTIME_ID, tp), respectively.
A thread-specific time base that measures CPU time consumed by the
calling thread. The time and resolution in this time base are the same
as those retrieved by
clock_gettime(CLOCK_THREAD_CPUTIME_ID, res) and
clock_getres(CLOCK_THREAD_CPUTIME_ID, tp), respectively.
timespec_get() returns the nonzero base if
it is a supported time base and the current time was successfully
retrieved, or 0 otherwise.
timespec_getres() returns the nonzero base
if it is a supported time base, or 0 otherwise.
For an explanation of the terms used in this section, see attributes(7).
| Interface | Attribute | Value |
| Thread safety | MT-Safe |
C23, POSIX.1-2024.
TIME_UTC 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.
C23, glibc 2.43.
clock_gettime(2), clock_getres(2)