]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/gettimeofday.2
man*/: srcfix (Use .P instead of .PP or .LP)
[thirdparty/man-pages.git] / man2 / gettimeofday.2
CommitLineData
fea681da
MK
1.\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
2.\"
5fbde956 3.\" SPDX-License-Identifier: Linux-man-pages-copyleft
fea681da
MK
4.\"
5.\" Modified by Michael Haardt (michael@moria.de)
6.\" Modified 1993-07-23 by Rik Faith (faith@cs.unc.edu)
7.\" Modified 1994-08-21 by Michael Chastain (mec@shell.portal.com):
8.\" Fixed necessary '#include' lines.
9.\" Modified 1995-04-15 by Michael Chastain (mec@shell.portal.com):
10.\" Added reference to adjtimex.
11.\" Removed some nonsense lines pointed out by Urs Thuermann,
12.\" (urs@isnogud.escape.de), aeb, 950722.
13.\" Modified 1997-01-14 by Austin Donnelly (and1000@debian.org):
14.\" Added return values section, and bit on EFAULT
15.\" Added clarification on timezone, aeb, 971210.
16.\" Removed "#include <unistd.h>", aeb, 010316.
c11b1abf 17.\" Modified, 2004-05-27 by Michael Kerrisk <mtk.manpages@gmail.com>
fea681da
MK
18.\" Added notes on capability requirement.
19.\"
4c1c5274 20.TH gettimeofday 2 (date) "Linux man-pages (unreleased)"
fea681da
MK
21.SH NAME
22gettimeofday, settimeofday \- get / set time
3fb8d136
AC
23.SH LIBRARY
24Standard C library
8fc3b2cf 25.RI ( libc ", " \-lc )
fea681da 26.SH SYNOPSIS
a028f2ed 27.nf
fea681da 28.B #include <sys/time.h>
c6d039a3 29.P
1b213b4f 30.BI "int gettimeofday(struct timeval *restrict " tv ,
47419737 31.BI " struct timezone *_Nullable restrict " tz );
1b213b4f 32.BI "int settimeofday(const struct timeval *" tv ,
47419737 33.BI " const struct timezone *_Nullable " tz );
a028f2ed 34.fi
c6d039a3 35.P
d39ad78f 36.RS -4
cc4615cc
MK
37Feature Test Macro Requirements for glibc (see
38.BR feature_test_macros (7)):
d39ad78f 39.RE
c6d039a3 40.P
cc4615cc 41.BR settimeofday ():
9d281e06 42.nf
51c612fb
MK
43 Since glibc 2.19:
44 _DEFAULT_SOURCE
75c018a1 45 glibc 2.19 and earlier:
51c612fb 46 _BSD_SOURCE
9d281e06 47.fi
fea681da
MK
48.SH DESCRIPTION
49The functions
e511ffb6 50.BR gettimeofday ()
fea681da 51and
e511ffb6 52.BR settimeofday ()
fea681da 53can get and set the time as well as a timezone.
c6d039a3 54.P
c13182ef 55The
fea681da 56.I tv
c13182ef
MK
57argument is a
58.I struct timeval
0425de01 59(as specified in
8478ee02 60.IR <sys/time.h> ):
c6d039a3 61.P
a08ea57c 62.in +4n
58c2a94b 63.EX
fea681da 64struct timeval {
1c382af4
MK
65 time_t tv_sec; /* seconds */
66 suseconds_t tv_usec; /* microseconds */
fea681da 67};
58c2a94b 68.EE
a08ea57c 69.in
c6d039a3 70.P
fea681da
MK
71and gives the number of seconds and microseconds since the Epoch (see
72.BR time (2)).
c6d039a3 73.P
c13182ef 74The
fea681da 75.I tz
c13182ef 76argument is a
8478ee02 77.IR "struct timezone" :
c6d039a3 78.P
a08ea57c 79.in +4n
58c2a94b 80.EX
fea681da 81struct timezone {
79893ac3
MK
82 int tz_minuteswest; /* minutes west of Greenwich */
83 int tz_dsttime; /* type of DST correction */
fea681da 84};
58c2a94b 85.EE
a08ea57c 86.in
c6d039a3 87.P
1c382af4
MK
88If either
89.I tv
c13182ef 90or
1c382af4
MK
91.I tz
92is NULL, the corresponding structure is not set or returned.
b324e17d 93.\" FIXME . The compilation warning looks to be going away in glibc 2.17
f70f94e8 94.\" see glibc commit 4b7634a5e03b0da6f8875de9d3f74c1cf6f2a6e8
f1e90ec4
MK
95(However, compilation warnings will result if
96.I tv
97is NULL.)
1c382af4 98.\" The following is covered under EPERM below:
c6d039a3 99.\" .P
1c382af4
MK
100.\" Only the superuser may use
101.\" .BR settimeofday ().
c6d039a3 102.P
c13182ef
MK
103The use of the
104.I timezone
1c382af4
MK
105structure is obsolete; the
106.I tz
7145b9a9 107argument should normally be specified as NULL.
d5c4e197 108(See NOTES below.)
c6d039a3 109.P
eb9a0b2f 110Under Linux, there are some peculiar "warp clock" semantics associated
d2296c22 111with the
e511ffb6 112.BR settimeofday ()
fea681da
MK
113system call if on the very first call (after booting)
114that has a non-NULL
115.I tz
116argument, the
117.I tv
118argument is NULL and the
119.I tz_minuteswest
c7094399 120field is nonzero.
d5c4e197
MK
121(The
122.I tz_dsttime
123field should be zero for this case.)
c13182ef 124In such a case it is assumed that the CMOS clock
fea681da
MK
125is on local time, and that it has to be incremented by this amount
126to get UTC system time.
127No doubt it is a bad idea to use this feature.
47297adb 128.SH RETURN VALUE
e511ffb6 129.BR gettimeofday ()
fea681da 130and
e511ffb6 131.BR settimeofday ()
c112329f
MK
132return 0 for success.
133On error, \-1 is returned and
fea681da 134.I errno
c112329f 135is set to indicate the error.
fea681da
MK
136.SH ERRORS
137.TP
138.B EFAULT
c13182ef 139One of
fea681da
MK
140.I tv
141or
142.I tz
143pointed outside the accessible address space.
144.TP
145.B EINVAL
9a82d4d9
MK
146.RB ( settimeofday ()):
147.I timezone
148is invalid.
fea681da 149.TP
018c296c
MK
150.B EINVAL
151.RB ( settimeofday ()):
152.I tv.tv_sec
153is negative or
154.I tv.tv_usec
147a60d7 155is outside the range [0, 999,999].
018c296c 156.TP
bccacb94
MK
157.BR EINVAL " (since Linux 4.3)"
158.\" commit e1d7ba8735551ed79c7a0463a042353574b96da3
159.RB ( settimeofday ()):
160An attempt was made to set the time to a value less than
161the current value of the
162.B CLOCK_MONOTONIC
163clock (see
164.BR clock_gettime (2)).
165.TP
fea681da 166.B EPERM
c13182ef 167The calling process has insufficient privilege to call
e511ffb6 168.BR settimeofday ();
fea681da
MK
169under Linux the
170.B CAP_SYS_TIME
171capability is required.
4131356c
AC
172.SH VERSIONS
173.SS C library/kernel differences
174On some architectures, an implementation of
175.BR gettimeofday ()
176is provided in the
177.BR vdso (7).
c6d039a3 178.P
eacc876c
ZZ
179The kernel accepts NULL for both
180.I tv
181and
e670e8a6 182.IR tz .
eacc876c
ZZ
183The timezone argument is ignored by glibc and musl,
184and not passed to/from the kernel.
185Android's bionic passes the timezone argument to/from the kernel,
186but Android does not update the kernel timezone
187based on the device timezone in Settings,
188so the kernel's timezone is typically UTC.
3113c7f3 189.SH STANDARDS
4131356c
AC
190.TP
191.BR gettimeofday ()
192POSIX.1-2008 (obsolete).
193.TP
194.BR settimeofday ()
195None.
196.SH HISTORY
a1d5f77c
MK
197SVr4, 4.3BSD.
198POSIX.1-2001 describes
199.BR gettimeofday ()
200but not
201.BR settimeofday ().
f428c08a
MK
202POSIX.1-2008 marks
203.BR gettimeofday ()
4c9f4bc8 204as obsolete, recommending the use of
3f372391
MK
205.BR clock_gettime (2)
206instead.
c6d039a3 207.P
c13182ef
MK
208Traditionally, the fields of
209.I struct timeval
e3e25559
MK
210were of type
211.IR long .
53e0c293
MK
212.\"
213.SS The tz_dsttime field
43d6713e
CD
214On a non-Linux kernel, with glibc, the
215.I tz_dsttime
216field of
217.I struct timezone
53e0c293 218will be set to a nonzero value by
43d6713e
CD
219.BR gettimeofday ()
220if the current timezone has ever had or will have a daylight saving
53e0c293
MK
221rule applied.
222In this sense it exactly mirrors the meaning of
43d6713e
CD
223.BR daylight (3)
224for the current zone.
225On Linux, with glibc, the setting of the
d5c4e197 226.I tz_dsttime
43d6713e
CD
227field of
228.I struct timezone
229has never been used by
230.BR settimeofday ()
231or
232.BR gettimeofday ().
d5c4e197
MK
233.\" it has not
234.\" been and will not be supported by libc or glibc.
235.\" Each and every occurrence of this field in the kernel source
236.\" (other than the declaration) is a bug.
53e0c293 237Thus, the following is purely of historical interest.
c6d039a3 238.P
d5c4e197
MK
239On old systems, the field
240.I tz_dsttime
241contains a symbolic constant (values are given below)
242that indicates in which part of the year Daylight Saving Time
243is in force.
244(Note: this value is constant throughout the year:
245it does not indicate that DST is in force, it just selects an
246algorithm.)
247The daylight saving time algorithms defined are as follows:
c6d039a3 248.P
d5c4e197 249.in +4n
58c2a94b 250.EX
d5c4e197 251\fBDST_NONE\fP /* not on DST */
d5c4e197 252\fBDST_USA\fP /* USA style DST */
d5c4e197 253\fBDST_AUST\fP /* Australian style DST */
d5c4e197 254\fBDST_WET\fP /* Western European DST */
d5c4e197 255\fBDST_MET\fP /* Middle European DST */
d5c4e197 256\fBDST_EET\fP /* Eastern European DST */
d5c4e197 257\fBDST_CAN\fP /* Canada */
d5c4e197 258\fBDST_GB\fP /* Great Britain and Eire */
d5c4e197 259\fBDST_RUM\fP /* Romania */
d5c4e197 260\fBDST_TUR\fP /* Turkey */
d5c4e197 261\fBDST_AUSTALT\fP /* Australian style with shift in 1986 */
58c2a94b 262.EE
d5c4e197 263.in
c6d039a3 264.P
d5c4e197
MK
265Of course it turned out that the period in which
266Daylight Saving Time is in force cannot be given
267by a simple algorithm, one per country; indeed,
268this period is determined by unpredictable political
269decisions.
270So this method of representing timezones
271has been abandoned.
4131356c
AC
272.SH NOTES
273The time returned by
274.BR gettimeofday ()
275.I is
276affected by discontinuous jumps in the system time
277(e.g., if the system administrator manually changes the system time).
278If you need a monotonically increasing clock, see
279.BR clock_gettime (2).
c6d039a3 280.P
4131356c
AC
281Macros for operating on
282.I timeval
283structures are described in
284.BR timeradd (3).
47297adb 285.SH SEE ALSO
fea681da
MK
286.BR date (1),
287.BR adjtimex (2),
fb6fc612 288.BR clock_gettime (2),
fea681da
MK
289.BR time (2),
290.BR ctime (3),
291.BR ftime (3),
d5c4e197 292.BR timeradd (3),
eafd5ce1 293.BR capabilities (7),
7032b90a 294.BR time (7),
1ce611a3
MK
295.BR vdso (7),
296.BR hwclock (8)