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