xref: /openbsd-src/lib/libc/sys/clock_gettime.2 (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1.\"	$OpenBSD: clock_gettime.2,v 1.30 2019/01/18 05:27:25 cheloha 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 18 2019 $
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.In 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 the following 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_PROCESS_CPUTIME_ID
66time that increments when the CPU is running in user or kernel mode
67on behalf of the calling process
68.It Dv CLOCK_THREAD_CPUTIME_ID
69time that increments when the CPU is running in user or kernel mode
70on behalf of the calling thread
71.It Dv CLOCK_MONOTONIC
72time that increments as a wall clock should but whose absolute value
73is meaningless and cannot jump,
74providing accurate realtime interval measurement,
75even across suspend and resume
76.It Dv CLOCK_BOOTTIME
77time whose absolute value is the time that has elapsed since the
78system was booted
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
115.Rv -std
116.Sh ERRORS
117.Fn clock_gettime ,
118.Fn clock_settime ,
119and
120.Fn clock_getres
121will fail if:
122.Bl -tag -width Er
123.It Bq Er EINVAL
124.Fa clock_id
125is not a valid value.
126.It Bq Er EFAULT
127The
128.Fa tp
129argument address referenced invalid memory.
130.El
131.Pp
132In addition,
133.Fn clock_settime
134may return the following errors:
135.Bl -tag -width Er
136.It Bq Er EPERM
137A user other than the superuser attempted to set the time.
138.It Bq Er EINVAL
139.Fa clock_id
140specifies a clock that isn't settable,
141.Fa tp
142specifies a nanosecond value less than zero or greater than or equal to
1431000 million,
144or a value outside the range of the specified clock.
145.El
146.Sh SEE ALSO
147.Xr date 1 ,
148.Xr adjtime 2 ,
149.Xr getitimer 2 ,
150.Xr gettimeofday 2 ,
151.Xr clock_getcpuclockid 3 ,
152.Xr ctime 3 ,
153.Xr pthread_getcpuclockid 3
154.Sh STANDARDS
155The
156.Fn clock_getres ,
157.Fn clock_gettime ,
158and
159.Fn clock_settime
160functions conform to
161.St -p1003.1-2008 .
162.Pp
163The
164.Dv CLOCK_BOOTTIME
165and
166.Dv CLOCK_UPTIME
167clocks are extensions 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 .
183The
184.Dv CLOCK_BOOTTIME
185clock first appeared in
186Linux 2.6.39
187and was added to
188.Ox
189in
190.Ox 6.3 .
191