]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/time-util.c
130acaa9de138949f7ae605b556fa85d714fc1ff
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 nsec_t
timespec_load_nsec(const struct timespec
*ts
);
45 static clockid_t
map_clock_id(clockid_t c
) {
47 /* Some more exotic archs (s390, ppc, …) lack the "ALARM" flavour of the clocks. Thus, clock_gettime() will
48 * fail for them. Since they are essentially the same as their non-ALARM pendants (their only difference is
49 * when timers are set on them), let's just map them accordingly. This way, we can get the correct time even on
54 case CLOCK_BOOTTIME_ALARM
:
55 return CLOCK_BOOTTIME
;
57 case CLOCK_REALTIME_ALARM
:
58 return CLOCK_REALTIME
;
65 usec_t
now(clockid_t clock_id
) {
68 assert_se(clock_gettime(map_clock_id(clock_id
), &ts
) == 0);
70 return timespec_load(&ts
);
73 nsec_t
now_nsec(clockid_t clock_id
) {
76 assert_se(clock_gettime(map_clock_id(clock_id
), &ts
) == 0);
78 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 dual_timestamp
* dual_timestamp_from_realtime(dual_timestamp
*ts
, usec_t u
) {
94 if (u
== USEC_INFINITY
|| u
<= 0) {
95 ts
->realtime
= ts
->monotonic
= u
;
101 delta
= (int64_t) now(CLOCK_REALTIME
) - (int64_t) u
;
102 ts
->monotonic
= usec_sub(now(CLOCK_MONOTONIC
), delta
);
107 dual_timestamp
* dual_timestamp_from_monotonic(dual_timestamp
*ts
, usec_t u
) {
111 if (u
== USEC_INFINITY
) {
112 ts
->realtime
= ts
->monotonic
= USEC_INFINITY
;
117 delta
= (int64_t) now(CLOCK_MONOTONIC
) - (int64_t) u
;
118 ts
->realtime
= usec_sub(now(CLOCK_REALTIME
), delta
);
123 dual_timestamp
* dual_timestamp_from_boottime_or_monotonic(dual_timestamp
*ts
, usec_t u
) {
126 if (u
== USEC_INFINITY
) {
127 ts
->realtime
= ts
->monotonic
= USEC_INFINITY
;
131 dual_timestamp_get(ts
);
132 delta
= (int64_t) now(clock_boottime_or_monotonic()) - (int64_t) u
;
133 ts
->realtime
= usec_sub(ts
->realtime
, delta
);
134 ts
->monotonic
= usec_sub(ts
->monotonic
, delta
);
139 usec_t
timespec_load(const struct timespec
*ts
) {
142 if (ts
->tv_sec
== (time_t) -1 &&
143 ts
->tv_nsec
== (long) -1)
144 return USEC_INFINITY
;
146 if ((usec_t
) ts
->tv_sec
> (UINT64_MAX
- (ts
->tv_nsec
/ NSEC_PER_USEC
)) / USEC_PER_SEC
)
147 return USEC_INFINITY
;
150 (usec_t
) ts
->tv_sec
* USEC_PER_SEC
+
151 (usec_t
) ts
->tv_nsec
/ NSEC_PER_USEC
;
154 static nsec_t
timespec_load_nsec(const struct timespec
*ts
) {
157 if (ts
->tv_sec
== (time_t) -1 &&
158 ts
->tv_nsec
== (long) -1)
159 return NSEC_INFINITY
;
162 (nsec_t
) ts
->tv_sec
* NSEC_PER_SEC
+
163 (nsec_t
) ts
->tv_nsec
;
166 struct timespec
*timespec_store(struct timespec
*ts
, usec_t u
) {
169 if (u
== USEC_INFINITY
) {
170 ts
->tv_sec
= (time_t) -1;
171 ts
->tv_nsec
= (long) -1;
175 ts
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
176 ts
->tv_nsec
= (long int) ((u
% USEC_PER_SEC
) * NSEC_PER_USEC
);
181 usec_t
timeval_load(const struct timeval
*tv
) {
184 if (tv
->tv_sec
== (time_t) -1 &&
185 tv
->tv_usec
== (suseconds_t
) -1)
186 return USEC_INFINITY
;
188 if ((usec_t
) tv
->tv_sec
> (UINT64_MAX
- tv
->tv_usec
) / USEC_PER_SEC
)
189 return USEC_INFINITY
;
192 (usec_t
) tv
->tv_sec
* USEC_PER_SEC
+
193 (usec_t
) tv
->tv_usec
;
196 struct timeval
*timeval_store(struct timeval
*tv
, usec_t u
) {
199 if (u
== USEC_INFINITY
) {
200 tv
->tv_sec
= (time_t) -1;
201 tv
->tv_usec
= (suseconds_t
) -1;
203 tv
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
204 tv
->tv_usec
= (suseconds_t
) (u
% USEC_PER_SEC
);
210 static char *format_timestamp_internal(char *buf
, size_t l
, usec_t t
,
219 if (t
<= 0 || t
== USEC_INFINITY
)
222 sec
= (time_t) (t
/ USEC_PER_SEC
);
223 localtime_or_gmtime_r(&sec
, &tm
, utc
);
226 k
= strftime(buf
, l
, "%a %Y-%m-%d %H:%M:%S", &tm
);
228 k
= strftime(buf
, l
, "%a %Y-%m-%d %H:%M:%S %Z", &tm
);
233 snprintf(buf
+ strlen(buf
), l
- strlen(buf
), ".%06llu", (unsigned long long) (t
% USEC_PER_SEC
));
234 if (strftime(buf
+ strlen(buf
), l
- strlen(buf
), " %Z", &tm
) <= 0)
241 char *format_timestamp(char *buf
, size_t l
, usec_t t
) {
242 return format_timestamp_internal(buf
, l
, t
, false, false);
245 char *format_timestamp_utc(char *buf
, size_t l
, usec_t t
) {
246 return format_timestamp_internal(buf
, l
, t
, true, false);
249 char *format_timestamp_us(char *buf
, size_t l
, usec_t t
) {
250 return format_timestamp_internal(buf
, l
, t
, false, true);
253 char *format_timestamp_us_utc(char *buf
, size_t l
, usec_t t
) {
254 return format_timestamp_internal(buf
, l
, t
, true, true);
257 char *format_timestamp_relative(char *buf
, size_t l
, usec_t t
) {
261 if (t
<= 0 || t
== USEC_INFINITY
)
264 n
= now(CLOCK_REALTIME
);
273 if (d
>= USEC_PER_YEAR
)
274 snprintf(buf
, l
, USEC_FMT
" years " USEC_FMT
" months %s",
276 (d
% USEC_PER_YEAR
) / USEC_PER_MONTH
, s
);
277 else if (d
>= USEC_PER_MONTH
)
278 snprintf(buf
, l
, USEC_FMT
" months " USEC_FMT
" days %s",
280 (d
% USEC_PER_MONTH
) / USEC_PER_DAY
, s
);
281 else if (d
>= USEC_PER_WEEK
)
282 snprintf(buf
, l
, USEC_FMT
" weeks " USEC_FMT
" days %s",
284 (d
% USEC_PER_WEEK
) / USEC_PER_DAY
, s
);
285 else if (d
>= 2*USEC_PER_DAY
)
286 snprintf(buf
, l
, USEC_FMT
" days %s", d
/ USEC_PER_DAY
, s
);
287 else if (d
>= 25*USEC_PER_HOUR
)
288 snprintf(buf
, l
, "1 day " USEC_FMT
"h %s",
289 (d
- USEC_PER_DAY
) / USEC_PER_HOUR
, s
);
290 else if (d
>= 6*USEC_PER_HOUR
)
291 snprintf(buf
, l
, USEC_FMT
"h %s",
292 d
/ USEC_PER_HOUR
, s
);
293 else if (d
>= USEC_PER_HOUR
)
294 snprintf(buf
, l
, USEC_FMT
"h " USEC_FMT
"min %s",
296 (d
% USEC_PER_HOUR
) / USEC_PER_MINUTE
, s
);
297 else if (d
>= 5*USEC_PER_MINUTE
)
298 snprintf(buf
, l
, USEC_FMT
"min %s",
299 d
/ USEC_PER_MINUTE
, s
);
300 else if (d
>= USEC_PER_MINUTE
)
301 snprintf(buf
, l
, USEC_FMT
"min " USEC_FMT
"s %s",
303 (d
% USEC_PER_MINUTE
) / USEC_PER_SEC
, s
);
304 else if (d
>= USEC_PER_SEC
)
305 snprintf(buf
, l
, USEC_FMT
"s %s",
306 d
/ USEC_PER_SEC
, s
);
307 else if (d
>= USEC_PER_MSEC
)
308 snprintf(buf
, l
, USEC_FMT
"ms %s",
309 d
/ USEC_PER_MSEC
, s
);
311 snprintf(buf
, l
, USEC_FMT
"us %s",
314 snprintf(buf
, l
, "now");
320 char *format_timespan(char *buf
, size_t l
, usec_t t
, usec_t accuracy
) {
321 static const struct {
325 { "y", USEC_PER_YEAR
},
326 { "month", USEC_PER_MONTH
},
327 { "w", USEC_PER_WEEK
},
328 { "d", USEC_PER_DAY
},
329 { "h", USEC_PER_HOUR
},
330 { "min", USEC_PER_MINUTE
},
331 { "s", USEC_PER_SEC
},
332 { "ms", USEC_PER_MSEC
},
338 bool something
= false;
343 if (t
== USEC_INFINITY
) {
344 strncpy(p
, "infinity", l
-1);
350 strncpy(p
, "0", l
-1);
355 /* The result of this function can be parsed with parse_sec */
357 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
366 if (t
< accuracy
&& something
)
369 if (t
< table
[i
].usec
)
375 a
= t
/ table
[i
].usec
;
376 b
= t
% table
[i
].usec
;
378 /* Let's see if we should shows this in dot notation */
379 if (t
< USEC_PER_MINUTE
&& b
> 0) {
384 for (cc
= table
[i
].usec
; cc
> 1; cc
/= 10)
387 for (cc
= accuracy
; cc
> 1; cc
/= 10) {
394 "%s"USEC_FMT
".%0*llu%s",
398 (unsigned long long) b
,
406 /* No? Then let's show it normally */
417 n
= MIN((size_t) k
, l
);
430 void dual_timestamp_serialize(FILE *f
, const char *name
, dual_timestamp
*t
) {
436 if (!dual_timestamp_is_set(t
))
439 fprintf(f
, "%s="USEC_FMT
" "USEC_FMT
"\n",
445 int dual_timestamp_deserialize(const char *value
, dual_timestamp
*t
) {
446 unsigned long long a
, b
;
451 if (sscanf(value
, "%llu %llu", &a
, &b
) != 2) {
452 log_debug("Failed to parse dual timestamp value \"%s\": %m", value
);
462 int timestamp_deserialize(const char *value
, usec_t
*timestamp
) {
467 r
= safe_atou64(value
, timestamp
);
469 return log_debug_errno(r
, "Failed to parse timestamp value \"%s\": %m", value
);
474 int parse_timestamp(const char *t
, usec_t
*usec
) {
475 static const struct {
499 usec_t x_usec
, plus
= 0, minus
= 0, ret
;
506 * 2012-09-22 16:34:22
507 * 2012-09-22 16:34 (seconds will be set to 0)
508 * 2012-09-22 (time will be set to 00:00:00)
509 * 16:34:22 (date will be set to today)
510 * 16:34 (date will be set to today, seconds to 0)
512 * yesterday (time is set to 00:00:00)
513 * today (time is set to 00:00:00)
514 * tomorrow (time is set to 00:00:00)
517 * @2147483647 (seconds since epoch)
525 return parse_sec(t
+ 1, usec
);
527 ret
= now(CLOCK_REALTIME
);
532 else if (t
[0] == '+') {
533 r
= parse_sec(t
+1, &plus
);
539 } else if (t
[0] == '-') {
540 r
= parse_sec(t
+1, &minus
);
546 } else if ((k
= endswith(t
, " ago"))) {
547 t
= strndupa(t
, k
- t
);
549 r
= parse_sec(t
, &minus
);
555 } else if ((k
= endswith(t
, " left"))) {
556 t
= strndupa(t
, k
- t
);
558 r
= parse_sec(t
, &plus
);
565 utc
= endswith_no_case(t
, " UTC");
567 t
= strndupa(t
, utc
- t
);
569 x
= ret
/ USEC_PER_SEC
;
572 assert_se(localtime_or_gmtime_r(&x
, &tm
, utc
));
575 if (streq(t
, "today")) {
576 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
579 } else if (streq(t
, "yesterday")) {
581 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
584 } else if (streq(t
, "tomorrow")) {
586 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
591 for (i
= 0; i
< ELEMENTSOF(day_nr
); i
++) {
594 if (!startswith_no_case(t
, day_nr
[i
].name
))
597 skip
= strlen(day_nr
[i
].name
);
601 weekday
= day_nr
[i
].nr
;
607 k
= strptime(t
, "%y-%m-%d %H:%M:%S", &tm
);
616 k
= strptime(t
, "%Y-%m-%d %H:%M:%S", &tm
);
625 k
= strptime(t
, "%y-%m-%d %H:%M", &tm
);
632 k
= strptime(t
, "%Y-%m-%d %H:%M", &tm
);
639 k
= strptime(t
, "%y-%m-%d", &tm
);
641 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
646 k
= strptime(t
, "%Y-%m-%d", &tm
);
648 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
653 k
= strptime(t
, "%H:%M:%S", &tm
);
662 k
= strptime(t
, "%H:%M", &tm
);
675 r
= parse_fractional_part_u(&k
, 6, &add
);
687 x
= mktime_or_timegm(&tm
, utc
);
688 if (x
== (time_t) -1)
691 if (weekday
>= 0 && tm
.tm_wday
!= weekday
)
694 ret
= (usec_t
) x
* USEC_PER_SEC
+ x_usec
;
708 static char* extract_multiplier(char *p
, usec_t
*multiplier
) {
709 static const struct {
713 { "seconds", USEC_PER_SEC
},
714 { "second", USEC_PER_SEC
},
715 { "sec", USEC_PER_SEC
},
716 { "s", USEC_PER_SEC
},
717 { "minutes", USEC_PER_MINUTE
},
718 { "minute", USEC_PER_MINUTE
},
719 { "min", USEC_PER_MINUTE
},
720 { "months", USEC_PER_MONTH
},
721 { "month", USEC_PER_MONTH
},
722 { "M", USEC_PER_MONTH
},
723 { "msec", USEC_PER_MSEC
},
724 { "ms", USEC_PER_MSEC
},
725 { "m", USEC_PER_MINUTE
},
726 { "hours", USEC_PER_HOUR
},
727 { "hour", USEC_PER_HOUR
},
728 { "hr", USEC_PER_HOUR
},
729 { "h", USEC_PER_HOUR
},
730 { "days", USEC_PER_DAY
},
731 { "day", USEC_PER_DAY
},
732 { "d", USEC_PER_DAY
},
733 { "weeks", USEC_PER_WEEK
},
734 { "week", USEC_PER_WEEK
},
735 { "w", USEC_PER_WEEK
},
736 { "years", USEC_PER_YEAR
},
737 { "year", USEC_PER_YEAR
},
738 { "y", USEC_PER_YEAR
},
744 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
747 e
= startswith(p
, table
[i
].suffix
);
749 *multiplier
= table
[i
].usec
;
757 int parse_time(const char *t
, usec_t
*usec
, usec_t default_unit
) {
760 bool something
= false;
764 assert(default_unit
> 0);
768 p
+= strspn(p
, WHITESPACE
);
769 s
= startswith(p
, "infinity");
771 s
+= strspn(s
, WHITESPACE
);
775 *usec
= USEC_INFINITY
;
783 usec_t multiplier
= default_unit
, k
;
785 p
+= strspn(p
, WHITESPACE
);
795 l
= strtoll(p
, &e
, 10);
805 z
= strtoll(b
, &e
, 10);
820 e
+= strspn(e
, WHITESPACE
);
821 p
= extract_multiplier(e
, &multiplier
);
825 k
= (usec_t
) z
* multiplier
;
830 r
+= (usec_t
) l
* multiplier
+ k
;
838 int parse_sec(const char *t
, usec_t
*usec
) {
839 return parse_time(t
, usec
, USEC_PER_SEC
);
842 int parse_nsec(const char *t
, nsec_t
*nsec
) {
843 static const struct {
847 { "seconds", NSEC_PER_SEC
},
848 { "second", NSEC_PER_SEC
},
849 { "sec", NSEC_PER_SEC
},
850 { "s", NSEC_PER_SEC
},
851 { "minutes", NSEC_PER_MINUTE
},
852 { "minute", NSEC_PER_MINUTE
},
853 { "min", NSEC_PER_MINUTE
},
854 { "months", NSEC_PER_MONTH
},
855 { "month", NSEC_PER_MONTH
},
856 { "msec", NSEC_PER_MSEC
},
857 { "ms", NSEC_PER_MSEC
},
858 { "m", NSEC_PER_MINUTE
},
859 { "hours", NSEC_PER_HOUR
},
860 { "hour", NSEC_PER_HOUR
},
861 { "hr", NSEC_PER_HOUR
},
862 { "h", NSEC_PER_HOUR
},
863 { "days", NSEC_PER_DAY
},
864 { "day", NSEC_PER_DAY
},
865 { "d", NSEC_PER_DAY
},
866 { "weeks", NSEC_PER_WEEK
},
867 { "week", NSEC_PER_WEEK
},
868 { "w", NSEC_PER_WEEK
},
869 { "years", NSEC_PER_YEAR
},
870 { "year", NSEC_PER_YEAR
},
871 { "y", NSEC_PER_YEAR
},
872 { "usec", NSEC_PER_USEC
},
873 { "us", NSEC_PER_USEC
},
876 { "", 1ULL }, /* default is nsec */
881 bool something
= false;
888 p
+= strspn(p
, WHITESPACE
);
889 s
= startswith(p
, "infinity");
891 s
+= strspn(s
, WHITESPACE
);
895 *nsec
= NSEC_INFINITY
;
904 p
+= strspn(p
, WHITESPACE
);
914 l
= strtoll(p
, &e
, 10);
926 z
= strtoll(b
, &e
, 10);
941 e
+= strspn(e
, WHITESPACE
);
943 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
944 if (startswith(e
, table
[i
].suffix
)) {
945 nsec_t k
= (nsec_t
) z
* table
[i
].nsec
;
950 r
+= (nsec_t
) l
* table
[i
].nsec
+ k
;
951 p
= e
+ strlen(table
[i
].suffix
);
957 if (i
>= ELEMENTSOF(table
))
967 bool ntp_synced(void) {
968 struct timex txc
= {};
970 if (adjtimex(&txc
) < 0)
973 if (txc
.status
& STA_UNSYNC
)
979 int get_timezones(char ***ret
) {
980 _cleanup_fclose_
FILE *f
= NULL
;
981 _cleanup_strv_free_
char **zones
= NULL
;
982 size_t n_zones
= 0, n_allocated
= 0;
986 zones
= strv_new("UTC", NULL
);
993 f
= fopen("/usr/share/zoneinfo/zone.tab", "re");
997 FOREACH_LINE(l
, f
, return -errno
) {
1003 if (isempty(p
) || *p
== '#')
1006 /* Skip over country code */
1007 p
+= strcspn(p
, WHITESPACE
);
1008 p
+= strspn(p
, WHITESPACE
);
1010 /* Skip over coordinates */
1011 p
+= strcspn(p
, WHITESPACE
);
1012 p
+= strspn(p
, WHITESPACE
);
1014 /* Found timezone name */
1015 k
= strcspn(p
, WHITESPACE
);
1023 if (!GREEDY_REALLOC(zones
, n_allocated
, n_zones
+ 2)) {
1028 zones
[n_zones
++] = w
;
1029 zones
[n_zones
] = NULL
;
1034 } else if (errno
!= ENOENT
)
1043 bool timezone_is_valid(const char *name
) {
1054 for (p
= name
; *p
; p
++) {
1055 if (!(*p
>= '0' && *p
<= '9') &&
1056 !(*p
>= 'a' && *p
<= 'z') &&
1057 !(*p
>= 'A' && *p
<= 'Z') &&
1058 !(*p
== '-' || *p
== '_' || *p
== '+' || *p
== '/'))
1074 t
= strjoina("/usr/share/zoneinfo/", name
);
1075 if (stat(t
, &st
) < 0)
1078 if (!S_ISREG(st
.st_mode
))
1084 clockid_t
clock_boottime_or_monotonic(void) {
1085 static clockid_t clock
= -1;
1091 fd
= timerfd_create(CLOCK_BOOTTIME
, TFD_NONBLOCK
|TFD_CLOEXEC
);
1093 clock
= CLOCK_MONOTONIC
;
1096 clock
= CLOCK_BOOTTIME
;
1102 int get_timezone(char **tz
) {
1103 _cleanup_free_
char *t
= NULL
;
1108 r
= readlink_malloc("/etc/localtime", &t
);
1110 return r
; /* returns EINVAL if not a symlink */
1112 e
= path_startswith(t
, "/usr/share/zoneinfo/");
1114 e
= path_startswith(t
, "../usr/share/zoneinfo/");
1118 if (!timezone_is_valid(e
))
1129 time_t mktime_or_timegm(struct tm
*tm
, bool utc
) {
1130 return utc
? timegm(tm
) : mktime(tm
);
1133 struct tm
*localtime_or_gmtime_r(const time_t *t
, struct tm
*tm
, bool utc
) {
1134 return utc
? gmtime_r(t
, tm
) : localtime_r(t
, tm
);
1137 unsigned long usec_to_jiffies(usec_t u
) {
1138 static thread_local
unsigned long hz
= 0;
1142 r
= sysconf(_SC_CLK_TCK
);
1145 hz
= (unsigned long) r
;
1148 return DIV_ROUND_UP(u
, USEC_PER_SEC
/ hz
);