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