clock_gettime() — Retrieve the time of the specified clock

Standards

Standards / Extensions C or C++ Dependencies
POSIX.1-2001
POSIX.1-2008
Single UNIX Specification, Version 2
both  

Format

#define _XOPEN_SOURCE 500
#include <time.h>
int clock_gettime(clockid_t clockid, struct timespec *tp);

General description

The clock_gettime() function retrieves the time of the specified clock identifier. The clockid argument specifies the clock identifier, which can be one of the following values:
CLOCK_MONOTONIC
Clock that represents monotonic time since some unspecified starting point.
CLOCK_REALTIME
System-wide real-time clock.
The tp argument is a timespec structure that is defined in <time.h>:
struct timespec {
    time_t   tv_sec;        /* seconds */
    long     tv_nsec;       /* nanoseconds */
};

Returned value

If successful, clock_gettime() returns 0.

If unsuccessful, clock_gettime() returns -1 and sets errno to one of the following values:

Error Code
Description
EFAULT
tp points outside the accessible address space.
EINVAL
clockid does not refer to a valid instance of a clock object or is not supported.
EMVSTODNOTSET
System TOD clock is not set.

Related information