2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/timerfd.h>
27 #include <sys/timex.h>
28 #include <sys/types.h>
31 #include "alloc-util.h"
37 #include "parse-util.h"
38 #include "path-util.h"
39 #include "string-util.h"
41 #include "time-util.h"
43 static clockid_t map_clock_id(clockid_t c) {
45 /* Some more exotic archs (s390, ppc, …) lack the "ALARM" flavour of the clocks. Thus, clock_gettime() will
46 * fail for them. Since they are essentially the same as their non-ALARM pendants (their only difference is
47 * when timers are set on them), let's just map them accordingly. This way, we can get the correct time even on
52 case CLOCK_BOOTTIME_ALARM:
53 return CLOCK_BOOTTIME;
55 case CLOCK_REALTIME_ALARM:
56 return CLOCK_REALTIME;
63 usec_t now(clockid_t clock_id) {
66 assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
68 return timespec_load(&ts);
71 #if 0 /// UNNEEDED by elogind
72 nsec_t now_nsec(clockid_t clock_id) {
75 assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
77 return timespec_load_nsec(&ts);
81 dual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
84 ts->realtime = now(CLOCK_REALTIME);
85 ts->monotonic = now(CLOCK_MONOTONIC);
90 triple_timestamp* triple_timestamp_get(triple_timestamp *ts) {
93 ts->realtime = now(CLOCK_REALTIME);
94 ts->monotonic = now(CLOCK_MONOTONIC);
95 ts->boottime = clock_boottime_supported() ? now(CLOCK_BOOTTIME) : USEC_INFINITY;
100 dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
104 if (u == USEC_INFINITY || u <= 0) {
105 ts->realtime = ts->monotonic = u;
111 delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u;
112 ts->monotonic = usec_sub_signed(now(CLOCK_MONOTONIC), delta);
117 #if 0 /// UNNEEDED by elogind
118 triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u) {
123 if (u == USEC_INFINITY || u <= 0) {
124 ts->realtime = ts->monotonic = ts->boottime = u;
129 delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u;
130 ts->monotonic = usec_sub_signed(now(CLOCK_MONOTONIC), delta);
131 ts->boottime = clock_boottime_supported() ? usec_sub_signed(now(CLOCK_BOOTTIME), delta) : USEC_INFINITY;
136 dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
140 if (u == USEC_INFINITY) {
141 ts->realtime = ts->monotonic = USEC_INFINITY;
146 delta = (int64_t) now(CLOCK_MONOTONIC) - (int64_t) u;
147 ts->realtime = usec_sub_signed(now(CLOCK_REALTIME), delta);
152 dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, usec_t u) {
155 if (u == USEC_INFINITY) {
156 ts->realtime = ts->monotonic = USEC_INFINITY;
160 dual_timestamp_get(ts);
161 delta = (int64_t) now(clock_boottime_or_monotonic()) - (int64_t) u;
162 ts->realtime = usec_sub_signed(ts->realtime, delta);
163 ts->monotonic = usec_sub_signed(ts->monotonic, delta);
169 usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock) {
174 case CLOCK_REALTIME_ALARM:
177 case CLOCK_MONOTONIC:
178 return ts->monotonic;
181 case CLOCK_BOOTTIME_ALARM:
185 return USEC_INFINITY;
189 usec_t timespec_load(const struct timespec *ts) {
192 if (ts->tv_sec < 0 || ts->tv_nsec < 0)
193 return USEC_INFINITY;
195 if ((usec_t) ts->tv_sec > (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC)
196 return USEC_INFINITY;
199 (usec_t) ts->tv_sec * USEC_PER_SEC +
200 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
203 #if 0 /// UNNEEDED by elogind
204 nsec_t timespec_load_nsec(const struct timespec *ts) {
207 if (ts->tv_sec < 0 || ts->tv_nsec < 0)
208 return NSEC_INFINITY;
210 if ((nsec_t) ts->tv_sec >= (UINT64_MAX - ts->tv_nsec) / NSEC_PER_SEC)
211 return NSEC_INFINITY;
213 return (nsec_t) ts->tv_sec * NSEC_PER_SEC + (nsec_t) ts->tv_nsec;
217 struct timespec *timespec_store(struct timespec *ts, usec_t u) {
220 if (u == USEC_INFINITY ||
221 u / USEC_PER_SEC >= TIME_T_MAX) {
222 ts->tv_sec = (time_t) -1;
223 ts->tv_nsec = (long) -1;
227 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
228 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
233 usec_t timeval_load(const struct timeval *tv) {
236 if (tv->tv_sec < 0 || tv->tv_usec < 0)
237 return USEC_INFINITY;
239 if ((usec_t) tv->tv_sec > (UINT64_MAX - tv->tv_usec) / USEC_PER_SEC)
240 return USEC_INFINITY;
243 (usec_t) tv->tv_sec * USEC_PER_SEC +
244 (usec_t) tv->tv_usec;
247 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
250 if (u == USEC_INFINITY ||
251 u / USEC_PER_SEC > TIME_T_MAX) {
252 tv->tv_sec = (time_t) -1;
253 tv->tv_usec = (suseconds_t) -1;
255 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
256 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
262 static char *format_timestamp_internal(
269 /* The weekdays in non-localized (English) form. We use this instead of the localized form, so that our
270 * generated timestamps may be parsed with parse_timestamp(), and always read the same. */
271 static const char * const weekdays[] = {
289 1 + 10 + /* space and date */
290 1 + 8 + /* space and time */
291 (us ? 1 + 6 : 0) + /* "." and microsecond part */
292 1 + 1 + /* space and shortest possible zone */
294 return NULL; /* Not enough space even for the shortest form. */
295 if (t <= 0 || t == USEC_INFINITY)
296 return NULL; /* Timestamp is unset */
298 /* Let's not format times with years > 9999 */
299 if (t > USEC_TIMESTAMP_FORMATTABLE_MAX)
302 sec = (time_t) (t / USEC_PER_SEC); /* Round down */
304 if (!localtime_or_gmtime_r(&sec, &tm, utc))
307 /* Start with the week day */
308 assert((size_t) tm.tm_wday < ELEMENTSOF(weekdays));
309 memcpy(buf, weekdays[tm.tm_wday], 4);
311 /* Add the main components */
312 if (strftime(buf + 3, l - 3, " %Y-%m-%d %H:%M:%S", &tm) <= 0)
313 return NULL; /* Doesn't fit */
315 /* Append the microseconds part, if that's requested */
319 return NULL; /* Microseconds part doesn't fit. */
321 sprintf(buf + n, ".%06"PRI_USEC, t % USEC_PER_SEC);
324 /* Append the timezone */
327 /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r() normally uses the
328 * obsolete "GMT" instead. */
330 return NULL; /* "UTC" doesn't fit. */
332 strcpy(buf + n, " UTC");
334 } else if (!isempty(tm.tm_zone)) {
337 /* An explicit timezone is specified, let's use it, if it fits */
338 tn = strlen(tm.tm_zone);
339 if (n + 1 + tn + 1 > l) {
340 /* The full time zone does not fit in. Yuck. */
342 if (n + 1 + _POSIX_TZNAME_MAX + 1 > l)
343 return NULL; /* Not even enough space for the POSIX minimum (of 6)? In that case, complain that it doesn't fit */
345 /* So the time zone doesn't fit in fully, but the caller passed enough space for the POSIX
346 * minimum time zone length. In this case suppress the timezone entirely, in order not to dump
347 * an overly long, hard to read string on the user. This should be safe, because the user will
348 * assume the local timezone anyway if none is shown. And so does parse_timestamp(). */
351 strcpy(buf + n, tm.tm_zone);
358 char *format_timestamp(char *buf, size_t l, usec_t t) {
359 return format_timestamp_internal(buf, l, t, false, false);
362 #if 0 /// UNNEEDED by elogind
363 char *format_timestamp_utc(char *buf, size_t l, usec_t t) {
364 return format_timestamp_internal(buf, l, t, true, false);
368 char *format_timestamp_us(char *buf, size_t l, usec_t t) {
369 return format_timestamp_internal(buf, l, t, false, true);
372 #if 0 /// UNNEEDED by elogind
373 char *format_timestamp_us_utc(char *buf, size_t l, usec_t t) {
374 return format_timestamp_internal(buf, l, t, true, true);
378 char *format_timestamp_relative(char *buf, size_t l, usec_t t) {
382 if (t <= 0 || t == USEC_INFINITY)
385 n = now(CLOCK_REALTIME);
394 if (d >= USEC_PER_YEAR)
395 snprintf(buf, l, USEC_FMT " years " USEC_FMT " months %s",
397 (d % USEC_PER_YEAR) / USEC_PER_MONTH, s);
398 else if (d >= USEC_PER_MONTH)
399 snprintf(buf, l, USEC_FMT " months " USEC_FMT " days %s",
401 (d % USEC_PER_MONTH) / USEC_PER_DAY, s);
402 else if (d >= USEC_PER_WEEK)
403 snprintf(buf, l, USEC_FMT " weeks " USEC_FMT " days %s",
405 (d % USEC_PER_WEEK) / USEC_PER_DAY, s);
406 else if (d >= 2*USEC_PER_DAY)
407 snprintf(buf, l, USEC_FMT " days %s", d / USEC_PER_DAY, s);
408 else if (d >= 25*USEC_PER_HOUR)
409 snprintf(buf, l, "1 day " USEC_FMT "h %s",
410 (d - USEC_PER_DAY) / USEC_PER_HOUR, s);
411 else if (d >= 6*USEC_PER_HOUR)
412 snprintf(buf, l, USEC_FMT "h %s",
413 d / USEC_PER_HOUR, s);
414 else if (d >= USEC_PER_HOUR)
415 snprintf(buf, l, USEC_FMT "h " USEC_FMT "min %s",
417 (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s);
418 else if (d >= 5*USEC_PER_MINUTE)
419 snprintf(buf, l, USEC_FMT "min %s",
420 d / USEC_PER_MINUTE, s);
421 else if (d >= USEC_PER_MINUTE)
422 snprintf(buf, l, USEC_FMT "min " USEC_FMT "s %s",
424 (d % USEC_PER_MINUTE) / USEC_PER_SEC, s);
425 else if (d >= USEC_PER_SEC)
426 snprintf(buf, l, USEC_FMT "s %s",
427 d / USEC_PER_SEC, s);
428 else if (d >= USEC_PER_MSEC)
429 snprintf(buf, l, USEC_FMT "ms %s",
430 d / USEC_PER_MSEC, s);
432 snprintf(buf, l, USEC_FMT"us %s",
435 snprintf(buf, l, "now");
441 char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
442 static const struct {
446 { "y", USEC_PER_YEAR },
447 { "month", USEC_PER_MONTH },
448 { "w", USEC_PER_WEEK },
449 { "d", USEC_PER_DAY },
450 { "h", USEC_PER_HOUR },
451 { "min", USEC_PER_MINUTE },
452 { "s", USEC_PER_SEC },
453 { "ms", USEC_PER_MSEC },
459 bool something = false;
464 if (t == USEC_INFINITY) {
465 strncpy(p, "infinity", l-1);
471 strncpy(p, "0", l-1);
476 /* The result of this function can be parsed with parse_sec */
478 for (i = 0; i < ELEMENTSOF(table); i++) {
487 if (t < accuracy && something)
490 if (t < table[i].usec)
496 a = t / table[i].usec;
497 b = t % table[i].usec;
499 /* Let's see if we should shows this in dot notation */
500 if (t < USEC_PER_MINUTE && b > 0) {
505 for (cc = table[i].usec; cc > 1; cc /= 10)
508 for (cc = accuracy; cc > 1; cc /= 10) {
515 "%s"USEC_FMT".%0*"PRI_USEC"%s",
527 /* No? Then let's show it normally */
538 n = MIN((size_t) k, l);
551 #if 0 /// UNNEEDED by elogind
552 void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) {
558 if (!dual_timestamp_is_set(t))
561 fprintf(f, "%s="USEC_FMT" "USEC_FMT"\n",
567 int dual_timestamp_deserialize(const char *value, dual_timestamp *t) {
574 pos = strspn(value, WHITESPACE);
575 if (value[pos] == '-')
577 pos += strspn(value + pos, DIGITS);
578 pos += strspn(value + pos, WHITESPACE);
579 if (value[pos] == '-')
582 r = sscanf(value, "%" PRIu64 "%" PRIu64 "%n", &a, &b, &pos);
584 log_debug("Failed to parse dual timestamp value \"%s\".", value);
588 if (value[pos] != '\0')
589 /* trailing garbage */
599 int timestamp_deserialize(const char *value, usec_t *timestamp) {
604 r = safe_atou64(value, timestamp);
606 return log_debug_errno(r, "Failed to parse timestamp value \"%s\": %m", value);
611 #if 0 /// UNNEEDED by elogind
612 static int parse_timestamp_impl(const char *t, usec_t *usec, bool with_tz) {
613 static const struct {
633 const char *k, *utc = NULL, *tzn = NULL;
636 usec_t x_usec, plus = 0, minus = 0, ret;
637 int r, weekday = -1, dst = -1;
643 * 2012-09-22 16:34:22
644 * 2012-09-22 16:34 (seconds will be set to 0)
645 * 2012-09-22 (time will be set to 00:00:00)
646 * 16:34:22 (date will be set to today)
647 * 16:34 (date will be set to today, seconds to 0)
649 * yesterday (time is set to 00:00:00)
650 * today (time is set to 00:00:00)
651 * tomorrow (time is set to 00:00:00)
654 * @2147483647 (seconds since epoch)
661 if (t[0] == '@' && !with_tz)
662 return parse_sec(t + 1, usec);
664 ret = now(CLOCK_REALTIME);
670 else if (t[0] == '+') {
671 r = parse_sec(t+1, &plus);
677 } else if (t[0] == '-') {
678 r = parse_sec(t+1, &minus);
684 } else if ((k = endswith(t, " ago"))) {
685 t = strndupa(t, k - t);
687 r = parse_sec(t, &minus);
693 } else if ((k = endswith(t, " left"))) {
694 t = strndupa(t, k - t);
696 r = parse_sec(t, &plus);
703 /* See if the timestamp is suffixed with UTC */
704 utc = endswith_no_case(t, " UTC");
706 t = strndupa(t, utc - t);
708 const char *e = NULL;
713 /* See if the timestamp is suffixed by either the DST or non-DST local timezone. Note that we only
714 * support the local timezones here, nothing else. Not because we wouldn't want to, but simply because
715 * there are no nice APIs available to cover this. By accepting the local time zone strings, we make
716 * sure that all timestamps written by format_timestamp() can be parsed correctly, even though we don't
717 * support arbitrary timezone specifications. */
719 for (j = 0; j <= 1; j++) {
721 if (isempty(tzname[j]))
724 e = endswith_no_case(t, tzname[j]);
735 if (IN_SET(j, 0, 1)) {
736 /* Found one of the two timezones specified. */
737 t = strndupa(t, e - t - 1);
744 x = (time_t) (ret / USEC_PER_SEC);
747 if (!localtime_or_gmtime_r(&x, &tm, utc))
756 if (streq(t, "today")) {
757 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
760 } else if (streq(t, "yesterday")) {
762 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
765 } else if (streq(t, "tomorrow")) {
767 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
771 for (i = 0; i < ELEMENTSOF(day_nr); i++) {
774 if (!startswith_no_case(t, day_nr[i].name))
777 skip = strlen(day_nr[i].name);
781 weekday = day_nr[i].nr;
787 k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
796 k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm);
805 k = strptime(t, "%y-%m-%d %H:%M", &tm);
812 k = strptime(t, "%Y-%m-%d %H:%M", &tm);
819 k = strptime(t, "%y-%m-%d", &tm);
821 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
826 k = strptime(t, "%Y-%m-%d", &tm);
828 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
833 k = strptime(t, "%H:%M:%S", &tm);
842 k = strptime(t, "%H:%M", &tm);
855 r = parse_fractional_part_u(&k, 6, &add);
866 x = mktime_or_timegm(&tm, utc);
870 if (weekday >= 0 && tm.tm_wday != weekday)
873 ret = (usec_t) x * USEC_PER_SEC + x_usec;
874 if (ret > USEC_TIMESTAMP_FORMATTABLE_MAX)
878 if (ret + plus < ret) /* overflow? */
881 if (ret > USEC_TIMESTAMP_FORMATTABLE_MAX)
895 typedef struct ParseTimestampResult {
898 } ParseTimestampResult;
900 int parse_timestamp(const char *t, usec_t *usec) {
901 char *last_space, *timezone = NULL;
902 ParseTimestampResult *shared, tmp;
906 last_space = strrchr(t, ' ');
908 if (last_space != NULL) {
909 if (timezone_is_valid(last_space + 1)) {
910 timezone = last_space + 1;
914 if (timezone == NULL || endswith_no_case(t, " UTC"))
915 return parse_timestamp_impl(t, usec, false);
917 t = strndupa(t, last_space - t);
919 shared = mmap(NULL, sizeof *shared, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
920 if (shared == MAP_FAILED)
921 return negative_errno();
926 int fork_errno = errno;
927 (void) munmap(shared, sizeof *shared);
932 if (setenv("TZ", timezone, 1) != 0) {
933 shared->return_value = negative_errno();
939 shared->return_value = parse_timestamp_impl(t, &shared->usec, true);
944 r = wait_for_terminate(pid, NULL);
946 (void) munmap(shared, sizeof *shared);
951 if (munmap(shared, sizeof *shared) != 0)
952 return negative_errno();
954 if (tmp.return_value == 0)
957 return tmp.return_value;
960 static char* extract_multiplier(char *p, usec_t *multiplier) {
961 static const struct {
965 { "seconds", USEC_PER_SEC },
966 { "second", USEC_PER_SEC },
967 { "sec", USEC_PER_SEC },
968 { "s", USEC_PER_SEC },
969 { "minutes", USEC_PER_MINUTE },
970 { "minute", USEC_PER_MINUTE },
971 { "min", USEC_PER_MINUTE },
972 { "months", USEC_PER_MONTH },
973 { "month", USEC_PER_MONTH },
974 { "M", USEC_PER_MONTH },
975 { "msec", USEC_PER_MSEC },
976 { "ms", USEC_PER_MSEC },
977 { "m", USEC_PER_MINUTE },
978 { "hours", USEC_PER_HOUR },
979 { "hour", USEC_PER_HOUR },
980 { "hr", USEC_PER_HOUR },
981 { "h", USEC_PER_HOUR },
982 { "days", USEC_PER_DAY },
983 { "day", USEC_PER_DAY },
984 { "d", USEC_PER_DAY },
985 { "weeks", USEC_PER_WEEK },
986 { "week", USEC_PER_WEEK },
987 { "w", USEC_PER_WEEK },
988 { "years", USEC_PER_YEAR },
989 { "year", USEC_PER_YEAR },
990 { "y", USEC_PER_YEAR },
997 for (i = 0; i < ELEMENTSOF(table); i++) {
1000 e = startswith(p, table[i].suffix);
1002 *multiplier = table[i].usec;
1010 int parse_time(const char *t, usec_t *usec, usec_t default_unit) {
1013 bool something = false;
1017 assert(default_unit > 0);
1021 p += strspn(p, WHITESPACE);
1022 s = startswith(p, "infinity");
1024 s += strspn(s, WHITESPACE);
1028 *usec = USEC_INFINITY;
1036 usec_t multiplier = default_unit, k;
1038 p += strspn(p, WHITESPACE);
1048 l = strtoll(p, &e, 10);
1058 z = strtoll(b, &e, 10);
1073 e += strspn(e, WHITESPACE);
1074 p = extract_multiplier(e, &multiplier);
1078 k = (usec_t) z * multiplier;
1083 r += (usec_t) l * multiplier + k;
1091 int parse_sec(const char *t, usec_t *usec) {
1092 return parse_time(t, usec, USEC_PER_SEC);
1095 #if 0 /// UNNEEDED by elogind
1096 int parse_sec_fix_0(const char *t, usec_t *usec) {
1097 t += strspn(t, WHITESPACE);
1098 if (streq(t, "0")) {
1099 *usec = USEC_INFINITY;
1103 return parse_sec(t, usec);
1106 int parse_nsec(const char *t, nsec_t *nsec) {
1107 static const struct {
1111 { "seconds", NSEC_PER_SEC },
1112 { "second", NSEC_PER_SEC },
1113 { "sec", NSEC_PER_SEC },
1114 { "s", NSEC_PER_SEC },
1115 { "minutes", NSEC_PER_MINUTE },
1116 { "minute", NSEC_PER_MINUTE },
1117 { "min", NSEC_PER_MINUTE },
1118 { "months", NSEC_PER_MONTH },
1119 { "month", NSEC_PER_MONTH },
1120 { "msec", NSEC_PER_MSEC },
1121 { "ms", NSEC_PER_MSEC },
1122 { "m", NSEC_PER_MINUTE },
1123 { "hours", NSEC_PER_HOUR },
1124 { "hour", NSEC_PER_HOUR },
1125 { "hr", NSEC_PER_HOUR },
1126 { "h", NSEC_PER_HOUR },
1127 { "days", NSEC_PER_DAY },
1128 { "day", NSEC_PER_DAY },
1129 { "d", NSEC_PER_DAY },
1130 { "weeks", NSEC_PER_WEEK },
1131 { "week", NSEC_PER_WEEK },
1132 { "w", NSEC_PER_WEEK },
1133 { "years", NSEC_PER_YEAR },
1134 { "year", NSEC_PER_YEAR },
1135 { "y", NSEC_PER_YEAR },
1136 { "usec", NSEC_PER_USEC },
1137 { "us", NSEC_PER_USEC },
1138 { "µs", NSEC_PER_USEC },
1141 { "", 1ULL }, /* default is nsec */
1146 bool something = false;
1153 p += strspn(p, WHITESPACE);
1154 s = startswith(p, "infinity");
1156 s += strspn(s, WHITESPACE);
1160 *nsec = NSEC_INFINITY;
1169 p += strspn(p, WHITESPACE);
1179 l = strtoll(p, &e, 10);
1191 z = strtoll(b, &e, 10);
1206 e += strspn(e, WHITESPACE);
1208 for (i = 0; i < ELEMENTSOF(table); i++)
1209 if (startswith(e, table[i].suffix)) {
1210 nsec_t k = (nsec_t) z * table[i].nsec;
1215 r += (nsec_t) l * table[i].nsec + k;
1216 p = e + strlen(table[i].suffix);
1222 if (i >= ELEMENTSOF(table))
1232 bool ntp_synced(void) {
1233 struct timex txc = {};
1235 if (adjtimex(&txc) < 0)
1238 if (txc.status & STA_UNSYNC)
1244 int get_timezones(char ***ret) {
1245 _cleanup_fclose_ FILE *f = NULL;
1246 _cleanup_strv_free_ char **zones = NULL;
1247 size_t n_zones = 0, n_allocated = 0;
1251 zones = strv_new("UTC", NULL);
1258 f = fopen("/usr/share/zoneinfo/zone.tab", "re");
1262 FOREACH_LINE(l, f, return -errno) {
1268 if (isempty(p) || *p == '#')
1271 /* Skip over country code */
1272 p += strcspn(p, WHITESPACE);
1273 p += strspn(p, WHITESPACE);
1275 /* Skip over coordinates */
1276 p += strcspn(p, WHITESPACE);
1277 p += strspn(p, WHITESPACE);
1279 /* Found timezone name */
1280 k = strcspn(p, WHITESPACE);
1288 if (!GREEDY_REALLOC(zones, n_allocated, n_zones + 2)) {
1293 zones[n_zones++] = w;
1294 zones[n_zones] = NULL;
1299 } else if (errno != ENOENT)
1308 bool timezone_is_valid(const char *name) {
1319 for (p = name; *p; p++) {
1320 if (!(*p >= '0' && *p <= '9') &&
1321 !(*p >= 'a' && *p <= 'z') &&
1322 !(*p >= 'A' && *p <= 'Z') &&
1323 !(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
1339 t = strjoina("/usr/share/zoneinfo/", name);
1340 if (stat(t, &st) < 0)
1343 if (!S_ISREG(st.st_mode))
1350 bool clock_boottime_supported(void) {
1351 static int supported = -1;
1353 /* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */
1355 if (supported < 0) {
1358 fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC);
1370 #if 0 /// UNNEEDED by elogind
1371 clockid_t clock_boottime_or_monotonic(void) {
1372 if (clock_boottime_supported())
1373 return CLOCK_BOOTTIME;
1375 return CLOCK_MONOTONIC;
1379 bool clock_supported(clockid_t clock) {
1384 case CLOCK_MONOTONIC:
1385 case CLOCK_REALTIME:
1388 case CLOCK_BOOTTIME:
1389 return clock_boottime_supported();
1391 case CLOCK_BOOTTIME_ALARM:
1392 if (!clock_boottime_supported())
1398 /* For everything else, check properly */
1399 return clock_gettime(clock, &ts) >= 0;
1403 #if 0 /// UNNEEDED by elogind
1404 int get_timezone(char **tz) {
1405 _cleanup_free_ char *t = NULL;
1410 r = readlink_malloc("/etc/localtime", &t);
1412 return r; /* returns EINVAL if not a symlink */
1414 e = path_startswith(t, "/usr/share/zoneinfo/");
1416 e = path_startswith(t, "../usr/share/zoneinfo/");
1420 if (!timezone_is_valid(e))
1431 time_t mktime_or_timegm(struct tm *tm, bool utc) {
1432 return utc ? timegm(tm) : mktime(tm);
1436 struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc) {
1437 return utc ? gmtime_r(t, tm) : localtime_r(t, tm);
1440 #if 0 /// UNNEEDED by elogind
1441 unsigned long usec_to_jiffies(usec_t u) {
1442 static thread_local unsigned long hz = 0;
1446 r = sysconf(_SC_CLK_TCK);
1452 return DIV_ROUND_UP(u , USEC_PER_SEC / hz);
1455 usec_t usec_shift_clock(usec_t x, clockid_t from, clockid_t to) {
1458 if (x == USEC_INFINITY)
1459 return USEC_INFINITY;
1460 if (map_clock_id(from) == map_clock_id(to))
1467 /* x lies in the future */
1468 return usec_add(b, usec_sub_unsigned(x, a));
1470 /* x lies in the past */
1471 return usec_sub_unsigned(b, usec_sub_unsigned(a, x));