]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/time-util.c
Merge pull request #17549 from yuwata/tiny-fixes
[thirdparty/systemd.git] / src / basic / time-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <ctype.h>
4 #include <errno.h>
5 #include <limits.h>
6 #include <stdlib.h>
7 #include <sys/mman.h>
8 #include <sys/time.h>
9 #include <sys/timerfd.h>
10 #include <sys/timex.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13
14 #include "alloc-util.h"
15 #include "fd-util.h"
16 #include "fileio.h"
17 #include "fs-util.h"
18 #include "io-util.h"
19 #include "log.h"
20 #include "macro.h"
21 #include "missing_timerfd.h"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "process-util.h"
25 #include "stat-util.h"
26 #include "string-table.h"
27 #include "string-util.h"
28 #include "strv.h"
29 #include "time-util.h"
30
31 static clockid_t map_clock_id(clockid_t c) {
32
33 /* Some more exotic archs (s390, ppc, …) lack the "ALARM" flavour of the clocks. Thus, clock_gettime() will
34 * fail for them. Since they are essentially the same as their non-ALARM pendants (their only difference is
35 * when timers are set on them), let's just map them accordingly. This way, we can get the correct time even on
36 * those archs. */
37
38 switch (c) {
39
40 case CLOCK_BOOTTIME_ALARM:
41 return CLOCK_BOOTTIME;
42
43 case CLOCK_REALTIME_ALARM:
44 return CLOCK_REALTIME;
45
46 default:
47 return c;
48 }
49 }
50
51 usec_t now(clockid_t clock_id) {
52 struct timespec ts;
53
54 assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
55
56 return timespec_load(&ts);
57 }
58
59 nsec_t now_nsec(clockid_t clock_id) {
60 struct timespec ts;
61
62 assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
63
64 return timespec_load_nsec(&ts);
65 }
66
67 dual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
68 assert(ts);
69
70 ts->realtime = now(CLOCK_REALTIME);
71 ts->monotonic = now(CLOCK_MONOTONIC);
72
73 return ts;
74 }
75
76 triple_timestamp* triple_timestamp_get(triple_timestamp *ts) {
77 assert(ts);
78
79 ts->realtime = now(CLOCK_REALTIME);
80 ts->monotonic = now(CLOCK_MONOTONIC);
81 ts->boottime = clock_boottime_supported() ? now(CLOCK_BOOTTIME) : USEC_INFINITY;
82
83 return ts;
84 }
85
86 static usec_t map_clock_usec_internal(usec_t from, usec_t from_base, usec_t to_base) {
87
88 /* Maps the time 'from' between two clocks, based on a common reference point where the first clock
89 * is at 'from_base' and the second clock at 'to_base'. Basically calculates:
90 *
91 * from - from_base + to_base
92 *
93 * But takes care of overflows/underflows and avoids signed operations. */
94
95 if (from >= from_base) { /* In the future */
96 usec_t delta = from - from_base;
97
98 if (to_base >= USEC_INFINITY - delta) /* overflow? */
99 return USEC_INFINITY;
100
101 return to_base + delta;
102
103 } else { /* In the past */
104 usec_t delta = from_base - from;
105
106 if (to_base <= delta) /* underflow? */
107 return 0;
108
109 return to_base - delta;
110 }
111 }
112
113 usec_t map_clock_usec(usec_t from, clockid_t from_clock, clockid_t to_clock) {
114
115 /* Try to avoid any inaccuracy needlessly added in case we convert from effectively the same clock
116 * onto itself */
117 if (map_clock_id(from_clock) == map_clock_id(to_clock))
118 return from;
119
120 /* Keep infinity as is */
121 if (from == USEC_INFINITY)
122 return from;
123
124 return map_clock_usec_internal(from, now(from_clock), now(to_clock));
125 }
126
127 dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
128 assert(ts);
129
130 if (u == USEC_INFINITY || u == 0) {
131 ts->realtime = ts->monotonic = u;
132 return ts;
133 }
134
135 ts->realtime = u;
136 ts->monotonic = map_clock_usec(u, CLOCK_REALTIME, CLOCK_MONOTONIC);
137 return ts;
138 }
139
140 triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u) {
141 usec_t nowr;
142
143 assert(ts);
144
145 if (u == USEC_INFINITY || u == 0) {
146 ts->realtime = ts->monotonic = ts->boottime = u;
147 return ts;
148 }
149
150 nowr = now(CLOCK_REALTIME);
151
152 ts->realtime = u;
153 ts->monotonic = map_clock_usec_internal(u, nowr, now(CLOCK_MONOTONIC));
154 ts->boottime = clock_boottime_supported() ?
155 map_clock_usec_internal(u, nowr, now(CLOCK_BOOTTIME)) :
156 USEC_INFINITY;
157
158 return ts;
159 }
160
161 dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
162 assert(ts);
163
164 if (u == USEC_INFINITY) {
165 ts->realtime = ts->monotonic = USEC_INFINITY;
166 return ts;
167 }
168
169 ts->monotonic = u;
170 ts->realtime = map_clock_usec(u, CLOCK_MONOTONIC, CLOCK_REALTIME);
171 return ts;
172 }
173
174 dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, usec_t u) {
175 clockid_t cid;
176 usec_t nowm;
177
178 if (u == USEC_INFINITY) {
179 ts->realtime = ts->monotonic = USEC_INFINITY;
180 return ts;
181 }
182
183 cid = clock_boottime_or_monotonic();
184 nowm = now(cid);
185
186 if (cid == CLOCK_MONOTONIC)
187 ts->monotonic = u;
188 else
189 ts->monotonic = map_clock_usec_internal(u, nowm, now(CLOCK_MONOTONIC));
190
191 ts->realtime = map_clock_usec_internal(u, nowm, now(CLOCK_REALTIME));
192 return ts;
193 }
194
195 usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock) {
196
197 switch (clock) {
198
199 case CLOCK_REALTIME:
200 case CLOCK_REALTIME_ALARM:
201 return ts->realtime;
202
203 case CLOCK_MONOTONIC:
204 return ts->monotonic;
205
206 case CLOCK_BOOTTIME:
207 case CLOCK_BOOTTIME_ALARM:
208 return ts->boottime;
209
210 default:
211 return USEC_INFINITY;
212 }
213 }
214
215 usec_t timespec_load(const struct timespec *ts) {
216 assert(ts);
217
218 if (ts->tv_sec < 0 || ts->tv_nsec < 0)
219 return USEC_INFINITY;
220
221 if ((usec_t) ts->tv_sec > (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC)
222 return USEC_INFINITY;
223
224 return
225 (usec_t) ts->tv_sec * USEC_PER_SEC +
226 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
227 }
228
229 nsec_t timespec_load_nsec(const struct timespec *ts) {
230 assert(ts);
231
232 if (ts->tv_sec < 0 || ts->tv_nsec < 0)
233 return NSEC_INFINITY;
234
235 if ((nsec_t) ts->tv_sec >= (UINT64_MAX - ts->tv_nsec) / NSEC_PER_SEC)
236 return NSEC_INFINITY;
237
238 return (nsec_t) ts->tv_sec * NSEC_PER_SEC + (nsec_t) ts->tv_nsec;
239 }
240
241 struct timespec *timespec_store(struct timespec *ts, usec_t u) {
242 assert(ts);
243
244 if (u == USEC_INFINITY ||
245 u / USEC_PER_SEC >= TIME_T_MAX) {
246 ts->tv_sec = (time_t) -1;
247 ts->tv_nsec = -1L;
248 return ts;
249 }
250
251 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
252 ts->tv_nsec = (long) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
253
254 return ts;
255 }
256
257 struct timespec *timespec_store_nsec(struct timespec *ts, nsec_t n) {
258 assert(ts);
259
260 if (n == NSEC_INFINITY ||
261 n / NSEC_PER_SEC >= TIME_T_MAX) {
262 ts->tv_sec = (time_t) -1;
263 ts->tv_nsec = -1L;
264 return ts;
265 }
266
267 ts->tv_sec = (time_t) (n / NSEC_PER_SEC);
268 ts->tv_nsec = (long) (n % NSEC_PER_SEC);
269
270 return ts;
271 }
272
273 usec_t timeval_load(const struct timeval *tv) {
274 assert(tv);
275
276 if (tv->tv_sec < 0 || tv->tv_usec < 0)
277 return USEC_INFINITY;
278
279 if ((usec_t) tv->tv_sec > (UINT64_MAX - tv->tv_usec) / USEC_PER_SEC)
280 return USEC_INFINITY;
281
282 return
283 (usec_t) tv->tv_sec * USEC_PER_SEC +
284 (usec_t) tv->tv_usec;
285 }
286
287 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
288 assert(tv);
289
290 if (u == USEC_INFINITY ||
291 u / USEC_PER_SEC > TIME_T_MAX) {
292 tv->tv_sec = (time_t) -1;
293 tv->tv_usec = (suseconds_t) -1;
294 } else {
295 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
296 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
297 }
298
299 return tv;
300 }
301
302 char *format_timestamp_style(
303 char *buf,
304 size_t l,
305 usec_t t,
306 TimestampStyle style) {
307
308 /* The weekdays in non-localized (English) form. We use this instead of the localized form, so that our
309 * generated timestamps may be parsed with parse_timestamp(), and always read the same. */
310 static const char * const weekdays[] = {
311 [0] = "Sun",
312 [1] = "Mon",
313 [2] = "Tue",
314 [3] = "Wed",
315 [4] = "Thu",
316 [5] = "Fri",
317 [6] = "Sat",
318 };
319
320 struct tm tm;
321 time_t sec;
322 size_t n;
323 bool utc = false, us = false;
324
325 assert(buf);
326
327 switch (style) {
328 case TIMESTAMP_PRETTY:
329 break;
330 case TIMESTAMP_US:
331 us = true;
332 break;
333 case TIMESTAMP_UTC:
334 utc = true;
335 break;
336 case TIMESTAMP_US_UTC:
337 us = true;
338 utc = true;
339 break;
340 default:
341 return NULL;
342 }
343
344 if (l < (size_t) (3 + /* week day */
345 1 + 10 + /* space and date */
346 1 + 8 + /* space and time */
347 (us ? 1 + 6 : 0) + /* "." and microsecond part */
348 1 + 1 + /* space and shortest possible zone */
349 1))
350 return NULL; /* Not enough space even for the shortest form. */
351 if (t <= 0 || t == USEC_INFINITY)
352 return NULL; /* Timestamp is unset */
353
354 /* Let's not format times with years > 9999 */
355 if (t > USEC_TIMESTAMP_FORMATTABLE_MAX) {
356 assert(l >= STRLEN("--- XXXX-XX-XX XX:XX:XX") + 1);
357 strcpy(buf, "--- XXXX-XX-XX XX:XX:XX");
358 return buf;
359 }
360
361 sec = (time_t) (t / USEC_PER_SEC); /* Round down */
362
363 if (!localtime_or_gmtime_r(&sec, &tm, utc))
364 return NULL;
365
366 /* Start with the week day */
367 assert((size_t) tm.tm_wday < ELEMENTSOF(weekdays));
368 memcpy(buf, weekdays[tm.tm_wday], 4);
369
370 /* Add the main components */
371 if (strftime(buf + 3, l - 3, " %Y-%m-%d %H:%M:%S", &tm) <= 0)
372 return NULL; /* Doesn't fit */
373
374 /* Append the microseconds part, if that's requested */
375 if (us) {
376 n = strlen(buf);
377 if (n + 8 > l)
378 return NULL; /* Microseconds part doesn't fit. */
379
380 sprintf(buf + n, ".%06"PRI_USEC, t % USEC_PER_SEC);
381 }
382
383 /* Append the timezone */
384 n = strlen(buf);
385 if (utc) {
386 /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r() normally uses the
387 * obsolete "GMT" instead. */
388 if (n + 5 > l)
389 return NULL; /* "UTC" doesn't fit. */
390
391 strcpy(buf + n, " UTC");
392
393 } else if (!isempty(tm.tm_zone)) {
394 size_t tn;
395
396 /* An explicit timezone is specified, let's use it, if it fits */
397 tn = strlen(tm.tm_zone);
398 if (n + 1 + tn + 1 > l) {
399 /* The full time zone does not fit in. Yuck. */
400
401 if (n + 1 + _POSIX_TZNAME_MAX + 1 > l)
402 return NULL; /* Not even enough space for the POSIX minimum (of 6)? In that case, complain that it doesn't fit */
403
404 /* So the time zone doesn't fit in fully, but the caller passed enough space for the POSIX
405 * minimum time zone length. In this case suppress the timezone entirely, in order not to dump
406 * an overly long, hard to read string on the user. This should be safe, because the user will
407 * assume the local timezone anyway if none is shown. And so does parse_timestamp(). */
408 } else {
409 buf[n++] = ' ';
410 strcpy(buf + n, tm.tm_zone);
411 }
412 }
413
414 return buf;
415 }
416
417 char *format_timestamp_relative(char *buf, size_t l, usec_t t) {
418 const char *s;
419 usec_t n, d;
420
421 if (t <= 0 || t == USEC_INFINITY)
422 return NULL;
423
424 n = now(CLOCK_REALTIME);
425 if (n > t) {
426 d = n - t;
427 s = "ago";
428 } else {
429 d = t - n;
430 s = "left";
431 }
432
433 if (d >= USEC_PER_YEAR)
434 snprintf(buf, l, USEC_FMT " years " USEC_FMT " months %s",
435 d / USEC_PER_YEAR,
436 (d % USEC_PER_YEAR) / USEC_PER_MONTH, s);
437 else if (d >= USEC_PER_MONTH)
438 snprintf(buf, l, USEC_FMT " months " USEC_FMT " days %s",
439 d / USEC_PER_MONTH,
440 (d % USEC_PER_MONTH) / USEC_PER_DAY, s);
441 else if (d >= USEC_PER_WEEK)
442 snprintf(buf, l, USEC_FMT " weeks " USEC_FMT " days %s",
443 d / USEC_PER_WEEK,
444 (d % USEC_PER_WEEK) / USEC_PER_DAY, s);
445 else if (d >= 2*USEC_PER_DAY)
446 snprintf(buf, l, USEC_FMT " days %s", d / USEC_PER_DAY, s);
447 else if (d >= 25*USEC_PER_HOUR)
448 snprintf(buf, l, "1 day " USEC_FMT "h %s",
449 (d - USEC_PER_DAY) / USEC_PER_HOUR, s);
450 else if (d >= 6*USEC_PER_HOUR)
451 snprintf(buf, l, USEC_FMT "h %s",
452 d / USEC_PER_HOUR, s);
453 else if (d >= USEC_PER_HOUR)
454 snprintf(buf, l, USEC_FMT "h " USEC_FMT "min %s",
455 d / USEC_PER_HOUR,
456 (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s);
457 else if (d >= 5*USEC_PER_MINUTE)
458 snprintf(buf, l, USEC_FMT "min %s",
459 d / USEC_PER_MINUTE, s);
460 else if (d >= USEC_PER_MINUTE)
461 snprintf(buf, l, USEC_FMT "min " USEC_FMT "s %s",
462 d / USEC_PER_MINUTE,
463 (d % USEC_PER_MINUTE) / USEC_PER_SEC, s);
464 else if (d >= USEC_PER_SEC)
465 snprintf(buf, l, USEC_FMT "s %s",
466 d / USEC_PER_SEC, s);
467 else if (d >= USEC_PER_MSEC)
468 snprintf(buf, l, USEC_FMT "ms %s",
469 d / USEC_PER_MSEC, s);
470 else if (d > 0)
471 snprintf(buf, l, USEC_FMT"us %s",
472 d, s);
473 else
474 snprintf(buf, l, "now");
475
476 buf[l-1] = 0;
477 return buf;
478 }
479
480 char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
481 static const struct {
482 const char *suffix;
483 usec_t usec;
484 } table[] = {
485 { "y", USEC_PER_YEAR },
486 { "month", USEC_PER_MONTH },
487 { "w", USEC_PER_WEEK },
488 { "d", USEC_PER_DAY },
489 { "h", USEC_PER_HOUR },
490 { "min", USEC_PER_MINUTE },
491 { "s", USEC_PER_SEC },
492 { "ms", USEC_PER_MSEC },
493 { "us", 1 },
494 };
495
496 size_t i;
497 char *p = buf;
498 bool something = false;
499
500 assert(buf);
501 assert(l > 0);
502
503 if (t == USEC_INFINITY) {
504 strncpy(p, "infinity", l-1);
505 p[l-1] = 0;
506 return p;
507 }
508
509 if (t <= 0) {
510 strncpy(p, "0", l-1);
511 p[l-1] = 0;
512 return p;
513 }
514
515 /* The result of this function can be parsed with parse_sec */
516
517 for (i = 0; i < ELEMENTSOF(table); i++) {
518 int k = 0;
519 size_t n;
520 bool done = false;
521 usec_t a, b;
522
523 if (t <= 0)
524 break;
525
526 if (t < accuracy && something)
527 break;
528
529 if (t < table[i].usec)
530 continue;
531
532 if (l <= 1)
533 break;
534
535 a = t / table[i].usec;
536 b = t % table[i].usec;
537
538 /* Let's see if we should shows this in dot notation */
539 if (t < USEC_PER_MINUTE && b > 0) {
540 usec_t cc;
541 signed char j;
542
543 j = 0;
544 for (cc = table[i].usec; cc > 1; cc /= 10)
545 j++;
546
547 for (cc = accuracy; cc > 1; cc /= 10) {
548 b /= 10;
549 j--;
550 }
551
552 if (j > 0) {
553 k = snprintf(p, l,
554 "%s"USEC_FMT".%0*"PRI_USEC"%s",
555 p > buf ? " " : "",
556 a,
557 j,
558 b,
559 table[i].suffix);
560
561 t = 0;
562 done = true;
563 }
564 }
565
566 /* No? Then let's show it normally */
567 if (!done) {
568 k = snprintf(p, l,
569 "%s"USEC_FMT"%s",
570 p > buf ? " " : "",
571 a,
572 table[i].suffix);
573
574 t = b;
575 }
576
577 n = MIN((size_t) k, l);
578
579 l -= n;
580 p += n;
581
582 something = true;
583 }
584
585 *p = 0;
586
587 return buf;
588 }
589
590 static int parse_timestamp_impl(const char *t, usec_t *usec, bool with_tz) {
591 static const struct {
592 const char *name;
593 const int nr;
594 } day_nr[] = {
595 { "Sunday", 0 },
596 { "Sun", 0 },
597 { "Monday", 1 },
598 { "Mon", 1 },
599 { "Tuesday", 2 },
600 { "Tue", 2 },
601 { "Wednesday", 3 },
602 { "Wed", 3 },
603 { "Thursday", 4 },
604 { "Thu", 4 },
605 { "Friday", 5 },
606 { "Fri", 5 },
607 { "Saturday", 6 },
608 { "Sat", 6 },
609 };
610
611 const char *k, *utc = NULL, *tzn = NULL;
612 struct tm tm, copy;
613 time_t x;
614 usec_t x_usec, plus = 0, minus = 0, ret;
615 int r, weekday = -1, dst = -1;
616 size_t i;
617
618 /* Allowed syntaxes:
619 *
620 * 2012-09-22 16:34:22
621 * 2012-09-22 16:34 (seconds will be set to 0)
622 * 2012-09-22 (time will be set to 00:00:00)
623 * 16:34:22 (date will be set to today)
624 * 16:34 (date will be set to today, seconds to 0)
625 * now
626 * yesterday (time is set to 00:00:00)
627 * today (time is set to 00:00:00)
628 * tomorrow (time is set to 00:00:00)
629 * +5min
630 * -5days
631 * @2147483647 (seconds since epoch)
632 */
633
634 assert(t);
635
636 if (t[0] == '@' && !with_tz)
637 return parse_sec(t + 1, usec);
638
639 ret = now(CLOCK_REALTIME);
640
641 if (!with_tz) {
642 if (streq(t, "now"))
643 goto finish;
644
645 else if (t[0] == '+') {
646 r = parse_sec(t+1, &plus);
647 if (r < 0)
648 return r;
649
650 goto finish;
651
652 } else if (t[0] == '-') {
653 r = parse_sec(t+1, &minus);
654 if (r < 0)
655 return r;
656
657 goto finish;
658
659 } else if ((k = endswith(t, " ago"))) {
660 t = strndupa(t, k - t);
661
662 r = parse_sec(t, &minus);
663 if (r < 0)
664 return r;
665
666 goto finish;
667
668 } else if ((k = endswith(t, " left"))) {
669 t = strndupa(t, k - t);
670
671 r = parse_sec(t, &plus);
672 if (r < 0)
673 return r;
674
675 goto finish;
676 }
677
678 /* See if the timestamp is suffixed with UTC */
679 utc = endswith_no_case(t, " UTC");
680 if (utc)
681 t = strndupa(t, utc - t);
682 else {
683 const char *e = NULL;
684 int j;
685
686 tzset();
687
688 /* See if the timestamp is suffixed by either the DST or non-DST local timezone. Note that we only
689 * support the local timezones here, nothing else. Not because we wouldn't want to, but simply because
690 * there are no nice APIs available to cover this. By accepting the local time zone strings, we make
691 * sure that all timestamps written by format_timestamp() can be parsed correctly, even though we don't
692 * support arbitrary timezone specifications. */
693
694 for (j = 0; j <= 1; j++) {
695
696 if (isempty(tzname[j]))
697 continue;
698
699 e = endswith_no_case(t, tzname[j]);
700 if (!e)
701 continue;
702 if (e == t)
703 continue;
704 if (e[-1] != ' ')
705 continue;
706
707 break;
708 }
709
710 if (IN_SET(j, 0, 1)) {
711 /* Found one of the two timezones specified. */
712 t = strndupa(t, e - t - 1);
713 dst = j;
714 tzn = tzname[j];
715 }
716 }
717 }
718
719 x = (time_t) (ret / USEC_PER_SEC);
720 x_usec = 0;
721
722 if (!localtime_or_gmtime_r(&x, &tm, utc))
723 return -EINVAL;
724
725 tm.tm_isdst = dst;
726 if (!with_tz && tzn)
727 tm.tm_zone = tzn;
728
729 if (streq(t, "today")) {
730 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
731 goto from_tm;
732
733 } else if (streq(t, "yesterday")) {
734 tm.tm_mday--;
735 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
736 goto from_tm;
737
738 } else if (streq(t, "tomorrow")) {
739 tm.tm_mday++;
740 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
741 goto from_tm;
742 }
743
744 for (i = 0; i < ELEMENTSOF(day_nr); i++) {
745 size_t skip;
746
747 if (!startswith_no_case(t, day_nr[i].name))
748 continue;
749
750 skip = strlen(day_nr[i].name);
751 if (t[skip] != ' ')
752 continue;
753
754 weekday = day_nr[i].nr;
755 t += skip + 1;
756 break;
757 }
758
759 copy = tm;
760 k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
761 if (k) {
762 if (*k == '.')
763 goto parse_usec;
764 else if (*k == 0)
765 goto from_tm;
766 }
767
768 tm = copy;
769 k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm);
770 if (k) {
771 if (*k == '.')
772 goto parse_usec;
773 else if (*k == 0)
774 goto from_tm;
775 }
776
777 tm = copy;
778 k = strptime(t, "%y-%m-%d %H:%M", &tm);
779 if (k && *k == 0) {
780 tm.tm_sec = 0;
781 goto from_tm;
782 }
783
784 tm = copy;
785 k = strptime(t, "%Y-%m-%d %H:%M", &tm);
786 if (k && *k == 0) {
787 tm.tm_sec = 0;
788 goto from_tm;
789 }
790
791 tm = copy;
792 k = strptime(t, "%y-%m-%d", &tm);
793 if (k && *k == 0) {
794 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
795 goto from_tm;
796 }
797
798 tm = copy;
799 k = strptime(t, "%Y-%m-%d", &tm);
800 if (k && *k == 0) {
801 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
802 goto from_tm;
803 }
804
805 tm = copy;
806 k = strptime(t, "%H:%M:%S", &tm);
807 if (k) {
808 if (*k == '.')
809 goto parse_usec;
810 else if (*k == 0)
811 goto from_tm;
812 }
813
814 tm = copy;
815 k = strptime(t, "%H:%M", &tm);
816 if (k && *k == 0) {
817 tm.tm_sec = 0;
818 goto from_tm;
819 }
820
821 return -EINVAL;
822
823 parse_usec:
824 {
825 unsigned add;
826
827 k++;
828 r = parse_fractional_part_u(&k, 6, &add);
829 if (r < 0)
830 return -EINVAL;
831
832 if (*k)
833 return -EINVAL;
834
835 x_usec = add;
836 }
837
838 from_tm:
839 if (weekday >= 0 && tm.tm_wday != weekday)
840 return -EINVAL;
841
842 x = mktime_or_timegm(&tm, utc);
843 if (x < 0)
844 return -EINVAL;
845
846 ret = (usec_t) x * USEC_PER_SEC + x_usec;
847 if (ret > USEC_TIMESTAMP_FORMATTABLE_MAX)
848 return -EINVAL;
849
850 finish:
851 if (ret + plus < ret) /* overflow? */
852 return -EINVAL;
853 ret += plus;
854 if (ret > USEC_TIMESTAMP_FORMATTABLE_MAX)
855 return -EINVAL;
856
857 if (ret >= minus)
858 ret -= minus;
859 else
860 return -EINVAL;
861
862 if (usec)
863 *usec = ret;
864 return 0;
865 }
866
867 typedef struct ParseTimestampResult {
868 usec_t usec;
869 int return_value;
870 } ParseTimestampResult;
871
872 int parse_timestamp(const char *t, usec_t *usec) {
873 char *last_space, *tz = NULL;
874 ParseTimestampResult *shared, tmp;
875 int r;
876
877 last_space = strrchr(t, ' ');
878 if (last_space != NULL && timezone_is_valid(last_space + 1, LOG_DEBUG))
879 tz = last_space + 1;
880
881 if (!tz || endswith_no_case(t, " UTC"))
882 return parse_timestamp_impl(t, usec, false);
883
884 shared = mmap(NULL, sizeof *shared, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
885 if (shared == MAP_FAILED)
886 return negative_errno();
887
888 r = safe_fork("(sd-timestamp)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT, NULL);
889 if (r < 0) {
890 (void) munmap(shared, sizeof *shared);
891 return r;
892 }
893 if (r == 0) {
894 bool with_tz = true;
895 char *colon_tz;
896
897 /* tzset(3) says $TZ should be prefixed with ":" if we reference timezone files */
898 colon_tz = strjoina(":", tz);
899
900 if (setenv("TZ", colon_tz, 1) != 0) {
901 shared->return_value = negative_errno();
902 _exit(EXIT_FAILURE);
903 }
904
905 tzset();
906
907 /* If there is a timezone that matches the tzname fields, leave the parsing to the implementation.
908 * Otherwise just cut it off. */
909 with_tz = !STR_IN_SET(tz, tzname[0], tzname[1]);
910
911 /* Cut off the timezone if we don't need it. */
912 if (with_tz)
913 t = strndupa(t, last_space - t);
914
915 shared->return_value = parse_timestamp_impl(t, &shared->usec, with_tz);
916
917 _exit(EXIT_SUCCESS);
918 }
919
920 tmp = *shared;
921 if (munmap(shared, sizeof *shared) != 0)
922 return negative_errno();
923
924 if (tmp.return_value == 0 && usec)
925 *usec = tmp.usec;
926
927 return tmp.return_value;
928 }
929
930 static const char* extract_multiplier(const char *p, usec_t *multiplier) {
931 static const struct {
932 const char *suffix;
933 usec_t usec;
934 } table[] = {
935 { "seconds", USEC_PER_SEC },
936 { "second", USEC_PER_SEC },
937 { "sec", USEC_PER_SEC },
938 { "s", USEC_PER_SEC },
939 { "minutes", USEC_PER_MINUTE },
940 { "minute", USEC_PER_MINUTE },
941 { "min", USEC_PER_MINUTE },
942 { "months", USEC_PER_MONTH },
943 { "month", USEC_PER_MONTH },
944 { "M", USEC_PER_MONTH },
945 { "msec", USEC_PER_MSEC },
946 { "ms", USEC_PER_MSEC },
947 { "m", USEC_PER_MINUTE },
948 { "hours", USEC_PER_HOUR },
949 { "hour", USEC_PER_HOUR },
950 { "hr", USEC_PER_HOUR },
951 { "h", USEC_PER_HOUR },
952 { "days", USEC_PER_DAY },
953 { "day", USEC_PER_DAY },
954 { "d", USEC_PER_DAY },
955 { "weeks", USEC_PER_WEEK },
956 { "week", USEC_PER_WEEK },
957 { "w", USEC_PER_WEEK },
958 { "years", USEC_PER_YEAR },
959 { "year", USEC_PER_YEAR },
960 { "y", USEC_PER_YEAR },
961 { "usec", 1ULL },
962 { "us", 1ULL },
963 { "µs", 1ULL },
964 };
965 size_t i;
966
967 for (i = 0; i < ELEMENTSOF(table); i++) {
968 char *e;
969
970 e = startswith(p, table[i].suffix);
971 if (e) {
972 *multiplier = table[i].usec;
973 return e;
974 }
975 }
976
977 return p;
978 }
979
980 int parse_time(const char *t, usec_t *usec, usec_t default_unit) {
981 const char *p, *s;
982 usec_t r = 0;
983 bool something = false;
984
985 assert(t);
986 assert(default_unit > 0);
987
988 p = t;
989
990 p += strspn(p, WHITESPACE);
991 s = startswith(p, "infinity");
992 if (s) {
993 s += strspn(s, WHITESPACE);
994 if (*s != 0)
995 return -EINVAL;
996
997 if (usec)
998 *usec = USEC_INFINITY;
999 return 0;
1000 }
1001
1002 for (;;) {
1003 usec_t multiplier = default_unit, k;
1004 long long l;
1005 char *e;
1006
1007 p += strspn(p, WHITESPACE);
1008
1009 if (*p == 0) {
1010 if (!something)
1011 return -EINVAL;
1012
1013 break;
1014 }
1015
1016 if (*p == '-') /* Don't allow "-0" */
1017 return -ERANGE;
1018
1019 errno = 0;
1020 l = strtoll(p, &e, 10);
1021 if (errno > 0)
1022 return -errno;
1023 if (l < 0)
1024 return -ERANGE;
1025
1026 if (*e == '.') {
1027 p = e + 1;
1028 p += strspn(p, DIGITS);
1029 } else if (e == p)
1030 return -EINVAL;
1031 else
1032 p = e;
1033
1034 s = extract_multiplier(p + strspn(p, WHITESPACE), &multiplier);
1035 if (s == p && *s != '\0')
1036 /* Don't allow '12.34.56', but accept '12.34 .56' or '12.34s.56'*/
1037 return -EINVAL;
1038
1039 p = s;
1040
1041 if ((usec_t) l >= USEC_INFINITY / multiplier)
1042 return -ERANGE;
1043
1044 k = (usec_t) l * multiplier;
1045 if (k >= USEC_INFINITY - r)
1046 return -ERANGE;
1047
1048 r += k;
1049
1050 something = true;
1051
1052 if (*e == '.') {
1053 usec_t m = multiplier / 10;
1054 const char *b;
1055
1056 for (b = e + 1; *b >= '0' && *b <= '9'; b++, m /= 10) {
1057 k = (usec_t) (*b - '0') * m;
1058 if (k >= USEC_INFINITY - r)
1059 return -ERANGE;
1060
1061 r += k;
1062 }
1063
1064 /* Don't allow "0.-0", "3.+1", "3. 1", "3.sec" or "3.hoge"*/
1065 if (b == e + 1)
1066 return -EINVAL;
1067 }
1068 }
1069
1070 if (usec)
1071 *usec = r;
1072 return 0;
1073 }
1074
1075 int parse_sec(const char *t, usec_t *usec) {
1076 return parse_time(t, usec, USEC_PER_SEC);
1077 }
1078
1079 int parse_sec_fix_0(const char *t, usec_t *ret) {
1080 usec_t k;
1081 int r;
1082
1083 assert(t);
1084 assert(ret);
1085
1086 r = parse_sec(t, &k);
1087 if (r < 0)
1088 return r;
1089
1090 *ret = k == 0 ? USEC_INFINITY : k;
1091 return r;
1092 }
1093
1094 int parse_sec_def_infinity(const char *t, usec_t *ret) {
1095 t += strspn(t, WHITESPACE);
1096 if (isempty(t)) {
1097 *ret = USEC_INFINITY;
1098 return 0;
1099 }
1100 return parse_sec(t, ret);
1101 }
1102
1103 static const char* extract_nsec_multiplier(const char *p, nsec_t *multiplier) {
1104 static const struct {
1105 const char *suffix;
1106 nsec_t nsec;
1107 } table[] = {
1108 { "seconds", NSEC_PER_SEC },
1109 { "second", NSEC_PER_SEC },
1110 { "sec", NSEC_PER_SEC },
1111 { "s", NSEC_PER_SEC },
1112 { "minutes", NSEC_PER_MINUTE },
1113 { "minute", NSEC_PER_MINUTE },
1114 { "min", NSEC_PER_MINUTE },
1115 { "months", NSEC_PER_MONTH },
1116 { "month", NSEC_PER_MONTH },
1117 { "M", NSEC_PER_MONTH },
1118 { "msec", NSEC_PER_MSEC },
1119 { "ms", NSEC_PER_MSEC },
1120 { "m", NSEC_PER_MINUTE },
1121 { "hours", NSEC_PER_HOUR },
1122 { "hour", NSEC_PER_HOUR },
1123 { "hr", NSEC_PER_HOUR },
1124 { "h", NSEC_PER_HOUR },
1125 { "days", NSEC_PER_DAY },
1126 { "day", NSEC_PER_DAY },
1127 { "d", NSEC_PER_DAY },
1128 { "weeks", NSEC_PER_WEEK },
1129 { "week", NSEC_PER_WEEK },
1130 { "w", NSEC_PER_WEEK },
1131 { "years", NSEC_PER_YEAR },
1132 { "year", NSEC_PER_YEAR },
1133 { "y", NSEC_PER_YEAR },
1134 { "usec", NSEC_PER_USEC },
1135 { "us", NSEC_PER_USEC },
1136 { "µs", NSEC_PER_USEC },
1137 { "nsec", 1ULL },
1138 { "ns", 1ULL },
1139 { "", 1ULL }, /* default is nsec */
1140 };
1141 size_t i;
1142
1143 for (i = 0; i < ELEMENTSOF(table); i++) {
1144 char *e;
1145
1146 e = startswith(p, table[i].suffix);
1147 if (e) {
1148 *multiplier = table[i].nsec;
1149 return e;
1150 }
1151 }
1152
1153 return p;
1154 }
1155
1156 int parse_nsec(const char *t, nsec_t *nsec) {
1157 const char *p, *s;
1158 nsec_t r = 0;
1159 bool something = false;
1160
1161 assert(t);
1162 assert(nsec);
1163
1164 p = t;
1165
1166 p += strspn(p, WHITESPACE);
1167 s = startswith(p, "infinity");
1168 if (s) {
1169 s += strspn(s, WHITESPACE);
1170 if (*s != 0)
1171 return -EINVAL;
1172
1173 *nsec = NSEC_INFINITY;
1174 return 0;
1175 }
1176
1177 for (;;) {
1178 nsec_t multiplier = 1, k;
1179 long long l;
1180 char *e;
1181
1182 p += strspn(p, WHITESPACE);
1183
1184 if (*p == 0) {
1185 if (!something)
1186 return -EINVAL;
1187
1188 break;
1189 }
1190
1191 if (*p == '-') /* Don't allow "-0" */
1192 return -ERANGE;
1193
1194 errno = 0;
1195 l = strtoll(p, &e, 10);
1196 if (errno > 0)
1197 return -errno;
1198 if (l < 0)
1199 return -ERANGE;
1200
1201 if (*e == '.') {
1202 p = e + 1;
1203 p += strspn(p, DIGITS);
1204 } else if (e == p)
1205 return -EINVAL;
1206 else
1207 p = e;
1208
1209 s = extract_nsec_multiplier(p + strspn(p, WHITESPACE), &multiplier);
1210 if (s == p && *s != '\0')
1211 /* Don't allow '12.34.56', but accept '12.34 .56' or '12.34s.56'*/
1212 return -EINVAL;
1213
1214 p = s;
1215
1216 if ((nsec_t) l >= NSEC_INFINITY / multiplier)
1217 return -ERANGE;
1218
1219 k = (nsec_t) l * multiplier;
1220 if (k >= NSEC_INFINITY - r)
1221 return -ERANGE;
1222
1223 r += k;
1224
1225 something = true;
1226
1227 if (*e == '.') {
1228 nsec_t m = multiplier / 10;
1229 const char *b;
1230
1231 for (b = e + 1; *b >= '0' && *b <= '9'; b++, m /= 10) {
1232 k = (nsec_t) (*b - '0') * m;
1233 if (k >= NSEC_INFINITY - r)
1234 return -ERANGE;
1235
1236 r += k;
1237 }
1238
1239 /* Don't allow "0.-0", "3.+1", "3. 1", "3.sec" or "3.hoge"*/
1240 if (b == e + 1)
1241 return -EINVAL;
1242 }
1243 }
1244
1245 *nsec = r;
1246
1247 return 0;
1248 }
1249
1250 bool ntp_synced(void) {
1251 struct timex txc = {};
1252
1253 if (adjtimex(&txc) < 0)
1254 return false;
1255
1256 /* Consider the system clock synchronized if the reported maximum error is smaller than the maximum
1257 * value (16 seconds). Ignore the STA_UNSYNC flag as it may have been set to prevent the kernel from
1258 * touching the RTC. */
1259 if (txc.maxerror >= 16000000)
1260 return false;
1261
1262 return true;
1263 }
1264
1265 int get_timezones(char ***ret) {
1266 _cleanup_fclose_ FILE *f = NULL;
1267 _cleanup_strv_free_ char **zones = NULL;
1268 size_t n_zones = 0, n_allocated = 0;
1269 int r;
1270
1271 assert(ret);
1272
1273 zones = strv_new("UTC");
1274 if (!zones)
1275 return -ENOMEM;
1276
1277 n_allocated = 2;
1278 n_zones = 1;
1279
1280 f = fopen("/usr/share/zoneinfo/zone1970.tab", "re");
1281 if (f) {
1282 for (;;) {
1283 _cleanup_free_ char *line = NULL;
1284 char *p, *w;
1285 size_t k;
1286
1287 r = read_line(f, LONG_LINE_MAX, &line);
1288 if (r < 0)
1289 return r;
1290 if (r == 0)
1291 break;
1292
1293 p = strstrip(line);
1294
1295 if (isempty(p) || *p == '#')
1296 continue;
1297
1298 /* Skip over country code */
1299 p += strcspn(p, WHITESPACE);
1300 p += strspn(p, WHITESPACE);
1301
1302 /* Skip over coordinates */
1303 p += strcspn(p, WHITESPACE);
1304 p += strspn(p, WHITESPACE);
1305
1306 /* Found timezone name */
1307 k = strcspn(p, WHITESPACE);
1308 if (k <= 0)
1309 continue;
1310
1311 w = strndup(p, k);
1312 if (!w)
1313 return -ENOMEM;
1314
1315 if (!GREEDY_REALLOC(zones, n_allocated, n_zones + 2)) {
1316 free(w);
1317 return -ENOMEM;
1318 }
1319
1320 zones[n_zones++] = w;
1321 zones[n_zones] = NULL;
1322 }
1323
1324 strv_sort(zones);
1325 strv_uniq(zones);
1326
1327 } else if (errno != ENOENT)
1328 return -errno;
1329
1330 *ret = TAKE_PTR(zones);
1331
1332 return 0;
1333 }
1334
1335 bool timezone_is_valid(const char *name, int log_level) {
1336 bool slash = false;
1337 const char *p, *t;
1338 _cleanup_close_ int fd = -1;
1339 char buf[4];
1340 int r;
1341
1342 if (isempty(name))
1343 return false;
1344
1345 /* Always accept "UTC" as valid timezone, since it's the fallback, even if user has no timezones installed. */
1346 if (streq(name, "UTC"))
1347 return true;
1348
1349 if (name[0] == '/')
1350 return false;
1351
1352 for (p = name; *p; p++) {
1353 if (!(*p >= '0' && *p <= '9') &&
1354 !(*p >= 'a' && *p <= 'z') &&
1355 !(*p >= 'A' && *p <= 'Z') &&
1356 !IN_SET(*p, '-', '_', '+', '/'))
1357 return false;
1358
1359 if (*p == '/') {
1360
1361 if (slash)
1362 return false;
1363
1364 slash = true;
1365 } else
1366 slash = false;
1367 }
1368
1369 if (slash)
1370 return false;
1371
1372 if (p - name >= PATH_MAX)
1373 return false;
1374
1375 t = strjoina("/usr/share/zoneinfo/", name);
1376
1377 fd = open(t, O_RDONLY|O_CLOEXEC);
1378 if (fd < 0) {
1379 log_full_errno(log_level, errno, "Failed to open timezone file '%s': %m", t);
1380 return false;
1381 }
1382
1383 r = fd_verify_regular(fd);
1384 if (r < 0) {
1385 log_full_errno(log_level, r, "Timezone file '%s' is not a regular file: %m", t);
1386 return false;
1387 }
1388
1389 r = loop_read_exact(fd, buf, 4, false);
1390 if (r < 0) {
1391 log_full_errno(log_level, r, "Failed to read from timezone file '%s': %m", t);
1392 return false;
1393 }
1394
1395 /* Magic from tzfile(5) */
1396 if (memcmp(buf, "TZif", 4) != 0) {
1397 log_full(log_level, "Timezone file '%s' has wrong magic bytes", t);
1398 return false;
1399 }
1400
1401 return true;
1402 }
1403
1404 bool clock_boottime_supported(void) {
1405 static int supported = -1;
1406
1407 /* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */
1408
1409 if (supported < 0) {
1410 int fd;
1411
1412 fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC);
1413 if (fd < 0)
1414 supported = false;
1415 else {
1416 safe_close(fd);
1417 supported = true;
1418 }
1419 }
1420
1421 return supported;
1422 }
1423
1424 clockid_t clock_boottime_or_monotonic(void) {
1425 if (clock_boottime_supported())
1426 return CLOCK_BOOTTIME;
1427 else
1428 return CLOCK_MONOTONIC;
1429 }
1430
1431 bool clock_supported(clockid_t clock) {
1432 struct timespec ts;
1433
1434 switch (clock) {
1435
1436 case CLOCK_MONOTONIC:
1437 case CLOCK_REALTIME:
1438 return true;
1439
1440 case CLOCK_BOOTTIME:
1441 return clock_boottime_supported();
1442
1443 case CLOCK_BOOTTIME_ALARM:
1444 if (!clock_boottime_supported())
1445 return false;
1446
1447 _fallthrough_;
1448 default:
1449 /* For everything else, check properly */
1450 return clock_gettime(clock, &ts) >= 0;
1451 }
1452 }
1453
1454 int get_timezone(char **ret) {
1455 _cleanup_free_ char *t = NULL;
1456 const char *e;
1457 char *z;
1458 int r;
1459
1460 r = readlink_malloc("/etc/localtime", &t);
1461 if (r == -ENOENT) {
1462 /* If the symlink does not exist, assume "UTC", like glibc does*/
1463 z = strdup("UTC");
1464 if (!z)
1465 return -ENOMEM;
1466
1467 *ret = z;
1468 return 0;
1469 }
1470 if (r < 0)
1471 return r; /* returns EINVAL if not a symlink */
1472
1473 e = PATH_STARTSWITH_SET(t, "/usr/share/zoneinfo/", "../usr/share/zoneinfo/");
1474 if (!e)
1475 return -EINVAL;
1476
1477 if (!timezone_is_valid(e, LOG_DEBUG))
1478 return -EINVAL;
1479
1480 z = strdup(e);
1481 if (!z)
1482 return -ENOMEM;
1483
1484 *ret = z;
1485 return 0;
1486 }
1487
1488 time_t mktime_or_timegm(struct tm *tm, bool utc) {
1489 return utc ? timegm(tm) : mktime(tm);
1490 }
1491
1492 struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc) {
1493 return utc ? gmtime_r(t, tm) : localtime_r(t, tm);
1494 }
1495
1496 static uint32_t sysconf_clock_ticks_cached(void) {
1497 static thread_local uint32_t hz = 0;
1498 long r;
1499
1500 if (hz == 0) {
1501 r = sysconf(_SC_CLK_TCK);
1502
1503 assert(r > 0);
1504 hz = r;
1505 }
1506
1507 return hz;
1508 }
1509
1510 uint32_t usec_to_jiffies(usec_t u) {
1511 uint32_t hz = sysconf_clock_ticks_cached();
1512 return DIV_ROUND_UP(u, USEC_PER_SEC / hz);
1513 }
1514
1515 usec_t jiffies_to_usec(uint32_t j) {
1516 uint32_t hz = sysconf_clock_ticks_cached();
1517 return DIV_ROUND_UP(j * USEC_PER_SEC, hz);
1518 }
1519
1520 usec_t usec_shift_clock(usec_t x, clockid_t from, clockid_t to) {
1521 usec_t a, b;
1522
1523 if (x == USEC_INFINITY)
1524 return USEC_INFINITY;
1525 if (map_clock_id(from) == map_clock_id(to))
1526 return x;
1527
1528 a = now(from);
1529 b = now(to);
1530
1531 if (x > a)
1532 /* x lies in the future */
1533 return usec_add(b, usec_sub_unsigned(x, a));
1534 else
1535 /* x lies in the past */
1536 return usec_sub_unsigned(b, usec_sub_unsigned(a, x));
1537 }
1538
1539 bool in_utc_timezone(void) {
1540 tzset();
1541
1542 return timezone == 0 && daylight == 0;
1543 }
1544
1545 int time_change_fd(void) {
1546
1547 /* We only care for the cancellation event, hence we set the timeout to the latest possible value. */
1548 static const struct itimerspec its = {
1549 .it_value.tv_sec = TIME_T_MAX,
1550 };
1551
1552 _cleanup_close_ int fd;
1553
1554 assert_cc(sizeof(time_t) == sizeof(TIME_T_MAX));
1555
1556 /* Uses TFD_TIMER_CANCEL_ON_SET to get notifications whenever CLOCK_REALTIME makes a jump relative to
1557 * CLOCK_MONOTONIC. */
1558
1559 fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
1560 if (fd < 0)
1561 return -errno;
1562
1563 if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) >= 0)
1564 return TAKE_FD(fd);
1565
1566 /* So apparently there are systems where time_t is 64bit, but the kernel actually doesn't support
1567 * 64bit time_t. In that case configuring a timer to TIME_T_MAX will fail with EOPNOTSUPP or a
1568 * similar error. If that's the case let's try with INT32_MAX instead, maybe that works. It's a bit
1569 * of a black magic thing though, but what can we do?
1570 *
1571 * We don't want this code on x86-64, hence let's conditionalize this for systems with 64bit time_t
1572 * but where "long" is shorter than 64bit, i.e. 32bit archs.
1573 *
1574 * See: https://github.com/systemd/systemd/issues/14362 */
1575
1576 #if SIZEOF_TIME_T == 8 && ULONG_MAX < UINT64_MAX
1577 if (ERRNO_IS_NOT_SUPPORTED(errno) || errno == EOVERFLOW) {
1578 static const struct itimerspec its32 = {
1579 .it_value.tv_sec = INT32_MAX,
1580 };
1581
1582 if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its32, NULL) >= 0)
1583 return TAKE_FD(fd);
1584 }
1585 #endif
1586
1587 return -errno;
1588 }
1589
1590 static const char* const timestamp_style_table[_TIMESTAMP_STYLE_MAX] = {
1591 [TIMESTAMP_PRETTY] = "pretty",
1592 [TIMESTAMP_US] = "us",
1593 [TIMESTAMP_UTC] = "utc",
1594 [TIMESTAMP_US_UTC] = "us+utc",
1595 };
1596
1597 /* Use the macro for enum → string to allow for aliases */
1598 _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(timestamp_style, TimestampStyle,);
1599
1600 /* For the string → enum mapping we use the generic implementation, but also support two aliases */
1601 TimestampStyle timestamp_style_from_string(const char *s) {
1602 TimestampStyle t;
1603
1604 t = (TimestampStyle) string_table_lookup(timestamp_style_table, ELEMENTSOF(timestamp_style_table), s);
1605 if (t >= 0)
1606 return t;
1607 if (streq_ptr(s, "µs"))
1608 return TIMESTAMP_US;
1609 if (streq_ptr(s, "µs+uts"))
1610 return TIMESTAMP_US_UTC;
1611 return t;
1612 }