1.\" $NetBSD: gettimeofday.2,v 1.30 2022/10/16 20:37:03 christos 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.\" @(#)gettimeofday.2 8.2 (Berkeley) 5/26/95 31.\" 32.Dd October 16, 2022 33.Dt GETTIMEOFDAY 2 34.Os 35.Sh NAME 36.Nm gettimeofday , 37.Nm settimeofday 38.Nd get/set date and time 39.Sh LIBRARY 40.Lb libc 41.Sh SYNOPSIS 42.In sys/time.h 43.Ft int 44.Fn gettimeofday "struct timeval * restrict tp" "void * restrict tzp" 45.Ft int 46.Fn settimeofday "const struct timeval * restrict tp" "const void * restrict tzp" 47.Sh DESCRIPTION 48.Bf -symbolic 49Note: time zone information is no longer provided by this interface. 50See 51.Xr localtime 3 52for information on how to retrieve it. 53.Ef 54.Pp 55The system's notion of the current UTC time is obtained with the 56.Fn gettimeofday 57call, and set with the 58.Fn settimeofday 59call. 60The time is expressed in seconds and microseconds 61since midnight (0 hour), January 1, 1970. 62The resolution of the system clock is hardware dependent, 63and the time may be updated continuously or in 64.Dq ticks . 65.Pp 66If 67.Fa tp 68is NULL, the time will not be returned or set. 69Despite being declared 70.Fa void * , 71the objects pointed to by 72.Fa tzp 73shall be of type 74.Fa struct timezone . 75.Pp 76The structures pointed to by 77.Fa tp 78and 79.Fa tzp 80are defined in 81.In sys/time.h . 82The first one is described in 83.Xr timeval 3 84and the latter legacy structure is defined as: 85.Bd -literal -offset indent 86struct timezone { 87 int tz_minuteswest; /* of Greenwich */ 88 int tz_dsttime; /* type of dst correction to apply */ 89}; 90.Ed 91.Pp 92The 93.Fa timezone 94structure is provided only for source compatibility. 95It is ignored by 96.Fn settimeofday , 97and 98.Fn gettimeofday 99will always return zeroes. 100.Pp 101The 102.Fn settimeofday 103system call is available only for the super-user. 104If the calling user is not the super-user, the system call will fail, and the 105.Fn settimeofday 106function in the standard C library will try to use the 107.Xr clockctl 4 108device if present, thus making it possible for non privileged users to 109set the system time. 110If 111.Xr clockctl 4 112is not present or not accessible, then 113.Fn settimeofday 114returns 115.Er EPERM . 116.\" XXX uncomment when/if this is put into place! 117.\" If the system is running in secure mode (see 118.\" .Xr init 8 ), 119.\" the time may only be advanced. 120.\" This limitation is imposed to prevent a malicious super user 121.\" from setting arbitrary time stamps on files. 122.\" The system time can still be adjusted backwards using the 123.\" .Xr adjtime 2 124.\" system call even when the system is secure. 125.Sh RETURN VALUES 126A return value 0 indicates that the call succeeded. 127A return value \-1 indicates an error occurred, and in this 128case an error code is stored into the global variable 129.Va errno . 130.Sh ERRORS 131The following error codes may be set in 132.Va errno : 133.Bl -tag -width Er 134.It Bq Er EFAULT 135An argument address referenced invalid memory. 136.It Bq Er EINVAL 137.Fn settimeofday : 138.Ar tp.tv_sec 139is outside the range [0..2^36] or 140.Ar tp.tv_usec 141is outside the range [0..999,999]. 142.It Bq Er EPERM 143A user other than the super user attempted to set the time, or the specified 144time was less than the current time, which was not permitted at the current 145security level. 146.El 147.Sh SEE ALSO 148.Xr date 1 , 149.Xr adjtime 2 , 150.Xr ctime 3 , 151.Xr localtime 3 , 152.Xr clockctl 4 , 153.Xr timed 8 154.Sh HISTORY 155The 156.Fn gettimeofday 157function call appeared in 158.Bx 4.2 . 159The 160.Fa tzp 161argument was deprecated in 162.Bx 4.4 163(and many other systems). 164