1gettimeofday(2) System Calls Manual gettimeofday(2)
2
3
4
6 gettimeofday, settimeofday - get / set time
7
9 Standard C library (libc, -lc)
10
12 #include <sys/time.h>
13
14 int gettimeofday(struct timeval *restrict tv,
15 struct timezone *_Nullable restrict tz);
16 int settimeofday(const struct timeval *tv,
17 const struct timezone *_Nullable tz);
18
19 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
20
21 settimeofday():
22 Since glibc 2.19:
23 _DEFAULT_SOURCE
24 glibc 2.19 and earlier:
25 _BSD_SOURCE
26
28 The functions gettimeofday() and settimeofday() can get and set the
29 time as well as a timezone.
30
31 The tv argument is a struct timeval (as specified in <sys/time.h>):
32
33 struct timeval {
34 time_t tv_sec; /* seconds */
35 suseconds_t tv_usec; /* microseconds */
36 };
37
38 and gives the number of seconds and microseconds since the Epoch (see
39 time(2)).
40
41 The tz argument is a struct timezone:
42
43 struct timezone {
44 int tz_minuteswest; /* minutes west of Greenwich */
45 int tz_dsttime; /* type of DST correction */
46 };
47
48 If either tv or tz is NULL, the corresponding structure is not set or
49 returned. (However, compilation warnings will result if tv is NULL.)
50
51 The use of the timezone structure is obsolete; the tz argument should
52 normally be specified as NULL. (See NOTES below.)
53
54 Under Linux, there are some peculiar "warp clock" semantics associated
55 with the settimeofday() system call if on the very first call (after
56 booting) that has a non-NULL tz argument, the tv argument is NULL and
57 the tz_minuteswest field is nonzero. (The tz_dsttime field should be
58 zero for this case.) In such a case it is assumed that the CMOS clock
59 is on local time, and that it has to be incremented by this amount to
60 get UTC system time. No doubt it is a bad idea to use this feature.
61
63 gettimeofday() and settimeofday() return 0 for success. On error, -1
64 is returned and errno is set to indicate the error.
65
67 EFAULT One of tv or tz pointed outside the accessible address space.
68
69 EINVAL (settimeofday()): timezone is invalid.
70
71 EINVAL (settimeofday()): tv.tv_sec is negative or tv.tv_usec is outside
72 the range [0, 999,999].
73
74 EINVAL (since Linux 4.3)
75 (settimeofday()): An attempt was made to set the time to a value
76 less than the current value of the CLOCK_MONOTONIC clock (see
77 clock_gettime(2)).
78
79 EPERM The calling process has insufficient privilege to call settime‐
80 ofday(); under Linux the CAP_SYS_TIME capability is required.
81
83 C library/kernel differences
84 On some architectures, an implementation of gettimeofday() is provided
85 in the vdso(7).
86
87 The kernel accepts NULL for both tv and tz. The timezone argument is
88 ignored by glibc and musl, and not passed to/from the kernel. An‐
89 droid's bionic passes the timezone argument to/from the kernel, but An‐
90 droid does not update the kernel timezone based on the device timezone
91 in Settings, so the kernel's timezone is typically UTC.
92
94 gettimeofday()
95 POSIX.1-2008 (obsolete).
96
97 settimeofday()
98 None.
99
101 SVr4, 4.3BSD. POSIX.1-2001 describes gettimeofday() but not settimeof‐
102 day(). POSIX.1-2008 marks gettimeofday() as obsolete, recommending the
103 use of clock_gettime(2) instead.
104
105 Traditionally, the fields of struct timeval were of type long.
106
107 The tz_dsttime field
108 On a non-Linux kernel, with glibc, the tz_dsttime field of struct time‐
109 zone will be set to a nonzero value by gettimeofday() if the current
110 timezone has ever had or will have a daylight saving rule applied. In
111 this sense it exactly mirrors the meaning of daylight(3) for the cur‐
112 rent zone. On Linux, with glibc, the setting of the tz_dsttime field
113 of struct timezone has never been used by settimeofday() or gettimeof‐
114 day(). Thus, the following is purely of historical interest.
115
116 On old systems, the field tz_dsttime contains a symbolic constant (val‐
117 ues are given below) that indicates in which part of the year Daylight
118 Saving Time is in force. (Note: this value is constant throughout the
119 year: it does not indicate that DST is in force, it just selects an al‐
120 gorithm.) The daylight saving time algorithms defined are as follows:
121
122 DST_NONE /* not on DST */
123 DST_USA /* USA style DST */
124 DST_AUST /* Australian style DST */
125 DST_WET /* Western European DST */
126 DST_MET /* Middle European DST */
127 DST_EET /* Eastern European DST */
128 DST_CAN /* Canada */
129 DST_GB /* Great Britain and Eire */
130 DST_RUM /* Romania */
131 DST_TUR /* Turkey */
132 DST_AUSTALT /* Australian style with shift in 1986 */
133
134 Of course it turned out that the period in which Daylight Saving Time
135 is in force cannot be given by a simple algorithm, one per country; in‐
136 deed, this period is determined by unpredictable political decisions.
137 So this method of representing timezones has been abandoned.
138
140 The time returned by gettimeofday() is affected by discontinuous jumps
141 in the system time (e.g., if the system administrator manually changes
142 the system time). If you need a monotonically increasing clock, see
143 clock_gettime(2).
144
145 Macros for operating on timeval structures are described in timer‐
146 add(3).
147
149 date(1), adjtimex(2), clock_gettime(2), time(2), ctime(3), ftime(3),
150 timeradd(3), capabilities(7), time(7), vdso(7), hwclock(8)
151
152
153
154Linux man-pages 6.05 2023-07-28 gettimeofday(2)