1.\" $OpenBSD: clock_gettime.2,v 1.4 1997/05/08 20:21:16 kstailey Exp $ 2.\" 3.\" Copyright (c) 1980, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.Dd October 15, 2025 31.Dt CLOCK_GETTIME 2 32.Os 33.Sh NAME 34.Nm clock_gettime , 35.Nm clock_settime , 36.Nm clock_getres 37.Nd get/set/calibrate date and time 38.Sh LIBRARY 39.Lb libc 40.Sh SYNOPSIS 41.In time.h 42.Ft int 43.Fn clock_gettime "clockid_t clock_id" "struct timespec *tp" 44.Ft int 45.Fn clock_settime "clockid_t clock_id" "const struct timespec *tp" 46.Ft int 47.Fn clock_getres "clockid_t clock_id" "struct timespec *tp" 48.Sh DESCRIPTION 49The 50.Fn clock_gettime 51and 52.Fn clock_settime 53system calls allow the calling process to retrieve or set the value 54used by a clock which is specified by 55.Fa clock_id . 56.Pp 57The 58.Fa clock_id 59argument can be a value obtained from 60.Xr clock_getcpuclockid 3 61or 62.Xr pthread_getcpuclockid 3 63as well as the following values: 64.Pp 65.Bl -tag -width indent -compact 66.It Dv CLOCK_REALTIME 67.It Dv CLOCK_REALTIME_PRECISE 68.It Dv CLOCK_REALTIME_FAST 69.It Dv CLOCK_REALTIME_COARSE 70Increments in SI seconds like a wall clock. 71It uses a 1970 epoch and implements the UTC timescale. 72The count of physical SI seconds since 1970, adjusted by subtracting the number 73of positive leap seconds and adding the number of negative leap seconds. 74Behavior during a leap second is not defined by and POSIX standard. 75.It Dv CLOCK_MONOTONIC 76.It Dv CLOCK_MONOTONIC_PRECISE 77.It Dv CLOCK_MONOTONIC_FAST 78.It Dv CLOCK_MONOTONIC_COARSE 79.It Dv CLOCK_BOOTTIME 80Increments in SI seconds, even while the system is suspended. 81Its epoch is unspecified. 82The count is not adjusted by leap seconds. 83.It Dv CLOCK_UPTIME 84.It Dv CLOCK_UPTIME_PRECISE 85.It Dv CLOCK_UPTIME_FAST 86Increments monotonically in SI seconds while the machine is running. 87The count is not adjusted by leap seconds. 88The epoch is unspecified. 89.It Dv CLOCK_VIRTUAL 90Increments only when 91the CPU is running in user mode on behalf of the calling process. 92.It Dv CLOCK_PROF 93Increments when the CPU is running in user or kernel mode. 94.It Dv CLOCK_SECOND 95Returns the current second without performing a full time counter 96query, using an in-kernel cached value of the current second. 97.It Dv CLOCK_PROCESS_CPUTIME_ID 98Returns the execution time of the calling process. 99.It Dv CLOCK_THREAD_CPUTIME_ID 100Returns the execution time of the calling thread. 101.It Dv CLOCK_TAI 102Increments in SI seconds like a wall clock. 103It uses a 1970 epoch and implements the TAI timescale. 104Similar to 105.Dv CLOCK_REALTIME , 106but without leap seconds. 107It will increase monotonically during a leap second. 108Will return 109.Er EINVAL 110if the current offset between TAI and UTC is not known, 111which may be the case early in boot before NTP or other time daemon has 112synchronized. 113.El 114.Pp 115The clock IDs 116.Dv CLOCK_BOOTTIME , 117.Dv CLOCK_REALTIME , 118.Dv CLOCK_TAI , 119.Dv CLOCK_MONOTONIC , 120and 121.Dv CLOCK_UPTIME 122perform a full time counter query. 123The clock IDs with the _FAST suffix, i.e., 124.Dv CLOCK_REALTIME_FAST , 125.Dv CLOCK_MONOTONIC_FAST , 126and 127.Dv CLOCK_UPTIME_FAST , 128do not perform 129a full time counter query, so their accuracy is one timer tick. 130Similarly, 131.Dv CLOCK_REALTIME_PRECISE , 132.Dv CLOCK_MONOTONIC_PRECISE , 133and 134.Dv CLOCK_UPTIME_PRECISE 135are used to get the most exact value as possible, at the expense of 136execution time. 137The clock IDs 138.Dv CLOCK_REALTIME_COARSE 139and 140.Dv CLOCK_MONOTONIC_COARSE 141are aliases of corresponding IDs with _FAST suffix for compatibility with other 142systems. 143Finally, 144.Dv CLOCK_BOOTTIME 145is an alias for 146.Dv CLOCK_MONOTONIC 147for compatibility with other systems and is unrelated to the 148.Fa kern.boottime 149.Xr sysctl 8 . 150.Pp 151The structure pointed to by 152.Fa tp 153is defined in 154.In sys/timespec.h 155as: 156.Bd -literal 157struct timespec { 158 time_t tv_sec; /* seconds */ 159 long tv_nsec; /* and nanoseconds */ 160}; 161.Ed 162.Pp 163Only the super-user may set the time of day, using only 164.Dv CLOCK_REALTIME . 165If the system 166.Xr securelevel 7 167is greater than 1 (see 168.Xr init 8 ) , 169the time may only be advanced. 170This limitation is imposed to prevent a malicious super-user 171from setting arbitrary time stamps on files. 172The system time can still be adjusted backwards using the 173.Xr adjtime 2 174system call even when the system is secure. 175.Pp 176The resolution (granularity) of a clock is returned by the 177.Fn clock_getres 178system call. 179This value is placed in a (non-NULL) 180.Fa *tp . 181.Sh RETURN VALUES 182.Rv -std 183.Sh ERRORS 184The following error codes may be set in 185.Va errno : 186.Bl -tag -width Er 187.It Bq Er EINVAL 188The 189.Fa clock_id 190or 191.Fa timespec 192argument 193was not a valid value. 194.It Bq Er EPERM 195A user other than the super-user attempted to set the time. 196.El 197.Sh SEE ALSO 198.Xr date 1 , 199.Xr adjtime 2 , 200.Xr clock_getcpuclockid 3 , 201.Xr ctime 3 , 202.Xr pthread_getcpuclockid 3 203.Sh STANDARDS 204The 205.Fn clock_gettime , 206.Fn clock_settime , 207and 208.Fn clock_getres 209system calls conform to 210.St -p1003.1-2008 . 211The clock IDs 212.Dv CLOCK_BOOTTIME , 213.Dv CLOCK_MONOTONIC_FAST , 214.Dv CLOCK_MONOTONIC_PRECISE , 215.Dv CLOCK_REALTIME_FAST , 216.Dv CLOCK_REALTIME_PRECISE , 217.Dv CLOCK_SECOND , 218.Dv CLOCK_TAI , 219.Dv CLOCK_UPTIME , 220.Dv CLOCK_UPTIME_FAST , 221and 222.Dv CLOCK_UPTIME_PRECISE 223are 224.Fx 225extensions to the POSIX interface. 226.Pp 227UTC is defined by ITU-R TF.460-6, Standard-frequency and time-signal emissions. 228However, the 229.Vt time_t 230type is a simple count that does not provide a unique encoding for leap seconds, 231nor a specification for what values should be used to encode a leap second. 232.Pp 233.Sh HISTORY 234The 235.Fn clock_gettime , 236.Fn clock_settime , 237and 238.Fn clock_getres 239system calls first appeared in 240.Fx 3.0 . 241