1.\" $OpenBSD: clock_gettime.2,v 1.24 2014/01/21 03:15:45 schwarze 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 $Mdocdate: January 21 2014 $ 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 SYNOPSIS 39.Fd #include <time.h> 40.Ft int 41.Fn clock_gettime "clockid_t clock_id" "struct timespec *tp" 42.Ft int 43.Fn clock_settime "clockid_t clock_id" "const struct timespec *tp" 44.Ft int 45.Fn clock_getres "clockid_t clock_id" "struct timespec *tp" 46.Sh DESCRIPTION 47The 48.Fn clock_gettime 49and 50.Fn clock_settime 51functions 52allow the calling process to retrieve or set the value used by a clock 53which is specified by 54.Fa clock_id . 55.Pp 56.Fa clock_id 57can be a value from 58.Xr clock_getcpuclockid 3 59or 60.Xr pthread_getcpuclockid 3 61or one of five predefined values: 62.Bl -tag -width CLOCK_MONOTONIC 63.It Dv CLOCK_REALTIME 64time that increments as a wall clock should 65.It Dv CLOCK_VIRTUAL 66time that increments only when 67the CPU is running in user mode on behalf of the calling process 68.It Dv CLOCK_PROCESS_CPUTIME_ID 69time that increments when the CPU is running in user or kernel mode 70on behalf of the calling process 71.It Dv CLOCK_THREAD_CPUTIME_ID 72time that increments when the CPU is running in user or kernel mode 73on behalf of the calling thread 74.It Dv CLOCK_MONOTONIC 75time that increments as a wall clock should but whose absolute value 76is meaningless and cannot jump, 77providing accurate realtime interval measurement, 78even across suspend and resume 79.It Dv CLOCK_UPTIME 80time whose absolute value is the time the system has been running 81and not suspended, 82providing accurate uptime measurement, both absolute and interval 83.El 84.Pp 85The structure pointed to by 86.Fa tp 87is defined in 88.In sys/time.h 89as: 90.Bd -literal -offset indent 91struct timespec { 92 time_t tv_sec; /* seconds */ 93 long tv_nsec; /* and nanoseconds */ 94}; 95.Ed 96.Pp 97Only the 98.Dv CLOCK_REALTIME 99clock can be set, and only the superuser may do so. 100If the system securelevel is greater than 1 (see 101.Xr init 8 ) , 102the time may only be advanced. 103This limitation is imposed to prevent a malicious superuser 104from setting arbitrary time stamps on files. 105The system time can still be adjusted backwards using the 106.Xr adjtime 2 107system call even when the system is secure. 108.Pp 109The resolution (granularity) of a clock is returned by the 110.Fn clock_getres 111call. 112This value is placed in a (non-null) 113.Fa *tp . 114.Sh RETURN VALUES 115A 0 return value indicates that the call succeeded. 116A \-1 return value indicates an error occurred, and in this 117case an error code is stored into the global variable 118.Va errno . 119.Sh ERRORS 120.Fn clock_gettime , 121.Fn clock_settime , 122and 123.Fn clock_getres 124will fail if: 125.Bl -tag -width Er 126.It Bq Er EINVAL 127.Fa clock_id 128is not a valid value. 129.It Bq Er EFAULT 130The 131.Fa tp 132argument address referenced invalid memory. 133.El 134.Pp 135In addition, 136.Fn clock_settime 137may return the following errors: 138.Bl -tag -width Er 139.It Bq Er EPERM 140A user other than the superuser attempted to set the time. 141.It Bq Er EINVAL 142.Fa clock_id 143specifies a clock that isn't settable, 144.Fa tp 145specifies a nanosecond value less than zero or greater than 1000 million, 146or a value outside the range of the specified clock. 147.El 148.Sh SEE ALSO 149.Xr date 1 , 150.Xr adjtime 2 , 151.Xr getitimer 2 , 152.Xr gettimeofday 2 , 153.Xr clock_getcpuclockid 3 , 154.Xr ctime 3 , 155.Xr pthread_getcpuclockid 3 156.Sh STANDARDS 157The 158.Fn clock_getres , 159.Fn clock_gettime , 160and 161.Fn clock_settime 162functions conform to 163.St -p1003.1-2008 . 164.Pp 165The 166.Dv CLOCK_UPTIME 167clock is an extension to that. 168.Sh HISTORY 169The 170.Dv CLOCK_PROCESS_CPUTIME_ID 171and 172.Dv CLOCK_THREAD_CPUTIME_ID 173clocks appeared in 174.Ox 5.4 . 175The 176.Dv CLOCK_UPTIME 177clock first appeared in 178.Fx 7.0 179and was added to 180.Ox 181in 182.Ox 5.5 . 183