1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
15 #include <sys/timerfd.h>
16 #include <sys/timex.h>
17 #include <sys/types.h>
20 #include "alloc-util.h"
26 #include "parse-util.h"
27 #include "path-util.h"
28 //#include "process-util.h"
29 #include "string-util.h"
31 #include "time-util.h"
33 static clockid_t map_clock_id(clockid_t c) {
35 /* Some more exotic archs (s390, ppc, …) lack the "ALARM" flavour of the clocks. Thus, clock_gettime() will
36 * fail for them. Since they are essentially the same as their non-ALARM pendants (their only difference is
37 * when timers are set on them), let's just map them accordingly. This way, we can get the correct time even on
42 case CLOCK_BOOTTIME_ALARM:
43 return CLOCK_BOOTTIME;
45 case CLOCK_REALTIME_ALARM:
46 return CLOCK_REALTIME;
53 usec_t now(clockid_t clock_id) {
56 assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
58 return timespec_load(&ts);
61 #if 0 /// UNNEEDED by elogind
62 nsec_t now_nsec(clockid_t clock_id) {
65 assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
67 return timespec_load_nsec(&ts);
71 dual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
74 ts->realtime = now(CLOCK_REALTIME);
75 ts->monotonic = now(CLOCK_MONOTONIC);
80 triple_timestamp* triple_timestamp_get(triple_timestamp *ts) {
83 ts->realtime = now(CLOCK_REALTIME);
84 ts->monotonic = now(CLOCK_MONOTONIC);
85 ts->boottime = clock_boottime_supported() ? now(CLOCK_BOOTTIME) : USEC_INFINITY;
90 dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
94 if (u == USEC_INFINITY || u <= 0) {
95 ts->realtime = ts->monotonic = u;
101 delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u;
102 ts->monotonic = usec_sub_signed(now(CLOCK_MONOTONIC), delta);
107 #if 0 /// UNNEEDED by elogind
108 triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u) {
113 if (u == USEC_INFINITY || u <= 0) {
114 ts->realtime = ts->monotonic = ts->boottime = u;
119 delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u;
120 ts->monotonic = usec_sub_signed(now(CLOCK_MONOTONIC), delta);
121 ts->boottime = clock_boottime_supported() ? usec_sub_signed(now(CLOCK_BOOTTIME), delta) : USEC_INFINITY;
126 dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
130 if (u == USEC_INFINITY) {
131 ts->realtime = ts->monotonic = USEC_INFINITY;
136 delta = (int64_t) now(CLOCK_MONOTONIC) - (int64_t) u;
137 ts->realtime = usec_sub_signed(now(CLOCK_REALTIME), delta);
142 dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, usec_t u) {
145 if (u == USEC_INFINITY) {
146 ts->realtime = ts->monotonic = USEC_INFINITY;
150 dual_timestamp_get(ts);
151 delta = (int64_t) now(clock_boottime_or_monotonic()) - (int64_t) u;
152 ts->realtime = usec_sub_signed(ts->realtime, delta);
153 ts->monotonic = usec_sub_signed(ts->monotonic, delta);
159 usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock) {
164 case CLOCK_REALTIME_ALARM:
167 case CLOCK_MONOTONIC:
168 return ts->monotonic;
171 case CLOCK_BOOTTIME_ALARM:
175 return USEC_INFINITY;
179 usec_t timespec_load(const struct timespec *ts) {
182 if (ts->tv_sec < 0 || ts->tv_nsec < 0)
183 return USEC_INFINITY;
185 if ((usec_t) ts->tv_sec > (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC)
186 return USEC_INFINITY;
189 (usec_t) ts->tv_sec * USEC_PER_SEC +
190 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
193 #if 0 /// UNNEEDED by elogind
194 nsec_t timespec_load_nsec(const struct timespec *ts) {
197 if (ts->tv_sec < 0 || ts->tv_nsec < 0)
198 return NSEC_INFINITY;
200 if ((nsec_t) ts->tv_sec >= (UINT64_MAX - ts->tv_nsec) / NSEC_PER_SEC)
201 return NSEC_INFINITY;
203 return (nsec_t) ts->tv_sec * NSEC_PER_SEC + (nsec_t) ts->tv_nsec;
207 struct timespec *timespec_store(struct timespec *ts, usec_t u) {
210 if (u == USEC_INFINITY ||
211 u / USEC_PER_SEC >= TIME_T_MAX) {
212 ts->tv_sec = (time_t) -1;
213 ts->tv_nsec = (long) -1;
217 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
218 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
223 usec_t timeval_load(const struct timeval *tv) {
226 if (tv->tv_sec < 0 || tv->tv_usec < 0)
227 return USEC_INFINITY;
229 if ((usec_t) tv->tv_sec > (UINT64_MAX - tv->tv_usec) / USEC_PER_SEC)
230 return USEC_INFINITY;
233 (usec_t) tv->tv_sec * USEC_PER_SEC +
234 (usec_t) tv->tv_usec;
237 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
240 if (u == USEC_INFINITY ||
241 u / USEC_PER_SEC > TIME_T_MAX) {
242 tv->tv_sec = (time_t) -1;
243 tv->tv_usec = (suseconds_t) -1;
245 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
246 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
252 static char *format_timestamp_internal(
259 /* The weekdays in non-localized (English) form. We use this instead of the localized form, so that our
260 * generated timestamps may be parsed with parse_timestamp(), and always read the same. */
261 static const char * const weekdays[] = {
279 1 + 10 + /* space and date */
280 1 + 8 + /* space and time */
281 (us ? 1 + 6 : 0) + /* "." and microsecond part */
282 1 + 1 + /* space and shortest possible zone */
284 return NULL; /* Not enough space even for the shortest form. */
285 if (t <= 0 || t == USEC_INFINITY)
286 return NULL; /* Timestamp is unset */
288 /* Let's not format times with years > 9999 */
289 if (t > USEC_TIMESTAMP_FORMATTABLE_MAX)
292 sec = (time_t) (t / USEC_PER_SEC); /* Round down */
294 if (!localtime_or_gmtime_r(&sec, &tm, utc))
297 /* Start with the week day */
298 assert((size_t) tm.tm_wday < ELEMENTSOF(weekdays));
299 memcpy(buf, weekdays[tm.tm_wday], 4);
301 /* Add the main components */
302 if (strftime(buf + 3, l - 3, " %Y-%m-%d %H:%M:%S", &tm) <= 0)
303 return NULL; /* Doesn't fit */
305 /* Append the microseconds part, if that's requested */
309 return NULL; /* Microseconds part doesn't fit. */
311 sprintf(buf + n, ".%06"PRI_USEC, t % USEC_PER_SEC);
314 /* Append the timezone */
317 /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r() normally uses the
318 * obsolete "GMT" instead. */
320 return NULL; /* "UTC" doesn't fit. */
322 strcpy(buf + n, " UTC");
324 } else if (!isempty(tm.tm_zone)) {
327 /* An explicit timezone is specified, let's use it, if it fits */
328 tn = strlen(tm.tm_zone);
329 if (n + 1 + tn + 1 > l) {
330 /* The full time zone does not fit in. Yuck. */
332 if (n + 1 + _POSIX_TZNAME_MAX + 1 > l)
333 return NULL; /* Not even enough space for the POSIX minimum (of 6)? In that case, complain that it doesn't fit */
335 /* So the time zone doesn't fit in fully, but the caller passed enough space for the POSIX
336 * minimum time zone length. In this case suppress the timezone entirely, in order not to dump
337 * an overly long, hard to read string on the user. This should be safe, because the user will
338 * assume the local timezone anyway if none is shown. And so does parse_timestamp(). */
341 strcpy(buf + n, tm.tm_zone);
348 char *format_timestamp(char *buf, size_t l, usec_t t) {
349 return format_timestamp_internal(buf, l, t, false, false);
352 #if 0 /// UNNEEDED by elogind
353 char *format_timestamp_utc(char *buf, size_t l, usec_t t) {
354 return format_timestamp_internal(buf, l, t, true, false);
358 char *format_timestamp_us(char *buf, size_t l, usec_t t) {
359 return format_timestamp_internal(buf, l, t, false, true);
362 #if 0 /// UNNEEDED by elogind
363 char *format_timestamp_us_utc(char *buf, size_t l, usec_t t) {
364 return format_timestamp_internal(buf, l, t, true, true);
368 char *format_timestamp_relative(char *buf, size_t l, usec_t t) {
372 if (t <= 0 || t == USEC_INFINITY)
375 n = now(CLOCK_REALTIME);
384 if (d >= USEC_PER_YEAR)
385 snprintf(buf, l, USEC_FMT " years " USEC_FMT " months %s",
387 (d % USEC_PER_YEAR) / USEC_PER_MONTH, s);
388 else if (d >= USEC_PER_MONTH)
389 snprintf(buf, l, USEC_FMT " months " USEC_FMT " days %s",
391 (d % USEC_PER_MONTH) / USEC_PER_DAY, s);
392 else if (d >= USEC_PER_WEEK)
393 snprintf(buf, l, USEC_FMT " weeks " USEC_FMT " days %s",
395 (d % USEC_PER_WEEK) / USEC_PER_DAY, s);
396 else if (d >= 2*USEC_PER_DAY)
397 snprintf(buf, l, USEC_FMT " days %s", d / USEC_PER_DAY, s);
398 else if (d >= 25*USEC_PER_HOUR)
399 snprintf(buf, l, "1 day " USEC_FMT "h %s",
400 (d - USEC_PER_DAY) / USEC_PER_HOUR, s);
401 else if (d >= 6*USEC_PER_HOUR)
402 snprintf(buf, l, USEC_FMT "h %s",
403 d / USEC_PER_HOUR, s);
404 else if (d >= USEC_PER_HOUR)
405 snprintf(buf, l, USEC_FMT "h " USEC_FMT "min %s",
407 (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s);
408 else if (d >= 5*USEC_PER_MINUTE)
409 snprintf(buf, l, USEC_FMT "min %s",
410 d / USEC_PER_MINUTE, s);
411 else if (d >= USEC_PER_MINUTE)
412 snprintf(buf, l, USEC_FMT "min " USEC_FMT "s %s",
414 (d % USEC_PER_MINUTE) / USEC_PER_SEC, s);
415 else if (d >= USEC_PER_SEC)
416 snprintf(buf, l, USEC_FMT "s %s",
417 d / USEC_PER_SEC, s);
418 else if (d >= USEC_PER_MSEC)
419 snprintf(buf, l, USEC_FMT "ms %s",
420 d / USEC_PER_MSEC, s);
422 snprintf(buf, l, USEC_FMT"us %s",
425 snprintf(buf, l, "now");
431 char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
432 static const struct {
436 { "y", USEC_PER_YEAR },
437 { "month", USEC_PER_MONTH },
438 { "w", USEC_PER_WEEK },
439 { "d", USEC_PER_DAY },
440 { "h", USEC_PER_HOUR },
441 { "min", USEC_PER_MINUTE },
442 { "s", USEC_PER_SEC },
443 { "ms", USEC_PER_MSEC },
449 bool something = false;
454 if (t == USEC_INFINITY) {
455 strncpy(p, "infinity", l-1);
461 strncpy(p, "0", l-1);
466 /* The result of this function can be parsed with parse_sec */
468 for (i = 0; i < ELEMENTSOF(table); i++) {
477 if (t < accuracy && something)
480 if (t < table[i].usec)
486 a = t / table[i].usec;
487 b = t % table[i].usec;
489 /* Let's see if we should shows this in dot notation */
490 if (t < USEC_PER_MINUTE && b > 0) {
495 for (cc = table[i].usec; cc > 1; cc /= 10)
498 for (cc = accuracy; cc > 1; cc /= 10) {
505 "%s"USEC_FMT".%0*"PRI_USEC"%s",
517 /* No? Then let's show it normally */
528 n = MIN((size_t) k, l);
541 #if 0 /// UNNEEDED by elogind
542 void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) {
548 if (!dual_timestamp_is_set(t))
551 fprintf(f, "%s="USEC_FMT" "USEC_FMT"\n",
557 int dual_timestamp_deserialize(const char *value, dual_timestamp *t) {
564 pos = strspn(value, WHITESPACE);
565 if (value[pos] == '-')
567 pos += strspn(value + pos, DIGITS);
568 pos += strspn(value + pos, WHITESPACE);
569 if (value[pos] == '-')
572 r = sscanf(value, "%" PRIu64 "%" PRIu64 "%n", &a, &b, &pos);
574 log_debug("Failed to parse dual timestamp value \"%s\".", value);
578 if (value[pos] != '\0')
579 /* trailing garbage */
589 int timestamp_deserialize(const char *value, usec_t *timestamp) {
594 r = safe_atou64(value, timestamp);
596 return log_debug_errno(r, "Failed to parse timestamp value \"%s\": %m", value);
601 #if 0 /// UNNEEDED by elogind
602 static int parse_timestamp_impl(const char *t, usec_t *usec, bool with_tz) {
603 static const struct {
623 const char *k, *utc = NULL, *tzn = NULL;
626 usec_t x_usec, plus = 0, minus = 0, ret;
627 int r, weekday = -1, dst = -1;
633 * 2012-09-22 16:34:22
634 * 2012-09-22 16:34 (seconds will be set to 0)
635 * 2012-09-22 (time will be set to 00:00:00)
636 * 16:34:22 (date will be set to today)
637 * 16:34 (date will be set to today, seconds to 0)
639 * yesterday (time is set to 00:00:00)
640 * today (time is set to 00:00:00)
641 * tomorrow (time is set to 00:00:00)
644 * @2147483647 (seconds since epoch)
651 if (t[0] == '@' && !with_tz)
652 return parse_sec(t + 1, usec);
654 ret = now(CLOCK_REALTIME);
660 else if (t[0] == '+') {
661 r = parse_sec(t+1, &plus);
667 } else if (t[0] == '-') {
668 r = parse_sec(t+1, &minus);
674 } else if ((k = endswith(t, " ago"))) {
675 t = strndupa(t, k - t);
677 r = parse_sec(t, &minus);
683 } else if ((k = endswith(t, " left"))) {
684 t = strndupa(t, k - t);
686 r = parse_sec(t, &plus);
693 /* See if the timestamp is suffixed with UTC */
694 utc = endswith_no_case(t, " UTC");
696 t = strndupa(t, utc - t);
698 const char *e = NULL;
703 /* See if the timestamp is suffixed by either the DST or non-DST local timezone. Note that we only
704 * support the local timezones here, nothing else. Not because we wouldn't want to, but simply because
705 * there are no nice APIs available to cover this. By accepting the local time zone strings, we make
706 * sure that all timestamps written by format_timestamp() can be parsed correctly, even though we don't
707 * support arbitrary timezone specifications. */
709 for (j = 0; j <= 1; j++) {
711 if (isempty(tzname[j]))
714 e = endswith_no_case(t, tzname[j]);
725 if (IN_SET(j, 0, 1)) {
726 /* Found one of the two timezones specified. */
727 t = strndupa(t, e - t - 1);
734 x = (time_t) (ret / USEC_PER_SEC);
737 if (!localtime_or_gmtime_r(&x, &tm, utc))
744 if (streq(t, "today")) {
745 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
748 } else if (streq(t, "yesterday")) {
750 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
753 } else if (streq(t, "tomorrow")) {
755 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
759 for (i = 0; i < ELEMENTSOF(day_nr); i++) {
762 if (!startswith_no_case(t, day_nr[i].name))
765 skip = strlen(day_nr[i].name);
769 weekday = day_nr[i].nr;
775 k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
784 k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm);
793 k = strptime(t, "%y-%m-%d %H:%M", &tm);
800 k = strptime(t, "%Y-%m-%d %H:%M", &tm);
807 k = strptime(t, "%y-%m-%d", &tm);
809 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
814 k = strptime(t, "%Y-%m-%d", &tm);
816 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
821 k = strptime(t, "%H:%M:%S", &tm);
830 k = strptime(t, "%H:%M", &tm);
843 r = parse_fractional_part_u(&k, 6, &add);
854 if (weekday >= 0 && tm.tm_wday != weekday)
857 x = mktime_or_timegm(&tm, utc);
861 ret = (usec_t) x * USEC_PER_SEC + x_usec;
862 if (ret > USEC_TIMESTAMP_FORMATTABLE_MAX)
866 if (ret + plus < ret) /* overflow? */
869 if (ret > USEC_TIMESTAMP_FORMATTABLE_MAX)
882 typedef struct ParseTimestampResult {
885 } ParseTimestampResult;
887 int parse_timestamp(const char *t, usec_t *usec) {
888 char *last_space, *tz = NULL;
889 ParseTimestampResult *shared, tmp;
892 last_space = strrchr(t, ' ');
893 if (last_space != NULL && timezone_is_valid(last_space + 1))
896 if (!tz || endswith_no_case(t, " UTC"))
897 return parse_timestamp_impl(t, usec, false);
899 shared = mmap(NULL, sizeof *shared, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
900 if (shared == MAP_FAILED)
901 return negative_errno();
903 r = safe_fork("(sd-timestamp)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT, NULL);
905 (void) munmap(shared, sizeof *shared);
911 if (setenv("TZ", tz, 1) != 0) {
912 shared->return_value = negative_errno();
918 /* If there is a timezone that matches the tzname fields, leave the parsing to the implementation.
919 * Otherwise just cut it off */
920 with_tz = !STR_IN_SET(tz, tzname[0], tzname[1]);
922 /*cut off the timezone if we dont need it*/
924 t = strndupa(t, last_space - t);
926 shared->return_value = parse_timestamp_impl(t, &shared->usec, with_tz);
932 if (munmap(shared, sizeof *shared) != 0)
933 return negative_errno();
935 if (tmp.return_value == 0)
938 return tmp.return_value;
942 static char* extract_multiplier(char *p, usec_t *multiplier) {
943 static const struct {
947 { "seconds", USEC_PER_SEC },
948 { "second", USEC_PER_SEC },
949 { "sec", USEC_PER_SEC },
950 { "s", USEC_PER_SEC },
951 { "minutes", USEC_PER_MINUTE },
952 { "minute", USEC_PER_MINUTE },
953 { "min", USEC_PER_MINUTE },
954 { "months", USEC_PER_MONTH },
955 { "month", USEC_PER_MONTH },
956 { "M", USEC_PER_MONTH },
957 { "msec", USEC_PER_MSEC },
958 { "ms", USEC_PER_MSEC },
959 { "m", USEC_PER_MINUTE },
960 { "hours", USEC_PER_HOUR },
961 { "hour", USEC_PER_HOUR },
962 { "hr", USEC_PER_HOUR },
963 { "h", USEC_PER_HOUR },
964 { "days", USEC_PER_DAY },
965 { "day", USEC_PER_DAY },
966 { "d", USEC_PER_DAY },
967 { "weeks", USEC_PER_WEEK },
968 { "week", USEC_PER_WEEK },
969 { "w", USEC_PER_WEEK },
970 { "years", USEC_PER_YEAR },
971 { "year", USEC_PER_YEAR },
972 { "y", USEC_PER_YEAR },
979 for (i = 0; i < ELEMENTSOF(table); i++) {
982 e = startswith(p, table[i].suffix);
984 *multiplier = table[i].usec;
992 int parse_time(const char *t, usec_t *usec, usec_t default_unit) {
995 bool something = false;
999 assert(default_unit > 0);
1003 p += strspn(p, WHITESPACE);
1004 s = startswith(p, "infinity");
1006 s += strspn(s, WHITESPACE);
1010 *usec = USEC_INFINITY;
1018 usec_t multiplier = default_unit, k;
1020 p += strspn(p, WHITESPACE);
1030 l = strtoll(p, &e, 10);
1040 z = strtoll(b, &e, 10);
1055 e += strspn(e, WHITESPACE);
1056 p = extract_multiplier(e, &multiplier);
1060 k = (usec_t) z * multiplier;
1065 r += (usec_t) l * multiplier + k;
1073 int parse_sec(const char *t, usec_t *usec) {
1074 return parse_time(t, usec, USEC_PER_SEC);
1077 #if 0 /// UNNEEDED by elogind
1078 int parse_sec_fix_0(const char *t, usec_t *usec) {
1082 t += strspn(t, WHITESPACE);
1084 if (streq(t, "0")) {
1085 *usec = USEC_INFINITY;
1089 return parse_sec(t, usec);
1092 int parse_nsec(const char *t, nsec_t *nsec) {
1093 static const struct {
1097 { "seconds", NSEC_PER_SEC },
1098 { "second", NSEC_PER_SEC },
1099 { "sec", NSEC_PER_SEC },
1100 { "s", NSEC_PER_SEC },
1101 { "minutes", NSEC_PER_MINUTE },
1102 { "minute", NSEC_PER_MINUTE },
1103 { "min", NSEC_PER_MINUTE },
1104 { "months", NSEC_PER_MONTH },
1105 { "month", NSEC_PER_MONTH },
1106 { "msec", NSEC_PER_MSEC },
1107 { "ms", NSEC_PER_MSEC },
1108 { "m", NSEC_PER_MINUTE },
1109 { "hours", NSEC_PER_HOUR },
1110 { "hour", NSEC_PER_HOUR },
1111 { "hr", NSEC_PER_HOUR },
1112 { "h", NSEC_PER_HOUR },
1113 { "days", NSEC_PER_DAY },
1114 { "day", NSEC_PER_DAY },
1115 { "d", NSEC_PER_DAY },
1116 { "weeks", NSEC_PER_WEEK },
1117 { "week", NSEC_PER_WEEK },
1118 { "w", NSEC_PER_WEEK },
1119 { "years", NSEC_PER_YEAR },
1120 { "year", NSEC_PER_YEAR },
1121 { "y", NSEC_PER_YEAR },
1122 { "usec", NSEC_PER_USEC },
1123 { "us", NSEC_PER_USEC },
1124 { "µs", NSEC_PER_USEC },
1127 { "", 1ULL }, /* default is nsec */
1132 bool something = false;
1139 p += strspn(p, WHITESPACE);
1140 s = startswith(p, "infinity");
1142 s += strspn(s, WHITESPACE);
1146 *nsec = NSEC_INFINITY;
1155 p += strspn(p, WHITESPACE);
1165 l = strtoll(p, &e, 10);
1177 z = strtoll(b, &e, 10);
1192 e += strspn(e, WHITESPACE);
1194 for (i = 0; i < ELEMENTSOF(table); i++)
1195 if (startswith(e, table[i].suffix)) {
1196 nsec_t k = (nsec_t) z * table[i].nsec;
1201 r += (nsec_t) l * table[i].nsec + k;
1202 p = e + strlen(table[i].suffix);
1208 if (i >= ELEMENTSOF(table))
1218 bool ntp_synced(void) {
1219 struct timex txc = {};
1221 if (adjtimex(&txc) < 0)
1224 if (txc.status & STA_UNSYNC)
1230 int get_timezones(char ***ret) {
1231 _cleanup_fclose_ FILE *f = NULL;
1232 _cleanup_strv_free_ char **zones = NULL;
1233 size_t n_zones = 0, n_allocated = 0;
1237 zones = strv_new("UTC", NULL);
1244 f = fopen("/usr/share/zoneinfo/zone.tab", "re");
1248 FOREACH_LINE(l, f, return -errno) {
1254 if (isempty(p) || *p == '#')
1257 /* Skip over country code */
1258 p += strcspn(p, WHITESPACE);
1259 p += strspn(p, WHITESPACE);
1261 /* Skip over coordinates */
1262 p += strcspn(p, WHITESPACE);
1263 p += strspn(p, WHITESPACE);
1265 /* Found timezone name */
1266 k = strcspn(p, WHITESPACE);
1274 if (!GREEDY_REALLOC(zones, n_allocated, n_zones + 2)) {
1279 zones[n_zones++] = w;
1280 zones[n_zones] = NULL;
1285 } else if (errno != ENOENT)
1288 *ret = TAKE_PTR(zones);
1293 bool timezone_is_valid(const char *name) {
1304 for (p = name; *p; p++) {
1305 if (!(*p >= '0' && *p <= '9') &&
1306 !(*p >= 'a' && *p <= 'z') &&
1307 !(*p >= 'A' && *p <= 'Z') &&
1308 !IN_SET(*p, '-', '_', '+', '/'))
1324 t = strjoina("/usr/share/zoneinfo/", name);
1325 if (stat(t, &st) < 0)
1328 if (!S_ISREG(st.st_mode))
1335 bool clock_boottime_supported(void) {
1336 static int supported = -1;
1338 /* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */
1340 if (supported < 0) {
1343 fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC);
1355 #if 0 /// UNNEEDED by elogind
1356 clockid_t clock_boottime_or_monotonic(void) {
1357 if (clock_boottime_supported())
1358 return CLOCK_BOOTTIME;
1360 return CLOCK_MONOTONIC;
1364 #if 1 /// let's add a diagnostic push to silence -Wimplicit-fallthrough to elogind
1365 # if defined(__GNUC__) && (__GNUC__ > 6)
1366 # pragma GCC diagnostic push
1367 # pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
1370 bool clock_supported(clockid_t clock) {
1375 case CLOCK_MONOTONIC:
1376 case CLOCK_REALTIME:
1379 case CLOCK_BOOTTIME:
1380 return clock_boottime_supported();
1382 case CLOCK_BOOTTIME_ALARM:
1383 if (!clock_boottime_supported())
1388 /* For everything else, check properly */
1389 return clock_gettime(clock, &ts) >= 0;
1392 #if 1 /// end diagnostic push in elogind
1394 # pragma GCC diagnostic pop
1398 #if 0 /// UNNEEDED by elogind
1399 int get_timezone(char **tz) {
1400 _cleanup_free_ char *t = NULL;
1405 r = readlink_malloc("/etc/localtime", &t);
1407 return r; /* returns EINVAL if not a symlink */
1409 e = path_startswith(t, "/usr/share/zoneinfo/");
1411 e = path_startswith(t, "../usr/share/zoneinfo/");
1415 if (!timezone_is_valid(e))
1426 time_t mktime_or_timegm(struct tm *tm, bool utc) {
1427 return utc ? timegm(tm) : mktime(tm);
1431 struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc) {
1432 return utc ? gmtime_r(t, tm) : localtime_r(t, tm);
1435 #if 0 /// UNNEEDED by elogind
1436 unsigned long usec_to_jiffies(usec_t u) {
1437 static thread_local unsigned long hz = 0;
1441 r = sysconf(_SC_CLK_TCK);
1447 return DIV_ROUND_UP(u , USEC_PER_SEC / hz);
1450 usec_t usec_shift_clock(usec_t x, clockid_t from, clockid_t to) {
1453 if (x == USEC_INFINITY)
1454 return USEC_INFINITY;
1455 if (map_clock_id(from) == map_clock_id(to))
1462 /* x lies in the future */
1463 return usec_add(b, usec_sub_unsigned(x, a));
1465 /* x lies in the past */
1466 return usec_sub_unsigned(b, usec_sub_unsigned(a, x));
1470 bool in_utc_timezone(void) {
1473 return timezone == 0 && daylight == 0;