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 #if 0 /// UNNEEDED by elogind
44 static nsec_t timespec_load_nsec(const struct timespec *ts);
47 static clockid_t map_clock_id(clockid_t c) {
49 /* Some more exotic archs (s390, ppc, …) lack the "ALARM" flavour of the clocks. Thus, clock_gettime() will
50 * fail for them. Since they are essentially the same as their non-ALARM pendants (their only difference is
51 * when timers are set on them), let's just map them accordingly. This way, we can get the correct time even on
56 case CLOCK_BOOTTIME_ALARM:
57 return CLOCK_BOOTTIME;
59 case CLOCK_REALTIME_ALARM:
60 return CLOCK_REALTIME;
67 usec_t now(clockid_t clock_id) {
70 assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
72 return timespec_load(&ts);
75 #if 0 /// UNNEEDED by elogind
76 nsec_t now_nsec(clockid_t clock_id) {
79 assert_se(clock_gettime(map_clock_id(clock_id), &ts) == 0);
81 return timespec_load_nsec(&ts);
85 dual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
88 ts->realtime = now(CLOCK_REALTIME);
89 ts->monotonic = now(CLOCK_MONOTONIC);
94 triple_timestamp* triple_timestamp_get(triple_timestamp *ts) {
97 ts->realtime = now(CLOCK_REALTIME);
98 ts->monotonic = now(CLOCK_MONOTONIC);
99 ts->boottime = clock_boottime_supported() ? now(CLOCK_BOOTTIME) : USEC_INFINITY;
104 dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
108 if (u == USEC_INFINITY || u <= 0) {
109 ts->realtime = ts->monotonic = u;
115 delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u;
116 ts->monotonic = usec_sub(now(CLOCK_MONOTONIC), delta);
121 #if 0 /// UNNEEDED by elogind
122 triple_timestamp* triple_timestamp_from_realtime(triple_timestamp *ts, usec_t u) {
127 if (u == USEC_INFINITY || u <= 0) {
128 ts->realtime = ts->monotonic = ts->boottime = u;
133 delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u;
134 ts->monotonic = usec_sub(now(CLOCK_MONOTONIC), delta);
135 ts->boottime = clock_boottime_supported() ? usec_sub(now(CLOCK_BOOTTIME), delta) : USEC_INFINITY;
140 dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
144 if (u == USEC_INFINITY) {
145 ts->realtime = ts->monotonic = USEC_INFINITY;
150 delta = (int64_t) now(CLOCK_MONOTONIC) - (int64_t) u;
151 ts->realtime = usec_sub(now(CLOCK_REALTIME), delta);
156 dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, usec_t u) {
159 if (u == USEC_INFINITY) {
160 ts->realtime = ts->monotonic = USEC_INFINITY;
164 dual_timestamp_get(ts);
165 delta = (int64_t) now(clock_boottime_or_monotonic()) - (int64_t) u;
166 ts->realtime = usec_sub(ts->realtime, delta);
167 ts->monotonic = usec_sub(ts->monotonic, delta);
173 usec_t triple_timestamp_by_clock(triple_timestamp *ts, clockid_t clock) {
178 case CLOCK_REALTIME_ALARM:
181 case CLOCK_MONOTONIC:
182 return ts->monotonic;
185 case CLOCK_BOOTTIME_ALARM:
189 return USEC_INFINITY;
193 usec_t timespec_load(const struct timespec *ts) {
196 if (ts->tv_sec == (time_t) -1 && ts->tv_nsec == (long) -1)
197 return USEC_INFINITY;
199 if ((usec_t) ts->tv_sec > (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC)
200 return USEC_INFINITY;
203 (usec_t) ts->tv_sec * USEC_PER_SEC +
204 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
207 #if 0 /// UNNEEDED by elogind
208 static nsec_t timespec_load_nsec(const struct timespec *ts) {
211 if (ts->tv_sec == (time_t) -1 && ts->tv_nsec == (long) -1)
212 return NSEC_INFINITY;
214 if ((nsec_t) ts->tv_sec >= (UINT64_MAX - ts->tv_nsec) / NSEC_PER_SEC)
215 return NSEC_INFINITY;
217 return (nsec_t) ts->tv_sec * NSEC_PER_SEC + (nsec_t) ts->tv_nsec;
221 struct timespec *timespec_store(struct timespec *ts, usec_t u) {
224 if (u == USEC_INFINITY) {
225 ts->tv_sec = (time_t) -1;
226 ts->tv_nsec = (long) -1;
230 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
231 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
236 usec_t timeval_load(const struct timeval *tv) {
239 if (tv->tv_sec == (time_t) -1 &&
240 tv->tv_usec == (suseconds_t) -1)
241 return USEC_INFINITY;
243 if ((usec_t) tv->tv_sec > (UINT64_MAX - tv->tv_usec) / USEC_PER_SEC)
244 return USEC_INFINITY;
247 (usec_t) tv->tv_sec * USEC_PER_SEC +
248 (usec_t) tv->tv_usec;
251 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
254 if (u == USEC_INFINITY) {
255 tv->tv_sec = (time_t) -1;
256 tv->tv_usec = (suseconds_t) -1;
258 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
259 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
265 static char *format_timestamp_internal(
272 /* The weekdays in non-localized (English) form. We use this instead of the localized form, so that our
273 * generated timestamps may be parsed with parse_timestamp(), and always read the same. */
274 static const char * const weekdays[] = {
292 1 + 10 + /* space and date */
293 1 + 8 + /* space and time */
294 (us ? 1 + 6 : 0) + /* "." and microsecond part */
295 1 + 1 + /* space and shortest possible zone */
297 return NULL; /* Not enough space even for the shortest form. */
298 if (t <= 0 || t == USEC_INFINITY)
299 return NULL; /* Timestamp is unset */
301 sec = (time_t) (t / USEC_PER_SEC); /* Round down */
302 if ((usec_t) sec != (t / USEC_PER_SEC))
303 return NULL; /* overflow? */
305 if (!localtime_or_gmtime_r(&sec, &tm, utc))
308 /* Start with the week day */
309 assert((size_t) tm.tm_wday < ELEMENTSOF(weekdays));
310 memcpy(buf, weekdays[tm.tm_wday], 4);
312 /* Add the main components */
313 if (strftime(buf + 3, l - 3, " %Y-%m-%d %H:%M:%S", &tm) <= 0)
314 return NULL; /* Doesn't fit */
316 /* Append the microseconds part, if that's requested */
320 return NULL; /* Microseconds part doesn't fit. */
322 sprintf(buf + n, ".%06llu", (unsigned long long) (t % USEC_PER_SEC));
325 /* Append the timezone */
328 /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r() normally uses the
329 * obsolete "GMT" instead. */
331 return NULL; /* "UTC" doesn't fit. */
333 strcpy(buf + n, " UTC");
335 } else if (!isempty(tm.tm_zone)) {
338 /* An explicit timezone is specified, let's use it, if it fits */
339 tn = strlen(tm.tm_zone);
340 if (n + 1 + tn + 1 > l) {
341 /* The full time zone does not fit in. Yuck. */
343 if (n + 1 + _POSIX_TZNAME_MAX + 1 > l)
344 return NULL; /* Not even enough space for the POSIX minimum (of 6)? In that case, complain that it doesn't fit */
346 /* So the time zone doesn't fit in fully, but the caller passed enough space for the POSIX
347 * minimum time zone length. In this case suppress the timezone entirely, in order not to dump
348 * an overly long, hard to read string on the user. This should be safe, because the user will
349 * assume the local timezone anyway if none is shown. And so does parse_timestamp(). */
352 strcpy(buf + n, tm.tm_zone);
359 char *format_timestamp(char *buf, size_t l, usec_t t) {
360 return format_timestamp_internal(buf, l, t, false, false);
363 #if 0 /// UNNEEDED by elogind
364 char *format_timestamp_utc(char *buf, size_t l, usec_t t) {
365 return format_timestamp_internal(buf, l, t, true, false);
369 char *format_timestamp_us(char *buf, size_t l, usec_t t) {
370 return format_timestamp_internal(buf, l, t, false, true);
373 #if 0 /// UNNEEDED by elogind
374 char *format_timestamp_us_utc(char *buf, size_t l, usec_t t) {
375 return format_timestamp_internal(buf, l, t, true, true);
379 char *format_timestamp_relative(char *buf, size_t l, usec_t t) {
383 if (t <= 0 || t == USEC_INFINITY)
386 n = now(CLOCK_REALTIME);
395 if (d >= USEC_PER_YEAR)
396 snprintf(buf, l, USEC_FMT " years " USEC_FMT " months %s",
398 (d % USEC_PER_YEAR) / USEC_PER_MONTH, s);
399 else if (d >= USEC_PER_MONTH)
400 snprintf(buf, l, USEC_FMT " months " USEC_FMT " days %s",
402 (d % USEC_PER_MONTH) / USEC_PER_DAY, s);
403 else if (d >= USEC_PER_WEEK)
404 snprintf(buf, l, USEC_FMT " weeks " USEC_FMT " days %s",
406 (d % USEC_PER_WEEK) / USEC_PER_DAY, s);
407 else if (d >= 2*USEC_PER_DAY)
408 snprintf(buf, l, USEC_FMT " days %s", d / USEC_PER_DAY, s);
409 else if (d >= 25*USEC_PER_HOUR)
410 snprintf(buf, l, "1 day " USEC_FMT "h %s",
411 (d - USEC_PER_DAY) / USEC_PER_HOUR, s);
412 else if (d >= 6*USEC_PER_HOUR)
413 snprintf(buf, l, USEC_FMT "h %s",
414 d / USEC_PER_HOUR, s);
415 else if (d >= USEC_PER_HOUR)
416 snprintf(buf, l, USEC_FMT "h " USEC_FMT "min %s",
418 (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s);
419 else if (d >= 5*USEC_PER_MINUTE)
420 snprintf(buf, l, USEC_FMT "min %s",
421 d / USEC_PER_MINUTE, s);
422 else if (d >= USEC_PER_MINUTE)
423 snprintf(buf, l, USEC_FMT "min " USEC_FMT "s %s",
425 (d % USEC_PER_MINUTE) / USEC_PER_SEC, s);
426 else if (d >= USEC_PER_SEC)
427 snprintf(buf, l, USEC_FMT "s %s",
428 d / USEC_PER_SEC, s);
429 else if (d >= USEC_PER_MSEC)
430 snprintf(buf, l, USEC_FMT "ms %s",
431 d / USEC_PER_MSEC, s);
433 snprintf(buf, l, USEC_FMT"us %s",
436 snprintf(buf, l, "now");
442 char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
443 static const struct {
447 { "y", USEC_PER_YEAR },
448 { "month", USEC_PER_MONTH },
449 { "w", USEC_PER_WEEK },
450 { "d", USEC_PER_DAY },
451 { "h", USEC_PER_HOUR },
452 { "min", USEC_PER_MINUTE },
453 { "s", USEC_PER_SEC },
454 { "ms", USEC_PER_MSEC },
460 bool something = false;
465 if (t == USEC_INFINITY) {
466 strncpy(p, "infinity", l-1);
472 strncpy(p, "0", l-1);
477 /* The result of this function can be parsed with parse_sec */
479 for (i = 0; i < ELEMENTSOF(table); i++) {
488 if (t < accuracy && something)
491 if (t < table[i].usec)
497 a = t / table[i].usec;
498 b = t % table[i].usec;
500 /* Let's see if we should shows this in dot notation */
501 if (t < USEC_PER_MINUTE && b > 0) {
506 for (cc = table[i].usec; cc > 1; cc /= 10)
509 for (cc = accuracy; cc > 1; cc /= 10) {
516 "%s"USEC_FMT".%0*llu%s",
520 (unsigned long long) b,
528 /* No? Then let's show it normally */
539 n = MIN((size_t) k, l);
552 #if 0 /// UNNEEDED by elogind
553 void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) {
559 if (!dual_timestamp_is_set(t))
562 fprintf(f, "%s="USEC_FMT" "USEC_FMT"\n",
568 int dual_timestamp_deserialize(const char *value, dual_timestamp *t) {
569 unsigned long long a, b;
574 if (sscanf(value, "%llu %llu", &a, &b) != 2) {
575 log_debug("Failed to parse dual timestamp value \"%s\": %m", value);
586 int timestamp_deserialize(const char *value, usec_t *timestamp) {
591 r = safe_atou64(value, timestamp);
593 return log_debug_errno(r, "Failed to parse timestamp value \"%s\": %m", value);
598 #if 0 /// UNNEEDED by elogind
599 int parse_timestamp(const char *t, usec_t *usec) {
600 static const struct {
620 const char *k, *utc, *tzn = NULL;
623 usec_t x_usec, plus = 0, minus = 0, ret;
624 int r, weekday = -1, dst = -1;
630 * 2012-09-22 16:34:22
631 * 2012-09-22 16:34 (seconds will be set to 0)
632 * 2012-09-22 (time will be set to 00:00:00)
633 * 16:34:22 (date will be set to today)
634 * 16:34 (date will be set to today, seconds to 0)
636 * yesterday (time is set to 00:00:00)
637 * today (time is set to 00:00:00)
638 * tomorrow (time is set to 00:00:00)
641 * @2147483647 (seconds since epoch)
649 return parse_sec(t + 1, usec);
651 ret = now(CLOCK_REALTIME);
656 else if (t[0] == '+') {
657 r = parse_sec(t+1, &plus);
663 } else if (t[0] == '-') {
664 r = parse_sec(t+1, &minus);
670 } else if ((k = endswith(t, " ago"))) {
671 t = strndupa(t, k - t);
673 r = parse_sec(t, &minus);
679 } else if ((k = endswith(t, " left"))) {
680 t = strndupa(t, k - t);
682 r = parse_sec(t, &plus);
689 /* See if the timestamp is suffixed with UTC */
690 utc = endswith_no_case(t, " UTC");
692 t = strndupa(t, utc - t);
694 const char *e = NULL;
699 /* See if the timestamp is suffixed by either the DST or non-DST local timezone. Note that we only
700 * support the local timezones here, nothing else. Not because we wouldn't want to, but simply because
701 * there are no nice APIs available to cover this. By accepting the local time zone strings, we make
702 * sure that all timestamps written by format_timestamp() can be parsed correctly, even though we don't
703 * support arbitrary timezone specifications. */
705 for (j = 0; j <= 1; j++) {
707 if (isempty(tzname[j]))
710 e = endswith_no_case(t, tzname[j]);
721 if (IN_SET(j, 0, 1)) {
722 /* Found one of the two timezones specified. */
723 t = strndupa(t, e - t - 1);
729 x = (time_t) (ret / USEC_PER_SEC);
732 if (!localtime_or_gmtime_r(&x, &tm, utc))
739 if (streq(t, "today")) {
740 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
743 } else if (streq(t, "yesterday")) {
745 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
748 } else if (streq(t, "tomorrow")) {
750 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
754 for (i = 0; i < ELEMENTSOF(day_nr); i++) {
757 if (!startswith_no_case(t, day_nr[i].name))
760 skip = strlen(day_nr[i].name);
764 weekday = day_nr[i].nr;
770 k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
779 k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm);
788 k = strptime(t, "%y-%m-%d %H:%M", &tm);
795 k = strptime(t, "%Y-%m-%d %H:%M", &tm);
802 k = strptime(t, "%y-%m-%d", &tm);
804 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
809 k = strptime(t, "%Y-%m-%d", &tm);
811 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
816 k = strptime(t, "%H:%M:%S", &tm);
825 k = strptime(t, "%H:%M", &tm);
838 r = parse_fractional_part_u(&k, 6, &add);
849 x = mktime_or_timegm(&tm, utc);
850 if (x == (time_t) -1)
853 if (weekday >= 0 && tm.tm_wday != weekday)
856 ret = (usec_t) x * USEC_PER_SEC + x_usec;
871 static char* extract_multiplier(char *p, usec_t *multiplier) {
872 static const struct {
876 { "seconds", USEC_PER_SEC },
877 { "second", USEC_PER_SEC },
878 { "sec", USEC_PER_SEC },
879 { "s", USEC_PER_SEC },
880 { "minutes", USEC_PER_MINUTE },
881 { "minute", USEC_PER_MINUTE },
882 { "min", USEC_PER_MINUTE },
883 { "months", USEC_PER_MONTH },
884 { "month", USEC_PER_MONTH },
885 { "M", USEC_PER_MONTH },
886 { "msec", USEC_PER_MSEC },
887 { "ms", USEC_PER_MSEC },
888 { "m", USEC_PER_MINUTE },
889 { "hours", USEC_PER_HOUR },
890 { "hour", USEC_PER_HOUR },
891 { "hr", USEC_PER_HOUR },
892 { "h", USEC_PER_HOUR },
893 { "days", USEC_PER_DAY },
894 { "day", USEC_PER_DAY },
895 { "d", USEC_PER_DAY },
896 { "weeks", USEC_PER_WEEK },
897 { "week", USEC_PER_WEEK },
898 { "w", USEC_PER_WEEK },
899 { "years", USEC_PER_YEAR },
900 { "year", USEC_PER_YEAR },
901 { "y", USEC_PER_YEAR },
907 for (i = 0; i < ELEMENTSOF(table); i++) {
910 e = startswith(p, table[i].suffix);
912 *multiplier = table[i].usec;
920 int parse_time(const char *t, usec_t *usec, usec_t default_unit) {
923 bool something = false;
927 assert(default_unit > 0);
931 p += strspn(p, WHITESPACE);
932 s = startswith(p, "infinity");
934 s += strspn(s, WHITESPACE);
938 *usec = USEC_INFINITY;
946 usec_t multiplier = default_unit, k;
948 p += strspn(p, WHITESPACE);
958 l = strtoll(p, &e, 10);
968 z = strtoll(b, &e, 10);
983 e += strspn(e, WHITESPACE);
984 p = extract_multiplier(e, &multiplier);
988 k = (usec_t) z * multiplier;
993 r += (usec_t) l * multiplier + k;
1001 int parse_sec(const char *t, usec_t *usec) {
1002 return parse_time(t, usec, USEC_PER_SEC);
1005 #if 0 /// UNNEEDED by elogind
1006 int parse_nsec(const char *t, nsec_t *nsec) {
1007 static const struct {
1011 { "seconds", NSEC_PER_SEC },
1012 { "second", NSEC_PER_SEC },
1013 { "sec", NSEC_PER_SEC },
1014 { "s", NSEC_PER_SEC },
1015 { "minutes", NSEC_PER_MINUTE },
1016 { "minute", NSEC_PER_MINUTE },
1017 { "min", NSEC_PER_MINUTE },
1018 { "months", NSEC_PER_MONTH },
1019 { "month", NSEC_PER_MONTH },
1020 { "msec", NSEC_PER_MSEC },
1021 { "ms", NSEC_PER_MSEC },
1022 { "m", NSEC_PER_MINUTE },
1023 { "hours", NSEC_PER_HOUR },
1024 { "hour", NSEC_PER_HOUR },
1025 { "hr", NSEC_PER_HOUR },
1026 { "h", NSEC_PER_HOUR },
1027 { "days", NSEC_PER_DAY },
1028 { "day", NSEC_PER_DAY },
1029 { "d", NSEC_PER_DAY },
1030 { "weeks", NSEC_PER_WEEK },
1031 { "week", NSEC_PER_WEEK },
1032 { "w", NSEC_PER_WEEK },
1033 { "years", NSEC_PER_YEAR },
1034 { "year", NSEC_PER_YEAR },
1035 { "y", NSEC_PER_YEAR },
1036 { "usec", NSEC_PER_USEC },
1037 { "us", NSEC_PER_USEC },
1040 { "", 1ULL }, /* default is nsec */
1045 bool something = false;
1052 p += strspn(p, WHITESPACE);
1053 s = startswith(p, "infinity");
1055 s += strspn(s, WHITESPACE);
1059 *nsec = NSEC_INFINITY;
1068 p += strspn(p, WHITESPACE);
1078 l = strtoll(p, &e, 10);
1090 z = strtoll(b, &e, 10);
1105 e += strspn(e, WHITESPACE);
1107 for (i = 0; i < ELEMENTSOF(table); i++)
1108 if (startswith(e, table[i].suffix)) {
1109 nsec_t k = (nsec_t) z * table[i].nsec;
1114 r += (nsec_t) l * table[i].nsec + k;
1115 p = e + strlen(table[i].suffix);
1121 if (i >= ELEMENTSOF(table))
1131 bool ntp_synced(void) {
1132 struct timex txc = {};
1134 if (adjtimex(&txc) < 0)
1137 if (txc.status & STA_UNSYNC)
1143 int get_timezones(char ***ret) {
1144 _cleanup_fclose_ FILE *f = NULL;
1145 _cleanup_strv_free_ char **zones = NULL;
1146 size_t n_zones = 0, n_allocated = 0;
1150 zones = strv_new("UTC", NULL);
1157 f = fopen("/usr/share/zoneinfo/zone.tab", "re");
1161 FOREACH_LINE(l, f, return -errno) {
1167 if (isempty(p) || *p == '#')
1170 /* Skip over country code */
1171 p += strcspn(p, WHITESPACE);
1172 p += strspn(p, WHITESPACE);
1174 /* Skip over coordinates */
1175 p += strcspn(p, WHITESPACE);
1176 p += strspn(p, WHITESPACE);
1178 /* Found timezone name */
1179 k = strcspn(p, WHITESPACE);
1187 if (!GREEDY_REALLOC(zones, n_allocated, n_zones + 2)) {
1192 zones[n_zones++] = w;
1193 zones[n_zones] = NULL;
1198 } else if (errno != ENOENT)
1207 bool timezone_is_valid(const char *name) {
1218 for (p = name; *p; p++) {
1219 if (!(*p >= '0' && *p <= '9') &&
1220 !(*p >= 'a' && *p <= 'z') &&
1221 !(*p >= 'A' && *p <= 'Z') &&
1222 !(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
1238 t = strjoina("/usr/share/zoneinfo/", name);
1239 if (stat(t, &st) < 0)
1242 if (!S_ISREG(st.st_mode))
1249 bool clock_boottime_supported(void) {
1250 static int supported = -1;
1252 /* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */
1254 if (supported < 0) {
1257 fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC);
1269 #if 0 /// UNNEEDED by elogind
1270 clockid_t clock_boottime_or_monotonic(void) {
1271 if (clock_boottime_supported())
1272 return CLOCK_BOOTTIME;
1274 return CLOCK_MONOTONIC;
1278 bool clock_supported(clockid_t clock) {
1283 case CLOCK_MONOTONIC:
1284 case CLOCK_REALTIME:
1287 case CLOCK_BOOTTIME:
1288 return clock_boottime_supported();
1290 case CLOCK_BOOTTIME_ALARM:
1291 if (!clock_boottime_supported())
1294 /* fall through, after checking the cached value for CLOCK_BOOTTIME. */
1297 /* For everything else, check properly */
1298 return clock_gettime(clock, &ts) >= 0;
1302 #if 0 /// UNNEEDED by elogind
1303 int get_timezone(char **tz) {
1304 _cleanup_free_ char *t = NULL;
1309 r = readlink_malloc("/etc/localtime", &t);
1311 return r; /* returns EINVAL if not a symlink */
1313 e = path_startswith(t, "/usr/share/zoneinfo/");
1315 e = path_startswith(t, "../usr/share/zoneinfo/");
1319 if (!timezone_is_valid(e))
1330 time_t mktime_or_timegm(struct tm *tm, bool utc) {
1331 return utc ? timegm(tm) : mktime(tm);
1335 struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc) {
1336 return utc ? gmtime_r(t, tm) : localtime_r(t, tm);
1339 #if 0 /// UNNEEDED by elogind
1340 unsigned long usec_to_jiffies(usec_t u) {
1341 static thread_local unsigned long hz = 0;
1345 r = sysconf(_SC_CLK_TCK);
1348 hz = (unsigned long) r;
1351 return DIV_ROUND_UP(u , USEC_PER_SEC / hz);