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 usec_t now(clockid_t clock_id) {
50 assert_se(clock_gettime(clock_id, &ts) == 0);
52 return timespec_load(&ts);
55 #if 0 /// UNNEEDED by elogind
56 nsec_t now_nsec(clockid_t clock_id) {
59 assert_se(clock_gettime(clock_id, &ts) == 0);
61 return timespec_load_nsec(&ts);
65 dual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
68 ts->realtime = now(CLOCK_REALTIME);
69 ts->monotonic = now(CLOCK_MONOTONIC);
74 dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
78 if (u == USEC_INFINITY || u <= 0) {
79 ts->realtime = ts->monotonic = u;
85 delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u;
86 ts->monotonic = usec_sub(now(CLOCK_MONOTONIC), delta);
91 #if 0 /// UNNEEDED by elogind
92 dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
96 if (u == USEC_INFINITY) {
97 ts->realtime = ts->monotonic = USEC_INFINITY;
102 delta = (int64_t) now(CLOCK_MONOTONIC) - (int64_t) u;
103 ts->realtime = usec_sub(now(CLOCK_REALTIME), delta);
108 dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, usec_t u) {
111 if (u == USEC_INFINITY) {
112 ts->realtime = ts->monotonic = USEC_INFINITY;
116 dual_timestamp_get(ts);
117 delta = (int64_t) now(clock_boottime_or_monotonic()) - (int64_t) u;
118 ts->realtime = usec_sub(ts->realtime, delta);
119 ts->monotonic = usec_sub(ts->monotonic, delta);
125 usec_t timespec_load(const struct timespec *ts) {
128 if (ts->tv_sec == (time_t) -1 &&
129 ts->tv_nsec == (long) -1)
130 return USEC_INFINITY;
132 if ((usec_t) ts->tv_sec > (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC)
133 return USEC_INFINITY;
136 (usec_t) ts->tv_sec * USEC_PER_SEC +
137 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
140 #if 0 /// UNNEEDED by elogind
141 static nsec_t timespec_load_nsec(const struct timespec *ts) {
144 if (ts->tv_sec == (time_t) -1 &&
145 ts->tv_nsec == (long) -1)
146 return NSEC_INFINITY;
149 (nsec_t) ts->tv_sec * NSEC_PER_SEC +
150 (nsec_t) ts->tv_nsec;
154 struct timespec *timespec_store(struct timespec *ts, usec_t u) {
157 if (u == USEC_INFINITY) {
158 ts->tv_sec = (time_t) -1;
159 ts->tv_nsec = (long) -1;
163 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
164 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
169 usec_t timeval_load(const struct timeval *tv) {
172 if (tv->tv_sec == (time_t) -1 &&
173 tv->tv_usec == (suseconds_t) -1)
174 return USEC_INFINITY;
176 if ((usec_t) tv->tv_sec > (UINT64_MAX - tv->tv_usec) / USEC_PER_SEC)
177 return USEC_INFINITY;
180 (usec_t) tv->tv_sec * USEC_PER_SEC +
181 (usec_t) tv->tv_usec;
184 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
187 if (u == USEC_INFINITY) {
188 tv->tv_sec = (time_t) -1;
189 tv->tv_usec = (suseconds_t) -1;
191 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
192 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
198 static char *format_timestamp_internal(char *buf, size_t l, usec_t t,
207 if (t <= 0 || t == USEC_INFINITY)
210 sec = (time_t) (t / USEC_PER_SEC);
211 localtime_or_gmtime_r(&sec, &tm, utc);
214 k = strftime(buf, l, "%a %Y-%m-%d %H:%M:%S", &tm);
216 k = strftime(buf, l, "%a %Y-%m-%d %H:%M:%S %Z", &tm);
221 snprintf(buf + strlen(buf), l - strlen(buf), ".%06llu", (unsigned long long) (t % USEC_PER_SEC));
222 if (strftime(buf + strlen(buf), l - strlen(buf), " %Z", &tm) <= 0)
229 char *format_timestamp(char *buf, size_t l, usec_t t) {
230 return format_timestamp_internal(buf, l, t, false, false);
233 #if 0 /// UNNEEDED by elogind
234 char *format_timestamp_utc(char *buf, size_t l, usec_t t) {
235 return format_timestamp_internal(buf, l, t, true, false);
239 char *format_timestamp_us(char *buf, size_t l, usec_t t) {
240 return format_timestamp_internal(buf, l, t, false, true);
243 #if 0 /// UNNEEDED by elogind
244 char *format_timestamp_us_utc(char *buf, size_t l, usec_t t) {
245 return format_timestamp_internal(buf, l, t, true, true);
249 char *format_timestamp_relative(char *buf, size_t l, usec_t t) {
253 if (t <= 0 || t == USEC_INFINITY)
256 n = now(CLOCK_REALTIME);
265 if (d >= USEC_PER_YEAR)
266 snprintf(buf, l, USEC_FMT " years " USEC_FMT " months %s",
268 (d % USEC_PER_YEAR) / USEC_PER_MONTH, s);
269 else if (d >= USEC_PER_MONTH)
270 snprintf(buf, l, USEC_FMT " months " USEC_FMT " days %s",
272 (d % USEC_PER_MONTH) / USEC_PER_DAY, s);
273 else if (d >= USEC_PER_WEEK)
274 snprintf(buf, l, USEC_FMT " weeks " USEC_FMT " days %s",
276 (d % USEC_PER_WEEK) / USEC_PER_DAY, s);
277 else if (d >= 2*USEC_PER_DAY)
278 snprintf(buf, l, USEC_FMT " days %s", d / USEC_PER_DAY, s);
279 else if (d >= 25*USEC_PER_HOUR)
280 snprintf(buf, l, "1 day " USEC_FMT "h %s",
281 (d - USEC_PER_DAY) / USEC_PER_HOUR, s);
282 else if (d >= 6*USEC_PER_HOUR)
283 snprintf(buf, l, USEC_FMT "h %s",
284 d / USEC_PER_HOUR, s);
285 else if (d >= USEC_PER_HOUR)
286 snprintf(buf, l, USEC_FMT "h " USEC_FMT "min %s",
288 (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s);
289 else if (d >= 5*USEC_PER_MINUTE)
290 snprintf(buf, l, USEC_FMT "min %s",
291 d / USEC_PER_MINUTE, s);
292 else if (d >= USEC_PER_MINUTE)
293 snprintf(buf, l, USEC_FMT "min " USEC_FMT "s %s",
295 (d % USEC_PER_MINUTE) / USEC_PER_SEC, s);
296 else if (d >= USEC_PER_SEC)
297 snprintf(buf, l, USEC_FMT "s %s",
298 d / USEC_PER_SEC, s);
299 else if (d >= USEC_PER_MSEC)
300 snprintf(buf, l, USEC_FMT "ms %s",
301 d / USEC_PER_MSEC, s);
303 snprintf(buf, l, USEC_FMT"us %s",
306 snprintf(buf, l, "now");
312 char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
313 static const struct {
317 { "y", USEC_PER_YEAR },
318 { "month", USEC_PER_MONTH },
319 { "w", USEC_PER_WEEK },
320 { "d", USEC_PER_DAY },
321 { "h", USEC_PER_HOUR },
322 { "min", USEC_PER_MINUTE },
323 { "s", USEC_PER_SEC },
324 { "ms", USEC_PER_MSEC },
330 bool something = false;
335 if (t == USEC_INFINITY) {
336 strncpy(p, "infinity", l-1);
342 strncpy(p, "0", l-1);
347 /* The result of this function can be parsed with parse_sec */
349 for (i = 0; i < ELEMENTSOF(table); i++) {
358 if (t < accuracy && something)
361 if (t < table[i].usec)
367 a = t / table[i].usec;
368 b = t % table[i].usec;
370 /* Let's see if we should shows this in dot notation */
371 if (t < USEC_PER_MINUTE && b > 0) {
376 for (cc = table[i].usec; cc > 1; cc /= 10)
379 for (cc = accuracy; cc > 1; cc /= 10) {
386 "%s"USEC_FMT".%0*llu%s",
390 (unsigned long long) b,
398 /* No? Then let's show it normally */
409 n = MIN((size_t) k, l);
422 #if 0 /// UNNEEDED by elogind
423 void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) {
429 if (!dual_timestamp_is_set(t))
432 fprintf(f, "%s="USEC_FMT" "USEC_FMT"\n",
438 int dual_timestamp_deserialize(const char *value, dual_timestamp *t) {
439 unsigned long long a, b;
444 if (sscanf(value, "%llu %llu", &a, &b) != 2) {
445 log_debug("Failed to parse finish timestamp value %s.", value);
455 int deserialize_timestamp_value(const char *value, usec_t *timestamp) {
460 r = safe_atou64(value, timestamp);
463 return log_debug_errno(r, "Failed to parse finish timestamp value \"%s\": %m", value);
468 int parse_timestamp(const char *t, usec_t *usec) {
469 static const struct {
493 usec_t x_usec, plus = 0, minus = 0, ret;
500 * 2012-09-22 16:34:22
501 * 2012-09-22 16:34 (seconds will be set to 0)
502 * 2012-09-22 (time will be set to 00:00:00)
503 * 16:34:22 (date will be set to today)
504 * 16:34 (date will be set to today, seconds to 0)
506 * yesterday (time is set to 00:00:00)
507 * today (time is set to 00:00:00)
508 * tomorrow (time is set to 00:00:00)
511 * @2147483647 (seconds since epoch)
519 return parse_sec(t + 1, usec);
521 ret = now(CLOCK_REALTIME);
526 else if (t[0] == '+') {
527 r = parse_sec(t+1, &plus);
533 } else if (t[0] == '-') {
534 r = parse_sec(t+1, &minus);
540 } else if ((k = endswith(t, " ago"))) {
541 t = strndupa(t, k - t);
543 r = parse_sec(t, &minus);
549 } else if ((k = endswith(t, " left"))) {
550 t = strndupa(t, k - t);
552 r = parse_sec(t, &plus);
559 utc = endswith_no_case(t, " UTC");
561 t = strndupa(t, utc - t);
563 x = ret / USEC_PER_SEC;
566 assert_se(localtime_or_gmtime_r(&x, &tm, utc));
569 if (streq(t, "today")) {
570 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
573 } else if (streq(t, "yesterday")) {
575 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
578 } else if (streq(t, "tomorrow")) {
580 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
585 for (i = 0; i < ELEMENTSOF(day_nr); i++) {
588 if (!startswith_no_case(t, day_nr[i].name))
591 skip = strlen(day_nr[i].name);
595 weekday = day_nr[i].nr;
601 k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
610 k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm);
619 k = strptime(t, "%y-%m-%d %H:%M", &tm);
626 k = strptime(t, "%Y-%m-%d %H:%M", &tm);
633 k = strptime(t, "%y-%m-%d", &tm);
635 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
640 k = strptime(t, "%Y-%m-%d", &tm);
642 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
647 k = strptime(t, "%H:%M:%S", &tm);
656 k = strptime(t, "%H:%M", &tm);
669 r = parse_fractional_part_u(&k, 6, &add);
681 x = mktime_or_timegm(&tm, utc);
682 if (x == (time_t) -1)
685 if (weekday >= 0 && tm.tm_wday != weekday)
688 ret = (usec_t) x * USEC_PER_SEC + x_usec;
703 int parse_time(const char *t, usec_t *usec, usec_t default_unit) {
705 static const struct {
709 { "seconds", USEC_PER_SEC },
710 { "second", USEC_PER_SEC },
711 { "sec", USEC_PER_SEC },
712 { "s", USEC_PER_SEC },
713 { "minutes", USEC_PER_MINUTE },
714 { "minute", USEC_PER_MINUTE },
715 { "min", USEC_PER_MINUTE },
716 { "months", USEC_PER_MONTH },
717 { "month", USEC_PER_MONTH },
718 { "M", USEC_PER_MONTH },
719 { "msec", USEC_PER_MSEC },
720 { "ms", USEC_PER_MSEC },
721 { "m", USEC_PER_MINUTE },
722 { "hours", USEC_PER_HOUR },
723 { "hour", USEC_PER_HOUR },
724 { "hr", USEC_PER_HOUR },
725 { "h", USEC_PER_HOUR },
726 { "days", USEC_PER_DAY },
727 { "day", USEC_PER_DAY },
728 { "d", USEC_PER_DAY },
729 { "weeks", USEC_PER_WEEK },
730 { "week", USEC_PER_WEEK },
731 { "w", USEC_PER_WEEK },
732 { "years", USEC_PER_YEAR },
733 { "year", USEC_PER_YEAR },
734 { "y", USEC_PER_YEAR },
741 bool something = false;
745 assert(default_unit > 0);
749 p += strspn(p, WHITESPACE);
750 s = startswith(p, "infinity");
752 s += strspn(s, WHITESPACE);
756 *usec = USEC_INFINITY;
764 usec_t multiplier, k;
766 p += strspn(p, WHITESPACE);
776 l = strtoll(p, &e, 10);
788 z = strtoll(b, &e, 10);
803 e += strspn(e, WHITESPACE);
805 for (i = 0; i < ELEMENTSOF(table); i++)
806 if (startswith(e, table[i].suffix)) {
807 multiplier = table[i].usec;
808 p = e + strlen(table[i].suffix);
812 if (i >= ELEMENTSOF(table)) {
813 multiplier = default_unit;
819 k = (usec_t) z * multiplier;
824 r += (usec_t) l * multiplier + k;
832 int parse_sec(const char *t, usec_t *usec) {
833 return parse_time(t, usec, USEC_PER_SEC);
836 #if 0 /// UNNEEDED by elogind
837 int parse_nsec(const char *t, nsec_t *nsec) {
838 static const struct {
842 { "seconds", NSEC_PER_SEC },
843 { "second", NSEC_PER_SEC },
844 { "sec", NSEC_PER_SEC },
845 { "s", NSEC_PER_SEC },
846 { "minutes", NSEC_PER_MINUTE },
847 { "minute", NSEC_PER_MINUTE },
848 { "min", NSEC_PER_MINUTE },
849 { "months", NSEC_PER_MONTH },
850 { "month", NSEC_PER_MONTH },
851 { "msec", NSEC_PER_MSEC },
852 { "ms", NSEC_PER_MSEC },
853 { "m", NSEC_PER_MINUTE },
854 { "hours", NSEC_PER_HOUR },
855 { "hour", NSEC_PER_HOUR },
856 { "hr", NSEC_PER_HOUR },
857 { "h", NSEC_PER_HOUR },
858 { "days", NSEC_PER_DAY },
859 { "day", NSEC_PER_DAY },
860 { "d", NSEC_PER_DAY },
861 { "weeks", NSEC_PER_WEEK },
862 { "week", NSEC_PER_WEEK },
863 { "w", NSEC_PER_WEEK },
864 { "years", NSEC_PER_YEAR },
865 { "year", NSEC_PER_YEAR },
866 { "y", NSEC_PER_YEAR },
867 { "usec", NSEC_PER_USEC },
868 { "us", NSEC_PER_USEC },
871 { "", 1ULL }, /* default is nsec */
876 bool something = false;
883 p += strspn(p, WHITESPACE);
884 s = startswith(p, "infinity");
886 s += strspn(s, WHITESPACE);
890 *nsec = NSEC_INFINITY;
899 p += strspn(p, WHITESPACE);
909 l = strtoll(p, &e, 10);
921 z = strtoll(b, &e, 10);
936 e += strspn(e, WHITESPACE);
938 for (i = 0; i < ELEMENTSOF(table); i++)
939 if (startswith(e, table[i].suffix)) {
940 nsec_t k = (nsec_t) z * table[i].nsec;
945 r += (nsec_t) l * table[i].nsec + k;
946 p = e + strlen(table[i].suffix);
952 if (i >= ELEMENTSOF(table))
962 bool ntp_synced(void) {
963 struct timex txc = {};
965 if (adjtimex(&txc) < 0)
968 if (txc.status & STA_UNSYNC)
974 int get_timezones(char ***ret) {
975 _cleanup_fclose_ FILE *f = NULL;
976 _cleanup_strv_free_ char **zones = NULL;
977 size_t n_zones = 0, n_allocated = 0;
981 zones = strv_new("UTC", NULL);
988 f = fopen("/usr/share/zoneinfo/zone.tab", "re");
992 FOREACH_LINE(l, f, return -errno) {
998 if (isempty(p) || *p == '#')
1001 /* Skip over country code */
1002 p += strcspn(p, WHITESPACE);
1003 p += strspn(p, WHITESPACE);
1005 /* Skip over coordinates */
1006 p += strcspn(p, WHITESPACE);
1007 p += strspn(p, WHITESPACE);
1009 /* Found timezone name */
1010 k = strcspn(p, WHITESPACE);
1018 if (!GREEDY_REALLOC(zones, n_allocated, n_zones + 2)) {
1023 zones[n_zones++] = w;
1024 zones[n_zones] = NULL;
1029 } else if (errno != ENOENT)
1038 bool timezone_is_valid(const char *name) {
1049 for (p = name; *p; p++) {
1050 if (!(*p >= '0' && *p <= '9') &&
1051 !(*p >= 'a' && *p <= 'z') &&
1052 !(*p >= 'A' && *p <= 'Z') &&
1053 !(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
1069 t = strjoina("/usr/share/zoneinfo/", name);
1070 if (stat(t, &st) < 0)
1073 if (!S_ISREG(st.st_mode))
1079 clockid_t clock_boottime_or_monotonic(void) {
1080 static clockid_t clock = -1;
1086 fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC);
1088 clock = CLOCK_MONOTONIC;
1091 clock = CLOCK_BOOTTIME;
1097 int get_timezone(char **tz) {
1098 _cleanup_free_ char *t = NULL;
1103 r = readlink_malloc("/etc/localtime", &t);
1105 return r; /* returns EINVAL if not a symlink */
1107 e = path_startswith(t, "/usr/share/zoneinfo/");
1109 e = path_startswith(t, "../usr/share/zoneinfo/");
1113 if (!timezone_is_valid(e))
1124 time_t mktime_or_timegm(struct tm *tm, bool utc) {
1125 return utc ? timegm(tm) : mktime(tm);
1129 struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc) {
1130 return utc ? gmtime_r(t, tm) : localtime_r(t, tm);
1133 #if 0 /// UNNEEDED by elogind
1134 unsigned long usec_to_jiffies(usec_t u) {
1135 static thread_local unsigned long hz = 0;
1139 r = sysconf(_SC_CLK_TCK);
1142 hz = (unsigned long) r;
1145 return DIV_ROUND_UP(u , USEC_PER_SEC / hz);