1.\" $OpenBSD: gettimeofday.2,v 1.29 2015/09/10 17:55:21 schwarze 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 $Mdocdate: September 10 2015 $ 33.Dt GETTIMEOFDAY 2 34.Os 35.Sh NAME 36.Nm gettimeofday , 37.Nm settimeofday 38.Nd get/set date and time 39.Sh SYNOPSIS 40.In sys/time.h 41.Ft int 42.Fn gettimeofday "struct timeval *tp" "struct timezone *tzp" 43.Ft int 44.Fn settimeofday "const struct timeval *tp" "const struct timezone *tzp" 45.Sh DESCRIPTION 46.Bf -symbolic 47Note: timezone is no longer used; this information is kept outside 48the kernel. 49.Ef 50.Pp 51The system's notion of the current Greenwich time and the current time 52zone is obtained with the 53.Fn gettimeofday 54call, and set with the 55.Fn settimeofday 56call. 57The time is expressed in seconds and microseconds 58since midnight (0 hour), January 1, 1970. 59The resolution of the system clock is hardware dependent, and the time 60may be updated continuously or in 61.Dq ticks . 62If 63.Fa tp 64or 65.Fa tzp 66is 67.Dv NULL , 68the associated time 69information will not be returned or set. 70.Pp 71The structures pointed to by 72.Fa tp 73and 74.Fa tzp 75are defined in 76.In sys/time.h 77as: 78.Bd -literal 79struct timeval { 80 time_t tv_sec; /* seconds since Jan. 1, 1970 */ 81 suseconds_t 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 indicates the local time zone 93(measured in minutes of time westward from Greenwich), 94and a flag that, if nonzero, indicates that 95Daylight Saving time applies locally during 96the appropriate part of the year. 97.Pp 98Only the superuser may set the time of day or time zone. 99If the system securelevel is greater than 1 (see 100.Xr init 8 ) , 101the time may only be advanced. 102This limitation is imposed to prevent a malicious superuser 103from setting arbitrary time stamps on files. 104The system time can still be adjusted backwards using the 105.Xr adjtime 2 106system call even when the system is secure. 107.Sh RETURN VALUES 108.Rv -std 109.Sh ERRORS 110.Fn gettimeofday 111and 112.Fn settimeofday 113will succeed unless: 114.Bl -tag -width Er 115.It Bq Er EFAULT 116An argument address referenced invalid memory. 117.El 118.Pp 119In addition, 120.Fn settimeofday 121may return the following error: 122.Bl -tag -width Er 123.It Bq Er EPERM 124A user other than the superuser attempted to set the time. 125.El 126.Sh SEE ALSO 127.Xr date 1 , 128.Xr adjtime 2 , 129.Xr clock_gettime 2 , 130.Xr getitimer 2 , 131.Xr ctime 3 , 132.Xr time 3 133.Sh STANDARDS 134The 135.Fn gettimeofday 136function conforms to 137.St -p1003.1-2008 . 138.Sh HISTORY 139As predecessors of these functions, former system calls 140.Fn time 141and 142.Fn stime 143first appeared in 144.At v1 , 145and 146.Fn ftime 147first appeared in 148.At v7 . 149The 150.Fn gettimeofday 151and 152.Fn settimeofday 153system calls first appeared in 154.Bx 4.1c . 155.Sh CAVEATS 156Setting the time with 157.Fn settimeofday 158is dangerous; if possible use 159.Xr adjtime 2 160instead. 161Many daemon programming techniques utilize time-delta techniques 162using the results from 163.Fn gettimeofday 164instead of from 165.Xr clock_gettime 2 166on the 167.Dv CLOCK_MONOTONIC 168clock. 169Time jumps can cause these programs to malfunction in unexpected ways. 170If the time must be set, consider rebooting the machine for safety. 171