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
88 gettimeofday()
89 POSIX.1-2008 (obsolete).
90
91 settimeofday()
92 None.
93
95 SVr4, 4.3BSD. POSIX.1-2001 describes gettimeofday() but not settimeof‐
96 day(). POSIX.1-2008 marks gettimeofday() as obsolete, recommending the
97 use of clock_gettime(2) instead.
98
99 Traditionally, the fields of struct timeval were of type long.
100
101 The tz_dsttime field
102 On a non-Linux kernel, with glibc, the tz_dsttime field of struct time‐
103 zone will be set to a nonzero value by gettimeofday() if the current
104 timezone has ever had or will have a daylight saving rule applied. In
105 this sense it exactly mirrors the meaning of daylight(3) for the cur‐
106 rent zone. On Linux, with glibc, the setting of the tz_dsttime field
107 of struct timezone has never been used by settimeofday() or gettimeof‐
108 day(). Thus, the following is purely of historical interest.
109
110 On old systems, the field tz_dsttime contains a symbolic constant (val‐
111 ues are given below) that indicates in which part of the year Daylight
112 Saving Time is in force. (Note: this value is constant throughout the
113 year: it does not indicate that DST is in force, it just selects an al‐
114 gorithm.) The daylight saving time algorithms defined are as follows:
115
116 DST_NONE /* not on DST */
117 DST_USA /* USA style DST */
118 DST_AUST /* Australian style DST */
119 DST_WET /* Western European DST */
120 DST_MET /* Middle European DST */
121 DST_EET /* Eastern European DST */
122 DST_CAN /* Canada */
123 DST_GB /* Great Britain and Eire */
124 DST_RUM /* Romania */
125 DST_TUR /* Turkey */
126 DST_AUSTALT /* Australian style with shift in 1986 */
127
128 Of course it turned out that the period in which Daylight Saving Time
129 is in force cannot be given by a simple algorithm, one per country; in‐
130 deed, this period is determined by unpredictable political decisions.
131 So this method of representing timezones has been abandoned.
132
134 The time returned by gettimeofday() is affected by discontinuous jumps
135 in the system time (e.g., if the system administrator manually changes
136 the system time). If you need a monotonically increasing clock, see
137 clock_gettime(2).
138
139 Macros for operating on timeval structures are described in timer‐
140 add(3).
141
143 date(1), adjtimex(2), clock_gettime(2), time(2), ctime(3), ftime(3),
144 timeradd(3), capabilities(7), time(7), vdso(7), hwclock(8)
145
146
147
148Linux man-pages 6.04 2023-03-30 gettimeofday(2)