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(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(now(CLOCK_MONOTONIC), delta);
131 ts->boottime = clock_boottime_supported() ? usec_sub(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(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(ts->realtime, delta);
163 ts->monotonic = usec_sub(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 == (time_t) -1 && ts->tv_nsec == (long) -1)
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 == (time_t) -1 && ts->tv_nsec == (long) -1)
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 ts->tv_sec = (time_t) -1;
222 ts->tv_nsec = (long) -1;
226 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
227 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
232 usec_t timeval_load(const struct timeval *tv) {
235 if (tv->tv_sec == (time_t) -1 &&
236 tv->tv_usec == (suseconds_t) -1)
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 tv->tv_sec = (time_t) -1;
252 tv->tv_usec = (suseconds_t) -1;
254 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
255 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
261 static char *format_timestamp_internal(
268 /* The weekdays in non-localized (English) form. We use this instead of the localized form, so that our
269 * generated timestamps may be parsed with parse_timestamp(), and always read the same. */
270 static const char * const weekdays[] = {
288 1 + 10 + /* space and date */
289 1 + 8 + /* space and time */
290 (us ? 1 + 6 : 0) + /* "." and microsecond part */
291 1 + 1 + /* space and shortest possible zone */
293 return NULL; /* Not enough space even for the shortest form. */
294 if (t <= 0 || t == USEC_INFINITY)
295 return NULL; /* Timestamp is unset */
297 sec = (time_t) (t / USEC_PER_SEC); /* Round down */
298 if ((usec_t) sec != (t / USEC_PER_SEC))
299 return NULL; /* overflow? */
301 if (!localtime_or_gmtime_r(&sec, &tm, utc))
304 /* Start with the week day */
305 assert((size_t) tm.tm_wday < ELEMENTSOF(weekdays));
306 memcpy(buf, weekdays[tm.tm_wday], 4);
308 /* Add the main components */
309 if (strftime(buf + 3, l - 3, " %Y-%m-%d %H:%M:%S", &tm) <= 0)
310 return NULL; /* Doesn't fit */
312 /* Append the microseconds part, if that's requested */
316 return NULL; /* Microseconds part doesn't fit. */
318 sprintf(buf + n, ".%06"PRI_USEC, t % USEC_PER_SEC);
321 /* Append the timezone */
324 /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r() normally uses the
325 * obsolete "GMT" instead. */
327 return NULL; /* "UTC" doesn't fit. */
329 strcpy(buf + n, " UTC");
331 } else if (!isempty(tm.tm_zone)) {
334 /* An explicit timezone is specified, let's use it, if it fits */
335 tn = strlen(tm.tm_zone);
336 if (n + 1 + tn + 1 > l) {
337 /* The full time zone does not fit in. Yuck. */
339 if (n + 1 + _POSIX_TZNAME_MAX + 1 > l)
340 return NULL; /* Not even enough space for the POSIX minimum (of 6)? In that case, complain that it doesn't fit */
342 /* So the time zone doesn't fit in fully, but the caller passed enough space for the POSIX
343 * minimum time zone length. In this case suppress the timezone entirely, in order not to dump
344 * an overly long, hard to read string on the user. This should be safe, because the user will
345 * assume the local timezone anyway if none is shown. And so does parse_timestamp(). */
348 strcpy(buf + n, tm.tm_zone);
355 char *format_timestamp(char *buf, size_t l, usec_t t) {
356 return format_timestamp_internal(buf, l, t, false, false);
359 #if 0 /// UNNEEDED by elogind
360 char *format_timestamp_utc(char *buf, size_t l, usec_t t) {
361 return format_timestamp_internal(buf, l, t, true, false);
365 char *format_timestamp_us(char *buf, size_t l, usec_t t) {
366 return format_timestamp_internal(buf, l, t, false, true);
369 #if 0 /// UNNEEDED by elogind
370 char *format_timestamp_us_utc(char *buf, size_t l, usec_t t) {
371 return format_timestamp_internal(buf, l, t, true, true);
375 char *format_timestamp_relative(char *buf, size_t l, usec_t t) {
379 if (t <= 0 || t == USEC_INFINITY)
382 n = now(CLOCK_REALTIME);
391 if (d >= USEC_PER_YEAR)
392 snprintf(buf, l, USEC_FMT " years " USEC_FMT " months %s",
394 (d % USEC_PER_YEAR) / USEC_PER_MONTH, s);
395 else if (d >= USEC_PER_MONTH)
396 snprintf(buf, l, USEC_FMT " months " USEC_FMT " days %s",
398 (d % USEC_PER_MONTH) / USEC_PER_DAY, s);
399 else if (d >= USEC_PER_WEEK)
400 snprintf(buf, l, USEC_FMT " weeks " USEC_FMT " days %s",
402 (d % USEC_PER_WEEK) / USEC_PER_DAY, s);
403 else if (d >= 2*USEC_PER_DAY)
404 snprintf(buf, l, USEC_FMT " days %s", d / USEC_PER_DAY, s);
405 else if (d >= 25*USEC_PER_HOUR)
406 snprintf(buf, l, "1 day " USEC_FMT "h %s",
407 (d - USEC_PER_DAY) / USEC_PER_HOUR, s);
408 else if (d >= 6*USEC_PER_HOUR)
409 snprintf(buf, l, USEC_FMT "h %s",
410 d / USEC_PER_HOUR, s);
411 else if (d >= USEC_PER_HOUR)
412 snprintf(buf, l, USEC_FMT "h " USEC_FMT "min %s",
414 (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s);
415 else if (d >= 5*USEC_PER_MINUTE)
416 snprintf(buf, l, USEC_FMT "min %s",
417 d / USEC_PER_MINUTE, s);
418 else if (d >= USEC_PER_MINUTE)
419 snprintf(buf, l, USEC_FMT "min " USEC_FMT "s %s",
421 (d % USEC_PER_MINUTE) / USEC_PER_SEC, s);
422 else if (d >= USEC_PER_SEC)
423 snprintf(buf, l, USEC_FMT "s %s",
424 d / USEC_PER_SEC, s);
425 else if (d >= USEC_PER_MSEC)
426 snprintf(buf, l, USEC_FMT "ms %s",
427 d / USEC_PER_MSEC, s);
429 snprintf(buf, l, USEC_FMT"us %s",
432 snprintf(buf, l, "now");
438 char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
439 static const struct {
443 { "y", USEC_PER_YEAR },
444 { "month", USEC_PER_MONTH },
445 { "w", USEC_PER_WEEK },
446 { "d", USEC_PER_DAY },
447 { "h", USEC_PER_HOUR },
448 { "min", USEC_PER_MINUTE },
449 { "s", USEC_PER_SEC },
450 { "ms", USEC_PER_MSEC },
456 bool something = false;
461 if (t == USEC_INFINITY) {
462 strncpy(p, "infinity", l-1);
468 strncpy(p, "0", l-1);
473 /* The result of this function can be parsed with parse_sec */
475 for (i = 0; i < ELEMENTSOF(table); i++) {
484 if (t < accuracy && something)
487 if (t < table[i].usec)
493 a = t / table[i].usec;
494 b = t % table[i].usec;
496 /* Let's see if we should shows this in dot notation */
497 if (t < USEC_PER_MINUTE && b > 0) {
502 for (cc = table[i].usec; cc > 1; cc /= 10)
505 for (cc = accuracy; cc > 1; cc /= 10) {
512 "%s"USEC_FMT".%0*"PRI_USEC"%s",
524 /* No? Then let's show it normally */
535 n = MIN((size_t) k, l);
548 #if 0 /// UNNEEDED by elogind
549 void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) {
555 if (!dual_timestamp_is_set(t))
558 fprintf(f, "%s="USEC_FMT" "USEC_FMT"\n",
564 int dual_timestamp_deserialize(const char *value, dual_timestamp *t) {
565 unsigned long long a, b;
570 if (sscanf(value, "%llu %llu", &a, &b) != 2) {
571 log_debug("Failed to parse dual timestamp value \"%s\": %m", value);
582 int timestamp_deserialize(const char *value, usec_t *timestamp) {
587 r = safe_atou64(value, timestamp);
589 return log_debug_errno(r, "Failed to parse timestamp value \"%s\": %m", value);
594 #if 0 /// UNNEEDED by elogind
595 int parse_timestamp(const char *t, usec_t *usec) {
596 static const struct {
616 const char *k, *utc, *tzn = NULL;
619 usec_t x_usec, plus = 0, minus = 0, ret;
620 int r, weekday = -1, dst = -1;
626 * 2012-09-22 16:34:22
627 * 2012-09-22 16:34 (seconds will be set to 0)
628 * 2012-09-22 (time will be set to 00:00:00)
629 * 16:34:22 (date will be set to today)
630 * 16:34 (date will be set to today, seconds to 0)
632 * yesterday (time is set to 00:00:00)
633 * today (time is set to 00:00:00)
634 * tomorrow (time is set to 00:00:00)
637 * @2147483647 (seconds since epoch)
645 return parse_sec(t + 1, usec);
647 ret = now(CLOCK_REALTIME);
652 else if (t[0] == '+') {
653 r = parse_sec(t+1, &plus);
659 } else if (t[0] == '-') {
660 r = parse_sec(t+1, &minus);
666 } else if ((k = endswith(t, " ago"))) {
667 t = strndupa(t, k - t);
669 r = parse_sec(t, &minus);
675 } else if ((k = endswith(t, " left"))) {
676 t = strndupa(t, k - t);
678 r = parse_sec(t, &plus);
685 /* See if the timestamp is suffixed with UTC */
686 utc = endswith_no_case(t, " UTC");
688 t = strndupa(t, utc - t);
690 const char *e = NULL;
695 /* See if the timestamp is suffixed by either the DST or non-DST local timezone. Note that we only
696 * support the local timezones here, nothing else. Not because we wouldn't want to, but simply because
697 * there are no nice APIs available to cover this. By accepting the local time zone strings, we make
698 * sure that all timestamps written by format_timestamp() can be parsed correctly, even though we don't
699 * support arbitrary timezone specifications. */
701 for (j = 0; j <= 1; j++) {
703 if (isempty(tzname[j]))
706 e = endswith_no_case(t, tzname[j]);
717 if (IN_SET(j, 0, 1)) {
718 /* Found one of the two timezones specified. */
719 t = strndupa(t, e - t - 1);
725 x = (time_t) (ret / USEC_PER_SEC);
728 if (!localtime_or_gmtime_r(&x, &tm, utc))
735 if (streq(t, "today")) {
736 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
739 } else if (streq(t, "yesterday")) {
741 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
744 } else if (streq(t, "tomorrow")) {
746 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
750 for (i = 0; i < ELEMENTSOF(day_nr); i++) {
753 if (!startswith_no_case(t, day_nr[i].name))
756 skip = strlen(day_nr[i].name);
760 weekday = day_nr[i].nr;
766 k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
775 k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm);
784 k = strptime(t, "%y-%m-%d %H:%M", &tm);
791 k = strptime(t, "%Y-%m-%d %H:%M", &tm);
798 k = strptime(t, "%y-%m-%d", &tm);
800 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
805 k = strptime(t, "%Y-%m-%d", &tm);
807 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
812 k = strptime(t, "%H:%M:%S", &tm);
821 k = strptime(t, "%H:%M", &tm);
834 r = parse_fractional_part_u(&k, 6, &add);
845 x = mktime_or_timegm(&tm, utc);
846 if (x == (time_t) -1)
849 if (weekday >= 0 && tm.tm_wday != weekday)
852 ret = (usec_t) x * USEC_PER_SEC + x_usec;
867 static char* extract_multiplier(char *p, usec_t *multiplier) {
868 static const struct {
872 { "seconds", USEC_PER_SEC },
873 { "second", USEC_PER_SEC },
874 { "sec", USEC_PER_SEC },
875 { "s", USEC_PER_SEC },
876 { "minutes", USEC_PER_MINUTE },
877 { "minute", USEC_PER_MINUTE },
878 { "min", USEC_PER_MINUTE },
879 { "months", USEC_PER_MONTH },
880 { "month", USEC_PER_MONTH },
881 { "M", USEC_PER_MONTH },
882 { "msec", USEC_PER_MSEC },
883 { "ms", USEC_PER_MSEC },
884 { "m", USEC_PER_MINUTE },
885 { "hours", USEC_PER_HOUR },
886 { "hour", USEC_PER_HOUR },
887 { "hr", USEC_PER_HOUR },
888 { "h", USEC_PER_HOUR },
889 { "days", USEC_PER_DAY },
890 { "day", USEC_PER_DAY },
891 { "d", USEC_PER_DAY },
892 { "weeks", USEC_PER_WEEK },
893 { "week", USEC_PER_WEEK },
894 { "w", USEC_PER_WEEK },
895 { "years", USEC_PER_YEAR },
896 { "year", USEC_PER_YEAR },
897 { "y", USEC_PER_YEAR },
903 for (i = 0; i < ELEMENTSOF(table); i++) {
906 e = startswith(p, table[i].suffix);
908 *multiplier = table[i].usec;
916 int parse_time(const char *t, usec_t *usec, usec_t default_unit) {
919 bool something = false;
923 assert(default_unit > 0);
927 p += strspn(p, WHITESPACE);
928 s = startswith(p, "infinity");
930 s += strspn(s, WHITESPACE);
934 *usec = USEC_INFINITY;
942 usec_t multiplier = default_unit, k;
944 p += strspn(p, WHITESPACE);
954 l = strtoll(p, &e, 10);
964 z = strtoll(b, &e, 10);
979 e += strspn(e, WHITESPACE);
980 p = extract_multiplier(e, &multiplier);
984 k = (usec_t) z * multiplier;
989 r += (usec_t) l * multiplier + k;
997 int parse_sec(const char *t, usec_t *usec) {
998 return parse_time(t, usec, USEC_PER_SEC);
1001 #if 0 /// UNNEEDED by elogind
1002 int parse_nsec(const char *t, nsec_t *nsec) {
1003 static const struct {
1007 { "seconds", NSEC_PER_SEC },
1008 { "second", NSEC_PER_SEC },
1009 { "sec", NSEC_PER_SEC },
1010 { "s", NSEC_PER_SEC },
1011 { "minutes", NSEC_PER_MINUTE },
1012 { "minute", NSEC_PER_MINUTE },
1013 { "min", NSEC_PER_MINUTE },
1014 { "months", NSEC_PER_MONTH },
1015 { "month", NSEC_PER_MONTH },
1016 { "msec", NSEC_PER_MSEC },
1017 { "ms", NSEC_PER_MSEC },
1018 { "m", NSEC_PER_MINUTE },
1019 { "hours", NSEC_PER_HOUR },
1020 { "hour", NSEC_PER_HOUR },
1021 { "hr", NSEC_PER_HOUR },
1022 { "h", NSEC_PER_HOUR },
1023 { "days", NSEC_PER_DAY },
1024 { "day", NSEC_PER_DAY },
1025 { "d", NSEC_PER_DAY },
1026 { "weeks", NSEC_PER_WEEK },
1027 { "week", NSEC_PER_WEEK },
1028 { "w", NSEC_PER_WEEK },
1029 { "years", NSEC_PER_YEAR },
1030 { "year", NSEC_PER_YEAR },
1031 { "y", NSEC_PER_YEAR },
1032 { "usec", NSEC_PER_USEC },
1033 { "us", NSEC_PER_USEC },
1036 { "", 1ULL }, /* default is nsec */
1041 bool something = false;
1048 p += strspn(p, WHITESPACE);
1049 s = startswith(p, "infinity");
1051 s += strspn(s, WHITESPACE);
1055 *nsec = NSEC_INFINITY;
1064 p += strspn(p, WHITESPACE);
1074 l = strtoll(p, &e, 10);
1086 z = strtoll(b, &e, 10);
1101 e += strspn(e, WHITESPACE);
1103 for (i = 0; i < ELEMENTSOF(table); i++)
1104 if (startswith(e, table[i].suffix)) {
1105 nsec_t k = (nsec_t) z * table[i].nsec;
1110 r += (nsec_t) l * table[i].nsec + k;
1111 p = e + strlen(table[i].suffix);
1117 if (i >= ELEMENTSOF(table))
1127 bool ntp_synced(void) {
1128 struct timex txc = {};
1130 if (adjtimex(&txc) < 0)
1133 if (txc.status & STA_UNSYNC)
1139 int get_timezones(char ***ret) {
1140 _cleanup_fclose_ FILE *f = NULL;
1141 _cleanup_strv_free_ char **zones = NULL;
1142 size_t n_zones = 0, n_allocated = 0;
1146 zones = strv_new("UTC", NULL);
1153 f = fopen("/usr/share/zoneinfo/zone.tab", "re");
1157 FOREACH_LINE(l, f, return -errno) {
1163 if (isempty(p) || *p == '#')
1166 /* Skip over country code */
1167 p += strcspn(p, WHITESPACE);
1168 p += strspn(p, WHITESPACE);
1170 /* Skip over coordinates */
1171 p += strcspn(p, WHITESPACE);
1172 p += strspn(p, WHITESPACE);
1174 /* Found timezone name */
1175 k = strcspn(p, WHITESPACE);
1183 if (!GREEDY_REALLOC(zones, n_allocated, n_zones + 2)) {
1188 zones[n_zones++] = w;
1189 zones[n_zones] = NULL;
1194 } else if (errno != ENOENT)
1203 bool timezone_is_valid(const char *name) {
1214 for (p = name; *p; p++) {
1215 if (!(*p >= '0' && *p <= '9') &&
1216 !(*p >= 'a' && *p <= 'z') &&
1217 !(*p >= 'A' && *p <= 'Z') &&
1218 !(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
1234 t = strjoina("/usr/share/zoneinfo/", name);
1235 if (stat(t, &st) < 0)
1238 if (!S_ISREG(st.st_mode))
1245 bool clock_boottime_supported(void) {
1246 static int supported = -1;
1248 /* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */
1250 if (supported < 0) {
1253 fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC);
1265 #if 0 /// UNNEEDED by elogind
1266 clockid_t clock_boottime_or_monotonic(void) {
1267 if (clock_boottime_supported())
1268 return CLOCK_BOOTTIME;
1270 return CLOCK_MONOTONIC;
1274 bool clock_supported(clockid_t clock) {
1279 case CLOCK_MONOTONIC:
1280 case CLOCK_REALTIME:
1283 case CLOCK_BOOTTIME:
1284 return clock_boottime_supported();
1286 case CLOCK_BOOTTIME_ALARM:
1287 if (!clock_boottime_supported())
1290 /* fall through, after checking the cached value for CLOCK_BOOTTIME. */
1293 /* For everything else, check properly */
1294 return clock_gettime(clock, &ts) >= 0;
1298 #if 0 /// UNNEEDED by elogind
1299 int get_timezone(char **tz) {
1300 _cleanup_free_ char *t = NULL;
1305 r = readlink_malloc("/etc/localtime", &t);
1307 return r; /* returns EINVAL if not a symlink */
1309 e = path_startswith(t, "/usr/share/zoneinfo/");
1311 e = path_startswith(t, "../usr/share/zoneinfo/");
1315 if (!timezone_is_valid(e))
1326 time_t mktime_or_timegm(struct tm *tm, bool utc) {
1327 return utc ? timegm(tm) : mktime(tm);
1331 struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc) {
1332 return utc ? gmtime_r(t, tm) : localtime_r(t, tm);
1335 #if 0 /// UNNEEDED by elogind
1336 unsigned long usec_to_jiffies(usec_t u) {
1337 static thread_local unsigned long hz = 0;
1341 r = sysconf(_SC_CLK_TCK);
1347 return DIV_ROUND_UP(u , USEC_PER_SEC / hz);