The C11 timespec_get() function stores calendar time in a struct timespec, which has whole seconds in tv_sec and a fractional nanosecond field in tv_nsec.
Pass TIME_UTC as the time base. Success means the function returns that same base; zero means the requested base is unsupported or the time could not be obtained. Include <time.h>.
Syntax
Start with this concrete timespec_get() call:
C
timespec_get(&now, TIME_UTC);
Call
What it does
timespec_get(&now, TIME_UTC);
Stores the current TIME_UTC calendar time in now.
Declare the required objects with the time types shown below, include <time.h>, and check the documented result before using output data.
Library declaration
C
int timespec_get(struct timespec *ts, int base);
Parameters
Parameter
Description
ts
Pointer to a writable struct timespec.
base
Requested time base; standard C11 defines TIME_UTC.
Return Value
Return Value
Description
TIME_UTC
The requested calendar time was stored successfully.
0
The base is unsupported or time acquisition failed.
C Examples
Example 1 — Read Seconds and Nanoseconds
This example demonstrates timespec_get() with a small, complete program and checks the relevant result before using it.