The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group


clock_settime, clock_gettime, clock_getres - clock and timer functions (REALTIME)


#include <time.h>

int clock_settime(clockid_t clock_id, const struct timespec *tp);
int clock_gettime(clockid_t clock_id, struct timespec *tp);
int clock_getres(clockid_t clock_id, struct timespec *res);


The clock_settime() function sets the specified clock, clock_id, to the value specified by tp. Time values that are between two consecutive non-negative integer multiples of the resolution of the specified clock are truncated down to the smaller multiple of the resolution.

The clock_gettime() function returns the current value tp for the specified clock, clock_id.

The resolution of any clock can be obtained by calling clock_getres(). Clock resolutions are implementation-dependent and cannot be set by a process. If the argument res is not NULL, the resolution of the specified clock is stored in the location pointed to by res. If res is NULL, the clock resolution is not returned. If the time argument of clock_settime() is not a multiple of res, then the value is truncated to a multiple of res.

A clock may be systemwide (that is, visible to all processes) or per-process (measuring time that is meaningful only within a process). All implementations support a clock_id of CLOCK_REALTIME defined in <time.h>. This clock represents the realtime clock for the system. For this clock, the values returned by clock_gettime() and specified by clock_settime() represent the amount of time (in seconds and nanoseconds) since the Epoch. An implementation may also support additional clocks. The interpretation of time values for these clocks is unspecified.

The effect of setting a clock via clock_settime() on armed per-process timers associated with that clock is implementation-dependent.

The appropriate privilege to set a particular clock is implementation-dependent.


A return value of 0 indicates that the call succeeded. A return value of -1 indicates that an error occurred, and errno is set to indicate the error.


The clock_settime(), clock_gettime() and clock_getres() functions will fail if:
The clock_id argument does not specify a known clock.
The functions clock_settime(), clock_gettime(), and clock_getres() are not supported by this implementation.

The clock_settime() function will fail if:

The tp argument to clock_settime() is outside the range for the given clock id.
The tp argument specified a nanosecond value less than zero or greater than or equal to 1000 million.

The clock_settime() function may fail if:

The requesting process does not have the appropriate privilege to set the specified clock.








timer_gettime(), time(), ctime(), <time.h>.


Derived from the POSIX Realtime Extension (1003.1b-1993/1003.1i-1995)

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group