xref: /openbsd/lib/libc/sys/clock_gettime.2 (revision fc61954a)
1.\"	$OpenBSD: clock_gettime.2,v 1.27 2015/09/10 17:55:21 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: September 10 2015 $
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 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_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_UPTIME
77time whose absolute value is the time the system has been running
78and not suspended,
79providing accurate uptime measurement, both absolute and interval
80.El
81.Pp
82The structure pointed to by
83.Fa tp
84is defined in
85.In sys/time.h
86as:
87.Bd -literal -offset indent
88struct timespec {
89	time_t	tv_sec;		/* seconds */
90	long	tv_nsec;	/* and nanoseconds */
91};
92.Ed
93.Pp
94Only the
95.Dv CLOCK_REALTIME
96clock can be set, and only the superuser may do so.
97If the system securelevel is greater than 1 (see
98.Xr init 8 ) ,
99the time may only be advanced.
100This limitation is imposed to prevent a malicious superuser
101from setting arbitrary time stamps on files.
102The system time can still be adjusted backwards using the
103.Xr adjtime 2
104system call even when the system is secure.
105.Pp
106The resolution (granularity) of a clock is returned by the
107.Fn clock_getres
108call.
109This value is placed in a (non-null)
110.Fa *tp .
111.Sh RETURN VALUES
112.Rv -std
113.Sh ERRORS
114.Fn clock_gettime ,
115.Fn clock_settime ,
116and
117.Fn clock_getres
118will fail if:
119.Bl -tag -width Er
120.It Bq Er EINVAL
121.Fa clock_id
122is not a valid value.
123.It Bq Er EFAULT
124The
125.Fa tp
126argument address referenced invalid memory.
127.El
128.Pp
129In addition,
130.Fn clock_settime
131may return the following errors:
132.Bl -tag -width Er
133.It Bq Er EPERM
134A user other than the superuser attempted to set the time.
135.It Bq Er EINVAL
136.Fa clock_id
137specifies a clock that isn't settable,
138.Fa tp
139specifies a nanosecond value less than zero or greater than 1000 million,
140or a value outside the range of the specified clock.
141.El
142.Sh SEE ALSO
143.Xr date 1 ,
144.Xr adjtime 2 ,
145.Xr getitimer 2 ,
146.Xr gettimeofday 2 ,
147.Xr clock_getcpuclockid 3 ,
148.Xr ctime 3 ,
149.Xr pthread_getcpuclockid 3
150.Sh STANDARDS
151The
152.Fn clock_getres ,
153.Fn clock_gettime ,
154and
155.Fn clock_settime
156functions conform to
157.St -p1003.1-2008 .
158.Pp
159The
160.Dv CLOCK_UPTIME
161clock is an extension to that.
162.Sh HISTORY
163The
164.Dv CLOCK_PROCESS_CPUTIME_ID
165and
166.Dv CLOCK_THREAD_CPUTIME_ID
167clocks appeared in
168.Ox 5.4 .
169The
170.Dv CLOCK_UPTIME
171clock first appeared in
172.Fx 7.0
173and was added to
174.Ox
175in
176.Ox 5.5 .
177