]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-time-util.c
test: unset TZ before timezone-sensitive unit tests are run
[thirdparty/systemd.git] / src / test / test-time-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "env-util.h"
4 #include "fd-util.h"
5 #include "fileio.h"
6 #include "random-util.h"
7 #include "serialize.h"
8 #include "string-util.h"
9 #include "strv.h"
10 #include "tests.h"
11 #include "time-util.h"
12
13 #define TRIAL 100u
14
15 TEST(parse_sec) {
16 usec_t u;
17
18 assert_se(parse_sec("5s", &u) >= 0);
19 assert_se(u == 5 * USEC_PER_SEC);
20 assert_se(parse_sec("5s500ms", &u) >= 0);
21 assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
22 assert_se(parse_sec(" 5s 500ms ", &u) >= 0);
23 assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
24 assert_se(parse_sec(" 5.5s ", &u) >= 0);
25 assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
26 assert_se(parse_sec(" 5.5s 0.5ms ", &u) >= 0);
27 assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC + 500);
28 assert_se(parse_sec(" .22s ", &u) >= 0);
29 assert_se(u == 220 * USEC_PER_MSEC);
30 assert_se(parse_sec(" .50y ", &u) >= 0);
31 assert_se(u == USEC_PER_YEAR / 2);
32 assert_se(parse_sec("2.5", &u) >= 0);
33 assert_se(u == 2500 * USEC_PER_MSEC);
34 assert_se(parse_sec(".7", &u) >= 0);
35 assert_se(u == 700 * USEC_PER_MSEC);
36 assert_se(parse_sec("23us", &u) >= 0);
37 assert_se(u == 23);
38 assert_se(parse_sec("23μs", &u) >= 0); /* greek small letter mu */
39 assert_se(u == 23);
40 assert_se(parse_sec("23µs", &u) >= 0); /* micro symbol */
41 assert_se(u == 23);
42 assert_se(parse_sec("infinity", &u) >= 0);
43 assert_se(u == USEC_INFINITY);
44 assert_se(parse_sec(" infinity ", &u) >= 0);
45 assert_se(u == USEC_INFINITY);
46 assert_se(parse_sec("+3.1s", &u) >= 0);
47 assert_se(u == 3100 * USEC_PER_MSEC);
48 assert_se(parse_sec("3.1s.2", &u) >= 0);
49 assert_se(u == 3300 * USEC_PER_MSEC);
50 assert_se(parse_sec("3.1 .2", &u) >= 0);
51 assert_se(u == 3300 * USEC_PER_MSEC);
52 assert_se(parse_sec("3.1 sec .2 sec", &u) >= 0);
53 assert_se(u == 3300 * USEC_PER_MSEC);
54 assert_se(parse_sec("3.1 sec 1.2 sec", &u) >= 0);
55 assert_se(u == 4300 * USEC_PER_MSEC);
56
57 assert_se(parse_sec(" xyz ", &u) < 0);
58 assert_se(parse_sec("", &u) < 0);
59 assert_se(parse_sec(" . ", &u) < 0);
60 assert_se(parse_sec(" 5. ", &u) < 0);
61 assert_se(parse_sec(".s ", &u) < 0);
62 assert_se(parse_sec("-5s ", &u) < 0);
63 assert_se(parse_sec("-0.3s ", &u) < 0);
64 assert_se(parse_sec("-0.0s ", &u) < 0);
65 assert_se(parse_sec("-0.-0s ", &u) < 0);
66 assert_se(parse_sec("0.-0s ", &u) < 0);
67 assert_se(parse_sec("3.-0s ", &u) < 0);
68 assert_se(parse_sec(" infinity .7", &u) < 0);
69 assert_se(parse_sec(".3 infinity", &u) < 0);
70 assert_se(parse_sec("3.+1s", &u) < 0);
71 assert_se(parse_sec("3. 1s", &u) < 0);
72 assert_se(parse_sec("3.s", &u) < 0);
73 assert_se(parse_sec("12.34.56", &u) < 0);
74 assert_se(parse_sec("12..34", &u) < 0);
75 assert_se(parse_sec("..1234", &u) < 0);
76 assert_se(parse_sec("1234..", &u) < 0);
77 }
78
79 TEST(parse_sec_fix_0) {
80 usec_t u;
81
82 assert_se(parse_sec_fix_0("5s", &u) >= 0);
83 assert_se(u == 5 * USEC_PER_SEC);
84 assert_se(parse_sec_fix_0("0s", &u) >= 0);
85 assert_se(u == USEC_INFINITY);
86 assert_se(parse_sec_fix_0("0", &u) >= 0);
87 assert_se(u == USEC_INFINITY);
88 assert_se(parse_sec_fix_0(" 0", &u) >= 0);
89 assert_se(u == USEC_INFINITY);
90 }
91
92 TEST(parse_sec_def_infinity) {
93 usec_t u;
94
95 assert_se(parse_sec_def_infinity("5s", &u) >= 0);
96 assert_se(u == 5 * USEC_PER_SEC);
97 assert_se(parse_sec_def_infinity("", &u) >= 0);
98 assert_se(u == USEC_INFINITY);
99 assert_se(parse_sec_def_infinity(" ", &u) >= 0);
100 assert_se(u == USEC_INFINITY);
101 assert_se(parse_sec_def_infinity("0s", &u) >= 0);
102 assert_se(u == 0);
103 assert_se(parse_sec_def_infinity("0", &u) >= 0);
104 assert_se(u == 0);
105 assert_se(parse_sec_def_infinity(" 0", &u) >= 0);
106 assert_se(u == 0);
107 assert_se(parse_sec_def_infinity("-5s", &u) < 0);
108 }
109
110 TEST(parse_time) {
111 usec_t u;
112
113 assert_se(parse_time("5", &u, 1) >= 0);
114 assert_se(u == 5);
115
116 assert_se(parse_time("5", &u, USEC_PER_MSEC) >= 0);
117 assert_se(u == 5 * USEC_PER_MSEC);
118
119 assert_se(parse_time("5", &u, USEC_PER_SEC) >= 0);
120 assert_se(u == 5 * USEC_PER_SEC);
121
122 assert_se(parse_time("5s", &u, 1) >= 0);
123 assert_se(u == 5 * USEC_PER_SEC);
124
125 assert_se(parse_time("5s", &u, USEC_PER_SEC) >= 0);
126 assert_se(u == 5 * USEC_PER_SEC);
127
128 assert_se(parse_time("5s", &u, USEC_PER_MSEC) >= 0);
129 assert_se(u == 5 * USEC_PER_SEC);
130
131 assert_se(parse_time("11111111111111y", &u, 1) == -ERANGE);
132 assert_se(parse_time("1.1111111111111y", &u, 1) >= 0);
133 }
134
135 TEST(parse_nsec) {
136 nsec_t u;
137
138 assert_se(parse_nsec("5s", &u) >= 0);
139 assert_se(u == 5 * NSEC_PER_SEC);
140 assert_se(parse_nsec("5s500ms", &u) >= 0);
141 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
142 assert_se(parse_nsec(" 5s 500ms ", &u) >= 0);
143 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
144 assert_se(parse_nsec(" 5.5s ", &u) >= 0);
145 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
146 assert_se(parse_nsec(" 5.5s 0.5ms ", &u) >= 0);
147 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC + 500 * NSEC_PER_USEC);
148 assert_se(parse_nsec(" .22s ", &u) >= 0);
149 assert_se(u == 220 * NSEC_PER_MSEC);
150 assert_se(parse_nsec(" .50y ", &u) >= 0);
151 assert_se(u == NSEC_PER_YEAR / 2);
152 assert_se(parse_nsec("2.5", &u) >= 0);
153 assert_se(u == 2);
154 assert_se(parse_nsec(".7", &u) >= 0);
155 assert_se(u == 0);
156 assert_se(parse_nsec("infinity", &u) >= 0);
157 assert_se(u == NSEC_INFINITY);
158 assert_se(parse_nsec(" infinity ", &u) >= 0);
159 assert_se(u == NSEC_INFINITY);
160 assert_se(parse_nsec("+3.1s", &u) >= 0);
161 assert_se(u == 3100 * NSEC_PER_MSEC);
162 assert_se(parse_nsec("3.1s.2", &u) >= 0);
163 assert_se(u == 3100 * NSEC_PER_MSEC);
164 assert_se(parse_nsec("3.1 .2s", &u) >= 0);
165 assert_se(u == 200 * NSEC_PER_MSEC + 3);
166 assert_se(parse_nsec("3.1 sec .2 sec", &u) >= 0);
167 assert_se(u == 3300 * NSEC_PER_MSEC);
168 assert_se(parse_nsec("3.1 sec 1.2 sec", &u) >= 0);
169 assert_se(u == 4300 * NSEC_PER_MSEC);
170
171 assert_se(parse_nsec(" xyz ", &u) < 0);
172 assert_se(parse_nsec("", &u) < 0);
173 assert_se(parse_nsec(" . ", &u) < 0);
174 assert_se(parse_nsec(" 5. ", &u) < 0);
175 assert_se(parse_nsec(".s ", &u) < 0);
176 assert_se(parse_nsec(" infinity .7", &u) < 0);
177 assert_se(parse_nsec(".3 infinity", &u) < 0);
178 assert_se(parse_nsec("-5s ", &u) < 0);
179 assert_se(parse_nsec("-0.3s ", &u) < 0);
180 assert_se(parse_nsec("-0.0s ", &u) < 0);
181 assert_se(parse_nsec("-0.-0s ", &u) < 0);
182 assert_se(parse_nsec("0.-0s ", &u) < 0);
183 assert_se(parse_nsec("3.-0s ", &u) < 0);
184 assert_se(parse_nsec(" infinity .7", &u) < 0);
185 assert_se(parse_nsec(".3 infinity", &u) < 0);
186 assert_se(parse_nsec("3.+1s", &u) < 0);
187 assert_se(parse_nsec("3. 1s", &u) < 0);
188 assert_se(parse_nsec("3.s", &u) < 0);
189 assert_se(parse_nsec("12.34.56", &u) < 0);
190 assert_se(parse_nsec("12..34", &u) < 0);
191 assert_se(parse_nsec("..1234", &u) < 0);
192 assert_se(parse_nsec("1234..", &u) < 0);
193 assert_se(parse_nsec("1111111111111y", &u) == -ERANGE);
194 assert_se(parse_nsec("1.111111111111y", &u) >= 0);
195 }
196
197 static void test_format_timespan_one(usec_t x, usec_t accuracy) {
198 char l[FORMAT_TIMESPAN_MAX];
199 const char *t;
200 usec_t y;
201
202 log_debug(USEC_FMT" (at accuracy "USEC_FMT")", x, accuracy);
203
204 assert_se(t = format_timespan(l, sizeof l, x, accuracy));
205 log_debug(" = <%s>", t);
206
207 assert_se(parse_sec(t, &y) >= 0);
208 log_debug(" = "USEC_FMT, y);
209
210 if (accuracy <= 0)
211 accuracy = 1;
212
213 assert_se(x / accuracy == y / accuracy);
214 }
215
216 static void test_format_timespan_accuracy(usec_t accuracy) {
217 log_info("/* %s accuracy="USEC_FMT" */", __func__, accuracy);
218
219 test_format_timespan_one(0, accuracy);
220 test_format_timespan_one(1, accuracy);
221 test_format_timespan_one(1*USEC_PER_SEC, accuracy);
222 test_format_timespan_one(999*USEC_PER_MSEC, accuracy);
223 test_format_timespan_one(1234567, accuracy);
224 test_format_timespan_one(12, accuracy);
225 test_format_timespan_one(123, accuracy);
226 test_format_timespan_one(1234, accuracy);
227 test_format_timespan_one(12345, accuracy);
228 test_format_timespan_one(123456, accuracy);
229 test_format_timespan_one(1234567, accuracy);
230 test_format_timespan_one(12345678, accuracy);
231 test_format_timespan_one(1200000, accuracy);
232 test_format_timespan_one(1230000, accuracy);
233 test_format_timespan_one(1234000, accuracy);
234 test_format_timespan_one(1234500, accuracy);
235 test_format_timespan_one(1234560, accuracy);
236 test_format_timespan_one(1234567, accuracy);
237 test_format_timespan_one(986087, accuracy);
238 test_format_timespan_one(500 * USEC_PER_MSEC, accuracy);
239 test_format_timespan_one(9*USEC_PER_YEAR/5 - 23, accuracy);
240 test_format_timespan_one(USEC_INFINITY, accuracy);
241 }
242
243 TEST(format_timespan) {
244 test_format_timespan_accuracy(1);
245 test_format_timespan_accuracy(USEC_PER_MSEC);
246 test_format_timespan_accuracy(USEC_PER_SEC);
247
248 /* See issue #23928. */
249 _cleanup_free_ char *buf = NULL;
250 assert_se(buf = new(char, 5));
251 assert_se(buf == format_timespan(buf, 5, 100005, 1000));
252 }
253
254 TEST(verify_timezone) {
255 assert_se(verify_timezone("Europe/Berlin", LOG_DEBUG) == 0);
256 assert_se(verify_timezone("Australia/Sydney", LOG_DEBUG) == 0);
257 assert_se(verify_timezone("Europe/Do not exist", LOG_DEBUG) == -EINVAL);
258 assert_se(verify_timezone("Europe/DoNotExist", LOG_DEBUG) == -ENOENT);
259 assert_se(verify_timezone("/DoNotExist", LOG_DEBUG) == -EINVAL);
260 assert_se(verify_timezone("DoNotExist/", LOG_DEBUG) == -EINVAL);
261 }
262
263 TEST(timezone_is_valid) {
264 assert_se(timezone_is_valid("Europe/Berlin", LOG_ERR));
265 assert_se(timezone_is_valid("Australia/Sydney", LOG_ERR));
266 assert_se(!timezone_is_valid("Europe/Do not exist", LOG_ERR));
267 }
268
269 TEST(get_timezones) {
270 _cleanup_strv_free_ char **zones = NULL;
271 int r;
272
273 r = get_timezones(&zones);
274 assert_se(r == 0);
275
276 STRV_FOREACH(zone, zones) {
277 r = verify_timezone(*zone, LOG_ERR);
278 log_debug_errno(r, "verify_timezone(\"%s\"): %m", *zone);
279 assert_se(r >= 0 || r == -ENOENT);
280 }
281 }
282
283 TEST(usec_add) {
284 assert_se(usec_add(0, 0) == 0);
285 assert_se(usec_add(1, 4) == 5);
286 assert_se(usec_add(USEC_INFINITY, 5) == USEC_INFINITY);
287 assert_se(usec_add(5, USEC_INFINITY) == USEC_INFINITY);
288 assert_se(usec_add(USEC_INFINITY-5, 2) == USEC_INFINITY-3);
289 assert_se(usec_add(USEC_INFINITY-2, 2) == USEC_INFINITY);
290 assert_se(usec_add(USEC_INFINITY-1, 2) == USEC_INFINITY);
291 assert_se(usec_add(USEC_INFINITY, 2) == USEC_INFINITY);
292 }
293
294 TEST(usec_sub_unsigned) {
295 assert_se(usec_sub_unsigned(0, 0) == 0);
296 assert_se(usec_sub_unsigned(0, 2) == 0);
297 assert_se(usec_sub_unsigned(0, USEC_INFINITY) == 0);
298 assert_se(usec_sub_unsigned(1, 0) == 1);
299 assert_se(usec_sub_unsigned(1, 1) == 0);
300 assert_se(usec_sub_unsigned(1, 2) == 0);
301 assert_se(usec_sub_unsigned(1, 3) == 0);
302 assert_se(usec_sub_unsigned(1, USEC_INFINITY) == 0);
303 assert_se(usec_sub_unsigned(USEC_INFINITY-1, 0) == USEC_INFINITY-1);
304 assert_se(usec_sub_unsigned(USEC_INFINITY-1, 1) == USEC_INFINITY-2);
305 assert_se(usec_sub_unsigned(USEC_INFINITY-1, 2) == USEC_INFINITY-3);
306 assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY-2) == 1);
307 assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY-1) == 0);
308 assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY) == 0);
309 assert_se(usec_sub_unsigned(USEC_INFINITY, 0) == USEC_INFINITY);
310 assert_se(usec_sub_unsigned(USEC_INFINITY, 1) == USEC_INFINITY);
311 assert_se(usec_sub_unsigned(USEC_INFINITY, 2) == USEC_INFINITY);
312 assert_se(usec_sub_unsigned(USEC_INFINITY, USEC_INFINITY) == USEC_INFINITY);
313 }
314
315 TEST(usec_sub_signed) {
316 assert_se(usec_sub_signed(0, 0) == 0);
317 assert_se(usec_sub_signed(4, 1) == 3);
318 assert_se(usec_sub_signed(4, 4) == 0);
319 assert_se(usec_sub_signed(4, 5) == 0);
320
321 assert_se(usec_sub_signed(USEC_INFINITY-3, -3) == USEC_INFINITY);
322 assert_se(usec_sub_signed(USEC_INFINITY-3, -4) == USEC_INFINITY);
323 assert_se(usec_sub_signed(USEC_INFINITY-3, -5) == USEC_INFINITY);
324 assert_se(usec_sub_signed(USEC_INFINITY, 5) == USEC_INFINITY);
325
326 assert_se(usec_sub_signed(0, INT64_MAX) == 0);
327 assert_se(usec_sub_signed(0, -INT64_MAX) == INT64_MAX);
328 assert_se(usec_sub_signed(0, INT64_MIN) == (usec_t) INT64_MAX + 1);
329 assert_se(usec_sub_signed(0, -(INT64_MIN+1)) == 0);
330
331 assert_se(usec_sub_signed(USEC_INFINITY, INT64_MAX) == USEC_INFINITY);
332 assert_se(usec_sub_signed(USEC_INFINITY, -INT64_MAX) == USEC_INFINITY);
333 assert_se(usec_sub_signed(USEC_INFINITY, INT64_MIN) == USEC_INFINITY);
334 assert_se(usec_sub_signed(USEC_INFINITY, -(INT64_MIN+1)) == USEC_INFINITY);
335
336 assert_se(usec_sub_signed(USEC_INFINITY-1, INT64_MAX) == USEC_INFINITY-1-INT64_MAX);
337 assert_se(usec_sub_signed(USEC_INFINITY-1, -INT64_MAX) == USEC_INFINITY);
338 assert_se(usec_sub_signed(USEC_INFINITY-1, INT64_MIN) == USEC_INFINITY);
339 assert_se(usec_sub_signed(USEC_INFINITY-1, -(INT64_MIN+1)) == USEC_INFINITY-1-((usec_t) (-(INT64_MIN+1))));
340 }
341
342 TEST(format_timestamp) {
343 for (unsigned i = 0; i < TRIAL; i++) {
344 char buf[CONST_MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
345 usec_t x, y;
346
347 x = random_u64_range(USEC_TIMESTAMP_FORMATTABLE_MAX - USEC_PER_SEC) + USEC_PER_SEC;
348
349 assert_se(format_timestamp(buf, sizeof(buf), x));
350 log_debug("%s", buf);
351 assert_se(parse_timestamp(buf, &y) >= 0);
352 assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
353
354 assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_UNIX));
355 log_debug("%s", buf);
356 assert_se(parse_timestamp(buf, &y) >= 0);
357 assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
358
359 assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_UTC));
360 log_debug("%s", buf);
361 assert_se(parse_timestamp(buf, &y) >= 0);
362 assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
363
364 assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_US));
365 log_debug("%s", buf);
366 assert_se(parse_timestamp(buf, &y) >= 0);
367 assert_se(x == y);
368
369 assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_US_UTC));
370 log_debug("%s", buf);
371 assert_se(parse_timestamp(buf, &y) >= 0);
372 assert_se(x == y);
373
374 if (x > 2 * USEC_PER_DAY) {
375 assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_DATE));
376 log_debug("%s", buf);
377 assert_se(parse_timestamp(buf, &y) >= 0);
378 assert_se(y > usec_sub_unsigned(x, 2 * USEC_PER_DAY) && y < usec_add(x, 2 * USEC_PER_DAY));
379 }
380
381 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
382 log_debug("%s", buf);
383 assert_se(parse_timestamp(buf, &y) >= 0);
384
385 /* The two calls above will run with a slightly different local time. Make sure we are in the same
386 * range however, but give enough leeway that this is unlikely to explode. And of course,
387 * format_timestamp_relative() scales the accuracy with the distance from the current time up to one
388 * month, cover for that too. */
389 assert_se(y > x ? y - x : x - y <= USEC_PER_MONTH + USEC_PER_DAY);
390 }
391 }
392
393 static void test_format_timestamp_impl(usec_t x) {
394 bool success, override;
395 const char *xx, *yy;
396 usec_t y;
397
398 xx = FORMAT_TIMESTAMP(x);
399 assert_se(xx);
400 assert_se(parse_timestamp(xx, &y) >= 0);
401 yy = FORMAT_TIMESTAMP(y);
402 assert_se(yy);
403
404 success = (x / USEC_PER_SEC == y / USEC_PER_SEC) && streq(xx, yy);
405 /* Workaround for https://github.com/systemd/systemd/issues/28472 */
406 override = !success &&
407 (STRPTR_IN_SET(tzname[0], "CAT", "EAT") ||
408 STRPTR_IN_SET(tzname[1], "CAT", "EAT")) &&
409 DIV_ROUND_UP(y - x, USEC_PER_SEC) == 3600; /* 1 hour, ignore fractional second */
410 log_full(success ? LOG_DEBUG : override ? LOG_WARNING : LOG_ERR,
411 "@" USEC_FMT " → %s → @" USEC_FMT " → %s%s",
412 x, xx, y, yy,
413 override ? ", ignoring." : "");
414 if (!override) {
415 assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
416 assert_se(streq(xx, yy));
417 }
418 }
419
420 static void test_format_timestamp_loop(void) {
421 test_format_timestamp_impl(USEC_PER_SEC);
422 test_format_timestamp_impl(USEC_TIMESTAMP_FORMATTABLE_MAX_32BIT-1);
423 test_format_timestamp_impl(USEC_TIMESTAMP_FORMATTABLE_MAX_32BIT);
424 test_format_timestamp_impl(USEC_TIMESTAMP_FORMATTABLE_MAX-1);
425 test_format_timestamp_impl(USEC_TIMESTAMP_FORMATTABLE_MAX);
426
427 /* Two cases which trigger https://github.com/systemd/systemd/issues/28472 */
428 test_format_timestamp_impl(1504938962980066);
429 test_format_timestamp_impl(1509482094632752);
430
431 for (unsigned i = 0; i < TRIAL; i++) {
432 usec_t x;
433
434 x = random_u64_range(USEC_TIMESTAMP_FORMATTABLE_MAX - USEC_PER_SEC) + USEC_PER_SEC;
435 test_format_timestamp_impl(x);
436 }
437 }
438
439 TEST(FORMAT_TIMESTAMP) {
440 test_format_timestamp_loop();
441 }
442
443 static void test_format_timestamp_with_tz_one(const char *tz) {
444 const char *saved_tz, *colon_tz;
445
446 if (!timezone_is_valid(tz, LOG_DEBUG))
447 return;
448
449 log_info("/* %s(%s) */", __func__, tz);
450
451 saved_tz = getenv("TZ");
452
453 assert_se(colon_tz = strjoina(":", tz));
454 assert_se(setenv("TZ", colon_tz, 1) >= 0);
455 tzset();
456 log_debug("%s: tzname[0]=%s, tzname[1]=%s", tz, strempty(tzname[0]), strempty(tzname[1]));
457
458 test_format_timestamp_loop();
459
460 assert_se(set_unset_env("TZ", saved_tz, true) == 0);
461 tzset();
462 }
463
464 TEST(FORMAT_TIMESTAMP_with_tz) {
465 _cleanup_strv_free_ char **timezones = NULL;
466
467 test_format_timestamp_with_tz_one("UTC");
468
469 if (!slow_tests_enabled())
470 return (void) log_tests_skipped("slow tests are disabled");
471
472 assert_se(get_timezones(&timezones) >= 0);
473 STRV_FOREACH(tz, timezones)
474 test_format_timestamp_with_tz_one(*tz);
475 }
476
477 TEST(format_timestamp_relative_full) {
478 char buf[CONST_MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
479 usec_t x;
480
481 /* Years and months */
482 x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 1*USEC_PER_MONTH);
483 assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
484 log_debug("%s", buf);
485 assert_se(streq(buf, "1 year 1 month ago"));
486
487 x = now(CLOCK_MONOTONIC) + (1*USEC_PER_YEAR + 1.5*USEC_PER_MONTH);
488 assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_MONOTONIC, false));
489 log_debug("%s", buf);
490 assert_se(streq(buf, "1 year 1 month left"));
491
492 x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 2*USEC_PER_MONTH);
493 assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
494 log_debug("%s", buf);
495 assert_se(streq(buf, "1 year 2 months ago"));
496
497 x = now(CLOCK_REALTIME) - (2*USEC_PER_YEAR + 1*USEC_PER_MONTH);
498 assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
499 log_debug("%s", buf);
500 assert_se(streq(buf, "2 years 1 month ago"));
501
502 x = now(CLOCK_REALTIME) - (2*USEC_PER_YEAR + 2*USEC_PER_MONTH);
503 assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
504 log_debug("%s", buf);
505 assert_se(streq(buf, "2 years 2 months ago"));
506
507 /* Months and days */
508 x = now(CLOCK_REALTIME) - (1*USEC_PER_MONTH + 1*USEC_PER_DAY);
509 assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
510 log_debug("%s", buf);
511 assert_se(streq(buf, "1 month 1 day ago"));
512
513 x = now(CLOCK_REALTIME) - (1*USEC_PER_MONTH + 2*USEC_PER_DAY);
514 assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
515 log_debug("%s", buf);
516 assert_se(streq(buf, "1 month 2 days ago"));
517
518 x = now(CLOCK_REALTIME) - (2*USEC_PER_MONTH + 1*USEC_PER_DAY);
519 assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
520 log_debug("%s", buf);
521 assert_se(streq(buf, "2 months 1 day ago"));
522
523 x = now(CLOCK_REALTIME) - (2*USEC_PER_MONTH + 2*USEC_PER_DAY);
524 assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
525 log_debug("%s", buf);
526 assert_se(streq(buf, "2 months 2 days ago"));
527
528 /* Weeks and days */
529 x = now(CLOCK_REALTIME) - (1*USEC_PER_WEEK + 1*USEC_PER_DAY);
530 assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
531 log_debug("%s", buf);
532 assert_se(streq(buf, "1 week 1 day ago"));
533
534 x = now(CLOCK_REALTIME) - (1*USEC_PER_WEEK + 2*USEC_PER_DAY);
535 assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
536 log_debug("%s", buf);
537 assert_se(streq(buf, "1 week 2 days ago"));
538
539 x = now(CLOCK_REALTIME) - (2*USEC_PER_WEEK + 1*USEC_PER_DAY);
540 assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
541 log_debug("%s", buf);
542 assert_se(streq(buf, "2 weeks 1 day ago"));
543
544 x = now(CLOCK_REALTIME) - (2*USEC_PER_WEEK + 2*USEC_PER_DAY);
545 assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true));
546 log_debug("%s", buf);
547 assert_se(streq(buf, "2 weeks 2 days ago"));
548 }
549
550 TEST(format_timestamp_relative) {
551 char buf[CONST_MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
552 usec_t x;
553
554 /* Only testing timestamps in the past so we don't need to add some delta to account for time passing
555 * by while we are running the tests (unless we're running on potatoes and 24 hours somehow passes
556 * between our call to now() and format_timestamp_relative's call to now()). */
557
558 /* Years and months */
559 x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 1*USEC_PER_MONTH);
560 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
561 log_debug("%s", buf);
562 assert_se(streq(buf, "1 year 1 month ago"));
563
564 x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 2*USEC_PER_MONTH);
565 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
566 log_debug("%s", buf);
567 assert_se(streq(buf, "1 year 2 months ago"));
568
569 x = now(CLOCK_REALTIME) - (2*USEC_PER_YEAR + 1*USEC_PER_MONTH);
570 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
571 log_debug("%s", buf);
572 assert_se(streq(buf, "2 years 1 month ago"));
573
574 x = now(CLOCK_REALTIME) - (2*USEC_PER_YEAR + 2*USEC_PER_MONTH);
575 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
576 log_debug("%s", buf);
577 assert_se(streq(buf, "2 years 2 months ago"));
578
579 /* Months and days */
580 x = now(CLOCK_REALTIME) - (1*USEC_PER_MONTH + 1*USEC_PER_DAY);
581 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
582 log_debug("%s", buf);
583 assert_se(streq(buf, "1 month 1 day ago"));
584
585 x = now(CLOCK_REALTIME) - (1*USEC_PER_MONTH + 2*USEC_PER_DAY);
586 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
587 log_debug("%s", buf);
588 assert_se(streq(buf, "1 month 2 days ago"));
589
590 x = now(CLOCK_REALTIME) - (2*USEC_PER_MONTH + 1*USEC_PER_DAY);
591 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
592 log_debug("%s", buf);
593 assert_se(streq(buf, "2 months 1 day ago"));
594
595 x = now(CLOCK_REALTIME) - (2*USEC_PER_MONTH + 2*USEC_PER_DAY);
596 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
597 log_debug("%s", buf);
598 assert_se(streq(buf, "2 months 2 days ago"));
599
600 /* Weeks and days */
601 x = now(CLOCK_REALTIME) - (1*USEC_PER_WEEK + 1*USEC_PER_DAY);
602 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
603 log_debug("%s", buf);
604 assert_se(streq(buf, "1 week 1 day ago"));
605
606 x = now(CLOCK_REALTIME) - (1*USEC_PER_WEEK + 2*USEC_PER_DAY);
607 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
608 log_debug("%s", buf);
609 assert_se(streq(buf, "1 week 2 days ago"));
610
611 x = now(CLOCK_REALTIME) - (2*USEC_PER_WEEK + 1*USEC_PER_DAY);
612 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
613 log_debug("%s", buf);
614 assert_se(streq(buf, "2 weeks 1 day ago"));
615
616 x = now(CLOCK_REALTIME) - (2*USEC_PER_WEEK + 2*USEC_PER_DAY);
617 assert_se(format_timestamp_relative(buf, sizeof(buf), x));
618 log_debug("%s", buf);
619 assert_se(streq(buf, "2 weeks 2 days ago"));
620 }
621
622 static void test_format_timestamp_one(usec_t val, TimestampStyle style, const char *result) {
623 char buf[FORMAT_TIMESTAMP_MAX];
624 const char *t;
625
626 t = format_timestamp_style(buf, sizeof(buf), val, style);
627 assert_se(streq_ptr(t, result));
628 }
629
630 TEST(format_timestamp_range) {
631 test_format_timestamp_one(0, TIMESTAMP_UTC, NULL);
632 test_format_timestamp_one(0, TIMESTAMP_DATE, NULL);
633 test_format_timestamp_one(0, TIMESTAMP_US_UTC, NULL);
634
635 test_format_timestamp_one(1, TIMESTAMP_UTC, "Thu 1970-01-01 00:00:00 UTC");
636 test_format_timestamp_one(1, TIMESTAMP_DATE, "Thu 1970-01-01");
637 test_format_timestamp_one(1, TIMESTAMP_US_UTC, "Thu 1970-01-01 00:00:00.000001 UTC");
638
639 test_format_timestamp_one(USEC_PER_SEC, TIMESTAMP_UTC, "Thu 1970-01-01 00:00:01 UTC");
640 test_format_timestamp_one(USEC_PER_SEC, TIMESTAMP_DATE, "Thu 1970-01-01");
641 test_format_timestamp_one(USEC_PER_SEC, TIMESTAMP_US_UTC, "Thu 1970-01-01 00:00:01.000000 UTC");
642
643 #if SIZEOF_TIME_T == 8
644 test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX, TIMESTAMP_UTC, "Thu 9999-12-30 23:59:59 UTC");
645 test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX, TIMESTAMP_DATE, "Thu 9999-12-30");
646 test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_UTC, "--- XXXX-XX-XX XX:XX:XX UTC");
647 test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_US_UTC, "--- XXXX-XX-XX XX:XX:XX.XXXXXX UTC");
648 test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_DATE, "--- XXXX-XX-XX");
649 #elif SIZEOF_TIME_T == 4
650 test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX, TIMESTAMP_UTC, "Mon 2038-01-18 03:14:07 UTC");
651 test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX, TIMESTAMP_DATE, "Mon 2038-01-18");
652 test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_UTC, "--- XXXX-XX-XX XX:XX:XX UTC");
653 test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_US_UTC, "--- XXXX-XX-XX XX:XX:XX.XXXXXX UTC");
654 test_format_timestamp_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, TIMESTAMP_DATE, "--- XXXX-XX-XX");
655 #endif
656
657 test_format_timestamp_one(USEC_INFINITY, TIMESTAMP_UTC, NULL);
658 }
659
660 static void test_parse_timestamp_one(const char *str, usec_t max_diff, usec_t expected) {
661 usec_t usec = USEC_INFINITY;
662 int r;
663
664 r = parse_timestamp(str, &usec);
665 log_debug("/* %s(%s): max_diff="USEC_FMT", expected="USEC_FMT", result="USEC_FMT" */", __func__, str, max_diff, expected, usec);
666 assert_se(r >= 0);
667 assert_se(usec >= expected);
668 assert_se(usec_sub_unsigned(usec, expected) <= max_diff);
669 }
670
671 static bool time_is_zero(usec_t usec) {
672 const char *s;
673
674 s = FORMAT_TIMESTAMP(usec);
675 return strstr(s, " 00:00:00 ");
676 }
677
678 static bool timezone_equal(usec_t today, usec_t target) {
679 const char *s, *t, *sz, *tz;
680
681 s = FORMAT_TIMESTAMP(today);
682 t = FORMAT_TIMESTAMP(target);
683 assert_se(sz = strrchr(s, ' '));
684 assert_se(tz = strrchr(t, ' '));
685 log_debug("%s("USEC_FMT", "USEC_FMT") -> %s, %s", __func__, today, target, s, t);
686 return streq(sz, tz);
687 }
688
689 static void test_parse_timestamp_impl(const char *tz) {
690 usec_t today, today2, now_usec;
691
692 /* Invalid: Ensure that systemctl reboot --when=show and --when=cancel
693 * will not result in ambiguities */
694 assert_se(parse_timestamp("show", NULL) == -EINVAL);
695 assert_se(parse_timestamp("cancel", NULL) == -EINVAL);
696
697 /* UTC */
698 test_parse_timestamp_one("Thu 1970-01-01 00:01 UTC", 0, USEC_PER_MINUTE);
699 test_parse_timestamp_one("Thu 1970-01-01 00:00:01 UTC", 0, USEC_PER_SEC);
700 test_parse_timestamp_one("Thu 1970-01-01 00:00:01.001 UTC", 0, USEC_PER_SEC + 1000);
701 test_parse_timestamp_one("Thu 1970-01-01 00:00:01.0010 UTC", 0, USEC_PER_SEC + 1000);
702
703 test_parse_timestamp_one("Thu 70-01-01 00:01 UTC", 0, USEC_PER_MINUTE);
704 test_parse_timestamp_one("Thu 70-01-01 00:00:01 UTC", 0, USEC_PER_SEC);
705 test_parse_timestamp_one("Thu 70-01-01 00:00:01.001 UTC", 0, USEC_PER_SEC + 1000);
706 test_parse_timestamp_one("Thu 70-01-01 00:00:01.0010 UTC", 0, USEC_PER_SEC + 1000);
707
708 test_parse_timestamp_one("1970-01-01 00:01 UTC", 0, USEC_PER_MINUTE);
709 test_parse_timestamp_one("1970-01-01 00:00:01 UTC", 0, USEC_PER_SEC);
710 test_parse_timestamp_one("1970-01-01 00:00:01.001 UTC", 0, USEC_PER_SEC + 1000);
711 test_parse_timestamp_one("1970-01-01 00:00:01.0010 UTC", 0, USEC_PER_SEC + 1000);
712
713 test_parse_timestamp_one("70-01-01 00:01 UTC", 0, USEC_PER_MINUTE);
714 test_parse_timestamp_one("70-01-01 00:00:01 UTC", 0, USEC_PER_SEC);
715 test_parse_timestamp_one("70-01-01 00:00:01.001 UTC", 0, USEC_PER_SEC + 1000);
716 test_parse_timestamp_one("70-01-01 00:00:01.0010 UTC", 0, USEC_PER_SEC + 1000);
717
718 /* Examples from RFC3339 */
719 test_parse_timestamp_one("1985-04-12T23:20:50.52Z", 0, 482196050 * USEC_PER_SEC + 520000);
720 test_parse_timestamp_one("1996-12-19T16:39:57-08:00", 0, 851042397 * USEC_PER_SEC + 000000);
721 test_parse_timestamp_one("1996-12-20T00:39:57Z", 0, 851042397 * USEC_PER_SEC + 000000);
722 test_parse_timestamp_one("1990-12-31T23:59:60Z", 0, 662688000 * USEC_PER_SEC + 000000);
723 test_parse_timestamp_one("1990-12-31T15:59:60-08:00", 0, 662688000 * USEC_PER_SEC + 000000);
724 assert_se(parse_timestamp("1937-01-01T12:00:27.87+00:20", NULL) == -EINVAL); /* we don't support pre-epoch timestamps */
725 /* We accept timestamps without seconds as well */
726 test_parse_timestamp_one("1996-12-20T00:39Z", 0, (851042397 - 57) * USEC_PER_SEC + 000000);
727 test_parse_timestamp_one("1990-12-31T15:59-08:00", 0, (662688000-60) * USEC_PER_SEC + 000000);
728 /* We drop day-of-week before parsing the timestamp */
729 test_parse_timestamp_one("Thu 1970-01-01T00:01 UTC", 0, USEC_PER_MINUTE);
730 test_parse_timestamp_one("Thu 1970-01-01T00:00:01 UTC", 0, USEC_PER_SEC);
731 test_parse_timestamp_one("Thu 1970-01-01T00:01Z", 0, USEC_PER_MINUTE);
732 test_parse_timestamp_one("Thu 1970-01-01T00:00:01Z", 0, USEC_PER_SEC);
733 /* RFC3339-style timezones can be welded to all formats */
734 assert_se(parse_timestamp("today UTC", &today) == 0);
735 assert_se(parse_timestamp("todayZ", &today2) == 0);
736 assert_se(today == today2);
737 assert_se(parse_timestamp("today +0200", &today) == 0);
738 assert_se(parse_timestamp("today+02:00", &today2) == 0);
739 assert_se(today == today2);
740
741 /* https://ijmacd.github.io/rfc3339-iso8601/ */
742 test_parse_timestamp_one("2023-09-06 12:49:27-00:00", 0, 1694004567 * USEC_PER_SEC + 000000);
743 test_parse_timestamp_one("2023-09-06 12:49:27.284-00:00", 0, 1694004567 * USEC_PER_SEC + 284000);
744 test_parse_timestamp_one("2023-09-06 12:49:27.284029Z", 0, 1694004567 * USEC_PER_SEC + 284029);
745 test_parse_timestamp_one("2023-09-06 12:49:27.284Z", 0, 1694004567 * USEC_PER_SEC + 284000);
746 test_parse_timestamp_one("2023-09-06 12:49:27.28Z", 0, 1694004567 * USEC_PER_SEC + 280000);
747 test_parse_timestamp_one("2023-09-06 12:49:27.2Z", 0, 1694004567 * USEC_PER_SEC + 200000);
748 test_parse_timestamp_one("2023-09-06 12:49:27Z", 0, 1694004567 * USEC_PER_SEC + 000000);
749 test_parse_timestamp_one("2023-09-06 14:49:27+02:00", 0, 1694004567 * USEC_PER_SEC + 000000);
750 test_parse_timestamp_one("2023-09-06 14:49:27.2+02:00", 0, 1694004567 * USEC_PER_SEC + 200000);
751 test_parse_timestamp_one("2023-09-06 14:49:27.28+02:00", 0, 1694004567 * USEC_PER_SEC + 280000);
752 test_parse_timestamp_one("2023-09-06 14:49:27.284+02:00", 0, 1694004567 * USEC_PER_SEC + 284000);
753 test_parse_timestamp_one("2023-09-06 14:49:27.284029+02:00", 0, 1694004567 * USEC_PER_SEC + 284029);
754 test_parse_timestamp_one("2023-09-06T12:49:27+00:00", 0, 1694004567 * USEC_PER_SEC + 000000);
755 test_parse_timestamp_one("2023-09-06T12:49:27-00:00", 0, 1694004567 * USEC_PER_SEC + 000000);
756 test_parse_timestamp_one("2023-09-06T12:49:27.284+00:00", 0, 1694004567 * USEC_PER_SEC + 284000);
757 test_parse_timestamp_one("2023-09-06T12:49:27.284-00:00", 0, 1694004567 * USEC_PER_SEC + 284000);
758 test_parse_timestamp_one("2023-09-06T12:49:27.284029Z", 0, 1694004567 * USEC_PER_SEC + 284029);
759 test_parse_timestamp_one("2023-09-06T12:49:27.284Z", 0, 1694004567 * USEC_PER_SEC + 284000);
760 test_parse_timestamp_one("2023-09-06T12:49:27.28Z", 0, 1694004567 * USEC_PER_SEC + 280000);
761 test_parse_timestamp_one("2023-09-06T12:49:27.2Z", 0, 1694004567 * USEC_PER_SEC + 200000);
762 test_parse_timestamp_one("2023-09-06T12:49:27Z", 0, 1694004567 * USEC_PER_SEC + 000000);
763 test_parse_timestamp_one("2023-09-06T14:49:27+02:00", 0, 1694004567 * USEC_PER_SEC + 000000);
764 test_parse_timestamp_one("2023-09-06T14:49:27.284+02:00", 0, 1694004567 * USEC_PER_SEC + 284000);
765 test_parse_timestamp_one("2023-09-06T14:49:27.284029+02:00", 0, 1694004567 * USEC_PER_SEC + 284029);
766 test_parse_timestamp_one("2023-09-06T21:34:27+08:45", 0, 1694004567 * USEC_PER_SEC + 000000);
767
768 if (timezone_is_valid("Asia/Tokyo", LOG_DEBUG)) {
769 /* Asia/Tokyo (+0900) */
770 test_parse_timestamp_one("Thu 1970-01-01 09:01 Asia/Tokyo", 0, USEC_PER_MINUTE);
771 test_parse_timestamp_one("Thu 1970-01-01 09:00:01 Asia/Tokyo", 0, USEC_PER_SEC);
772 test_parse_timestamp_one("Thu 1970-01-01 09:00:01.001 Asia/Tokyo", 0, USEC_PER_SEC + 1000);
773 test_parse_timestamp_one("Thu 1970-01-01 09:00:01.0010 Asia/Tokyo", 0, USEC_PER_SEC + 1000);
774
775 test_parse_timestamp_one("Thu 70-01-01 09:01 Asia/Tokyo", 0, USEC_PER_MINUTE);
776 test_parse_timestamp_one("Thu 70-01-01 09:00:01 Asia/Tokyo", 0, USEC_PER_SEC);
777 test_parse_timestamp_one("Thu 70-01-01 09:00:01.001 Asia/Tokyo", 0, USEC_PER_SEC + 1000);
778 test_parse_timestamp_one("Thu 70-01-01 09:00:01.0010 Asia/Tokyo", 0, USEC_PER_SEC + 1000);
779
780 test_parse_timestamp_one("1970-01-01 09:01 Asia/Tokyo", 0, USEC_PER_MINUTE);
781 test_parse_timestamp_one("1970-01-01 09:00:01 Asia/Tokyo", 0, USEC_PER_SEC);
782 test_parse_timestamp_one("1970-01-01 09:00:01.001 Asia/Tokyo", 0, USEC_PER_SEC + 1000);
783 test_parse_timestamp_one("1970-01-01 09:00:01.0010 Asia/Tokyo", 0, USEC_PER_SEC + 1000);
784
785 test_parse_timestamp_one("70-01-01 09:01 Asia/Tokyo", 0, USEC_PER_MINUTE);
786 test_parse_timestamp_one("70-01-01 09:00:01 Asia/Tokyo", 0, USEC_PER_SEC);
787 test_parse_timestamp_one("70-01-01 09:00:01.001 Asia/Tokyo", 0, USEC_PER_SEC + 1000);
788 test_parse_timestamp_one("70-01-01 09:00:01.0010 Asia/Tokyo", 0, USEC_PER_SEC + 1000);
789 }
790
791 if (streq_ptr(tz, "Asia/Tokyo")) {
792 /* JST (+0900) */
793 test_parse_timestamp_one("Thu 1970-01-01 09:01 JST", 0, USEC_PER_MINUTE);
794 test_parse_timestamp_one("Thu 1970-01-01 09:00:01 JST", 0, USEC_PER_SEC);
795 test_parse_timestamp_one("Thu 1970-01-01 09:00:01.001 JST", 0, USEC_PER_SEC + 1000);
796 test_parse_timestamp_one("Thu 1970-01-01 09:00:01.0010 JST", 0, USEC_PER_SEC + 1000);
797
798 test_parse_timestamp_one("Thu 70-01-01 09:01 JST", 0, USEC_PER_MINUTE);
799 test_parse_timestamp_one("Thu 70-01-01 09:00:01 JST", 0, USEC_PER_SEC);
800 test_parse_timestamp_one("Thu 70-01-01 09:00:01.001 JST", 0, USEC_PER_SEC + 1000);
801 test_parse_timestamp_one("Thu 70-01-01 09:00:01.0010 JST", 0, USEC_PER_SEC + 1000);
802
803 test_parse_timestamp_one("1970-01-01 09:01 JST", 0, USEC_PER_MINUTE);
804 test_parse_timestamp_one("1970-01-01 09:00:01 JST", 0, USEC_PER_SEC);
805 test_parse_timestamp_one("1970-01-01 09:00:01.001 JST", 0, USEC_PER_SEC + 1000);
806 test_parse_timestamp_one("1970-01-01 09:00:01.0010 JST", 0, USEC_PER_SEC + 1000);
807
808 test_parse_timestamp_one("70-01-01 09:01 JST", 0, USEC_PER_MINUTE);
809 test_parse_timestamp_one("70-01-01 09:00:01 JST", 0, USEC_PER_SEC);
810 test_parse_timestamp_one("70-01-01 09:00:01.001 JST", 0, USEC_PER_SEC + 1000);
811 test_parse_timestamp_one("70-01-01 09:00:01.0010 JST", 0, USEC_PER_SEC + 1000);
812 }
813
814 if (timezone_is_valid("America/New_York", LOG_DEBUG)) {
815 /* America/New_York (-0500) */
816 test_parse_timestamp_one("Wed 1969-12-31 19:01 America/New_York", 0, USEC_PER_MINUTE);
817 test_parse_timestamp_one("Wed 1969-12-31 19:00:01 America/New_York", 0, USEC_PER_SEC);
818 test_parse_timestamp_one("Wed 1969-12-31 19:00:01.001 America/New_York", 0, USEC_PER_SEC + 1000);
819 test_parse_timestamp_one("Wed 1969-12-31 19:00:01.0010 America/New_York", 0, USEC_PER_SEC + 1000);
820
821 test_parse_timestamp_one("Wed 69-12-31 19:01 America/New_York", 0, USEC_PER_MINUTE);
822 test_parse_timestamp_one("Wed 69-12-31 19:00:01 America/New_York", 0, USEC_PER_SEC);
823 test_parse_timestamp_one("Wed 69-12-31 19:00:01.001 America/New_York", 0, USEC_PER_SEC + 1000);
824 test_parse_timestamp_one("Wed 69-12-31 19:00:01.0010 America/New_York", 0, USEC_PER_SEC + 1000);
825
826 test_parse_timestamp_one("1969-12-31 19:01 America/New_York", 0, USEC_PER_MINUTE);
827 test_parse_timestamp_one("1969-12-31 19:00:01 America/New_York", 0, USEC_PER_SEC);
828 test_parse_timestamp_one("1969-12-31 19:00:01.001 America/New_York", 0, USEC_PER_SEC + 1000);
829 test_parse_timestamp_one("1969-12-31 19:00:01.0010 America/New_York", 0, USEC_PER_SEC + 1000);
830
831 test_parse_timestamp_one("69-12-31 19:01 America/New_York", 0, USEC_PER_MINUTE);
832 test_parse_timestamp_one("69-12-31 19:00:01 America/New_York", 0, USEC_PER_SEC);
833 test_parse_timestamp_one("69-12-31 19:00:01.001 America/New_York", 0, USEC_PER_SEC + 1000);
834 test_parse_timestamp_one("69-12-31 19:00:01.0010 America/New_York", 0, USEC_PER_SEC + 1000);
835 }
836
837 if (streq_ptr(tz, "America/New_York")) {
838 /* EST (-0500) */
839 test_parse_timestamp_one("Wed 1969-12-31 19:01 EST", 0, USEC_PER_MINUTE);
840 test_parse_timestamp_one("Wed 1969-12-31 19:00:01 EST", 0, USEC_PER_SEC);
841 test_parse_timestamp_one("Wed 1969-12-31 19:00:01.001 EST", 0, USEC_PER_SEC + 1000);
842 test_parse_timestamp_one("Wed 1969-12-31 19:00:01.0010 EST", 0, USEC_PER_SEC + 1000);
843
844 test_parse_timestamp_one("Wed 69-12-31 19:01 EST", 0, USEC_PER_MINUTE);
845 test_parse_timestamp_one("Wed 69-12-31 19:00:01 EST", 0, USEC_PER_SEC);
846 test_parse_timestamp_one("Wed 69-12-31 19:00:01.001 EST", 0, USEC_PER_SEC + 1000);
847 test_parse_timestamp_one("Wed 69-12-31 19:00:01.0010 EST", 0, USEC_PER_SEC + 1000);
848
849 test_parse_timestamp_one("1969-12-31 19:01 EST", 0, USEC_PER_MINUTE);
850 test_parse_timestamp_one("1969-12-31 19:00:01 EST", 0, USEC_PER_SEC);
851 test_parse_timestamp_one("1969-12-31 19:00:01.001 EST", 0, USEC_PER_SEC + 1000);
852 test_parse_timestamp_one("1969-12-31 19:00:01.0010 EST", 0, USEC_PER_SEC + 1000);
853
854 test_parse_timestamp_one("69-12-31 19:01 EST", 0, USEC_PER_MINUTE);
855 test_parse_timestamp_one("69-12-31 19:00:01 EST", 0, USEC_PER_SEC);
856 test_parse_timestamp_one("69-12-31 19:00:01.001 EST", 0, USEC_PER_SEC + 1000);
857 test_parse_timestamp_one("69-12-31 19:00:01.0010 EST", 0, USEC_PER_SEC + 1000);
858 }
859
860 /* -06 */
861 test_parse_timestamp_one("Wed 1969-12-31 18:01 -06", 0, USEC_PER_MINUTE);
862 test_parse_timestamp_one("Wed 1969-12-31 18:00:01 -06", 0, USEC_PER_SEC);
863 test_parse_timestamp_one("Wed 1969-12-31 18:00:01.001 -06", 0, USEC_PER_SEC + 1000);
864 test_parse_timestamp_one("Wed 1969-12-31 18:00:01.0010 -06", 0, USEC_PER_SEC + 1000);
865
866 test_parse_timestamp_one("Wed 69-12-31 18:01 -06", 0, USEC_PER_MINUTE);
867 test_parse_timestamp_one("Wed 69-12-31 18:00:01 -06", 0, USEC_PER_SEC);
868 test_parse_timestamp_one("Wed 69-12-31 18:00:01.001 -06", 0, USEC_PER_SEC + 1000);
869 test_parse_timestamp_one("Wed 69-12-31 18:00:01.0010 -06", 0, USEC_PER_SEC + 1000);
870
871 test_parse_timestamp_one("1969-12-31 18:01 -06", 0, USEC_PER_MINUTE);
872 test_parse_timestamp_one("1969-12-31 18:00:01 -06", 0, USEC_PER_SEC);
873 test_parse_timestamp_one("1969-12-31 18:00:01.001 -06", 0, USEC_PER_SEC + 1000);
874 test_parse_timestamp_one("1969-12-31 18:00:01.0010 -06", 0, USEC_PER_SEC + 1000);
875
876 test_parse_timestamp_one("69-12-31 18:01 -06", 0, USEC_PER_MINUTE);
877 test_parse_timestamp_one("69-12-31 18:00:01 -06", 0, USEC_PER_SEC);
878 test_parse_timestamp_one("69-12-31 18:00:01.001 -06", 0, USEC_PER_SEC + 1000);
879 test_parse_timestamp_one("69-12-31 18:00:01.0010 -06", 0, USEC_PER_SEC + 1000);
880
881 /* -0600 */
882 test_parse_timestamp_one("Wed 1969-12-31 18:01 -0600", 0, USEC_PER_MINUTE);
883 test_parse_timestamp_one("Wed 1969-12-31 18:00:01 -0600", 0, USEC_PER_SEC);
884 test_parse_timestamp_one("Wed 1969-12-31 18:00:01.001 -0600", 0, USEC_PER_SEC + 1000);
885 test_parse_timestamp_one("Wed 1969-12-31 18:00:01.0010 -0600", 0, USEC_PER_SEC + 1000);
886
887 test_parse_timestamp_one("Wed 69-12-31 18:01 -0600", 0, USEC_PER_MINUTE);
888 test_parse_timestamp_one("Wed 69-12-31 18:00:01 -0600", 0, USEC_PER_SEC);
889 test_parse_timestamp_one("Wed 69-12-31 18:00:01.001 -0600", 0, USEC_PER_SEC + 1000);
890 test_parse_timestamp_one("Wed 69-12-31 18:00:01.0010 -0600", 0, USEC_PER_SEC + 1000);
891
892 test_parse_timestamp_one("1969-12-31 18:01 -0600", 0, USEC_PER_MINUTE);
893 test_parse_timestamp_one("1969-12-31 18:00:01 -0600", 0, USEC_PER_SEC);
894 test_parse_timestamp_one("1969-12-31 18:00:01.001 -0600", 0, USEC_PER_SEC + 1000);
895 test_parse_timestamp_one("1969-12-31 18:00:01.0010 -0600", 0, USEC_PER_SEC + 1000);
896
897 test_parse_timestamp_one("69-12-31 18:01 -0600", 0, USEC_PER_MINUTE);
898 test_parse_timestamp_one("69-12-31 18:00:01 -0600", 0, USEC_PER_SEC);
899 test_parse_timestamp_one("69-12-31 18:00:01.001 -0600", 0, USEC_PER_SEC + 1000);
900 test_parse_timestamp_one("69-12-31 18:00:01.0010 -0600", 0, USEC_PER_SEC + 1000);
901
902 /* -06:00 */
903 test_parse_timestamp_one("Wed 1969-12-31 18:01 -06:00", 0, USEC_PER_MINUTE);
904 test_parse_timestamp_one("Wed 1969-12-31 18:00:01 -06:00", 0, USEC_PER_SEC);
905 test_parse_timestamp_one("Wed 1969-12-31 18:00:01.001 -06:00", 0, USEC_PER_SEC + 1000);
906 test_parse_timestamp_one("Wed 1969-12-31 18:00:01.0010 -06:00", 0, USEC_PER_SEC + 1000);
907
908 test_parse_timestamp_one("Wed 69-12-31 18:01 -06:00", 0, USEC_PER_MINUTE);
909 test_parse_timestamp_one("Wed 69-12-31 18:00:01 -06:00", 0, USEC_PER_SEC);
910 test_parse_timestamp_one("Wed 69-12-31 18:00:01.001 -06:00", 0, USEC_PER_SEC + 1000);
911 test_parse_timestamp_one("Wed 69-12-31 18:00:01.0010 -06:00", 0, USEC_PER_SEC + 1000);
912
913 test_parse_timestamp_one("1969-12-31 18:01 -06:00", 0, USEC_PER_MINUTE);
914 test_parse_timestamp_one("1969-12-31 18:00:01 -06:00", 0, USEC_PER_SEC);
915 test_parse_timestamp_one("1969-12-31 18:00:01.001 -06:00", 0, USEC_PER_SEC + 1000);
916 test_parse_timestamp_one("1969-12-31 18:00:01.0010 -06:00", 0, USEC_PER_SEC + 1000);
917
918 test_parse_timestamp_one("69-12-31 18:01 -06:00", 0, USEC_PER_MINUTE);
919 test_parse_timestamp_one("69-12-31 18:00:01 -06:00", 0, USEC_PER_SEC);
920 test_parse_timestamp_one("69-12-31 18:00:01.001 -06:00", 0, USEC_PER_SEC + 1000);
921 test_parse_timestamp_one("69-12-31 18:00:01.0010 -06:00", 0, USEC_PER_SEC + 1000);
922
923 /* without date */
924 assert_se(parse_timestamp("today", &today) == 0);
925 if (time_is_zero(today)) {
926 test_parse_timestamp_one("00:01", 0, today + USEC_PER_MINUTE);
927 test_parse_timestamp_one("00:00:01", 0, today + USEC_PER_SEC);
928 test_parse_timestamp_one("00:00:01.001", 0, today + USEC_PER_SEC + 1000);
929 test_parse_timestamp_one("00:00:01.0010", 0, today + USEC_PER_SEC + 1000);
930
931 if (timezone_equal(today, today + USEC_PER_DAY) && time_is_zero(today + USEC_PER_DAY))
932 test_parse_timestamp_one("tomorrow", 0, today + USEC_PER_DAY);
933 if (timezone_equal(today, today - USEC_PER_DAY) && time_is_zero(today - USEC_PER_DAY))
934 test_parse_timestamp_one("yesterday", 0, today - USEC_PER_DAY);
935 }
936
937 /* relative */
938 assert_se(parse_timestamp("now", &now_usec) == 0);
939 test_parse_timestamp_one("+5hours", USEC_PER_MINUTE, now_usec + 5 * USEC_PER_HOUR);
940 if (now_usec >= 10 * USEC_PER_DAY)
941 test_parse_timestamp_one("-10days", USEC_PER_MINUTE, now_usec - 10 * USEC_PER_DAY);
942 test_parse_timestamp_one("2weeks left", USEC_PER_MINUTE, now_usec + 2 * USEC_PER_WEEK);
943 if (now_usec >= 30 * USEC_PER_MINUTE)
944 test_parse_timestamp_one("30minutes ago", USEC_PER_MINUTE, now_usec - 30 * USEC_PER_MINUTE);
945 }
946
947 TEST(parse_timestamp) {
948 test_parse_timestamp_impl(NULL);
949 }
950
951 static void test_parse_timestamp_with_tz_one(const char *tz) {
952 const char *saved_tz, *colon_tz;
953
954 if (!timezone_is_valid(tz, LOG_DEBUG))
955 return;
956
957 log_info("/* %s(%s) */", __func__, tz);
958
959 saved_tz = getenv("TZ");
960
961 assert_se(colon_tz = strjoina(":", tz));
962 assert_se(setenv("TZ", colon_tz, 1) >= 0);
963 tzset();
964 log_debug("%s: tzname[0]=%s, tzname[1]=%s", tz, strempty(tzname[0]), strempty(tzname[1]));
965
966 test_parse_timestamp_impl(tz);
967
968 assert_se(set_unset_env("TZ", saved_tz, true) == 0);
969 tzset();
970 }
971
972 TEST(parse_timestamp_with_tz) {
973 _cleanup_strv_free_ char **timezones = NULL;
974
975 test_parse_timestamp_with_tz_one("UTC");
976
977 if (!slow_tests_enabled())
978 return (void) log_tests_skipped("slow tests are disabled");
979
980 assert_se(get_timezones(&timezones) >= 0);
981 STRV_FOREACH(tz, timezones)
982 test_parse_timestamp_with_tz_one(*tz);
983 }
984
985 TEST(deserialize_dual_timestamp) {
986 int r;
987 dual_timestamp t;
988
989 r = deserialize_dual_timestamp("1234 5678", &t);
990 assert_se(r == 0);
991 assert_se(t.realtime == 1234);
992 assert_se(t.monotonic == 5678);
993
994 r = deserialize_dual_timestamp("1234x 5678", &t);
995 assert_se(r == -EINVAL);
996
997 r = deserialize_dual_timestamp("1234 5678y", &t);
998 assert_se(r == -EINVAL);
999
1000 r = deserialize_dual_timestamp("-1234 5678", &t);
1001 assert_se(r == -EINVAL);
1002
1003 r = deserialize_dual_timestamp("1234 -5678", &t);
1004 assert_se(r == -EINVAL);
1005
1006 /* Check that output wasn't modified. */
1007 assert_se(t.realtime == 1234);
1008 assert_se(t.monotonic == 5678);
1009
1010 r = deserialize_dual_timestamp("+123 567", &t);
1011 assert_se(r == 0);
1012 assert_se(t.realtime == 123);
1013 assert_se(t.monotonic == 567);
1014
1015 /* Check that we get "infinity" on overflow. */
1016 r = deserialize_dual_timestamp("18446744073709551617 0", &t);
1017 assert_se(r == 0);
1018 assert_se(t.realtime == USEC_INFINITY);
1019 assert_se(t.monotonic == 0);
1020 }
1021
1022 static void assert_similar(usec_t a, usec_t b) {
1023 usec_t d;
1024
1025 if (a > b)
1026 d = a - b;
1027 else
1028 d = b - a;
1029
1030 assert_se(d < 10*USEC_PER_SEC);
1031 }
1032
1033 TEST(usec_shift_clock) {
1034 usec_t rt, mn, bt;
1035
1036 rt = now(CLOCK_REALTIME);
1037 mn = now(CLOCK_MONOTONIC);
1038 bt = now(CLOCK_BOOTTIME);
1039
1040 assert_se(usec_shift_clock(USEC_INFINITY, CLOCK_REALTIME, CLOCK_MONOTONIC) == USEC_INFINITY);
1041
1042 assert_similar(usec_shift_clock(rt + USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_MONOTONIC), mn + USEC_PER_HOUR);
1043 assert_similar(usec_shift_clock(rt + 2*USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_BOOTTIME), bt + 2*USEC_PER_HOUR);
1044 assert_se(usec_shift_clock(rt + 3*USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_REALTIME_ALARM) == rt + 3*USEC_PER_HOUR);
1045
1046 assert_similar(usec_shift_clock(mn + 4*USEC_PER_HOUR, CLOCK_MONOTONIC, CLOCK_REALTIME_ALARM), rt + 4*USEC_PER_HOUR);
1047 assert_similar(usec_shift_clock(mn + 5*USEC_PER_HOUR, CLOCK_MONOTONIC, CLOCK_BOOTTIME), bt + 5*USEC_PER_HOUR);
1048 assert_se(usec_shift_clock(mn + 6*USEC_PER_HOUR, CLOCK_MONOTONIC, CLOCK_MONOTONIC) == mn + 6*USEC_PER_HOUR);
1049
1050 assert_similar(usec_shift_clock(bt + 7*USEC_PER_HOUR, CLOCK_BOOTTIME, CLOCK_MONOTONIC), mn + 7*USEC_PER_HOUR);
1051 assert_similar(usec_shift_clock(bt + 8*USEC_PER_HOUR, CLOCK_BOOTTIME, CLOCK_REALTIME_ALARM), rt + 8*USEC_PER_HOUR);
1052 assert_se(usec_shift_clock(bt + 9*USEC_PER_HOUR, CLOCK_BOOTTIME, CLOCK_BOOTTIME) == bt + 9*USEC_PER_HOUR);
1053
1054 if (mn > USEC_PER_MINUTE) {
1055 assert_similar(usec_shift_clock(rt - 30 * USEC_PER_SEC, CLOCK_REALTIME_ALARM, CLOCK_MONOTONIC), mn - 30 * USEC_PER_SEC);
1056 assert_similar(usec_shift_clock(rt - 50 * USEC_PER_SEC, CLOCK_REALTIME, CLOCK_BOOTTIME), bt - 50 * USEC_PER_SEC);
1057 }
1058 }
1059
1060 TEST(in_utc_timezone) {
1061 const char *tz = getenv("TZ");
1062
1063 assert_se(setenv("TZ", ":UTC", 1) >= 0);
1064 assert_se(in_utc_timezone());
1065 assert_se(streq(tzname[0], "UTC"));
1066 assert_se(streq(tzname[1], "UTC"));
1067 assert_se(timezone == 0);
1068 assert_se(daylight == 0);
1069
1070 assert_se(setenv("TZ", ":Europe/Berlin", 1) >= 0);
1071 assert_se(!in_utc_timezone());
1072 assert_se(streq(tzname[0], "CET"));
1073 assert_se(streq(tzname[1], "CEST"));
1074
1075 assert_se(set_unset_env("TZ", tz, true) == 0);
1076 tzset();
1077 }
1078
1079 TEST(map_clock_usec) {
1080 usec_t nowr, x, y, z;
1081
1082 x = nowr = now(CLOCK_REALTIME); /* right now */
1083 y = map_clock_usec(x, CLOCK_REALTIME, CLOCK_MONOTONIC);
1084 z = map_clock_usec(y, CLOCK_MONOTONIC, CLOCK_REALTIME);
1085 /* Converting forth and back will introduce inaccuracies, since we cannot query both clocks atomically, but it should be small. Even on the slowest CI smaller than 1h */
1086
1087 assert_se((z > x ? z - x : x - z) < USEC_PER_HOUR);
1088
1089 assert_se(nowr < USEC_INFINITY - USEC_PER_DAY*7); /* overflow check */
1090 x = nowr + USEC_PER_DAY*7; /* 1 week from now */
1091 y = map_clock_usec(x, CLOCK_REALTIME, CLOCK_MONOTONIC);
1092 assert_se(timestamp_is_set(y));
1093 z = map_clock_usec(y, CLOCK_MONOTONIC, CLOCK_REALTIME);
1094 assert_se(timestamp_is_set(z));
1095 assert_se((z > x ? z - x : x - z) < USEC_PER_HOUR);
1096
1097 assert_se(nowr > USEC_PER_DAY * 7); /* underflow check */
1098 x = nowr - USEC_PER_DAY*7; /* 1 week ago */
1099 y = map_clock_usec(x, CLOCK_REALTIME, CLOCK_MONOTONIC);
1100 if (y != 0) { /* might underflow if machine is not up long enough for the monotonic clock to be beyond 1w */
1101 assert_se(y < USEC_INFINITY);
1102 z = map_clock_usec(y, CLOCK_MONOTONIC, CLOCK_REALTIME);
1103 assert_se(timestamp_is_set(z));
1104 assert_se((z > x ? z - x : x - z) < USEC_PER_HOUR);
1105 }
1106 }
1107
1108 static void test_timezone_offset_change_one(const char *utc, const char *pretty) {
1109 usec_t x, y, z;
1110 char *s;
1111
1112 assert_se(parse_timestamp(utc, &x) >= 0);
1113
1114 s = FORMAT_TIMESTAMP_STYLE(x, TIMESTAMP_UTC);
1115 assert_se(parse_timestamp(s, &y) >= 0);
1116 log_debug("%s -> " USEC_FMT " -> %s -> " USEC_FMT, utc, x, s, y);
1117 assert_se(streq(s, utc));
1118 assert_se(x == y);
1119
1120 assert_se(parse_timestamp(pretty, &y) >= 0);
1121 s = FORMAT_TIMESTAMP_STYLE(y, TIMESTAMP_PRETTY);
1122 assert_se(parse_timestamp(s, &z) >= 0);
1123 log_debug("%s -> " USEC_FMT " -> %s -> " USEC_FMT, pretty, y, s, z);
1124 assert_se(streq(s, pretty));
1125 assert_se(x == y);
1126 assert_se(x == z);
1127 }
1128
1129 TEST(timezone_offset_change) {
1130 const char *tz = getenv("TZ");
1131
1132 /* See issue #26370. */
1133
1134 if (timezone_is_valid("Africa/Casablanca", LOG_DEBUG)) {
1135 assert_se(setenv("TZ", ":Africa/Casablanca", 1) >= 0);
1136 tzset();
1137 log_debug("Africa/Casablanca: tzname[0]=%s, tzname[1]=%s", strempty(tzname[0]), strempty(tzname[1]));
1138
1139 test_timezone_offset_change_one("Sun 2015-10-25 01:59:59 UTC", "Sun 2015-10-25 02:59:59 +01");
1140 test_timezone_offset_change_one("Sun 2015-10-25 02:00:00 UTC", "Sun 2015-10-25 02:00:00 +00");
1141 test_timezone_offset_change_one("Sun 2018-06-17 01:59:59 UTC", "Sun 2018-06-17 01:59:59 +00");
1142 test_timezone_offset_change_one("Sun 2018-06-17 02:00:00 UTC", "Sun 2018-06-17 03:00:00 +01");
1143 test_timezone_offset_change_one("Sun 2018-10-28 01:59:59 UTC", "Sun 2018-10-28 02:59:59 +01");
1144 test_timezone_offset_change_one("Sun 2018-10-28 02:00:00 UTC", "Sun 2018-10-28 03:00:00 +01");
1145 }
1146
1147 if (timezone_is_valid("Asia/Atyrau", LOG_DEBUG)) {
1148 assert_se(setenv("TZ", ":Asia/Atyrau", 1) >= 0);
1149 tzset();
1150 log_debug("Asia/Atyrau: tzname[0]=%s, tzname[1]=%s", strempty(tzname[0]), strempty(tzname[1]));
1151
1152 test_timezone_offset_change_one("Sat 2004-03-27 21:59:59 UTC", "Sun 2004-03-28 01:59:59 +04");
1153 test_timezone_offset_change_one("Sat 2004-03-27 22:00:00 UTC", "Sun 2004-03-28 03:00:00 +05");
1154 test_timezone_offset_change_one("Sat 2004-10-30 21:59:59 UTC", "Sun 2004-10-31 02:59:59 +05");
1155 test_timezone_offset_change_one("Sat 2004-10-30 22:00:00 UTC", "Sun 2004-10-31 03:00:00 +05");
1156 }
1157
1158 if (timezone_is_valid("Chile/EasterIsland", LOG_DEBUG)) {
1159 assert_se(setenv("TZ", ":Chile/EasterIsland", 1) >= 0);
1160 tzset();
1161 log_debug("Chile/EasterIsland: tzname[0]=%s, tzname[1]=%s", strempty(tzname[0]), strempty(tzname[1]));
1162
1163 test_timezone_offset_change_one("Sun 1981-10-11 03:59:59 UTC", "Sat 1981-10-10 20:59:59 -07");
1164 test_timezone_offset_change_one("Sun 1981-10-11 04:00:00 UTC", "Sat 1981-10-10 22:00:00 -06");
1165 test_timezone_offset_change_one("Sun 1982-03-14 02:59:59 UTC", "Sat 1982-03-13 20:59:59 -06");
1166 test_timezone_offset_change_one("Sun 1982-03-14 03:00:00 UTC", "Sat 1982-03-13 21:00:00 -06");
1167 }
1168
1169 assert_se(set_unset_env("TZ", tz, true) == 0);
1170 tzset();
1171 }
1172
1173 static int intro(void) {
1174 /* Tests have hard-coded results that do not expect a specific timezone to be set by the caller */
1175 assert_se(unsetenv("TZ") >= 0);
1176
1177 log_info("realtime=" USEC_FMT "\n"
1178 "monotonic=" USEC_FMT "\n"
1179 "boottime=" USEC_FMT "\n",
1180 now(CLOCK_REALTIME),
1181 now(CLOCK_MONOTONIC),
1182 now(CLOCK_BOOTTIME));
1183
1184 /* Ensure time_t is signed */
1185 assert_cc((time_t) -1 < (time_t) 1);
1186
1187 /* Ensure TIME_T_MAX works correctly */
1188 uintmax_t x = TIME_T_MAX;
1189 x++;
1190 assert_se((time_t) x < 0);
1191
1192 return EXIT_SUCCESS;
1193 }
1194
1195 DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);