Start of man-pages-5.14: updating Changes and Changes.old
[man-pages.git] / man2 / gettimeofday.2
blob8101e311d10515da92ce22d49453e7e9fdca694b
1 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" Modified by Michael Haardt (michael@moria.de)
26 .\" Modified 1993-07-23 by Rik Faith (faith@cs.unc.edu)
27 .\" Modified 1994-08-21 by Michael Chastain (mec@shell.portal.com):
28 .\"   Fixed necessary '#include' lines.
29 .\" Modified 1995-04-15 by Michael Chastain (mec@shell.portal.com):
30 .\"   Added reference to adjtimex.
31 .\" Removed some nonsense lines pointed out by Urs Thuermann,
32 .\"   (urs@isnogud.escape.de), aeb, 950722.
33 .\" Modified 1997-01-14 by Austin Donnelly (and1000@debian.org):
34 .\"   Added return values section, and bit on EFAULT
35 .\" Added clarification on timezone, aeb, 971210.
36 .\" Removed "#include <unistd.h>", aeb, 010316.
37 .\" Modified, 2004-05-27 by Michael Kerrisk <mtk.manpages@gmail.com>
38 .\"   Added notes on capability requirement.
39 .\"
40 .TH GETTIMEOFDAY 2 2021-03-22 "Linux" "Linux Programmer's Manual"
41 .SH NAME
42 gettimeofday, settimeofday \- get / set time
43 .SH SYNOPSIS
44 .nf
45 .B #include <sys/time.h>
46 .PP
47 .BI "int gettimeofday(struct timeval *restrict " tv ,
48 .BI "                 struct timezone *restrict " tz );
49 .BI "int settimeofday(const struct timeval *" tv ,
50 .BI "                 const struct timezone *" tz );
51 .fi
52 .PP
53 .RS -4
54 Feature Test Macro Requirements for glibc (see
55 .BR feature_test_macros (7)):
56 .RE
57 .PP
58 .BR settimeofday ():
59 .nf
60     Since glibc 2.19:
61         _DEFAULT_SOURCE
62     Glibc 2.19 and earlier:
63         _BSD_SOURCE
64 .fi
65 .SH DESCRIPTION
66 The functions
67 .BR gettimeofday ()
68 and
69 .BR settimeofday ()
70 can get and set the time as well as a timezone.
71 .PP
72 The
73 .I tv
74 argument is a
75 .I struct timeval
76 (as specified in
77 .IR <sys/time.h> ):
78 .PP
79 .in +4n
80 .EX
81 struct timeval {
82     time_t      tv_sec;     /* seconds */
83     suseconds_t tv_usec;    /* microseconds */
85 .EE
86 .in
87 .PP
88 and gives the number of seconds and microseconds since the Epoch (see
89 .BR time (2)).
90 .PP
91 The
92 .I tz
93 argument is a
94 .IR "struct timezone" :
95 .PP
96 .in +4n
97 .EX
98 struct timezone {
99     int tz_minuteswest;     /* minutes west of Greenwich */
100     int tz_dsttime;         /* type of DST correction */
105 If either
106 .I tv
108 .I tz
109 is NULL, the corresponding structure is not set or returned.
110 .\" FIXME . The compilation warning looks to be going away in 2.17
111 .\" see glibc commit 4b7634a5e03b0da6f8875de9d3f74c1cf6f2a6e8
112 (However, compilation warnings will result if
113 .I tv
114 is NULL.)
115 .\" The following is covered under EPERM below:
116 .\" .PP
117 .\" Only the superuser may use
118 .\" .BR settimeofday ().
120 The use of the
121 .I timezone
122 structure is obsolete; the
123 .I tz
124 argument should normally be specified as NULL.
125 (See NOTES below.)
127 Under Linux, there are some peculiar "warp clock" semantics associated
128 with the
129 .BR settimeofday ()
130 system call if on the very first call (after booting)
131 that has a non-NULL
132 .I tz
133 argument, the
134 .I tv
135 argument is NULL and the
136 .I tz_minuteswest
137 field is nonzero.
138 (The
139 .I tz_dsttime
140 field should be zero for this case.)
141 In such a case it is assumed that the CMOS clock
142 is on local time, and that it has to be incremented by this amount
143 to get UTC system time.
144 No doubt it is a bad idea to use this feature.
145 .SH RETURN VALUE
146 .BR gettimeofday ()
148 .BR settimeofday ()
149 return 0 for success.
150 On error, \-1 is returned and
151 .I errno
152 is set to indicate the error.
153 .SH ERRORS
155 .B EFAULT
156 One of
157 .I tv
159 .I tz
160 pointed outside the accessible address space.
162 .B EINVAL
163 .RB ( settimeofday ()):
164 .I timezone
165 is invalid.
167 .B EINVAL
168 .RB ( settimeofday ()):
169 .I tv.tv_sec
170 is negative or
171 .I tv.tv_usec
172 is outside the range [0..999,999].
174 .BR EINVAL " (since Linux 4.3)"
175 .\" commit e1d7ba8735551ed79c7a0463a042353574b96da3
176 .RB ( settimeofday ()):
177 An attempt was made to set the time to a value less than
178 the current value of the
179 .B CLOCK_MONOTONIC
180 clock (see
181 .BR clock_gettime (2)).
183 .B EPERM
184 The calling process has insufficient privilege to call
185 .BR settimeofday ();
186 under Linux the
187 .B CAP_SYS_TIME
188 capability is required.
189 .SH CONFORMING TO
190 SVr4, 4.3BSD.
191 POSIX.1-2001 describes
192 .BR gettimeofday ()
193 but not
194 .BR settimeofday ().
195 POSIX.1-2008 marks
196 .BR gettimeofday ()
197 as obsolete, recommending the use of
198 .BR clock_gettime (2)
199 instead.
200 .SH NOTES
201 The time returned by
202 .BR gettimeofday ()
203 .I is
204 affected by discontinuous jumps in the system time
205 (e.g., if the system administrator manually changes the system time).
206 If you need a monotonically increasing clock, see
207 .BR clock_gettime (2).
209 Macros for operating on
210 .I timeval
211 structures are described in
212 .BR timeradd (3).
214 Traditionally, the fields of
215 .I struct timeval
216 were of type
217 .IR long .
219 .SS C library/kernel differences
220 On some architectures, an implementation of
221 .BR gettimeofday ()
222 is provided in the
223 .BR vdso (7).
225 .SS The tz_dsttime field
226 On a non-Linux kernel, with glibc, the
227 .I tz_dsttime
228 field of
229 .I struct timezone
230 will be set to a nonzero value by
231 .BR gettimeofday ()
232 if the current timezone has ever had or will have a daylight saving
233 rule applied.
234 In this sense it exactly mirrors the meaning of
235 .BR daylight (3)
236 for the current zone.
237 On Linux, with glibc, the setting of the
238 .I tz_dsttime
239 field of
240 .I struct timezone
241 has never been used by
242 .BR settimeofday ()
244 .BR gettimeofday ().
245 .\" it has not
246 .\" been and will not be supported by libc or glibc.
247 .\" Each and every occurrence of this field in the kernel source
248 .\" (other than the declaration) is a bug.
249 Thus, the following is purely of historical interest.
251 On old systems, the field
252 .I tz_dsttime
253 contains a symbolic constant (values are given below)
254 that indicates in which part of the year Daylight Saving Time
255 is in force.
256 (Note: this value is constant throughout the year:
257 it does not indicate that DST is in force, it just selects an
258 algorithm.)
259 The daylight saving time algorithms defined are as follows:
261 .in +4n
263 \fBDST_NONE\fP     /* not on DST */
264 \fBDST_USA\fP      /* USA style DST */
265 \fBDST_AUST\fP     /* Australian style DST */
266 \fBDST_WET\fP      /* Western European DST */
267 \fBDST_MET\fP      /* Middle European DST */
268 \fBDST_EET\fP      /* Eastern European DST */
269 \fBDST_CAN\fP      /* Canada */
270 \fBDST_GB\fP       /* Great Britain and Eire */
271 \fBDST_RUM\fP      /* Romania */
272 \fBDST_TUR\fP      /* Turkey */
273 \fBDST_AUSTALT\fP  /* Australian style with shift in 1986 */
277 Of course it turned out that the period in which
278 Daylight Saving Time is in force cannot be given
279 by a simple algorithm, one per country; indeed,
280 this period is determined by unpredictable political
281 decisions.
282 So this method of representing timezones
283 has been abandoned.
284 .SH SEE ALSO
285 .BR date (1),
286 .BR adjtimex (2),
287 .BR clock_gettime (2),
288 .BR time (2),
289 .BR ctime (3),
290 .BR ftime (3),
291 .BR timeradd (3),
292 .BR capabilities (7),
293 .BR time (7),
294 .BR vdso (7),
295 .BR hwclock (8)