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 dual timestamp value \"%s\": %m", value);
455 int timestamp_deserialize(const char *value, usec_t *timestamp) {
460 r = safe_atou64(value, timestamp);
462 return log_debug_errno(r, "Failed to parse timestamp value \"%s\": %m", value);
467 int parse_timestamp(const char *t, usec_t *usec) {
468 static const struct {
492 usec_t x_usec, plus = 0, minus = 0, ret;
499 * 2012-09-22 16:34:22
500 * 2012-09-22 16:34 (seconds will be set to 0)
501 * 2012-09-22 (time will be set to 00:00:00)
502 * 16:34:22 (date will be set to today)
503 * 16:34 (date will be set to today, seconds to 0)
505 * yesterday (time is set to 00:00:00)
506 * today (time is set to 00:00:00)
507 * tomorrow (time is set to 00:00:00)
510 * @2147483647 (seconds since epoch)
518 return parse_sec(t + 1, usec);
520 ret = now(CLOCK_REALTIME);
525 else if (t[0] == '+') {
526 r = parse_sec(t+1, &plus);
532 } else if (t[0] == '-') {
533 r = parse_sec(t+1, &minus);
539 } else if ((k = endswith(t, " ago"))) {
540 t = strndupa(t, k - t);
542 r = parse_sec(t, &minus);
548 } else if ((k = endswith(t, " left"))) {
549 t = strndupa(t, k - t);
551 r = parse_sec(t, &plus);
558 utc = endswith_no_case(t, " UTC");
560 t = strndupa(t, utc - t);
562 x = ret / USEC_PER_SEC;
565 assert_se(localtime_or_gmtime_r(&x, &tm, utc));
568 if (streq(t, "today")) {
569 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
572 } else if (streq(t, "yesterday")) {
574 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
577 } else if (streq(t, "tomorrow")) {
579 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
584 for (i = 0; i < ELEMENTSOF(day_nr); i++) {
587 if (!startswith_no_case(t, day_nr[i].name))
590 skip = strlen(day_nr[i].name);
594 weekday = day_nr[i].nr;
600 k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
609 k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm);
618 k = strptime(t, "%y-%m-%d %H:%M", &tm);
625 k = strptime(t, "%Y-%m-%d %H:%M", &tm);
632 k = strptime(t, "%y-%m-%d", &tm);
634 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
639 k = strptime(t, "%Y-%m-%d", &tm);
641 tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
646 k = strptime(t, "%H:%M:%S", &tm);
655 k = strptime(t, "%H:%M", &tm);
668 r = parse_fractional_part_u(&k, 6, &add);
680 x = mktime_or_timegm(&tm, utc);
681 if (x == (time_t) -1)
684 if (weekday >= 0 && tm.tm_wday != weekday)
687 ret = (usec_t) x * USEC_PER_SEC + x_usec;
702 int parse_time(const char *t, usec_t *usec, usec_t default_unit) {
704 static const struct {
708 { "seconds", USEC_PER_SEC },
709 { "second", USEC_PER_SEC },
710 { "sec", USEC_PER_SEC },
711 { "s", USEC_PER_SEC },
712 { "minutes", USEC_PER_MINUTE },
713 { "minute", USEC_PER_MINUTE },
714 { "min", USEC_PER_MINUTE },
715 { "months", USEC_PER_MONTH },
716 { "month", USEC_PER_MONTH },
717 { "M", USEC_PER_MONTH },
718 { "msec", USEC_PER_MSEC },
719 { "ms", USEC_PER_MSEC },
720 { "m", USEC_PER_MINUTE },
721 { "hours", USEC_PER_HOUR },
722 { "hour", USEC_PER_HOUR },
723 { "hr", USEC_PER_HOUR },
724 { "h", USEC_PER_HOUR },
725 { "days", USEC_PER_DAY },
726 { "day", USEC_PER_DAY },
727 { "d", USEC_PER_DAY },
728 { "weeks", USEC_PER_WEEK },
729 { "week", USEC_PER_WEEK },
730 { "w", USEC_PER_WEEK },
731 { "years", USEC_PER_YEAR },
732 { "year", USEC_PER_YEAR },
733 { "y", USEC_PER_YEAR },
740 bool something = false;
744 assert(default_unit > 0);
748 p += strspn(p, WHITESPACE);
749 s = startswith(p, "infinity");
751 s += strspn(s, WHITESPACE);
755 *usec = USEC_INFINITY;
763 usec_t multiplier, k;
765 p += strspn(p, WHITESPACE);
775 l = strtoll(p, &e, 10);
787 z = strtoll(b, &e, 10);
802 e += strspn(e, WHITESPACE);
804 for (i = 0; i < ELEMENTSOF(table); i++)
805 if (startswith(e, table[i].suffix)) {
806 multiplier = table[i].usec;
807 p = e + strlen(table[i].suffix);
811 if (i >= ELEMENTSOF(table)) {
812 multiplier = default_unit;
818 k = (usec_t) z * multiplier;
823 r += (usec_t) l * multiplier + k;
831 int parse_sec(const char *t, usec_t *usec) {
832 return parse_time(t, usec, USEC_PER_SEC);
835 #if 0 /// UNNEEDED by elogind
836 int parse_nsec(const char *t, nsec_t *nsec) {
837 static const struct {
841 { "seconds", NSEC_PER_SEC },
842 { "second", NSEC_PER_SEC },
843 { "sec", NSEC_PER_SEC },
844 { "s", NSEC_PER_SEC },
845 { "minutes", NSEC_PER_MINUTE },
846 { "minute", NSEC_PER_MINUTE },
847 { "min", NSEC_PER_MINUTE },
848 { "months", NSEC_PER_MONTH },
849 { "month", NSEC_PER_MONTH },
850 { "msec", NSEC_PER_MSEC },
851 { "ms", NSEC_PER_MSEC },
852 { "m", NSEC_PER_MINUTE },
853 { "hours", NSEC_PER_HOUR },
854 { "hour", NSEC_PER_HOUR },
855 { "hr", NSEC_PER_HOUR },
856 { "h", NSEC_PER_HOUR },
857 { "days", NSEC_PER_DAY },
858 { "day", NSEC_PER_DAY },
859 { "d", NSEC_PER_DAY },
860 { "weeks", NSEC_PER_WEEK },
861 { "week", NSEC_PER_WEEK },
862 { "w", NSEC_PER_WEEK },
863 { "years", NSEC_PER_YEAR },
864 { "year", NSEC_PER_YEAR },
865 { "y", NSEC_PER_YEAR },
866 { "usec", NSEC_PER_USEC },
867 { "us", NSEC_PER_USEC },
870 { "", 1ULL }, /* default is nsec */
875 bool something = false;
882 p += strspn(p, WHITESPACE);
883 s = startswith(p, "infinity");
885 s += strspn(s, WHITESPACE);
889 *nsec = NSEC_INFINITY;
898 p += strspn(p, WHITESPACE);
908 l = strtoll(p, &e, 10);
920 z = strtoll(b, &e, 10);
935 e += strspn(e, WHITESPACE);
937 for (i = 0; i < ELEMENTSOF(table); i++)
938 if (startswith(e, table[i].suffix)) {
939 nsec_t k = (nsec_t) z * table[i].nsec;
944 r += (nsec_t) l * table[i].nsec + k;
945 p = e + strlen(table[i].suffix);
951 if (i >= ELEMENTSOF(table))
961 bool ntp_synced(void) {
962 struct timex txc = {};
964 if (adjtimex(&txc) < 0)
967 if (txc.status & STA_UNSYNC)
973 int get_timezones(char ***ret) {
974 _cleanup_fclose_ FILE *f = NULL;
975 _cleanup_strv_free_ char **zones = NULL;
976 size_t n_zones = 0, n_allocated = 0;
980 zones = strv_new("UTC", NULL);
987 f = fopen("/usr/share/zoneinfo/zone.tab", "re");
991 FOREACH_LINE(l, f, return -errno) {
997 if (isempty(p) || *p == '#')
1000 /* Skip over country code */
1001 p += strcspn(p, WHITESPACE);
1002 p += strspn(p, WHITESPACE);
1004 /* Skip over coordinates */
1005 p += strcspn(p, WHITESPACE);
1006 p += strspn(p, WHITESPACE);
1008 /* Found timezone name */
1009 k = strcspn(p, WHITESPACE);
1017 if (!GREEDY_REALLOC(zones, n_allocated, n_zones + 2)) {
1022 zones[n_zones++] = w;
1023 zones[n_zones] = NULL;
1028 } else if (errno != ENOENT)
1037 bool timezone_is_valid(const char *name) {
1048 for (p = name; *p; p++) {
1049 if (!(*p >= '0' && *p <= '9') &&
1050 !(*p >= 'a' && *p <= 'z') &&
1051 !(*p >= 'A' && *p <= 'Z') &&
1052 !(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
1068 t = strjoina("/usr/share/zoneinfo/", name);
1069 if (stat(t, &st) < 0)
1072 if (!S_ISREG(st.st_mode))
1078 bool clock_boottime_supported(void) {
1079 static int supported = -1;
1081 /* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */
1083 if (supported < 0) {
1086 fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC);
1098 clockid_t clock_boottime_or_monotonic(void) {
1099 if (clock_boottime_supported())
1100 return CLOCK_BOOTTIME;
1102 return CLOCK_MONOTONIC;
1105 int get_timezone(char **tz) {
1106 _cleanup_free_ char *t = NULL;
1111 r = readlink_malloc("/etc/localtime", &t);
1113 return r; /* returns EINVAL if not a symlink */
1115 e = path_startswith(t, "/usr/share/zoneinfo/");
1117 e = path_startswith(t, "../usr/share/zoneinfo/");
1121 if (!timezone_is_valid(e))
1132 time_t mktime_or_timegm(struct tm *tm, bool utc) {
1133 return utc ? timegm(tm) : mktime(tm);
1137 struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc) {
1138 return utc ? gmtime_r(t, tm) : localtime_r(t, tm);
1141 #if 0 /// UNNEEDED by elogind
1142 unsigned long usec_to_jiffies(usec_t u) {
1143 static thread_local unsigned long hz = 0;
1147 r = sysconf(_SC_CLK_TCK);
1150 hz = (unsigned long) r;
1153 return DIV_ROUND_UP(u , USEC_PER_SEC / hz);