Debian Codegraph @

src/basic/time-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <sys/timerfd.h>
5 #include <threads.h>
6 #include <unistd.h>
7
8 #include "alloc-util.h"
9 #include "env-util.h"
10 #include "errno-util.h"
11 #include "extract-word.h"
12 #include "hexdecoct.h" /* IWYU pragma: keep */
13 #include "fd-util.h"
14 #include "fileio.h"
15 #include "fs-util.h"
16 #include "io-util.h"
17 #include "log.h"
18 #include "parse-util.h"
19 #include "path-util.h"
20 #include "stat-util.h"
21 #include "stdio-util.h"
22 #include "string-table.h"
23 #include "string-util.h"
24 #include "strv.h"
25 #include "time-util.h"
26
27 static clockid_t map_clock_id(clockid_t c) {
28
29 /* Some more exotic archs (s390, ppc, …) lack the "ALARM" flavour of the clocks. Thus,
30 * clock_gettime() will fail for them. Since they are essentially the same as their non-ALARM
31 * pendants (their only difference is when timers are set on them), let's just map them
32 * accordingly. This way, we can get the correct time even on those archs. */
33
34 switch (c) {
35
36 case CLOCK_BOOTTIME_ALARM:
37 return CLOCK_BOOTTIME;
38
39 case CLOCK_REALTIME_ALARM:
40 return CLOCK_REALTIME;
41
42 default:
43 return c;
44 }
45 }
46
47 usec_t now(clockid_t clock_id) {
48 struct timespec ts;
49
50 assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
51
52 usec_t n = timespec_load(&ts);
53
54 /* We use both 0 and USEC_INFINITY as niche values. If the current time collides with either, things are
55 * really weird and really broken. Let's not allow this to go through, it would break too many of our
56 * assumptions in code. */
57 assert(n > 0);
58 assert(n < USEC_INFINITY);
59
60 return n;
61 }
62
63 nsec_t now_nsec(clockid_t clock_id) {
64 struct timespec ts;
65
66 assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
67
68 nsec_t n = timespec_load_nsec(&ts);
69
70 assert(n > 0);
71 assert(n < NSEC_INFINITY);
72
73 return n;
74 }
75
76 dual_timestamp* dual_timestamp_now(dual_timestamp *ts) {
77 assert(ts);
78
79 ts->realtime = now(CLOCK_REALTIME);
80 ts->monotonic = now(CLOCK_MONOTONIC);
81
82 return ts;
83 }
84
85 triple_timestamp* triple_timestamp_now(triple_timestamp *ts) {
86 assert(ts);
87
88 ts->realtime = now(CLOCK_REALTIME);
89 ts->monotonic = now(CLOCK_MONOTONIC);
90 ts->boottime = now(CLOCK_BOOTTIME);
91
92 return ts;
93 }
94
95 usec_t map_clock_usec_raw(usec_t from, usec_t from_base, usec_t to_base) {
96
97 /* Maps the time 'from' between two clocks, based on a common reference point where the first clock
98 * is at 'from_base' and the second clock at 'to_base'. Basically calculates:
99 *
100 * from - from_base + to_base
101 *
102 * But takes care of overflows/underflows and avoids signed operations. */
103
104 if (from >= from_base) { /* In the future */
105 usec_t delta = from - from_base;
106
107 if (to_base >= USEC_INFINITY - delta) /* overflow? */
108 return USEC_INFINITY;
109
110 return to_base + delta;
111
112 } else { /* In the past */
113 usec_t delta = from_base - from;
114
115 if (to_base <= delta) /* underflow? */
116 return 0;
117
118 return to_base - delta;
119 }
120 }
121
122 usec_t map_clock_usec(usec_t from, clockid_t from_clock, clockid_t to_clock) {
123
124 /* Try to avoid any inaccuracy needlessly added in case we convert from effectively the same clock
125 * onto itself */
126 if (map_clock_id(from_clock) == map_clock_id(to_clock))
127 return from;
128
129 /* Keep infinity as is */
130 if (from == USEC_INFINITY)
131 return from;
132
133 return map_clock_usec_raw(from, now(from_clock), now(to_clock));
134 }
135
136 dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
137 assert(ts);
138
139 if (!timestamp_is_set(u)) {
140 ts->realtime = ts->monotonic = u;
141 return ts;
142 }
143
144 ts->realtime = u;
145 ts->monotonic = map_clock_usec(u, CLOCK_REALTIME, CLOCK_MONOTONIC);
146 return ts;
147 }
148
149 triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u) {
150 usec_t nowr;
151
152 assert(ts);
153
154 if (!timestamp_is_set(u)) {
155 ts->realtime = ts->monotonic = ts->boottime = u;
156 return ts;
157 }
158
159 nowr = now(CLOCK_REALTIME);
160
161 ts->realtime = u;
162 ts->monotonic = map_clock_usec_raw(u, nowr, now(CLOCK_MONOTONIC));
163 ts->boottime = map_clock_usec_raw(u, nowr, now(CLOCK_BOOTTIME));
164
165 return ts;
166 }
167
168 triple_timestamp* triple_timestamp_from_boottime(triple_timestamp *ts, usec_t u) {
169 usec_t nowb;
170
171 assert(ts);
172
173 if (u == USEC_INFINITY) {
174 ts->realtime = ts->monotonic = ts->boottime = u;
175 return ts;
176 }
177
178 nowb = now(CLOCK_BOOTTIME);
179
180 ts->boottime = u;
181 ts->monotonic = map_clock_usec_raw(u, nowb, now(CLOCK_MONOTONIC));
182 ts->realtime = map_clock_usec_raw(u, nowb, now(CLOCK_REALTIME));
183
184 return ts;
185 }
186
187 dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
188 assert(ts);
189
190 if (u == USEC_INFINITY) {
191 ts->realtime = ts->monotonic = USEC_INFINITY;
192 return ts;
193 }
194
195 ts->monotonic = u;
196 ts->realtime = map_clock_usec(u, CLOCK_MONOTONIC, CLOCK_REALTIME);
197 return ts;
198 }
199
200 dual_timestamp* dual_timestamp_from_boottime(dual_timestamp *ts, usec_t u) {
201 usec_t nowm;
202
203 assert(ts);
204
205 if (u == USEC_INFINITY) {
206 ts->realtime = ts->monotonic = USEC_INFINITY;
207 return ts;
208 }
209
210 nowm = now(CLOCK_BOOTTIME);
211 ts->monotonic = map_clock_usec_raw(u, nowm, now(CLOCK_MONOTONIC));
212 ts->realtime = map_clock_usec_raw(u, nowm, now(CLOCK_REALTIME));
213 return ts;
214 }
215
216 usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock) {
217 assert(ts);
218
219 switch (clock) {
220
221 case CLOCK_REALTIME:
222 case CLOCK_REALTIME_ALARM:
223 return ts->realtime;
224
225 case CLOCK_MONOTONIC:
226 return ts->monotonic;
227
228 case CLOCK_BOOTTIME:
229 case CLOCK_BOOTTIME_ALARM:
230 return ts->boottime;
231
232 default:
233 return USEC_INFINITY;
234 }
235 }
236
237 usec_t timespec_load(const struct timespec *ts) {
238 assert(ts);
239
240 if (ts->tv_sec < 0 || ts->tv_nsec < 0)
241 return USEC_INFINITY;
242
243 if ((usec_t) ts->tv_sec > (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC)
244 return USEC_INFINITY;
245
246 return
247 (usec_t) ts->tv_sec * USEC_PER_SEC +
248 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
249 }
250
251 nsec_t timespec_load_nsec(const struct timespec *ts) {
252 assert(ts);
253
254 if (ts->tv_sec < 0 || ts->tv_nsec < 0)
255 return NSEC_INFINITY;
256
257 if ((nsec_t) ts->tv_sec >= (UINT64_MAX - ts->tv_nsec) / NSEC_PER_SEC)
258 return NSEC_INFINITY;
259
260 return (nsec_t) ts->tv_sec * NSEC_PER_SEC + (nsec_t) ts->tv_nsec;
261 }
262
263 struct timespec *timespec_store(struct timespec *ts, usec_t u) {
264 assert(ts);
265
266 if (u == USEC_INFINITY ||
267 u / USEC_PER_SEC >= TIME_T_MAX) {
268 ts->tv_sec = (time_t) -1;
269 ts->tv_nsec = -1L;
270 return ts;
271 }
272
273 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
274 ts->tv_nsec = (long) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
275
276 return ts;
277 }
278
279 struct timespec *timespec_store_nsec(struct timespec *ts, nsec_t n) {
280 assert(ts);
281
282 if (n == NSEC_INFINITY ||
283 n / NSEC_PER_SEC >= TIME_T_MAX) {
284 ts->tv_sec = (time_t) -1;
285 ts->tv_nsec = -1L;
286 return ts;
287 }
288
289 ts->tv_sec = (time_t) (n / NSEC_PER_SEC);
290 ts->tv_nsec = (long) (n % NSEC_PER_SEC);
291
292 return ts;
293 }
294
295 usec_t timeval_load(const struct timeval *tv) {
296 assert(tv);
297
298 if (tv->tv_sec < 0 || tv->tv_usec < 0)
299 return USEC_INFINITY;
300
301 if ((usec_t) tv->tv_sec > (UINT64_MAX - tv->tv_usec) / USEC_PER_SEC)
302 return USEC_INFINITY;
303
304 return
305 (usec_t) tv->tv_sec * USEC_PER_SEC +
306 (usec_t) tv->tv_usec;
307 }
308
309 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
310 assert(tv);
311
312 if (u == USEC_INFINITY ||
313 u / USEC_PER_SEC > TIME_T_MAX) {
314 tv->tv_sec = (time_t) -1;
315 tv->tv_usec = (suseconds_t) -1;
316 } else {
317 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
318 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
319 }
320
321 return tv;
322 }
323
324 char* format_timestamp_style(
325 char *buf,
326 size_t l,
327 usec_t t,
328 TimestampStyle style) {
329
330 /* The weekdays in non-localized (English) form. We use this instead of the localized form, so that
331 * our generated timestamps may be parsed with parse_timestamp(), and always read the same. */
332 static const char * const weekdays[] = {
333 [0] = "Sun",
334 [1] = "Mon",
335 [2] = "Tue",
336 [3] = "Wed",
337 [4] = "Thu",
338 [5] = "Fri",
339 [6] = "Sat",
340 };
341
342 struct tm tm;
343 bool utc, us;
344 size_t n;
345
346 assert(buf);
347 assert(style >= 0);
348 assert(style < _TIMESTAMP_STYLE_MAX);
349
350 if (!timestamp_is_set(t))
351 return NULL; /* Timestamp is unset */
352
353 if (style == TIMESTAMP_UNIX) {
354 if (l < (size_t) (1 + 1 + 1))
355 return NULL; /* not enough space for even the shortest of forms */
356
357 return snprintf_ok(buf, l, "@" USEC_FMT, t / USEC_PER_SEC); /* round down μs → s */
358 }
359
360 utc = IN_SET(style, TIMESTAMP_UTC, TIMESTAMP_US_UTC, TIMESTAMP_DATE);
361 us = IN_SET(style, TIMESTAMP_US, TIMESTAMP_US_UTC);
362
363 if (l < (size_t) (3 + /* week day */
364 1 + 10 + /* space and date */
365 style == TIMESTAMP_DATE ? 0 :
366 (1 + 8 + /* space and time */
367 (us ? 1 + 6 : 0) + /* "." and microsecond part */
368 1 + (utc ? 3 : 1)) + /* space and shortest possible zone */
369 1))
370 return NULL; /* Not enough space even for the shortest form. */
371
372 /* Let's not format times with years > 9999 */
373 if (t > USEC_TIMESTAMP_FORMATTABLE_MAX) {
374 static const char* const xxx[_TIMESTAMP_STYLE_MAX] = {
375 [TIMESTAMP_PRETTY] = "--- XXXX-XX-XX XX:XX:XX",
376 [TIMESTAMP_US] = "--- XXXX-XX-XX XX:XX:XX.XXXXXX",
377 [TIMESTAMP_UTC] = "--- XXXX-XX-XX XX:XX:XX UTC",
378 [TIMESTAMP_US_UTC] = "--- XXXX-XX-XX XX:XX:XX.XXXXXX UTC",
379 [TIMESTAMP_DATE] = "--- XXXX-XX-XX",
380 };
381
382 assert(l >= strlen(xxx[style]) + 1);
383 return strcpy(buf, xxx[style]);
384 }
385
386 if (localtime_or_gmtime_usec(t, utc, &tm) < 0)
387 return NULL;
388
389 /* Start with the week day */
390 assert((size_t) tm.tm_wday < ELEMENTSOF(weekdays));
391 memcpy(buf, weekdays[tm.tm_wday], 4);
392
393 if (style == TIMESTAMP_DATE) {
394 /* Special format string if only date should be shown. */
395 if (strftime(buf + 3, l - 3, " %Y-%m-%d", &tm) <= 0)
396 return NULL; /* Doesn't fit */
397
398 return buf;
399 }
400
401 /* Add the main components */
402 if (strftime(buf + 3, l - 3, " %Y-%m-%d %H:%M:%S", &tm) <= 0)
403 return NULL; /* Doesn't fit */
404
405 /* Append the microseconds part, if that's requested */
406 if (us) {
407 n = strlen(buf);
408 if (n + 8 > l)
409 return NULL; /* Microseconds part doesn't fit. */
410
411 sprintf(buf + n, ".%06"PRI_USEC, t % USEC_PER_SEC);
412 }
413
414 /* Append the timezone */
415 n = strlen(buf);
416 if (utc) {
417 /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r()
418 * normally uses the obsolete "GMT" instead. */
419 if (n + 5 > l)
420 return NULL; /* "UTC" doesn't fit. */
421
422 strcpy(buf + n, " UTC");
423
424 } else if (!isempty(tm.tm_zone)) {
425 size_t tn;
426
427 /* An explicit timezone is specified, let's use it, if it fits */
428 tn = strlen(tm.tm_zone);
429 if (n + 1 + tn + 1 > l) {
430 /* The full time zone does not fit in. Yuck. */
431
432 if (n + 1 + _POSIX_TZNAME_MAX + 1 > l)
433 return NULL; /* Not even enough space for the POSIX minimum (of 6)? In that
434 * case, complain that it doesn't fit. */
435
436 /* So the time zone doesn't fit in fully, but the caller passed enough space for the
437 * POSIX minimum time zone length. In this case suppress the timezone entirely, in
438 * order not to dump an overly long, hard to read string on the user. This should be
439 * safe, because the user will assume the local timezone anyway if none is shown. And
440 * so does parse_timestamp(). */
441 } else {
442 buf[n++] = ' ';
443 strcpy(buf + n, tm.tm_zone);
444 }
445 }
446
447 return buf;
448 }
449
450 char* format_timestamp_relative_full(char *buf, size_t l, usec_t t, clockid_t clock, bool implicit_left) {
451 const char *s;
452 usec_t n, d;
453
454 assert(buf);
455
456 if (!timestamp_is_set(t))
457 return NULL;
458
459 n = now(clock);
460 if (n > t) {
461 d = n - t;
462 s = " ago";
463 } else {
464 d = t - n;
465 s = implicit_left ? "" : " left";
466 }
467
468 if (d >= USEC_PER_YEAR) {
469 usec_t years = d / USEC_PER_YEAR;
470 usec_t months = (d % USEC_PER_YEAR) / USEC_PER_MONTH;
471
472 (void) snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s%s",
473 years,
474 years == 1 ? "year" : "years",
475 months,
476 months == 1 ? "month" : "months",
477 s);
478 } else if (d >= USEC_PER_MONTH) {
479 usec_t months = d / USEC_PER_MONTH;
480 usec_t days = (d % USEC_PER_MONTH) / USEC_PER_DAY;
481
482 (void) snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s%s",
483 months,
484 months == 1 ? "month" : "months",
485 days,
486 days == 1 ? "day" : "days",
487 s);
488 } else if (d >= USEC_PER_WEEK) {
489 usec_t weeks = d / USEC_PER_WEEK;
490 usec_t days = (d % USEC_PER_WEEK) / USEC_PER_DAY;
491
492 (void) snprintf(buf, l, USEC_FMT " %s " USEC_FMT " %s%s",
493 weeks,
494 weeks == 1 ? "week" : "weeks",
495 days,
496 days == 1 ? "day" : "days",
497 s);
498 } else if (d >= 2*USEC_PER_DAY)
499 (void) snprintf(buf, l, USEC_FMT " days%s", d / USEC_PER_DAY,s);
500 else if (d >= 25*USEC_PER_HOUR)
501 (void) snprintf(buf, l, "1 day " USEC_FMT "h%s",
502 (d - USEC_PER_DAY) / USEC_PER_HOUR, s);
503 else if (d >= 6*USEC_PER_HOUR)
504 (void) snprintf(buf, l, USEC_FMT "h%s",
505 d / USEC_PER_HOUR, s);
506 else if (d >= USEC_PER_HOUR)
507 (void) snprintf(buf, l, USEC_FMT "h " USEC_FMT "min%s",
508 d / USEC_PER_HOUR,
509 (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s);
510 else if (d >= 5*USEC_PER_MINUTE)
511 (void) snprintf(buf, l, USEC_FMT "min%s",
512 d / USEC_PER_MINUTE, s);
513 else if (d >= USEC_PER_MINUTE)
514 (void) snprintf(buf, l, USEC_FMT "min " USEC_FMT "s%s",
515 d / USEC_PER_MINUTE,
516 (d % USEC_PER_MINUTE) / USEC_PER_SEC, s);
517 else if (d >= USEC_PER_SEC)
518 (void) snprintf(buf, l, USEC_FMT "s%s",
519 d / USEC_PER_SEC, s);
520 else if (d >= USEC_PER_MSEC)
521 (void) snprintf(buf, l, USEC_FMT "ms%s",
522 d / USEC_PER_MSEC, s);
523 else if (d > 0)
524 (void) snprintf(buf, l, USEC_FMT"us%s",
525 d, s);
526 else
527 (void) snprintf(buf, l, "now");
528
529 buf[l-1] = 0;
530 return buf;
531 }
532
533 char* format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
534 static const struct {
535 const char *suffix;
536 usec_t usec;
537 } table[] = {
538 { "y", USEC_PER_YEAR },
539 { "month", USEC_PER_MONTH },
540 { "w", USEC_PER_WEEK },
541 { "d", USEC_PER_DAY },
542 { "h", USEC_PER_HOUR },
543 { "min", USEC_PER_MINUTE },
544 { "s", USEC_PER_SEC },
545 { "ms", USEC_PER_MSEC },
546 { "us", 1 },
547 };
548
549 char *p = ASSERT_PTR(buf);
550 bool something = false;
551
552 assert(l > 0);
553
554 if (t == USEC_INFINITY) {
555 strncpy(p, "infinity", l-1);
556 p[l-1] = 0;
557 return p;
558 }
559
560 if (t <= 0) {
561 strncpy(p, "0", l-1);
562 p[l-1] = 0;
563 return p;
564 }
565
566 /* The result of this function can be parsed with parse_sec */
567
568 FOREACH_ELEMENT(i, table) {
569 int k = 0;
570 size_t n;
571 bool done = false;
572 usec_t a, b;
573
574 if (t <= 0)
575 break;
576
577 if (t < accuracy && something)
578 break;
579
580 if (t < i->usec)
581 continue;
582
583 if (l <= 1)
584 break;
585
586 a = t / i->usec;
587 b = t % i->usec;
588
589 /* Let's see if we should shows this in dot notation */
590 if (t < USEC_PER_MINUTE && b > 0) {
591 signed char j = 0;
592
593 for (usec_t cc = i->usec; cc > 1; cc /= 10)
594 j++;
595
596 for (usec_t cc = accuracy; cc > 1; cc /= 10) {
597 b /= 10;
598 j--;
599 }
600
601 if (j > 0) {
602 k = snprintf(p, l,
603 "%s"USEC_FMT".%0*"PRI_USEC"%s",
604 p > buf ? " " : "",
605 a,
606 j,
607 b,
608 i->suffix);
609
610 t = 0;
611 done = true;
612 }
613 }
614
615 /* No? Then let's show it normally */
616 if (!done) {
617 k = snprintf(p, l,
618 "%s"USEC_FMT"%s",
619 p > buf ? " " : "",
620 a,
621 i->suffix);
622
623 t = b;
624 }
625
626 n = MIN((size_t) k, l-1);
627
628 l -= n;
629 p += n;
630
631 something = true;
632 }
633
634 *p = 0;
635
636 return buf;
637 }
638
639 const char* get_tzname(bool dst) {
640 /* musl leaves the DST timezone name unset if there is no DST, map this back to no DST */
641 if (dst && isempty(tzname[1]))
642 dst = false;
643
644 return empty_to_null(tzname[dst]);
645 }
646
647 int parse_gmtoff(const char *t, long *ret) {
648 assert(t);
649
650 struct tm tm;
651 const char *k = strptime(t, "%z", &tm);
652 if (k && *k == '\0') {
653 /* Success! */
654 if (ret)
655 *ret = tm.tm_gmtoff;
656 return 0;
657 }
658
659 #ifdef __GLIBC__
660 return -EINVAL;
661 #else
662 int r;
663
664 /* musl v1.2.5 does not support %z specifier in strptime(). Since
665 * https://github.com/kraj/musl/commit/fced99e93daeefb0192fd16304f978d4401d1d77
666 * %z is supported, but it only supports strict RFC-822/ISO 8601 format, that is, 4 digits with sign
667 * (e.g. +0900 or -1400), but does not support extended format: 2 digits or colon separated 4 digits
668 * (e.g. +09 or -14:00). Let's add fallback logic to make it support the extended timezone spec. */
669
670 bool positive;
671 switch (*t) {
672 case '+':
673 positive = true;
674 break;
675 case '-':
676 positive = false;
677 break;
678 default:
679 return -EINVAL;
680 }
681
682 t++;
683 r = undecchar(*t);
684 if (r < 0)
685 return r;
686
687 usec_t u = r * 10 * USEC_PER_HOUR;
688
689 t++;
690 r = undecchar(*t);
691 if (r < 0)
692 return r;
693 u += r * USEC_PER_HOUR;
694
695 t++;
696 if (*t == '\0') /* 2 digits case */
697 goto finalize;
698
699 if (*t == ':') /* skip colon */
700 t++;
701
702 r = undecchar(*t);
703 if (r < 0)
704 return r;
705 if (r >= 6) /* refuse minutes equal to or larger than 60 */
706 return -EINVAL;
707
708 u += r * 10 * USEC_PER_MINUTE;
709
710 t++;
711 r = undecchar(*t);
712 if (r < 0)
713 return r;
714
715 u += r * USEC_PER_MINUTE;
716
717 t++;
718 if (*t != '\0')
719 return -EINVAL;
720
721 finalize:
722 if (u > USEC_PER_DAY) /* refuse larger than one day */
723 return -EINVAL;
724
725 if (ret) {
726 long gmtoff = u / USEC_PER_SEC;
727 *ret = positive ? gmtoff : -gmtoff;
728 }
729
730 return 0;
731 #endif
732 }
733
734 static int parse_timestamp_impl(
735 const char *t,
736 size_t max_len,
737 bool utc,
738 int isdst,
739 long gmtoff,
740 usec_t *ret) {
741
742 static const struct {
743 const char *name;
744 const int nr;
745 } day_nr[] = {
746 { "Sunday", 0 },
747 { "Sun", 0 },
748 { "Monday", 1 },
749 { "Mon", 1 },
750 { "Tuesday", 2 },
751 { "Tue", 2 },
752 { "Wednesday", 3 },
753 { "Wed", 3 },
754 { "Thursday", 4 },
755 { "Thu", 4 },
756 { "Friday", 5 },
757 { "Fri", 5 },
758 { "Saturday", 6 },
759 { "Sat", 6 },
760 };
761
762 _cleanup_free_ char *t_alloc = NULL;
763 usec_t usec, plus = 0, minus = 0;
764 bool with_tz = false;
765 int r, weekday = -1;
766 unsigned fractional = 0;
767 const char *k;
768 struct tm tm, copy;
769
770 /* Allowed syntaxes:
771 *
772 * 2012-09-22 16:34:22.1[2[3[4[5[6]]]]]
773 * 2012-09-22 16:34:22 (µsec will be set to 0)
774 * 2012-09-22 16:34 (seconds will be set to 0)
775 * 2012-09-22T16:34:22.1[2[3[4[5[6]]]]]
776 * 2012-09-22T16:34:22 (µsec will be set to 0)
777 * 2012-09-22T16:34 (seconds will be set to 0)
778 * 2012-09-22 (time will be set to 00:00:00)
779 * 16:34:22 (date will be set to today)
780 * 16:34 (date will be set to today, seconds to 0)
781 * now
782 * yesterday (time is set to 00:00:00)
783 * today (time is set to 00:00:00)
784 * tomorrow (time is set to 00:00:00)
785 * +5min
786 * -5days
787 * @2147483647 (seconds since epoch)
788 *
789 * Note, on DST change, 00:00:00 may not exist and in that case the time part may be shifted.
790 * E.g. "Sun 2023-03-13 America/Havana" is parsed as "Sun 2023-03-13 01:00:00 CDT".
791 *
792 * A simplified strptime-spelled RFC3339 ABNF looks like
793 * "%Y-%m-%d" "T" "%H" ":" "%M" ":" "%S" [".%N"] ("Z" / (("+" / "-") "%H:%M"))
794 * We additionally allow no seconds and inherited timezone
795 * for symmetry with our other syntaxes and improved interactive usability:
796 * "%Y-%m-%d" "T" "%H" ":" "%M" ":" ["%S" [".%N"]] ["Z" / (("+" / "-") "%H:%M")]
797 * RFC3339 defines time-secfrac to as "." 1*DIGIT, but we limit to 6 digits,
798 * since we're limited to 1µs resolution.
799 * We also accept "Sat 2012-09-22T16:34:22", RFC3339 warns against it.
800 */
801
802 assert(t);
803
804 if (max_len != SIZE_MAX) {
805 /* If the input string contains timezone, then cut it here. */
806
807 if (max_len == 0) /* Can't be the only field */
808 return -EINVAL;
809
810 t_alloc = strndup(t, max_len);
811 if (!t_alloc)
812 return -ENOMEM;
813
814 t = t_alloc;
815 with_tz = true;
816 }
817
818 if (utc) {
819 /* glibc accepts gmtoff more than 24 hours, but we refuse it. */
820 if ((usec_t) labs(gmtoff) * USEC_PER_SEC > USEC_PER_DAY)
821 return -EINVAL;
822 } else {
823 if (gmtoff != 0)
824 return -EINVAL;
825 }
826
827 if (t[0] == '@' && !with_tz)
828 return parse_sec(t + 1, ret);
829
830 usec = now(CLOCK_REALTIME);
831
832 if (!with_tz) {
833 if (streq(t, "now"))
834 goto finish;
835
836 if (t[0] == '+') {
837 r = parse_sec(t+1, &plus);
838 if (r < 0)
839 return r;
840
841 goto finish;
842 }
843
844 if (t[0] == '-') {
845 r = parse_sec(t+1, &minus);
846 if (r < 0)
847 return r;
848
849 goto finish;
850 }
851
852 if ((k = endswith(t, " ago"))) {
853 _cleanup_free_ char *buf = NULL;
854
855 buf = strndup(t, k - t);
856 if (!buf)
857 return -ENOMEM;
858
859 r = parse_sec(buf, &minus);
860 if (r < 0)
861 return r;
862
863 goto finish;
864 }
865
866 if ((k = endswith(t, " left"))) {
867 _cleanup_free_ char *buf = NULL;
868
869 buf = strndup(t, k - t);
870 if (!buf)
871 return -ENOMEM;
872
873 r = parse_sec(buf, &plus);
874 if (r < 0)
875 return r;
876
877 goto finish;
878 }
879 }
880
881 r = localtime_or_gmtime_usec(usec, utc, &tm);
882 if (r < 0)
883 return r;
884
885 tm.tm_isdst = isdst;
886
887 if (streq(t, "today")) {
888 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
889 goto from_tm;
890
891 } else if (streq(t, "yesterday")) {
892 tm.tm_mday--;
893 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
894 goto from_tm;
895
896 } else if (streq(t, "tomorrow")) {
897 tm.tm_mday++;
898 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
899 goto from_tm;
900 }
901
902 FOREACH_ELEMENT(day, day_nr) {
903 k = startswith_no_case(t, day->name);
904 if (!k || *k != ' ')
905 continue;
906
907 #ifdef __GLIBC__
908 /* musl does not set tm_wday field and set 0 unless it is explicitly requested by %w or so.
909 * In the below, let's only check tm_wday field only when built with glibc. */
910 weekday = day->nr;
911 #endif
912 t = k + 1;
913 break;
914 }
915
916 copy = tm;
917 k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
918 if (k) {
919 if (*k == '.')
920 goto parse_usec;
921 else if (*k == 0)
922 goto from_tm;
923 }
924
925 /* Our "canonical" RFC3339 syntax variant */
926 tm = copy;
927 k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm);
928 if (k) {
929 if (*k == '.')
930 goto parse_usec;
931 else if (*k == 0)
932 goto from_tm;
933 }
934
935 /* RFC3339 syntax */
936 tm = copy;
937 k = strptime(t, "%Y-%m-%dT%H:%M:%S", &tm);
938 if (k) {
939 if (*k == '.')
940 goto parse_usec;
941 else if (*k == 0)
942 goto from_tm;
943 }
944
945 /* Support OUTPUT_SHORT and OUTPUT_SHORT_PRECISE formats */
946 tm = copy;
947 k = strptime(t, "%b %d %H:%M:%S", &tm);
948 if (k) {
949 if (*k == '.')
950 goto parse_usec;
951 else if (*k == 0)
952 goto from_tm;
953 }
954
955 tm = copy;
956 k = strptime(t, "%y-%m-%d %H:%M", &tm);
957 if (k && *k == 0) {
958 tm.tm_sec = 0;
959 goto from_tm;
960 }
961
962 /* Our "canonical" RFC3339 syntax variant without seconds */
963 tm = copy;
964 k = strptime(t, "%Y-%m-%d %H:%M", &tm);
965 if (k && *k == 0) {
966 tm.tm_sec = 0;
967 goto from_tm;
968 }
969
970 /* RFC3339 syntax without seconds */
971 tm = copy;
972 k = strptime(t, "%Y-%m-%dT%H:%M", &tm);
973 if (k && *k == 0) {
974 tm.tm_sec = 0;
975 goto from_tm;
976 }
977
978 tm = copy;
979 k = strptime(t, "%y-%m-%d", &tm);
980 if (k && *k == 0) {
981 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
982 goto from_tm;
983 }
984
985 tm = copy;
986 k = strptime(t, "%Y-%m-%d", &tm);
987 if (k && *k == 0) {
988 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
989 goto from_tm;
990 }
991
992 tm = copy;
993 k = strptime(t, "%H:%M:%S", &tm);
994 if (k) {
995 if (*k == '.')
996 goto parse_usec;
997 else if (*k == 0)
998 goto from_tm;
999 }
1000
1001 tm = copy;
1002 k = strptime(t, "%H:%M", &tm);
1003 if (k && *k == 0) {
1004 tm.tm_sec = 0;
1005 goto from_tm;
1006 }
1007
1008 return -EINVAL;
1009
1010 parse_usec:
1011 k++;
1012 r = parse_fractional_part_u(&k, 6, &fractional);
1013 if (r < 0)
1014 return -EINVAL;
1015 if (*k != '\0')
1016 return -EINVAL;
1017
1018 from_tm:
1019 assert(plus == 0);
1020 assert(minus == 0);
1021
1022 if (weekday >= 0 && tm.tm_wday != weekday)
1023 return -EINVAL;
1024
1025 if (gmtoff < 0) {
1026 plus = -gmtoff * USEC_PER_SEC;
1027
1028 /* If gmtoff is negative, the string may be too old to be parsed as UTC.
1029 * E.g. 1969-12-31 23:00:00 -06 == 1970-01-01 05:00:00 UTC
1030 * We assumed that gmtoff is in the range of -24:00…+24:00, hence the only date we need to
1031 * handle here is 1969-12-31. So, let's shift the date with one day, then subtract the shift
1032 * later. */
1033 if (tm.tm_year == 69 && tm.tm_mon == 11 && tm.tm_mday == 31) {
1034 /* Thu 1970-01-01-00:00:00 */
1035 tm.tm_year = 70;
1036 tm.tm_mon = 0;
1037 tm.tm_mday = 1;
1038 tm.tm_wday = 4;
1039 tm.tm_yday = 0;
1040 minus = USEC_PER_DAY;
1041 }
1042 } else
1043 minus = gmtoff * USEC_PER_SEC;
1044
1045 r = mktime_or_timegm_usec(&tm, utc, &usec);
1046 if (r < 0)
1047 return r;
1048
1049 usec = usec_add(usec, fractional);
1050
1051 finish:
1052 usec = usec_add(usec, plus);
1053
1054 if (usec < minus)
1055 return -EINVAL;
1056
1057 usec = usec_sub_unsigned(usec, minus);
1058
1059 if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
1060 return -EINVAL;
1061
1062 if (ret)
1063 *ret = usec;
1064 return 0;
1065 }
1066
1067 int parse_timestamp(const char *t, usec_t *ret) {
1068 long gmtoff;
1069 int r;
1070
1071 assert(t);
1072
1073 size_t t_len = strlen(t);
1074 if (t_len > 2 && t[t_len - 1] == 'Z') {
1075 /* Try to parse as RFC3339-style welded UTC: "1985-04-12T23:20:50.52Z" */
1076 r = parse_timestamp_impl(t, t_len - 1, /* utc= */ true, /* isdst= */ -1, /* gmtoff= */ 0, ret);
1077 if (r >= 0)
1078 return r;
1079 }
1080
1081 /* RFC3339-style welded offset: "1990-12-31T15:59:60-08:00" */
1082 if (t_len > 7 && IN_SET(t[t_len - 6], '+', '-') && t[t_len - 7] != ' ' && parse_gmtoff(&t[t_len - 6], &gmtoff) >= 0)
1083 return parse_timestamp_impl(t, t_len - 6, /* utc= */ true, /* isdst= */ -1, gmtoff, ret);
1084
1085 const char *tz = strrchr(t, ' ');
1086 if (!tz)
1087 return parse_timestamp_impl(t, /* max_len= */ SIZE_MAX, /* utc= */ false, /* isdst= */ -1, /* gmtoff= */ 0, ret);
1088
1089 size_t max_len = tz - t;
1090 tz++;
1091
1092 /* Shortcut, parse the string as UTC. */
1093 if (streq(tz, "UTC"))
1094 return parse_timestamp_impl(t, max_len, /* utc= */ true, /* isdst= */ -1, /* gmtoff= */ 0, ret);
1095
1096 /* If the timezone is compatible with RFC-822/ISO 8601 (e.g. +06, or -03:00) then parse the string as
1097 * UTC and shift the result. Note, this must be earlier than the timezone check with tzname[], as
1098 * tzname[] may be in the same format. */
1099 if (parse_gmtoff(tz, &gmtoff) >= 0)
1100 return parse_timestamp_impl(t, max_len, /* utc= */ true, /* isdst= */ -1, gmtoff, ret);
1101
1102 /* Check if the last word matches tzname[] of the local timezone. Note, this must be done earlier
1103 * than the check by timezone_is_valid() below, as some short timezone specifications have their own
1104 * timezone files (e.g. WET has its own timezone file, but JST does not), but using such files does
1105 * not follow the timezone change in the current area. */
1106 tzset();
1107 for (int j = 0; j <= 1; j++) {
1108 if (!streq_ptr(tz, get_tzname(j)))
1109 continue;
1110
1111 /* The specified timezone matches tzname[] of the local timezone. */
1112 return parse_timestamp_impl(t, max_len, /* utc= */ false, /* isdst= */ j, /* gmtoff= */ 0, ret);
1113 }
1114
1115 /* If the last word is a valid timezone file (e.g. Asia/Tokyo), then save the current timezone, apply
1116 * the specified timezone, and parse the remaining string as a local time. */
1117 if (timezone_is_valid(tz, LOG_DEBUG)) {
1118 SAVE_TIMEZONE;
1119
1120 if (setenv("TZ", tz, /* overwrite= */ true) < 0)
1121 return negative_errno();
1122
1123 return parse_timestamp_impl(t, max_len, /* utc= */ false, /* isdst= */ -1, /* gmtoff= */ 0, ret);
1124 }
1125
1126 /* Otherwise, assume that the last word is a part of the time and try to parse the whole string as a
1127 * local time. */
1128 return parse_timestamp_impl(t, SIZE_MAX, /* utc= */ false, /* isdst= */ -1, /* gmtoff= */ 0, ret);
1129 }
1130
1131 static const char* extract_multiplier(const char *p, usec_t *ret) {
1132 static const struct {
1133 const char *suffix;
1134 usec_t usec;
1135 } table[] = {
1136 { "seconds", USEC_PER_SEC },
1137 { "second", USEC_PER_SEC },
1138 { "sec", USEC_PER_SEC },
1139 { "s", USEC_PER_SEC },
1140 { "minutes", USEC_PER_MINUTE },
1141 { "minute", USEC_PER_MINUTE },
1142 { "min", USEC_PER_MINUTE },
1143 { "months", USEC_PER_MONTH },
1144 { "month", USEC_PER_MONTH },
1145 { "M", USEC_PER_MONTH },
1146 { "msec", USEC_PER_MSEC },
1147 { "ms", USEC_PER_MSEC },
1148 { "m", USEC_PER_MINUTE },
1149 { "hours", USEC_PER_HOUR },
1150 { "hour", USEC_PER_HOUR },
1151 { "hr", USEC_PER_HOUR },
1152 { "h", USEC_PER_HOUR },
1153 { "days", USEC_PER_DAY },
1154 { "day", USEC_PER_DAY },
1155 { "d", USEC_PER_DAY },
1156 { "weeks", USEC_PER_WEEK },
1157 { "week", USEC_PER_WEEK },
1158 { "w", USEC_PER_WEEK },
1159 { "years", USEC_PER_YEAR },
1160 { "year", USEC_PER_YEAR },
1161 { "y", USEC_PER_YEAR },
1162 { "usec", 1ULL },
1163 { "us", 1ULL },
1164 { "μs", 1ULL }, /* U+03bc (aka GREEK SMALL LETTER MU) */
1165 { "µs", 1ULL }, /* U+b5 (aka MICRO SIGN) */
1166 };
1167
1168 assert(p);
1169 assert(ret);
1170
1171 FOREACH_ELEMENT(i, table) {
1172 const char *e = startswith(p, i->suffix);
1173 if (e) {
1174 *ret = i->usec;
1175 return e;
1176 }
1177 }
1178
1179 return p;
1180 }
1181
1182 int parse_time(const char *t, usec_t *ret, usec_t default_unit) {
1183 const char *p, *s;
1184
1185 assert(t);
1186 assert(default_unit > 0);
1187
1188 p = skip_leading_chars(t, /* bad= */ NULL);
1189 s = startswith(p, "infinity");
1190 if (s) {
1191 if (!in_charset(s, WHITESPACE))
1192 return -EINVAL;
1193
1194 if (ret)
1195 *ret = USEC_INFINITY;
1196 return 0;
1197 }
1198
1199 usec_t usec = 0;
1200
1201 for (bool something = false;;) {
1202 usec_t multiplier = default_unit, k;
1203 long long l;
1204 char *e;
1205
1206 p = skip_leading_chars(p, /* bad= */ NULL);
1207 if (*p == 0) {
1208 if (!something)
1209 return -EINVAL;
1210
1211 break;
1212 }
1213
1214 if (*p == '-') /* Don't allow "-0" */
1215 return -ERANGE;
1216
1217 errno = 0;
1218 l = strtoll(p, &e, 10);
1219 if (errno > 0)
1220 return -errno;
1221 if (l < 0)
1222 return -ERANGE;
1223
1224 if (*e == '.') {
1225 p = e + 1;
1226 p += strspn(p, DIGITS);
1227 } else if (e == p)
1228 return -EINVAL;
1229 else
1230 p = e;
1231
1232 s = extract_multiplier(p + strspn(p, WHITESPACE), &multiplier);
1233 if (s == p && *s != '\0')
1234 /* Don't allow '12.34.56', but accept '12.34 .56' or '12.34s.56' */
1235 return -EINVAL;
1236
1237 p = s;
1238
1239 if ((usec_t) l >= USEC_INFINITY / multiplier)
1240 return -ERANGE;
1241
1242 k = (usec_t) l * multiplier;
1243 if (k >= USEC_INFINITY - usec)
1244 return -ERANGE;
1245
1246 usec += k;
1247
1248 something = true;
1249
1250 if (*e == '.') {
1251 usec_t m = multiplier / 10;
1252 const char *b;
1253
1254 for (b = e + 1; *b >= '0' && *b <= '9'; b++, m /= 10) {
1255 k = (usec_t) (*b - '0') * m;
1256 if (k >= USEC_INFINITY - usec)
1257 return -ERANGE;
1258
1259 usec += k;
1260 }
1261
1262 /* Don't allow "0.-0", "3.+1", "3. 1", "3.sec" or "3.hoge" */
1263 if (b == e + 1)
1264 return -EINVAL;
1265 }
1266 }
1267
1268 if (ret)
1269 *ret = usec;
1270 return 0;
1271 }
1272
1273 int parse_sec(const char *t, usec_t *ret) {
1274 return parse_time(t, ret, USEC_PER_SEC);
1275 }
1276
1277 int parse_sec_fix_0(const char *t, usec_t *ret) {
1278 usec_t k;
1279 int r;
1280
1281 assert(t);
1282 assert(ret);
1283
1284 r = parse_sec(t, &k);
1285 if (r < 0)
1286 return r;
1287
1288 *ret = k == 0 ? USEC_INFINITY : k;
1289 return r;
1290 }
1291
1292 int parse_sec_def_infinity(const char *t, usec_t *ret) {
1293 assert(t);
1294 assert(ret);
1295
1296 t += strspn(t, WHITESPACE);
1297 if (isempty(t)) {
1298 *ret = USEC_INFINITY;
1299 return 0;
1300 }
1301 return parse_sec(t, ret);
1302 }
1303
1304 static const char* extract_nsec_multiplier(const char *p, nsec_t *ret) {
1305 static const struct {
1306 const char *suffix;
1307 nsec_t nsec;
1308 } table[] = {
1309 { "seconds", NSEC_PER_SEC },
1310 { "second", NSEC_PER_SEC },
1311 { "sec", NSEC_PER_SEC },
1312 { "s", NSEC_PER_SEC },
1313 { "minutes", NSEC_PER_MINUTE },
1314 { "minute", NSEC_PER_MINUTE },
1315 { "min", NSEC_PER_MINUTE },
1316 { "months", NSEC_PER_MONTH },
1317 { "month", NSEC_PER_MONTH },
1318 { "M", NSEC_PER_MONTH },
1319 { "msec", NSEC_PER_MSEC },
1320 { "ms", NSEC_PER_MSEC },
1321 { "m", NSEC_PER_MINUTE },
1322 { "hours", NSEC_PER_HOUR },
1323 { "hour", NSEC_PER_HOUR },
1324 { "hr", NSEC_PER_HOUR },
1325 { "h", NSEC_PER_HOUR },
1326 { "days", NSEC_PER_DAY },
1327 { "day", NSEC_PER_DAY },
1328 { "d", NSEC_PER_DAY },
1329 { "weeks", NSEC_PER_WEEK },
1330 { "week", NSEC_PER_WEEK },
1331 { "w", NSEC_PER_WEEK },
1332 { "years", NSEC_PER_YEAR },
1333 { "year", NSEC_PER_YEAR },
1334 { "y", NSEC_PER_YEAR },
1335 { "usec", NSEC_PER_USEC },
1336 { "us", NSEC_PER_USEC },
1337 { "μs", NSEC_PER_USEC }, /* U+03bc (aka GREEK LETTER MU) */
1338 { "µs", NSEC_PER_USEC }, /* U+b5 (aka MICRO SIGN) */
1339 { "nsec", 1ULL },
1340 { "ns", 1ULL },
1341 { "", 1ULL }, /* default is nsec */
1342 };
1343
1344 assert(p);
1345 assert(ret);
1346
1347 FOREACH_ELEMENT(i, table) {
1348 const char *e = startswith(p, i->suffix);
1349 if (e) {
1350 *ret = i->nsec;
1351 return e;
1352 }
1353 }
1354
1355 return p;
1356 }
1357
1358 int parse_nsec(const char *t, nsec_t *ret) {
1359 const char *p, *s;
1360 nsec_t nsec = 0;
1361 bool something = false;
1362
1363 assert(t);
1364 assert(ret);
1365
1366 p = t;
1367
1368 p += strspn(p, WHITESPACE);
1369 s = startswith(p, "infinity");
1370 if (s) {
1371 s += strspn(s, WHITESPACE);
1372 if (*s != 0)
1373 return -EINVAL;
1374
1375 *ret = NSEC_INFINITY;
1376 return 0;
1377 }
1378
1379 for (;;) {
1380 nsec_t multiplier = 1, k;
1381 long long l;
1382 char *e;
1383
1384 p += strspn(p, WHITESPACE);
1385
1386 if (*p == 0) {
1387 if (!something)
1388 return -EINVAL;
1389
1390 break;
1391 }
1392
1393 if (*p == '-') /* Don't allow "-0" */
1394 return -ERANGE;
1395
1396 errno = 0;
1397 l = strtoll(p, &e, 10);
1398 if (errno > 0)
1399 return -errno;
1400 if (l < 0)
1401 return -ERANGE;
1402
1403 if (*e == '.') {
1404 p = e + 1;
1405 p += strspn(p, DIGITS);
1406 } else if (e == p)
1407 return -EINVAL;
1408 else
1409 p = e;
1410
1411 s = extract_nsec_multiplier(p + strspn(p, WHITESPACE), &multiplier);
1412 if (s == p && *s != '\0')
1413 /* Don't allow '12.34.56', but accept '12.34 .56' or '12.34s.56' */
1414 return -EINVAL;
1415
1416 p = s;
1417
1418 if ((nsec_t) l >= NSEC_INFINITY / multiplier)
1419 return -ERANGE;
1420
1421 k = (nsec_t) l * multiplier;
1422 if (k >= NSEC_INFINITY - nsec)
1423 return -ERANGE;
1424
1425 nsec += k;
1426
1427 something = true;
1428
1429 if (*e == '.') {
1430 nsec_t m = multiplier / 10;
1431 const char *b;
1432
1433 for (b = e + 1; *b >= '0' && *b <= '9'; b++, m /= 10) {
1434 k = (nsec_t) (*b - '0') * m;
1435 if (k >= NSEC_INFINITY - nsec)
1436 return -ERANGE;
1437
1438 nsec += k;
1439 }
1440
1441 /* Don't allow "0.-0", "3.+1", "3. 1", "3.sec" or "3.hoge" */
1442 if (b == e + 1)
1443 return -EINVAL;
1444 }
1445 }
1446
1447 *ret = nsec;
1448
1449 return 0;
1450 }
1451
1452 static int get_timezones_from_zone1970_tab(char ***ret) {
1453 _cleanup_fclose_ FILE *f = NULL;
1454 _cleanup_strv_free_ char **zones = NULL;
1455 int r;
1456
1457 assert(ret);
1458
1459 f = fopen("/usr/share/zoneinfo/zone1970.tab", "re");
1460 if (!f)
1461 return -errno;
1462
1463 for (;;) {
1464 _cleanup_free_ char *line = NULL, *cc = NULL, *co = NULL, *tz = NULL;
1465
1466 r = read_line(f, LONG_LINE_MAX, &line);
1467 if (r < 0)
1468 return r;
1469 if (r == 0)
1470 break;
1471
1472 const char *p = line;
1473
1474 /* Line format is:
1475 * 'country codes' 'coordinates' 'timezone' 'comments' */
1476 r = extract_many_words(&p, NULL, 0, &cc, &co, &tz);
1477 if (r < 0)
1478 continue;
1479
1480 /* Lines that start with # are comments. */
1481 if (*cc == '#')
1482 continue;
1483
1484 if (!timezone_is_valid(tz, LOG_DEBUG))
1485 /* Don't list unusable timezones. */
1486 continue;
1487
1488 r = strv_extend(&zones, tz);
1489 if (r < 0)
1490 return r;
1491 }
1492
1493 *ret = TAKE_PTR(zones);
1494 return 0;
1495 }
1496
1497 static int get_timezones_from_tzdata_zi(char ***ret) {
1498 _cleanup_fclose_ FILE *f = NULL;
1499 _cleanup_strv_free_ char **zones = NULL;
1500 int r;
1501
1502 assert(ret);
1503
1504 f = fopen("/usr/share/zoneinfo/tzdata.zi", "re");
1505 if (!f)
1506 return -errno;
1507
1508 for (;;) {
1509 _cleanup_free_ char *line = NULL, *type = NULL, *f1 = NULL, *f2 = NULL;
1510
1511 r = read_line(f, LONG_LINE_MAX, &line);
1512 if (r < 0)
1513 return r;
1514 if (r == 0)
1515 break;
1516
1517 const char *p = line;
1518
1519 /* The only lines we care about are Zone and Link lines.
1520 * Zone line format is:
1521 * 'Zone' 'timezone' ...
1522 * Link line format is:
1523 * 'Link' 'target' 'alias'
1524 * See 'man zic' for more detail. */
1525 r = extract_many_words(&p, NULL, 0, &type, &f1, &f2);
1526 if (r < 0)
1527 continue;
1528
1529 char *tz;
1530 if (IN_SET(*type, 'Z', 'z'))
1531 /* Zone lines have timezone in field 1. */
1532 tz = f1;
1533 else if (IN_SET(*type, 'L', 'l'))
1534 /* Link lines have timezone in field 2. */
1535 tz = f2;
1536 else
1537 /* Not a line we care about. */
1538 continue;
1539
1540 if (!timezone_is_valid(tz, LOG_DEBUG))
1541 /* Don't list unusable timezones. */
1542 continue;
1543
1544 r = strv_extend(&zones, tz);
1545 if (r < 0)
1546 return r;
1547 }
1548
1549 *ret = TAKE_PTR(zones);
1550 return 0;
1551 }
1552
1553 int get_timezones(char ***ret) {
1554 _cleanup_strv_free_ char **zones = NULL;
1555 int r;
1556
1557 assert(ret);
1558
1559 r = get_timezones_from_tzdata_zi(&zones);
1560 if (r == -ENOENT) {
1561 log_debug_errno(r, "Could not get timezone data from tzdata.zi, using zone1970.tab: %m");
1562 r = get_timezones_from_zone1970_tab(&zones);
1563 if (r == -ENOENT)
1564 log_debug_errno(r, "Could not get timezone data from zone1970.tab, using UTC: %m");
1565 }
1566 if (r < 0 && r != -ENOENT)
1567 return r;
1568
1569 /* Always include UTC */
1570 r = strv_extend(&zones, "UTC");
1571 if (r < 0)
1572 return r;
1573
1574 strv_sort_uniq(zones);
1575
1576 *ret = TAKE_PTR(zones);
1577 return 0;
1578 }
1579
1580 int verify_timezone(const char *name, int log_level) {
1581 bool slash = false;
1582 const char *p, *t;
1583 _cleanup_close_ int fd = -EBADF;
1584 char buf[4];
1585 int r;
1586
1587 if (isempty(name))
1588 return -EINVAL;
1589
1590 /* Always accept "UTC" as valid timezone, since it's the fallback, even if user has no timezones installed. */
1591 if (streq(name, "UTC"))
1592 return 0;
1593
1594 if (name[0] == '/')
1595 return -EINVAL;
1596
1597 for (p = name; *p; p++) {
1598 if (!ascii_isdigit(*p) &&
1599 !ascii_isalpha(*p) &&
1600 !IN_SET(*p, '-', '_', '+', '/'))
1601 return -EINVAL;
1602
1603 if (*p == '/') {
1604
1605 if (slash)
1606 return -EINVAL;
1607
1608 slash = true;
1609 } else
1610 slash = false;
1611 }
1612
1613 if (slash)
1614 return -EINVAL;
1615
1616 if (p - name >= PATH_MAX)
1617 return -ENAMETOOLONG;
1618
1619 t = strjoina("/usr/share/zoneinfo/", name);
1620
1621 fd = open(t, O_RDONLY|O_CLOEXEC);
1622 if (fd < 0)
1623 return log_full_errno(log_level, errno, "Failed to open timezone file '%s': %m", t);
1624
1625 r = fd_verify_regular(fd);
1626 if (r < 0)
1627 return log_full_errno(log_level, r, "Timezone file '%s' is not a regular file: %m", t);
1628
1629 r = loop_read_exact(fd, buf, 4, false);
1630 if (r < 0)
1631 return log_full_errno(log_level, r, "Failed to read from timezone file '%s': %m", t);
1632
1633 /* Magic from tzfile(5) */
1634 if (memcmp(buf, "TZif", 4) != 0)
1635 return log_full_errno(log_level, SYNTHETIC_ERRNO(EBADMSG),
1636 "Timezone file '%s' has wrong magic bytes", t);
1637
1638 return 0;
1639 }
1640
1641 void reset_timezonep(char **p) {
1642 assert(p);
1643
1644 (void) set_unset_env("TZ", *p, /* overwrite= */ true);
1645 tzset();
1646 *p = mfree(*p);
1647 }
1648
1649 char* save_timezone(void) {
1650 const char *e = getenv("TZ");
1651 if (!e)
1652 return NULL;
1653
1654 char *s = strdup(e);
1655 if (!s)
1656 log_debug("Failed to save $TZ=%s, unsetting the environment variable.", e);
1657
1658 return s;
1659 }
1660
1661 bool clock_supported(clockid_t clock) {
1662 struct timespec ts;
1663
1664 switch (clock) {
1665
1666 case CLOCK_MONOTONIC:
1667 case CLOCK_REALTIME:
1668 case CLOCK_BOOTTIME:
1669 /* These three are always available in our baseline, and work in timerfd, as of kernel 3.15 */
1670 return true;
1671
1672 default:
1673 /* For everything else, check properly */
1674 return clock_gettime(clock, &ts) >= 0;
1675 }
1676 }
1677
1678 int get_timezone(char **ret) {
1679 _cleanup_free_ char *t = NULL;
1680 int r;
1681
1682 assert(ret);
1683
1684 r = readlink_malloc(etc_localtime(), &t);
1685 if (r == -ENOENT)
1686 /* If the symlink does not exist, assume "UTC", like glibc does */
1687 return strdup_to(ret, "UTC");
1688 if (r < 0)
1689 return r; /* Return EINVAL if not a symlink */
1690
1691 const char *e = PATH_STARTSWITH_SET(t, "/usr/share/zoneinfo/", "../usr/share/zoneinfo/");
1692 if (!e)
1693 return -EINVAL;
1694 if (!timezone_is_valid(e, LOG_DEBUG))
1695 return -EINVAL;
1696
1697 return strdup_to(ret, e);
1698 }
1699
1700 int get_timezone_prefer_env(char **ret) {
1701 assert(ret);
1702
1703 const char *e = getenv("TZ");
1704 if (e && e[0] == ':' && timezone_is_valid(e + 1, LOG_DEBUG))
1705 return strdup_to(ret, e + 1);
1706
1707 return get_timezone(ret);
1708 }
1709
1710 const char* etc_localtime(void) {
1711 static const char *cached = NULL;
1712
1713 if (!cached)
1714 cached = secure_getenv("SYSTEMD_ETC_LOCALTIME") ?: "/etc/localtime";
1715
1716 return cached;
1717 }
1718
1719 int mktime_or_timegm_usec(
1720 struct tm *tm, /* input + normalized output */
1721 bool utc,
1722 usec_t *ret) {
1723
1724 time_t t;
1725
1726 assert(tm);
1727
1728 if (tm->tm_year < 69) /* early check for negative (i.e. before 1970) time_t (Note that in some timezones the epoch is in the year 1969!) */
1729 return -ERANGE;
1730 if ((usec_t) tm->tm_year > CONST_MIN(USEC_INFINITY / USEC_PER_YEAR, (usec_t) TIME_T_MAX / (365U * 24U * 60U * 60U)) - 1900) /* early check for possible overrun of usec_t or time_t */
1731 return -ERANGE;
1732
1733 /* timegm()/mktime() is a bit weird to use, since it returns -1 in two cases: on error as well as a
1734 * valid time indicating one second before the UNIX epoch. Let's treat both cases the same here, and
1735 * return -ERANGE for anything negative, since usec_t is unsigned, and we can thus not express
1736 * negative times anyway. */
1737
1738 t = utc ? timegm(tm) : mktime(tm);
1739 if (t < 0) /* Refuse negative times and errors */
1740 return -ERANGE;
1741 if ((usec_t) t >= USEC_INFINITY / USEC_PER_SEC) /* Never return USEC_INFINITY by accident (or overflow) */
1742 return -ERANGE;
1743
1744 if (ret)
1745 *ret = (usec_t) t * USEC_PER_SEC;
1746 return 0;
1747 }
1748
1749 int localtime_or_gmtime_usec(
1750 usec_t t,
1751 bool utc,
1752 struct tm *ret) {
1753
1754 t /= USEC_PER_SEC; /* Round down */
1755 if (t > (usec_t) TIME_T_MAX)
1756 return -ERANGE;
1757 time_t sec = (time_t) t;
1758
1759 struct tm buf = {};
1760 if (!(utc ? gmtime_r(&sec, &buf) : localtime_r(&sec, &buf)))
1761 return -EINVAL;
1762
1763 if (ret)
1764 *ret = buf;
1765
1766 return 0;
1767 }
1768
1769 static uint32_t sysconf_clock_ticks_cached(void) {
1770 static thread_local uint32_t hz = 0;
1771 long r;
1772
1773 if (hz == 0) {
1774 r = sysconf(_SC_CLK_TCK);
1775
1776 assert(r > 0);
1777 hz = r;
1778 }
1779
1780 return hz;
1781 }
1782
1783 uint32_t usec_to_jiffies(usec_t u) {
1784 uint32_t hz = sysconf_clock_ticks_cached();
1785 return DIV_ROUND_UP(u, USEC_PER_SEC / hz);
1786 }
1787
1788 usec_t jiffies_to_usec(uint32_t j) {
1789 uint32_t hz = sysconf_clock_ticks_cached();
1790 return DIV_ROUND_UP(j * USEC_PER_SEC, hz);
1791 }
1792
1793 usec_t usec_shift_clock(usec_t x, clockid_t from, clockid_t to) {
1794 usec_t a, b;
1795
1796 if (x == USEC_INFINITY)
1797 return USEC_INFINITY;
1798 if (map_clock_id(from) == map_clock_id(to))
1799 return x;
1800
1801 a = now(from);
1802 b = now(to);
1803
1804 if (x > a)
1805 /* x lies in the future */
1806 return usec_add(b, usec_sub_unsigned(x, a));
1807 else
1808 /* x lies in the past */
1809 return usec_sub_unsigned(b, usec_sub_unsigned(a, x));
1810 }
1811
1812 bool in_utc_timezone(void) {
1813 tzset();
1814
1815 return timezone == 0 && daylight == 0;
1816 }
1817
1818 int usleep_safe(usec_t usec) {
1819 int r;
1820
1821 /* usleep() takes useconds_t that is (typically?) uint32_t. Also, usleep() may only support the
1822 * range [0, 1000000]. See usleep(3). Let's override usleep() with clock_nanosleep().
1823 *
1824 * ⚠️ Note we are not using plain nanosleep() here, since that operates on CLOCK_REALTIME, not
1825 * CLOCK_MONOTONIC! */
1826
1827 if (usec == 0)
1828 return 0;
1829
1830 if (usec == USEC_INFINITY)
1831 return RET_NERRNO(pause());
1832
1833 struct timespec t;
1834 timespec_store(&t, usec);
1835
1836 for (;;) {
1837 struct timespec remaining;
1838
1839 /* `clock_nanosleep()` does not use `errno`, but returns positive error codes. */
1840 r = -clock_nanosleep(CLOCK_MONOTONIC, /* flags= */ 0, &t, &remaining);
1841 if (r == -EINTR) {
1842 /* Interrupted. Continue sleeping for the remaining time. */
1843 t = remaining;
1844 continue;
1845 }
1846
1847 return r;
1848 }
1849 }
1850
1851 int time_change_fd(void) {
1852
1853 /* We only care for the cancellation event, hence we set the timeout to the latest possible value. */
1854 static const struct itimerspec its = {
1855 .it_value.tv_sec = TIME_T_MAX,
1856 };
1857
1858 _cleanup_close_ int fd = -EBADF;
1859
1860 /* Uses TFD_TIMER_CANCEL_ON_SET to get notifications whenever CLOCK_REALTIME makes a jump relative to
1861 * CLOCK_MONOTONIC. */
1862
1863 fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
1864 if (fd < 0)
1865 return -errno;
1866
1867 if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) >= 0)
1868 return TAKE_FD(fd);
1869
1870 /* So apparently there are systems where time_t is 64-bit, but the kernel actually doesn't support
1871 * 64-bit time_t. In that case configuring a timer to TIME_T_MAX will fail with EOPNOTSUPP or a
1872 * similar error. If that's the case let's try with INT32_MAX instead, maybe that works. It's a bit
1873 * of a black magic thing though, but what can we do?
1874 *
1875 * We don't want this code on x86-64, hence let's conditionalize this for systems with 64-bit time_t
1876 * but where "long" is shorter than 64-bit, i.e. 32-bit archs.
1877 *
1878 * See: https://github.com/systemd/systemd/issues/14362 */
1879
1880 #if SIZEOF_TIME_T == 8 && ULONG_MAX < UINT64_MAX
1881 if (ERRNO_IS_NOT_SUPPORTED(errno) || errno == EOVERFLOW) {
1882 static const struct itimerspec its32 = {
1883 .it_value.tv_sec = INT32_MAX,
1884 };
1885
1886 if (timerfd_settime(fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its32, NULL) >= 0)
1887 return TAKE_FD(fd);
1888 }
1889 #endif
1890
1891 return -errno;
1892 }
1893
1894 static const char* const timestamp_style_table[_TIMESTAMP_STYLE_MAX] = {
1895 [TIMESTAMP_PRETTY] = "pretty",
1896 [TIMESTAMP_US] = "us",
1897 [TIMESTAMP_UTC] = "utc",
1898 [TIMESTAMP_US_UTC] = "us+utc",
1899 [TIMESTAMP_UNIX] = "unix",
1900 };
1901
1902 /* Use the macro for enum → string to allow for aliases */
1903 DEFINE_STRING_TABLE_LOOKUP_TO_STRING(timestamp_style, TimestampStyle);
1904
1905 /* For the string → enum mapping we use the generic implementation, but also support two aliases */
1906 TimestampStyle timestamp_style_from_string(const char *s) {
1907 TimestampStyle t;
1908
1909 t = (TimestampStyle) string_table_lookup_from_string(timestamp_style_table, ELEMENTSOF(timestamp_style_table), s);
1910 if (t >= 0)
1911 return t;
1912 if (STRPTR_IN_SET(s, "µs", "μs")) /* accept both µ symbols in unicode, i.e. micro symbol + Greek small letter mu. */
1913 return TIMESTAMP_US;
1914 if (STRPTR_IN_SET(s, "µs+utc", "μs+utc"))
1915 return TIMESTAMP_US_UTC;
1916 return t;
1917 }
1918
1919 int parse_calendar_date_full(const char *s, bool allow_pre_epoch, usec_t *ret_usec, struct tm *ret_tm) {
1920 struct tm parsed_tm = {}, copy_tm;
1921 const char *k;
1922 int r;
1923
1924 assert(s);
1925
1926 k = strptime(s, "%Y-%m-%d", &parsed_tm);
1927 if (!k || *k)
1928 return -EINVAL;
1929
1930 copy_tm = parsed_tm;
1931
1932 usec_t usec = USEC_INFINITY;
1933
1934 if (allow_pre_epoch) {
1935 /* For birth dates we use timegm() directly since we need to accept pre-epoch dates.
1936 * timegm() returns (time_t) -1 both on error and for one second before the epoch.
1937 * Initialize wday to -1 beforehand: if it remains -1 after the call, it's a genuine
1938 * error; if timegm() changed it, the date was successfully normalized. */
1939 copy_tm.tm_wday = -1;
1940 if (timegm(&copy_tm) == (time_t) -1 && copy_tm.tm_wday == -1)
1941 return -EINVAL;
1942 } else {
1943 r = mktime_or_timegm_usec(&copy_tm, /* utc= */ true, &usec);
1944 if (r < 0)
1945 return r;
1946 }
1947
1948 /* Refuse non-normalized dates, e.g. Feb 30 */
1949 if (copy_tm.tm_mday != parsed_tm.tm_mday ||
1950 copy_tm.tm_mon != parsed_tm.tm_mon ||
1951 copy_tm.tm_year != parsed_tm.tm_year)
1952 return -EINVAL;
1953
1954 if (ret_usec)
1955 *ret_usec = usec;
1956 if (ret_tm) {
1957 /* Reset to unset, then fill in only the date fields we parsed and validated */
1958 *ret_tm = BIRTH_DATE_UNSET;
1959 ret_tm->tm_mday = parsed_tm.tm_mday;
1960 ret_tm->tm_mon = parsed_tm.tm_mon;
1961 ret_tm->tm_year = parsed_tm.tm_year;
1962 }
1963
1964 return 0;
1965 }