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 < 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 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 < 0 || tv->tv_usec < 0)
236 return USEC_INFINITY;
238 if ((usec_t) tv->tv_sec > (UINT64_MAX - tv->tv_usec) / USEC_PER_SEC)
239 return USEC_INFINITY;
242 (usec_t) tv->tv_sec * USEC_PER_SEC +
243 (usec_t) tv->tv_usec;
246 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
249 if (u == USEC_INFINITY) {
250 tv->tv_sec = (time_t) -1;
251 tv->tv_usec = (suseconds_t) -1;
253 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
254 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
260 static char *format_timestamp_internal(
267 /* The weekdays in non-localized (English) form. We use this instead of the localized form, so that our
268 * generated timestamps may be parsed with parse_timestamp(), and always read the same. */
269 static const char * const weekdays[] = {
287 1 + 10 + /* space and date */
288 1 + 8 + /* space and time */
289 (us ? 1 + 6 : 0) + /* "." and microsecond part */
290 1 + 1 + /* space and shortest possible zone */
292 return NULL; /* Not enough space even for the shortest form. */
293 if (t <= 0 || t == USEC_INFINITY)
294 return NULL; /* Timestamp is unset */
296 /* Let's not format times with years > 9999 */
297 if (t > USEC_TIMESTAMP_FORMATTABLE_MAX)
300 sec = (time_t) (t / USEC_PER_SEC); /* Round down */
302 if (!localtime_or_gmtime_r(&sec, &tm, utc))
305 /* Start with the week day */
306 assert((size_t) tm.tm_wday < ELEMENTSOF(weekdays));
307 memcpy(buf, weekdays[tm.tm_wday], 4);
309 /* Add the main components */
310 if (strftime(buf + 3, l - 3, " %Y-%m-%d %H:%M:%S", &tm) <= 0)
311 return NULL; /* Doesn't fit */
313 /* Append the microseconds part, if that's requested */
317 return NULL; /* Microseconds part doesn't fit. */
319 sprintf(buf + n, ".%06"PRI_USEC, t % USEC_PER_SEC);
322 /* Append the timezone */
325 /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r() normally uses the
326 * obsolete "GMT" instead. */
328 return NULL; /* "UTC" doesn't fit. */
330 strcpy(buf + n, " UTC");
332 } else if (!isempty(tm.tm_zone)) {
335 /* An explicit timezone is specified, let's use it, if it fits */
336 tn = strlen(tm.tm_zone);
337 if (n + 1 + tn + 1 > l) {
338 /* The full time zone does not fit in. Yuck. */
340 if (n + 1 + _POSIX_TZNAME_MAX + 1 > l)
341 return NULL; /* Not even enough space for the POSIX minimum (of 6)? In that case, complain that it doesn't fit */
343 /* So the time zone doesn't fit in fully, but the caller passed enough space for the POSIX
344 * minimum time zone length. In this case suppress the timezone entirely, in order not to dump
345 * an overly long, hard to read string on the user. This should be safe, because the user will
346 * assume the local timezone anyway if none is shown. And so does parse_timestamp(). */
349 strcpy(buf + n, tm.tm_zone);
356 char *format_timestamp(char *buf, size_t l, usec_t t) {
357 return format_timestamp_internal(buf, l, t, false, false);
360 #if 0 /// UNNEEDED by elogind
361 char *format_timestamp_utc(char *buf, size_t l, usec_t t) {
362 return format_timestamp_internal(buf, l, t, true, false);
366 char *format_timestamp_us(char *buf, size_t l, usec_t t) {
367 return format_timestamp_internal(buf, l, t, false, true);
370 #if 0 /// UNNEEDED by elogind
371 char *format_timestamp_us_utc(char *buf, size_t l, usec_t t) {
372 return format_timestamp_internal(buf, l, t, true, true);
376 char *format_timestamp_relative(char *buf, size_t l, usec_t t) {
380 if (t <= 0 || t == USEC_INFINITY)
383 n = now(CLOCK_REALTIME);
392 if (d >= USEC_PER_YEAR)
393 snprintf(buf, l, USEC_FMT " years " USEC_FMT " months %s",
395 (d % USEC_PER_YEAR) / USEC_PER_MONTH, s);
396 else if (d >= USEC_PER_MONTH)
397 snprintf(buf, l, USEC_FMT " months " USEC_FMT " days %s",
399 (d % USEC_PER_MONTH) / USEC_PER_DAY, s);
400 else if (d >= USEC_PER_WEEK)
401 snprintf(buf, l, USEC_FMT " weeks " USEC_FMT " days %s",
403 (d % USEC_PER_WEEK) / USEC_PER_DAY, s);
404 else if (d >= 2*USEC_PER_DAY)
405 snprintf(buf, l, USEC_FMT " days %s", d / USEC_PER_DAY, s);
406 else if (d >= 25*USEC_PER_HOUR)
407 snprintf(buf, l, "1 day " USEC_FMT "h %s",
408 (d - USEC_PER_DAY) / USEC_PER_HOUR, s);
409 else if (d >= 6*USEC_PER_HOUR)
410 snprintf(buf, l, USEC_FMT "h %s",
411 d / USEC_PER_HOUR, s);
412 else if (d >= USEC_PER_HOUR)
413 snprintf(buf, l, USEC_FMT "h " USEC_FMT "min %s",
415 (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s);
416 else if (d >= 5*USEC_PER_MINUTE)
417 snprintf(buf, l, USEC_FMT "min %s",
418 d / USEC_PER_MINUTE, s);
419 else if (d >= USEC_PER_MINUTE)
420 snprintf(buf, l, USEC_FMT "min " USEC_FMT "s %s",
422 (d % USEC_PER_MINUTE) / USEC_PER_SEC, s);
423 else if (d >= USEC_PER_SEC)
424 snprintf(buf, l, USEC_FMT "s %s",
425 d / USEC_PER_SEC, s);
426 else if (d >= USEC_PER_MSEC)
427 snprintf(buf, l, USEC_FMT "ms %s",
428 d / USEC_PER_MSEC, s);
430 snprintf(buf, l, USEC_FMT"us %s",
433 snprintf(buf, l, "now");
439 char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
440 static const struct {
444 { "y", USEC_PER_YEAR },
445 { "month", USEC_PER_MONTH },
446 { "w", USEC_PER_WEEK },
447 { "d", USEC_PER_DAY },
448 { "h", USEC_PER_HOUR },
449 { "min", USEC_PER_MINUTE },
450 { "s", USEC_PER_SEC },
451 { "ms", USEC_PER_MSEC },
457 bool something = false;
462 if (t == USEC_INFINITY) {
463 strncpy(p, "infinity", l-1);
469 strncpy(p, "0", l-1);
474 /* The result of this function can be parsed with parse_sec */
476 for (i = 0; i < ELEMENTSOF(table); i++) {
485 if (t < accuracy && something)
488 if (t < table[i].usec)
494 a = t / table[i].usec;
495 b = t % table[i].usec;
497 /* Let's see if we should shows this in dot notation */
498 if (t < USEC_PER_MINUTE && b > 0) {
503 for (cc = table[i].usec; cc > 1; cc /= 10)
506 for (cc = accuracy; cc > 1; cc /= 10) {
513 "%s"USEC_FMT".%0*"PRI_USEC"%s",
525 /* No? Then let's show it normally */
536 n = MIN((size_t) k, l);
549 #if 0 /// UNNEEDED by elogind
550 void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) {
556 if (!dual_timestamp_is_set(t))
559 fprintf(f, "%s="USEC_FMT" "USEC_FMT"\n",
565 int dual_timestamp_deserialize(const char *value, dual_timestamp *t) {
566 unsigned long long a, b;
571 if (sscanf(value, "%llu %llu", &a, &b) != 2) {
572 log_debug("Failed to parse dual timestamp value \"%s\": %m", value);
583 int timestamp_deserialize(const char *value, usec_t *timestamp) {
588 r = safe_atou64(value, timestamp);
590 return log_debug_errno(r, "Failed to parse timestamp value \"%s\": %m", value);
595 #if 0 /// UNNEEDED by elogind
596 int parse_timestamp(const char *t, usec_t *usec) {
597 static const struct {
617 const char *k, *utc, *tzn = NULL;
620 usec_t x_usec, plus = 0, minus = 0, ret;
621 int r, weekday = -1, dst = -1;
627 * 2012-09-22 16:34:22
628 * 2012-09-22 16:34 (seconds will be set to 0)
629 * 2012-09-22 (time will be set to 00:00:00)
630 * 16:34:22 (date will be set to today)
631 * 16:34 (date will be set to today, seconds to 0)
633 * yesterday (time is set to 00:00:00)
634 * today (time is set to 00:00:00)
635 * tomorrow (time is set to 00:00:00)
638 * @2147483647 (seconds since epoch)
646 return parse_sec(t + 1, usec);
648 ret = now(CLOCK_REALTIME);
653 else if (t[0] == '+') {
654 r = parse_sec(t+1, &plus);
660 } else if (t[0] == '-') {
661 r = parse_sec(t+1, &minus);
667 } else if ((k = endswith(t, " ago"))) {
668 t = strndupa(t, k - t);
670 r = parse_sec(t, &minus);
676 } else if ((k = endswith(t, " left"))) {
677 t = strndupa(t, k - t);
679 r = parse_sec(t, &plus);
686 /* See if the timestamp is suffixed with UTC */
687 utc = endswith_no_case(t, " UTC");
689 t = strndupa(t, utc - t);
691 const char *e = NULL;
696 /* See if the timestamp is suffixed by either the DST or non-DST local timezone. Note that we only
697 * support the local timezones here, nothing else. Not because we wouldn't want to, but simply because
698 * there are no nice APIs available to cover this. By accepting the local time zone strings, we make
699 * sure that all timestamps written by format_timestamp() can be parsed correctly, even though we don't
700 * support arbitrary timezone specifications. */
702 for (j = 0; j <= 1; j++) {
704 if (isempty(tzname[j]))
707 e = endswith_no_case(t, tzname[j]);
718 if (IN_SET(j, 0, 1)) {
719 /* Found one of the two timezones specified. */
720 t = strndupa(t, e - t - 1);
726 x = (time_t) (ret / USEC_PER_SEC);
729 if (!localtime_or_gmtime_r(&x, &tm, utc))
736 if (streq(t, "today")) {
737 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
740 } else if (streq(t, "yesterday")) {
742 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
745 } else if (streq(t, "tomorrow")) {
747 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
751 for (i = 0; i < ELEMENTSOF(day_nr); i++) {
754 if (!startswith_no_case(t, day_nr[i].name))
757 skip = strlen(day_nr[i].name);
761 weekday = day_nr[i].nr;
767 k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
776 k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm);
785 k = strptime(t, "%y-%m-%d %H:%M", &tm);
792 k = strptime(t, "%Y-%m-%d %H:%M", &tm);
799 k = strptime(t, "%y-%m-%d", &tm);
801 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
806 k = strptime(t, "%Y-%m-%d", &tm);
808 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
813 k = strptime(t, "%H:%M:%S", &tm);
822 k = strptime(t, "%H:%M", &tm);
835 r = parse_fractional_part_u(&k, 6, &add);
846 x = mktime_or_timegm(&tm, utc);
850 if (weekday >= 0 && tm.tm_wday != weekday)
853 ret = (usec_t) x * USEC_PER_SEC + x_usec;
854 if (ret > USEC_TIMESTAMP_FORMATTABLE_MAX)
859 if (ret > USEC_TIMESTAMP_FORMATTABLE_MAX)
873 static char* extract_multiplier(char *p, usec_t *multiplier) {
874 static const struct {
878 { "seconds", USEC_PER_SEC },
879 { "second", USEC_PER_SEC },
880 { "sec", USEC_PER_SEC },
881 { "s", USEC_PER_SEC },
882 { "minutes", USEC_PER_MINUTE },
883 { "minute", USEC_PER_MINUTE },
884 { "min", USEC_PER_MINUTE },
885 { "months", USEC_PER_MONTH },
886 { "month", USEC_PER_MONTH },
887 { "M", USEC_PER_MONTH },
888 { "msec", USEC_PER_MSEC },
889 { "ms", USEC_PER_MSEC },
890 { "m", USEC_PER_MINUTE },
891 { "hours", USEC_PER_HOUR },
892 { "hour", USEC_PER_HOUR },
893 { "hr", USEC_PER_HOUR },
894 { "h", USEC_PER_HOUR },
895 { "days", USEC_PER_DAY },
896 { "day", USEC_PER_DAY },
897 { "d", USEC_PER_DAY },
898 { "weeks", USEC_PER_WEEK },
899 { "week", USEC_PER_WEEK },
900 { "w", USEC_PER_WEEK },
901 { "years", USEC_PER_YEAR },
902 { "year", USEC_PER_YEAR },
903 { "y", USEC_PER_YEAR },
910 for (i = 0; i < ELEMENTSOF(table); i++) {
913 e = startswith(p, table[i].suffix);
915 *multiplier = table[i].usec;
923 int parse_time(const char *t, usec_t *usec, usec_t default_unit) {
926 bool something = false;
930 assert(default_unit > 0);
934 p += strspn(p, WHITESPACE);
935 s = startswith(p, "infinity");
937 s += strspn(s, WHITESPACE);
941 *usec = USEC_INFINITY;
949 usec_t multiplier = default_unit, k;
951 p += strspn(p, WHITESPACE);
961 l = strtoll(p, &e, 10);
971 z = strtoll(b, &e, 10);
986 e += strspn(e, WHITESPACE);
987 p = extract_multiplier(e, &multiplier);
991 k = (usec_t) z * multiplier;
996 r += (usec_t) l * multiplier + k;
1004 int parse_sec(const char *t, usec_t *usec) {
1005 return parse_time(t, usec, USEC_PER_SEC);
1008 #if 0 /// UNNEEDED by elogind
1009 int parse_nsec(const char *t, nsec_t *nsec) {
1010 static const struct {
1014 { "seconds", NSEC_PER_SEC },
1015 { "second", NSEC_PER_SEC },
1016 { "sec", NSEC_PER_SEC },
1017 { "s", NSEC_PER_SEC },
1018 { "minutes", NSEC_PER_MINUTE },
1019 { "minute", NSEC_PER_MINUTE },
1020 { "min", NSEC_PER_MINUTE },
1021 { "months", NSEC_PER_MONTH },
1022 { "month", NSEC_PER_MONTH },
1023 { "msec", NSEC_PER_MSEC },
1024 { "ms", NSEC_PER_MSEC },
1025 { "m", NSEC_PER_MINUTE },
1026 { "hours", NSEC_PER_HOUR },
1027 { "hour", NSEC_PER_HOUR },
1028 { "hr", NSEC_PER_HOUR },
1029 { "h", NSEC_PER_HOUR },
1030 { "days", NSEC_PER_DAY },
1031 { "day", NSEC_PER_DAY },
1032 { "d", NSEC_PER_DAY },
1033 { "weeks", NSEC_PER_WEEK },
1034 { "week", NSEC_PER_WEEK },
1035 { "w", NSEC_PER_WEEK },
1036 { "years", NSEC_PER_YEAR },
1037 { "year", NSEC_PER_YEAR },
1038 { "y", NSEC_PER_YEAR },
1039 { "usec", NSEC_PER_USEC },
1040 { "us", NSEC_PER_USEC },
1041 { "µs", NSEC_PER_USEC },
1044 { "", 1ULL }, /* default is nsec */
1049 bool something = false;
1056 p += strspn(p, WHITESPACE);
1057 s = startswith(p, "infinity");
1059 s += strspn(s, WHITESPACE);
1063 *nsec = NSEC_INFINITY;
1072 p += strspn(p, WHITESPACE);
1082 l = strtoll(p, &e, 10);
1094 z = strtoll(b, &e, 10);
1109 e += strspn(e, WHITESPACE);
1111 for (i = 0; i < ELEMENTSOF(table); i++)
1112 if (startswith(e, table[i].suffix)) {
1113 nsec_t k = (nsec_t) z * table[i].nsec;
1118 r += (nsec_t) l * table[i].nsec + k;
1119 p = e + strlen(table[i].suffix);
1125 if (i >= ELEMENTSOF(table))
1135 bool ntp_synced(void) {
1136 struct timex txc = {};
1138 if (adjtimex(&txc) < 0)
1141 if (txc.status & STA_UNSYNC)
1147 int get_timezones(char ***ret) {
1148 _cleanup_fclose_ FILE *f = NULL;
1149 _cleanup_strv_free_ char **zones = NULL;
1150 size_t n_zones = 0, n_allocated = 0;
1154 zones = strv_new("UTC", NULL);
1161 f = fopen("/usr/share/zoneinfo/zone.tab", "re");
1165 FOREACH_LINE(l, f, return -errno) {
1171 if (isempty(p) || *p == '#')
1174 /* Skip over country code */
1175 p += strcspn(p, WHITESPACE);
1176 p += strspn(p, WHITESPACE);
1178 /* Skip over coordinates */
1179 p += strcspn(p, WHITESPACE);
1180 p += strspn(p, WHITESPACE);
1182 /* Found timezone name */
1183 k = strcspn(p, WHITESPACE);
1191 if (!GREEDY_REALLOC(zones, n_allocated, n_zones + 2)) {
1196 zones[n_zones++] = w;
1197 zones[n_zones] = NULL;
1202 } else if (errno != ENOENT)
1211 bool timezone_is_valid(const char *name) {
1222 for (p = name; *p; p++) {
1223 if (!(*p >= '0' && *p <= '9') &&
1224 !(*p >= 'a' && *p <= 'z') &&
1225 !(*p >= 'A' && *p <= 'Z') &&
1226 !(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
1242 t = strjoina("/usr/share/zoneinfo/", name);
1243 if (stat(t, &st) < 0)
1246 if (!S_ISREG(st.st_mode))
1253 bool clock_boottime_supported(void) {
1254 static int supported = -1;
1256 /* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */
1258 if (supported < 0) {
1261 fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC);
1273 #if 0 /// UNNEEDED by elogind
1274 clockid_t clock_boottime_or_monotonic(void) {
1275 if (clock_boottime_supported())
1276 return CLOCK_BOOTTIME;
1278 return CLOCK_MONOTONIC;
1282 bool clock_supported(clockid_t clock) {
1287 case CLOCK_MONOTONIC:
1288 case CLOCK_REALTIME:
1291 case CLOCK_BOOTTIME:
1292 return clock_boottime_supported();
1294 case CLOCK_BOOTTIME_ALARM:
1295 if (!clock_boottime_supported())
1301 /* For everything else, check properly */
1302 return clock_gettime(clock, &ts) >= 0;
1306 #if 0 /// UNNEEDED by elogind
1307 int get_timezone(char **tz) {
1308 _cleanup_free_ char *t = NULL;
1313 r = readlink_malloc("/etc/localtime", &t);
1315 return r; /* returns EINVAL if not a symlink */
1317 e = path_startswith(t, "/usr/share/zoneinfo/");
1319 e = path_startswith(t, "../usr/share/zoneinfo/");
1323 if (!timezone_is_valid(e))
1334 time_t mktime_or_timegm(struct tm *tm, bool utc) {
1335 return utc ? timegm(tm) : mktime(tm);
1339 struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc) {
1340 return utc ? gmtime_r(t, tm) : localtime_r(t, tm);
1343 #if 0 /// UNNEEDED by elogind
1344 unsigned long usec_to_jiffies(usec_t u) {
1345 static thread_local unsigned long hz = 0;
1349 r = sysconf(_SC_CLK_TCK);
1355 return DIV_ROUND_UP(u , USEC_PER_SEC / hz);