---
name: "clock_gettime"
section: "2"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/clock_getres"
---

# clock\_gettime(2)

get/set/calibrate date and time

Part of [clock\_getres(2)](/clock_getres/2.md).

## NAME

**clock\_gettime**, **clock\_settime**, **clock\_getres** – get/set/calibrate date and time

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#include \<time.h\>**

*int*\

**clock\_gettime**(*clockid\_t clock\_id*, *struct timespec \*tp*);

*int*\

**clock\_settime**(*clockid\_t clock\_id*, *const struct timespec \*tp*);

*int*\

**clock\_getres**(*clockid\_t clock\_id*, *struct timespec \*tp*);

## DESCRIPTION

The **clock\_gettime**() and **clock\_settime**() system calls allow the calling process to retrieve or set the value used by a clock which is specified by *clock\_id*.

The *clock\_id* argument can be a value obtained from [clock\_getcpuclockid(3)](/clock_getcpuclockid/3.md) or [pthread\_getcpuclockid(3)](/pthread_getcpuclockid/3.md) as well as the following values:

- CLOCK\_REALTIME

  - CLOCK\_REALTIME\_PRECISE

  - CLOCK\_REALTIME\_FAST

  - CLOCK\_REALTIME\_COARSE

  Increments in SI seconds like a wall clock. It uses a 1970 epoch and implements the UTC timescale. The count of physical SI seconds since 1970, adjusted by subtracting the number of positive leap seconds and adding the number of negative leap seconds. Behavior during a leap second is not defined by any POSIX standard.

- CLOCK\_MONOTONIC

  - CLOCK\_MONOTONIC\_PRECISE

  - CLOCK\_MONOTONIC\_FAST

  - CLOCK\_MONOTONIC\_COARSE

  - CLOCK\_BOOTTIME

  Increments in SI seconds, even while the system is suspended. Its epoch is unspecified. The count is not adjusted by leap seconds.

- CLOCK\_UPTIME

  - CLOCK\_UPTIME\_PRECISE

  - CLOCK\_UPTIME\_FAST

  Increments monotonically in SI seconds while the machine is running. The count is not adjusted by leap seconds. The epoch is unspecified.

- CLOCK\_VIRTUAL

  Increments only when the CPU is running in user mode on behalf of the calling process.

- CLOCK\_PROF

  Increments when the CPU is running in user or kernel mode.

- CLOCK\_SECOND

  Returns the current second without performing a full time counter query, using an in-kernel cached value of the current second.

- CLOCK\_PROCESS\_CPUTIME\_ID

  Returns the execution time of the calling process.

- CLOCK\_THREAD\_CPUTIME\_ID

  Returns the execution time of the calling thread.

- CLOCK\_TAI

  Increments in SI seconds like a wall clock. It uses a 1970 epoch and implements the TAI timescale. Similar to CLOCK\_REALTIME, but without leap seconds. It will increase monotonically during a leap second. Will return EINVAL if the current offset between TAI and UTC is not known, which may be the case early in boot before NTP or another time daemon has synchronized.

The clock IDs CLOCK\_BOOTTIME, CLOCK\_REALTIME, CLOCK\_TAI, CLOCK\_MONOTONIC, and CLOCK\_UPTIME perform a full time counter query. The clock IDs with the \_FAST suffix, i.e., CLOCK\_REALTIME\_FAST, CLOCK\_MONOTONIC\_FAST, and CLOCK\_UPTIME\_FAST, do not perform a full time counter query, so their accuracy is one timer tick. Similarly, CLOCK\_REALTIME\_PRECISE, CLOCK\_MONOTONIC\_PRECISE, and CLOCK\_UPTIME\_PRECISE are used to get the most exact value as possible, at the expense of execution time. The clock IDs CLOCK\_REALTIME\_COARSE and CLOCK\_MONOTONIC\_COARSE are aliases of corresponding IDs with \_FAST suffix for compatibility with other systems. Finally, CLOCK\_BOOTTIME is an alias for CLOCK\_MONOTONIC for compatibility with other systems and is unrelated to the *kern.boottime* [sysctl(8)](/sysctl/8.md).

The structure pointed to by *tp* is defined in ⟨sys/timespec.h⟩ as:

```
struct timespec {
	time_t	tv_sec;		/* seconds */
	long	tv_nsec;	/* and nanoseconds */
};
```

Only the super-user may set the time of day, using only CLOCK\_REALTIME. If the system [securelevel(7)](/securelevel/7.md) is greater than 1 (see [init(8)](/init/8.md)), the time may only be advanced. This limitation is imposed to prevent a malicious super-user from setting arbitrary time stamps on files. The system time can still be adjusted backwards using the [adjtime(2)](/adjtime/2.md) system call even when the system is secure.

The resolution (granularity) of a clock is returned by the **clock\_getres**() system call. This value is placed in a (non-NULL) *\*tp*.

## RETURN VALUES

The **clock\_gettime()** function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

## ERRORS

The following error codes may be set in *errno*:

- \[EINVAL\]

  The *clock\_id* or *timespec* argument was not a valid value.

- \[EPERM\]

  A user other than the super-user attempted to set the time.

## SEE ALSO

[date(1)](/date/1.md), [adjtime(2)](/adjtime/2.md), [clock\_getcpuclockid(3)](/clock_getcpuclockid/3.md), [ctime(3)](/ctime/3.md), [pthread\_getcpuclockid(3)](/pthread_getcpuclockid/3.md)

## STANDARDS

The **clock\_gettime**(), **clock\_settime**(), and **clock\_getres**() system calls conform to IEEE Std 1003.1-2008 (“POSIX.1”). The clock IDs CLOCK\_BOOTTIME, CLOCK\_MONOTONIC\_FAST, CLOCK\_MONOTONIC\_PRECISE, CLOCK\_REALTIME\_FAST, CLOCK\_REALTIME\_PRECISE, CLOCK\_SECOND, CLOCK\_TAI, CLOCK\_UPTIME, CLOCK\_UPTIME\_FAST, and CLOCK\_UPTIME\_PRECISE are FreeBSD extensions to the POSIX interface.

UTC is defined by ITU-R TF.460-6, Standard-frequency and time-signal emissions. However, the *time\_t* type is a simple count that does not provide a unique encoding for leap seconds, nor a specification for what values should be used to encode a leap second.

## HISTORY

The **clock\_gettime**(), **clock\_settime**(), and **clock\_getres**() system calls first appeared in FreeBSD 3.0.
