xref: /freebsd-14.2/lib/libc/sys/clock_gettime.2 (revision ecfcbb9f)
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.\" 4. 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.\" $FreeBSD$
31.\"
32.Dd November 4, 2006
33.Dt CLOCK_GETTIME 2
34.Os
35.Sh NAME
36.Nm clock_gettime ,
37.Nm clock_settime ,
38.Nm clock_getres
39.Nd get/set/calibrate date and time
40.Sh LIBRARY
41.Lb libc
42.Sh SYNOPSIS
43.In time.h
44.Ft int
45.Fn clock_gettime "clockid_t clock_id" "struct timespec *tp"
46.Ft int
47.Fn clock_settime "clockid_t clock_id" "const struct timespec *tp"
48.Ft int
49.Fn clock_getres "clockid_t clock_id" "struct timespec *tp"
50.Sh DESCRIPTION
51The
52.Fn clock_gettime
53and
54.Fn clock_settime
55system calls allow the calling process to retrieve or set the value
56used by a clock which is specified by
57.Fa clock_id .
58.Pp
59The
60.Fa clock_id
61argument
62can be one of five values:
63.Dv CLOCK_REALTIME
64for time that increments as
65a wall clock should,
66.Dv CLOCK_MONOTONIC
67which increments in SI seconds,
68.Dv CLOCK_UPTIME
69which starts at zero when the kernel boots and increments
70monotonically in SI seconds while the machine is running,
71.Dv CLOCK_VIRTUAL
72for time that increments only when
73the CPU is running in user mode on behalf of the calling process, or
74.Dv CLOCK_PROF
75for time that increments when the CPU is running in user or
76kernel mode.
77.Pp
78The structure pointed to by
79.Fa tp
80is defined in
81.In sys/timespec.h
82as:
83.Pp
84.Bd -literal
85struct timespec {
86	time_t	tv_sec;		/* seconds */
87	long	tv_nsec;	/* and nanoseconds */
88};
89.Ed
90.Pp
91Only the super-user may set the time of day.
92If the system securelevel is greater than 1 (see
93.Xr init 8 ) ,
94the time may only be advanced.
95This limitation is imposed to prevent a malicious super-user
96from setting arbitrary time stamps on files.
97The system time can still be adjusted backwards using the
98.Xr adjtime 2
99system call even when the system is secure.
100.Pp
101The resolution (granularity) of a clock is returned by the
102.Fn clock_getres
103system call.
104This value is placed in a (non-NULL)
105.Fa *tp .
106.Sh RETURN VALUES
107.Rv -std
108.Sh ERRORS
109The following error codes may be set in
110.Va errno :
111.Bl -tag -width Er
112.It Bq Er EINVAL
113The
114.Fa clock_id
115argument
116was not a valid value.
117.It Bq Er EFAULT
118The
119.Fa *tp
120argument address referenced invalid memory.
121.It Bq Er EPERM
122A user other than the super-user attempted to set the time.
123.El
124.Sh SEE ALSO
125.Xr date 1 ,
126.Xr adjtime 2 ,
127.Xr ctime 3 ,
128.Xr timed 8
129.Sh STANDARDS
130The
131.Fn clock_gettime ,
132.Fn clock_settime ,
133and
134.Fn clock_getres
135system calls conform to
136.St -p1003.1b-93 .
137