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(char *buf, size_t l, usec_t t,
274 if (t <= 0 || t == USEC_INFINITY)
277 sec = (time_t) (t / USEC_PER_SEC);
278 localtime_or_gmtime_r(&sec, &tm, utc);
281 k = strftime(buf, l, "%a %Y-%m-%d %H:%M:%S", &tm);
283 k = strftime(buf, l, "%a %Y-%m-%d %H:%M:%S %Z", &tm);
288 snprintf(buf + strlen(buf), l - strlen(buf), ".%06llu", (unsigned long long) (t % USEC_PER_SEC));
289 if (strftime(buf + strlen(buf), l - strlen(buf), " %Z", &tm) <= 0)
296 char *format_timestamp(char *buf, size_t l, usec_t t) {
297 return format_timestamp_internal(buf, l, t, false, false);
300 #if 0 /// UNNEEDED by elogind
301 char *format_timestamp_utc(char *buf, size_t l, usec_t t) {
302 return format_timestamp_internal(buf, l, t, true, false);
306 char *format_timestamp_us(char *buf, size_t l, usec_t t) {
307 return format_timestamp_internal(buf, l, t, false, true);
310 #if 0 /// UNNEEDED by elogind
311 char *format_timestamp_us_utc(char *buf, size_t l, usec_t t) {
312 return format_timestamp_internal(buf, l, t, true, true);
316 char *format_timestamp_relative(char *buf, size_t l, usec_t t) {
320 if (t <= 0 || t == USEC_INFINITY)
323 n = now(CLOCK_REALTIME);
332 if (d >= USEC_PER_YEAR)
333 snprintf(buf, l, USEC_FMT " years " USEC_FMT " months %s",
335 (d % USEC_PER_YEAR) / USEC_PER_MONTH, s);
336 else if (d >= USEC_PER_MONTH)
337 snprintf(buf, l, USEC_FMT " months " USEC_FMT " days %s",
339 (d % USEC_PER_MONTH) / USEC_PER_DAY, s);
340 else if (d >= USEC_PER_WEEK)
341 snprintf(buf, l, USEC_FMT " weeks " USEC_FMT " days %s",
343 (d % USEC_PER_WEEK) / USEC_PER_DAY, s);
344 else if (d >= 2*USEC_PER_DAY)
345 snprintf(buf, l, USEC_FMT " days %s", d / USEC_PER_DAY, s);
346 else if (d >= 25*USEC_PER_HOUR)
347 snprintf(buf, l, "1 day " USEC_FMT "h %s",
348 (d - USEC_PER_DAY) / USEC_PER_HOUR, s);
349 else if (d >= 6*USEC_PER_HOUR)
350 snprintf(buf, l, USEC_FMT "h %s",
351 d / USEC_PER_HOUR, s);
352 else if (d >= USEC_PER_HOUR)
353 snprintf(buf, l, USEC_FMT "h " USEC_FMT "min %s",
355 (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s);
356 else if (d >= 5*USEC_PER_MINUTE)
357 snprintf(buf, l, USEC_FMT "min %s",
358 d / USEC_PER_MINUTE, s);
359 else if (d >= USEC_PER_MINUTE)
360 snprintf(buf, l, USEC_FMT "min " USEC_FMT "s %s",
362 (d % USEC_PER_MINUTE) / USEC_PER_SEC, s);
363 else if (d >= USEC_PER_SEC)
364 snprintf(buf, l, USEC_FMT "s %s",
365 d / USEC_PER_SEC, s);
366 else if (d >= USEC_PER_MSEC)
367 snprintf(buf, l, USEC_FMT "ms %s",
368 d / USEC_PER_MSEC, s);
370 snprintf(buf, l, USEC_FMT"us %s",
373 snprintf(buf, l, "now");
379 char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
380 static const struct {
384 { "y", USEC_PER_YEAR },
385 { "month", USEC_PER_MONTH },
386 { "w", USEC_PER_WEEK },
387 { "d", USEC_PER_DAY },
388 { "h", USEC_PER_HOUR },
389 { "min", USEC_PER_MINUTE },
390 { "s", USEC_PER_SEC },
391 { "ms", USEC_PER_MSEC },
397 bool something = false;
402 if (t == USEC_INFINITY) {
403 strncpy(p, "infinity", l-1);
409 strncpy(p, "0", l-1);
414 /* The result of this function can be parsed with parse_sec */
416 for (i = 0; i < ELEMENTSOF(table); i++) {
425 if (t < accuracy && something)
428 if (t < table[i].usec)
434 a = t / table[i].usec;
435 b = t % table[i].usec;
437 /* Let's see if we should shows this in dot notation */
438 if (t < USEC_PER_MINUTE && b > 0) {
443 for (cc = table[i].usec; cc > 1; cc /= 10)
446 for (cc = accuracy; cc > 1; cc /= 10) {
453 "%s"USEC_FMT".%0*llu%s",
457 (unsigned long long) b,
465 /* No? Then let's show it normally */
476 n = MIN((size_t) k, l);
489 #if 0 /// UNNEEDED by elogind
490 void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) {
496 if (!dual_timestamp_is_set(t))
499 fprintf(f, "%s="USEC_FMT" "USEC_FMT"\n",
505 int dual_timestamp_deserialize(const char *value, dual_timestamp *t) {
506 unsigned long long a, b;
511 if (sscanf(value, "%llu %llu", &a, &b) != 2) {
512 log_debug("Failed to parse dual timestamp value \"%s\": %m", value);
523 int timestamp_deserialize(const char *value, usec_t *timestamp) {
528 r = safe_atou64(value, timestamp);
530 return log_debug_errno(r, "Failed to parse timestamp value \"%s\": %m", value);
535 #if 0 /// UNNEEDED by elogind
536 int parse_timestamp(const char *t, usec_t *usec) {
537 static const struct {
561 usec_t x_usec, plus = 0, minus = 0, ret;
568 * 2012-09-22 16:34:22
569 * 2012-09-22 16:34 (seconds will be set to 0)
570 * 2012-09-22 (time will be set to 00:00:00)
571 * 16:34:22 (date will be set to today)
572 * 16:34 (date will be set to today, seconds to 0)
574 * yesterday (time is set to 00:00:00)
575 * today (time is set to 00:00:00)
576 * tomorrow (time is set to 00:00:00)
579 * @2147483647 (seconds since epoch)
587 return parse_sec(t + 1, usec);
589 ret = now(CLOCK_REALTIME);
594 else if (t[0] == '+') {
595 r = parse_sec(t+1, &plus);
601 } else if (t[0] == '-') {
602 r = parse_sec(t+1, &minus);
608 } else if ((k = endswith(t, " ago"))) {
609 t = strndupa(t, k - t);
611 r = parse_sec(t, &minus);
617 } else if ((k = endswith(t, " left"))) {
618 t = strndupa(t, k - t);
620 r = parse_sec(t, &plus);
627 utc = endswith_no_case(t, " UTC");
629 t = strndupa(t, utc - t);
631 x = ret / USEC_PER_SEC;
634 assert_se(localtime_or_gmtime_r(&x, &tm, utc));
637 if (streq(t, "today")) {
638 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
641 } else if (streq(t, "yesterday")) {
643 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
646 } else if (streq(t, "tomorrow")) {
648 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
653 for (i = 0; i < ELEMENTSOF(day_nr); i++) {
656 if (!startswith_no_case(t, day_nr[i].name))
659 skip = strlen(day_nr[i].name);
663 weekday = day_nr[i].nr;
669 k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
678 k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm);
687 k = strptime(t, "%y-%m-%d %H:%M", &tm);
694 k = strptime(t, "%Y-%m-%d %H:%M", &tm);
701 k = strptime(t, "%y-%m-%d", &tm);
703 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
708 k = strptime(t, "%Y-%m-%d", &tm);
710 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
715 k = strptime(t, "%H:%M:%S", &tm);
724 k = strptime(t, "%H:%M", &tm);
737 r = parse_fractional_part_u(&k, 6, &add);
749 x = mktime_or_timegm(&tm, utc);
750 if (x == (time_t) -1)
753 if (weekday >= 0 && tm.tm_wday != weekday)
756 ret = (usec_t) x * USEC_PER_SEC + x_usec;
771 static char* extract_multiplier(char *p, usec_t *multiplier) {
772 static const struct {
776 { "seconds", USEC_PER_SEC },
777 { "second", USEC_PER_SEC },
778 { "sec", USEC_PER_SEC },
779 { "s", USEC_PER_SEC },
780 { "minutes", USEC_PER_MINUTE },
781 { "minute", USEC_PER_MINUTE },
782 { "min", USEC_PER_MINUTE },
783 { "months", USEC_PER_MONTH },
784 { "month", USEC_PER_MONTH },
785 { "M", USEC_PER_MONTH },
786 { "msec", USEC_PER_MSEC },
787 { "ms", USEC_PER_MSEC },
788 { "m", USEC_PER_MINUTE },
789 { "hours", USEC_PER_HOUR },
790 { "hour", USEC_PER_HOUR },
791 { "hr", USEC_PER_HOUR },
792 { "h", USEC_PER_HOUR },
793 { "days", USEC_PER_DAY },
794 { "day", USEC_PER_DAY },
795 { "d", USEC_PER_DAY },
796 { "weeks", USEC_PER_WEEK },
797 { "week", USEC_PER_WEEK },
798 { "w", USEC_PER_WEEK },
799 { "years", USEC_PER_YEAR },
800 { "year", USEC_PER_YEAR },
801 { "y", USEC_PER_YEAR },
807 for (i = 0; i < ELEMENTSOF(table); i++) {
810 e = startswith(p, table[i].suffix);
812 *multiplier = table[i].usec;
820 int parse_time(const char *t, usec_t *usec, usec_t default_unit) {
823 bool something = false;
827 assert(default_unit > 0);
831 p += strspn(p, WHITESPACE);
832 s = startswith(p, "infinity");
834 s += strspn(s, WHITESPACE);
838 *usec = USEC_INFINITY;
846 usec_t multiplier = default_unit, k;
848 p += strspn(p, WHITESPACE);
858 l = strtoll(p, &e, 10);
868 z = strtoll(b, &e, 10);
883 e += strspn(e, WHITESPACE);
884 p = extract_multiplier(e, &multiplier);
888 k = (usec_t) z * multiplier;
893 r += (usec_t) l * multiplier + k;
901 int parse_sec(const char *t, usec_t *usec) {
902 return parse_time(t, usec, USEC_PER_SEC);
905 #if 0 /// UNNEEDED by elogind
906 int parse_nsec(const char *t, nsec_t *nsec) {
907 static const struct {
911 { "seconds", NSEC_PER_SEC },
912 { "second", NSEC_PER_SEC },
913 { "sec", NSEC_PER_SEC },
914 { "s", NSEC_PER_SEC },
915 { "minutes", NSEC_PER_MINUTE },
916 { "minute", NSEC_PER_MINUTE },
917 { "min", NSEC_PER_MINUTE },
918 { "months", NSEC_PER_MONTH },
919 { "month", NSEC_PER_MONTH },
920 { "msec", NSEC_PER_MSEC },
921 { "ms", NSEC_PER_MSEC },
922 { "m", NSEC_PER_MINUTE },
923 { "hours", NSEC_PER_HOUR },
924 { "hour", NSEC_PER_HOUR },
925 { "hr", NSEC_PER_HOUR },
926 { "h", NSEC_PER_HOUR },
927 { "days", NSEC_PER_DAY },
928 { "day", NSEC_PER_DAY },
929 { "d", NSEC_PER_DAY },
930 { "weeks", NSEC_PER_WEEK },
931 { "week", NSEC_PER_WEEK },
932 { "w", NSEC_PER_WEEK },
933 { "years", NSEC_PER_YEAR },
934 { "year", NSEC_PER_YEAR },
935 { "y", NSEC_PER_YEAR },
936 { "usec", NSEC_PER_USEC },
937 { "us", NSEC_PER_USEC },
940 { "", 1ULL }, /* default is nsec */
945 bool something = false;
952 p += strspn(p, WHITESPACE);
953 s = startswith(p, "infinity");
955 s += strspn(s, WHITESPACE);
959 *nsec = NSEC_INFINITY;
968 p += strspn(p, WHITESPACE);
978 l = strtoll(p, &e, 10);
990 z = strtoll(b, &e, 10);
1005 e += strspn(e, WHITESPACE);
1007 for (i = 0; i < ELEMENTSOF(table); i++)
1008 if (startswith(e, table[i].suffix)) {
1009 nsec_t k = (nsec_t) z * table[i].nsec;
1014 r += (nsec_t) l * table[i].nsec + k;
1015 p = e + strlen(table[i].suffix);
1021 if (i >= ELEMENTSOF(table))
1031 bool ntp_synced(void) {
1032 struct timex txc = {};
1034 if (adjtimex(&txc) < 0)
1037 if (txc.status & STA_UNSYNC)
1043 int get_timezones(char ***ret) {
1044 _cleanup_fclose_ FILE *f = NULL;
1045 _cleanup_strv_free_ char **zones = NULL;
1046 size_t n_zones = 0, n_allocated = 0;
1050 zones = strv_new("UTC", NULL);
1057 f = fopen("/usr/share/zoneinfo/zone.tab", "re");
1061 FOREACH_LINE(l, f, return -errno) {
1067 if (isempty(p) || *p == '#')
1070 /* Skip over country code */
1071 p += strcspn(p, WHITESPACE);
1072 p += strspn(p, WHITESPACE);
1074 /* Skip over coordinates */
1075 p += strcspn(p, WHITESPACE);
1076 p += strspn(p, WHITESPACE);
1078 /* Found timezone name */
1079 k = strcspn(p, WHITESPACE);
1087 if (!GREEDY_REALLOC(zones, n_allocated, n_zones + 2)) {
1092 zones[n_zones++] = w;
1093 zones[n_zones] = NULL;
1098 } else if (errno != ENOENT)
1107 bool timezone_is_valid(const char *name) {
1118 for (p = name; *p; p++) {
1119 if (!(*p >= '0' && *p <= '9') &&
1120 !(*p >= 'a' && *p <= 'z') &&
1121 !(*p >= 'A' && *p <= 'Z') &&
1122 !(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
1138 t = strjoina("/usr/share/zoneinfo/", name);
1139 if (stat(t, &st) < 0)
1142 if (!S_ISREG(st.st_mode))
1149 bool clock_boottime_supported(void) {
1150 static int supported = -1;
1152 /* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */
1154 if (supported < 0) {
1157 fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC);
1169 #if 0 /// UNNEEDED by elogind
1170 clockid_t clock_boottime_or_monotonic(void) {
1171 if (clock_boottime_supported())
1172 return CLOCK_BOOTTIME;
1174 return CLOCK_MONOTONIC;
1178 bool clock_supported(clockid_t clock) {
1183 case CLOCK_MONOTONIC:
1184 case CLOCK_REALTIME:
1187 case CLOCK_BOOTTIME:
1188 return clock_boottime_supported();
1190 case CLOCK_BOOTTIME_ALARM:
1191 if (!clock_boottime_supported())
1194 /* fall through, after checking the cached value for CLOCK_BOOTTIME. */
1197 /* For everything else, check properly */
1198 return clock_gettime(clock, &ts) >= 0;
1202 #if 0 /// UNNEEDED by elogind
1203 int get_timezone(char **tz) {
1204 _cleanup_free_ char *t = NULL;
1209 r = readlink_malloc("/etc/localtime", &t);
1211 return r; /* returns EINVAL if not a symlink */
1213 e = path_startswith(t, "/usr/share/zoneinfo/");
1215 e = path_startswith(t, "../usr/share/zoneinfo/");
1219 if (!timezone_is_valid(e))
1230 time_t mktime_or_timegm(struct tm *tm, bool utc) {
1231 return utc ? timegm(tm) : mktime(tm);
1235 struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc) {
1236 return utc ? gmtime_r(t, tm) : localtime_r(t, tm);
1239 #if 0 /// UNNEEDED by elogind
1240 unsigned long usec_to_jiffies(usec_t u) {
1241 static thread_local unsigned long hz = 0;
1245 r = sysconf(_SC_CLK_TCK);
1248 hz = (unsigned long) r;
1251 return DIV_ROUND_UP(u , USEC_PER_SEC / hz);