xref: /openbsd-src/lib/libc/sys/clock_gettime.2 (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1.\"	$OpenBSD: clock_gettime.2,v 1.18 2007/12/09 10:30:14 brad 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: December 9 2007 $
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 <sys/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
51allow the calling process to retrieve or set the value used by a clock
52which is specified by
53.Fa clock_id .
54.Pp
55.Fa clock_id
56can be one of four values:
57.Dv CLOCK_REALTIME
58for time that increments as
59a wall clock should,
60.Dv CLOCK_VIRTUAL
61for time that increments only when
62the CPU is running in user mode on behalf of the calling process,
63.Dv CLOCK_PROF
64for time that increments when the CPU is running in user or kernel mode, or
65.Dv CLOCK_MONOTONIC
66for time that increments at a steady rate (monotonically).
67.Pp
68The structure pointed to by
69.Fa tp
70is defined in
71.Aq Pa sys/time.h
72as:
73.Bd -literal -offset indent
74struct timespec {
75	time_t	tv_sec;		/* seconds */
76	long	tv_nsec;	/* and nanoseconds */
77};
78.Ed
79.Pp
80Only the superuser may set the time of day.
81If the system securelevel is greater than 1 (see
82.Xr init 8 ) ,
83the time may only be advanced.
84This limitation is imposed to prevent a malicious superuser
85from setting arbitrary time stamps on files.
86The system time can still be adjusted backwards using the
87.Xr adjtime 2
88system call even when the system is secure.
89.Pp
90The resolution (granularity) of a clock is returned by the
91.Fn clock_getres
92call.
93This value is placed in a (non-null)
94.Fa *tp .
95.Sh RETURN VALUES
96A 0 return value indicates that the call succeeded.
97A \-1 return value indicates an error occurred, and in this
98case an error code is stored into the global variable
99.Va errno .
100.Sh ERRORS
101The following error codes may be set in
102.Va errno :
103.Bl -tag -width Er
104.It Bq Er EINVAL
105The
106.Fa clock_id
107was not a valid value.
108.It Bq Er EFAULT
109The
110.Fa tp
111argument address referenced invalid memory.
112.It Bq Er EPERM
113A user other than the superuser attempted to set the time.
114.El
115.Sh SEE ALSO
116.Xr date 1 ,
117.Xr adjtime 2 ,
118.Xr ctime 3 ,
119.Xr timed 8
120.Sh STANDARDS
121The
122.Fn clock_gettime ,
123.Fn clock_settime ,
124and
125.Fn clock_getres
126functions conform to
127.St -p1003.1b-93 .
128