xref: /netbsd/lib/libc/sys/gettimeofday.2 (revision bf9ec67e)
1.\"	$NetBSD: gettimeofday.2,v 1.18 2002/02/08 01:28:18 ross 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. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     @(#)gettimeofday.2	8.2 (Berkeley) 5/26/95
35.\"
36.Dd May 26, 1995
37.Dt GETTIMEOFDAY 2
38.Os
39.Sh NAME
40.Nm gettimeofday ,
41.Nm settimeofday
42.Nd get/set date and time
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.Fd #include \*[Lt]sys/time.h\*[Gt]
47.Ft int
48.Fn gettimeofday "struct timeval *tp" "struct timezone *tzp"
49.Ft int
50.Fn settimeofday "const struct timeval *tp" "const struct timezone *tzp"
51.Sh DESCRIPTION
52.Bf -symbolic
53Note: time zone information is no longer provided by this interface.
54See
55.Xr localtime 3
56for information on how to retrieve it.
57.Ef
58.Pp
59The system's notion of the current UTC time is obtained with the
60.Fn gettimeofday
61call, and set with the
62.Fn settimeofday
63call.  The time is expressed in seconds and microseconds
64since midnight (0 hour), January 1, 1970.  The resolution of the system
65clock is hardware dependent, and the time may be updated continuously or
66in ``ticks.''  If
67.Fa tp
68is NULL, the time will not be returned or set.
69.Pp
70The structures pointed to by
71.Fa tp
72and
73.Fa tzp
74are defined in
75.Ao Pa sys/time.h Ac
76as:
77.Pp
78.Bd -literal
79struct timeval {
80	long	tv_sec;		/* seconds since Jan. 1, 1970 */
81	long	tv_usec;	/* and microseconds */
82};
83
84struct timezone {
85	int	tz_minuteswest; /* of Greenwich */
86	int	tz_dsttime;	/* type of dst correction to apply */
87};
88.Ed
89.Pp
90The
91.Fa timezone
92structure is provided only for source compatibility.  It is ignored by
93.Fn settimeofday ,
94and
95.Fn gettimeofday
96will always return zeroes.
97.Pp
98If the calling user is not the super-user, then the
99.Fn settimeofday
100function in the standard C library will try to use the
101.Xr clockctl 4
102device if present, thus making possible for non privileged users to
103set the system time. If
104.Xr clockctl 4
105is not present or not accessible, then
106.Fn settimeofday
107reverts to the
108.Fn settimeofday
109system call, which is restricted to the super user.
110.\" XXX uncomment when/if this is put into place!
111.\" If the system is running in secure mode (see
112.\" .Xr init 8 ),
113.\" the time may only be advanced.
114.\" This limitation is imposed to prevent a malicious super user
115.\" from setting arbitrary time stamps on files.
116.\" The system time can still be adjusted backwards using the
117.\" .Xr adjtime 2
118.\" system call even when the system is secure.
119.Sh RETURN VALUES
120A 0 return value indicates that the call succeeded.
121A -1 return value indicates an error occurred, and in this
122case an error code is stored into the global variable
123.Va errno .
124.Sh ERRORS
125The following error codes may be set in
126.Va errno :
127.Bl -tag -width Er
128.It Bq Er EFAULT
129An argument address referenced invalid memory.
130.It Bq Er EPERM
131A user other than the super user attempted to set the time, or the specified
132time was less than the current time, which was not permitted at the current
133security level.
134.El
135.Sh SEE ALSO
136.Xr date 1 ,
137.Xr adjtime 2 ,
138.Xr ctime 3 ,
139.Xr localtime 3 ,
140.Xr clockctl 4 ,
141.Xr timed 8
142.Sh HISTORY
143The
144.Fn gettimeofday
145function call appeared in
146.Bx 4.2 .
147The
148.Fa tzp
149argument was deprecated in
150.Bx 4.4
151(and many other systems).
152