]> git.saurik.com Git - apple/libc.git/blame - gen/clock_gettime.3
Libc-1439.100.3.tar.gz
[apple/libc.git] / gen / clock_gettime.3
CommitLineData
974e3884
A
1.\"
2.\" Copyright (c) 1980, 1991, 1993
3.\" The Regents of the University of California. All rights reserved.
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\" notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\" notice, this list of conditions and the following disclaimer in the
12.\" documentation and/or other materials provided with the distribution.
13.\" 3. Neither the name of the University nor the names of its contributors
14.\" may be used to endorse or promote products derived from this software
15.\" without specific prior written permission.
16.\"
17.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27.\" SUCH DAMAGE.
28.\"
29.Dd January 26, 2016
30.Dt CLOCK_GETTIME 3
31.Os
32.Sh NAME
33.Nm clock_gettime ,
34.Nm clock_settime ,
35.Nm clock_getres ,
36.Nm clock_gettime_nsec_np
37.Nd get/set 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.Ft uint64_t
47.Fn clock_gettime_nsec_np "clockid_t clock_id"
48.Sh DESCRIPTION
49The
50.Fn clock_gettime
51and
52.Fn clock_settime
53functions
54allow the calling process to retrieve or set the value used by a clock
55which is specified by
56.Fa clock_id .
57.Pp
58.Fa clock_id
59can be a value from
70ad1dc8 60one of 8 predefined values:
974e3884
A
61.Bl -tag -width Er
62.It Dv CLOCK_REALTIME
63the system's real time (i.e. wall time) clock, expressed as the amount of time
64since the Epoch.
65This is the same as the value returned by
66.Xr gettimeofday 2 .
67.It Dv CLOCK_MONOTONIC
68clock that increments monotonically, tracking the time since an arbitrary
69point, and will continue to increment while the system is asleep.
70.It Dv CLOCK_MONOTONIC_RAW
71clock that increments monotonically, tracking the time since an arbitrary point
72like CLOCK_MONOTONIC. However, this clock is unaffected by frequency or time
73adjustments. It should not be compared to other system time sources.
74.It Dv CLOCK_MONOTONIC_RAW_APPROX
75like CLOCK_MONOTONIC_RAW, but reads a value cached by the system at context
76switch. This can be read faster, but at a loss of accuracy as it may return
77values that are milliseconds old.
78.It Dv CLOCK_UPTIME_RAW
79clock that increments monotonically, in the same manner as
80.Dv CLOCK_MONOTONIC_RAW,
81but that does not increment while the system is asleep.
82The returned value is identical to the result of
83.Fn mach_absolute_time
84after the appropriate mach_timebase conversion is applied.
85.It Dv CLOCK_UPTIME_RAW_APPROX
86like CLOCK_UPTIME_RAW, but reads a value cached by the system at context
87switch. This can be read faster, but at a loss of accuracy as it may return
88values that are milliseconds old.
89.It Dv CLOCK_PROCESS_CPUTIME_ID
90clock that tracks the amount of CPU (in user- or kernel-mode) used by the
91calling process.
92.It Dv CLOCK_THREAD_CPUTIME_ID
93clock that tracks the amount of CPU (in user- or kernel-mode) used by the
94calling thread.
95.El
96.Pp
97The structure pointed to by
98.Fa tp
99is defined in
100.In sys/time.h
101as:
102.Bd -literal -offset indent
103struct timespec {
104 time_t tv_sec; /* seconds */
105 long tv_nsec; /* and nanoseconds */
106};
107.Ed
108.Pp
109Only the
110.Dv CLOCK_REALTIME
111clock can be set, and only the superuser may do so.
112.Pp
113The resolution of a clock is returned by the
114.Fn clock_getres
115call.
116This value is placed in a (non-null)
117.Fa *tp .
118This value may be smaller than the actual precision of the underlying clock,
119but represents a lower bound on the resolution.
120.Pp
121As a non-portable extension, the
122.Fn clock_gettime_nsec_np
123function will return the clock value in 64-bit nanoseconds.
124.Sh RETURN VALUES
125A 0 return value indicates that the call succeeded.
126A \-1 return value indicates an error occurred, and in this
127case an error code is stored into the global variable
128.Va errno .
129For
130.Fn clock_gettime_nsec_np
131a return value of non-0 indicates success. A 0 return value indicates an error
132occurred and an error code is stored in
133.Va errno .
134.Sh ERRORS
135.Fn clock_gettime ,
136.Fn clock_settime ,
137.Fn clock_getres ,
138and
139.Fn clock_gettime_nsec_np
140will fail if:
141.Bl -tag -width Er
142.It Bq Er EINVAL
143.Fa clock_id
144is not a valid value.
145.It Bq Er EFAULT
146The
147.Fa tp
148argument address referenced invalid memory.
149.El
150.Pp
151In addition,
152.Fn clock_settime
153may return the following errors:
154.Bl -tag -width Er
155.It Bq Er EPERM
156A user other than the superuser attempted to set the time.
157.It Bq Er EINVAL
158.Fa clock_id
159specifies a clock that isn't settable,
160.Fa tp
161specifies a nanosecond value less than zero or greater than 1000 million,
162or a value outside the range of the specified clock.
163.El
164.Sh SEE ALSO
165.Xr date 1 ,
166.Xr getitimer 2 ,
167.Xr gettimeofday 2 ,
168.Sh HISTORY
169These functions first appeared
170in
171Mac OSX 10.12
172.Sh STANDARDS
173The
174.Fn clock_gettime ,
175.Fn clock_settime ,
176and
177.Fn clock_getres
178system calls conform to
179.St -p1003.1b-93 .
180.Fn cleck_gettime_nsec_np
181is a non-portable Darwin extension.
182The clock IDs
183.Fa CLOCK_MONOTONIC_RAW
184and
185.Fa CLOCK_UPTIME_RAW
186are extensions to the POSIX interface.