]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/time-util.c
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 && ts
->tv_nsec
== (long) -1)
143 return USEC_INFINITY
;
145 if ((usec_t
) ts
->tv_sec
> (UINT64_MAX
- (ts
->tv_nsec
/ NSEC_PER_USEC
)) / USEC_PER_SEC
)
146 return USEC_INFINITY
;
149 (usec_t
) ts
->tv_sec
* USEC_PER_SEC
+
150 (usec_t
) ts
->tv_nsec
/ NSEC_PER_USEC
;
153 static nsec_t
timespec_load_nsec(const struct timespec
*ts
) {
156 if (ts
->tv_sec
== (time_t) -1 && ts
->tv_nsec
== (long) -1)
157 return NSEC_INFINITY
;
159 if ((nsec_t
) ts
->tv_sec
>= (UINT64_MAX
- ts
->tv_nsec
) / NSEC_PER_SEC
)
160 return NSEC_INFINITY
;
162 return (nsec_t
) ts
->tv_sec
* NSEC_PER_SEC
+ (nsec_t
) ts
->tv_nsec
;
165 struct timespec
*timespec_store(struct timespec
*ts
, usec_t u
) {
168 if (u
== USEC_INFINITY
) {
169 ts
->tv_sec
= (time_t) -1;
170 ts
->tv_nsec
= (long) -1;
174 ts
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
175 ts
->tv_nsec
= (long int) ((u
% USEC_PER_SEC
) * NSEC_PER_USEC
);
180 usec_t
timeval_load(const struct timeval
*tv
) {
183 if (tv
->tv_sec
== (time_t) -1 &&
184 tv
->tv_usec
== (suseconds_t
) -1)
185 return USEC_INFINITY
;
187 if ((usec_t
) tv
->tv_sec
> (UINT64_MAX
- tv
->tv_usec
) / USEC_PER_SEC
)
188 return USEC_INFINITY
;
191 (usec_t
) tv
->tv_sec
* USEC_PER_SEC
+
192 (usec_t
) tv
->tv_usec
;
195 struct timeval
*timeval_store(struct timeval
*tv
, usec_t u
) {
198 if (u
== USEC_INFINITY
) {
199 tv
->tv_sec
= (time_t) -1;
200 tv
->tv_usec
= (suseconds_t
) -1;
202 tv
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
203 tv
->tv_usec
= (suseconds_t
) (u
% USEC_PER_SEC
);
209 static char *format_timestamp_internal(char *buf
, size_t l
, usec_t t
,
218 if (t
<= 0 || t
== USEC_INFINITY
)
221 sec
= (time_t) (t
/ USEC_PER_SEC
);
222 localtime_or_gmtime_r(&sec
, &tm
, utc
);
225 k
= strftime(buf
, l
, "%a %Y-%m-%d %H:%M:%S", &tm
);
227 k
= strftime(buf
, l
, "%a %Y-%m-%d %H:%M:%S %Z", &tm
);
232 snprintf(buf
+ strlen(buf
), l
- strlen(buf
), ".%06llu", (unsigned long long) (t
% USEC_PER_SEC
));
233 if (strftime(buf
+ strlen(buf
), l
- strlen(buf
), " %Z", &tm
) <= 0)
240 char *format_timestamp(char *buf
, size_t l
, usec_t t
) {
241 return format_timestamp_internal(buf
, l
, t
, false, false);
244 char *format_timestamp_utc(char *buf
, size_t l
, usec_t t
) {
245 return format_timestamp_internal(buf
, l
, t
, true, false);
248 char *format_timestamp_us(char *buf
, size_t l
, usec_t t
) {
249 return format_timestamp_internal(buf
, l
, t
, false, true);
252 char *format_timestamp_us_utc(char *buf
, size_t l
, usec_t t
) {
253 return format_timestamp_internal(buf
, l
, t
, true, true);
256 char *format_timestamp_relative(char *buf
, size_t l
, usec_t t
) {
260 if (t
<= 0 || t
== USEC_INFINITY
)
263 n
= now(CLOCK_REALTIME
);
272 if (d
>= USEC_PER_YEAR
)
273 snprintf(buf
, l
, USEC_FMT
" years " USEC_FMT
" months %s",
275 (d
% USEC_PER_YEAR
) / USEC_PER_MONTH
, s
);
276 else if (d
>= USEC_PER_MONTH
)
277 snprintf(buf
, l
, USEC_FMT
" months " USEC_FMT
" days %s",
279 (d
% USEC_PER_MONTH
) / USEC_PER_DAY
, s
);
280 else if (d
>= USEC_PER_WEEK
)
281 snprintf(buf
, l
, USEC_FMT
" weeks " USEC_FMT
" days %s",
283 (d
% USEC_PER_WEEK
) / USEC_PER_DAY
, s
);
284 else if (d
>= 2*USEC_PER_DAY
)
285 snprintf(buf
, l
, USEC_FMT
" days %s", d
/ USEC_PER_DAY
, s
);
286 else if (d
>= 25*USEC_PER_HOUR
)
287 snprintf(buf
, l
, "1 day " USEC_FMT
"h %s",
288 (d
- USEC_PER_DAY
) / USEC_PER_HOUR
, s
);
289 else if (d
>= 6*USEC_PER_HOUR
)
290 snprintf(buf
, l
, USEC_FMT
"h %s",
291 d
/ USEC_PER_HOUR
, s
);
292 else if (d
>= USEC_PER_HOUR
)
293 snprintf(buf
, l
, USEC_FMT
"h " USEC_FMT
"min %s",
295 (d
% USEC_PER_HOUR
) / USEC_PER_MINUTE
, s
);
296 else if (d
>= 5*USEC_PER_MINUTE
)
297 snprintf(buf
, l
, USEC_FMT
"min %s",
298 d
/ USEC_PER_MINUTE
, s
);
299 else if (d
>= USEC_PER_MINUTE
)
300 snprintf(buf
, l
, USEC_FMT
"min " USEC_FMT
"s %s",
302 (d
% USEC_PER_MINUTE
) / USEC_PER_SEC
, s
);
303 else if (d
>= USEC_PER_SEC
)
304 snprintf(buf
, l
, USEC_FMT
"s %s",
305 d
/ USEC_PER_SEC
, s
);
306 else if (d
>= USEC_PER_MSEC
)
307 snprintf(buf
, l
, USEC_FMT
"ms %s",
308 d
/ USEC_PER_MSEC
, s
);
310 snprintf(buf
, l
, USEC_FMT
"us %s",
313 snprintf(buf
, l
, "now");
319 char *format_timespan(char *buf
, size_t l
, usec_t t
, usec_t accuracy
) {
320 static const struct {
324 { "y", USEC_PER_YEAR
},
325 { "month", USEC_PER_MONTH
},
326 { "w", USEC_PER_WEEK
},
327 { "d", USEC_PER_DAY
},
328 { "h", USEC_PER_HOUR
},
329 { "min", USEC_PER_MINUTE
},
330 { "s", USEC_PER_SEC
},
331 { "ms", USEC_PER_MSEC
},
337 bool something
= false;
342 if (t
== USEC_INFINITY
) {
343 strncpy(p
, "infinity", l
-1);
349 strncpy(p
, "0", l
-1);
354 /* The result of this function can be parsed with parse_sec */
356 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
365 if (t
< accuracy
&& something
)
368 if (t
< table
[i
].usec
)
374 a
= t
/ table
[i
].usec
;
375 b
= t
% table
[i
].usec
;
377 /* Let's see if we should shows this in dot notation */
378 if (t
< USEC_PER_MINUTE
&& b
> 0) {
383 for (cc
= table
[i
].usec
; cc
> 1; cc
/= 10)
386 for (cc
= accuracy
; cc
> 1; cc
/= 10) {
393 "%s"USEC_FMT
".%0*llu%s",
397 (unsigned long long) b
,
405 /* No? Then let's show it normally */
416 n
= MIN((size_t) k
, l
);
429 void dual_timestamp_serialize(FILE *f
, const char *name
, dual_timestamp
*t
) {
435 if (!dual_timestamp_is_set(t
))
438 fprintf(f
, "%s="USEC_FMT
" "USEC_FMT
"\n",
444 int dual_timestamp_deserialize(const char *value
, dual_timestamp
*t
) {
445 unsigned long long a
, b
;
450 if (sscanf(value
, "%llu %llu", &a
, &b
) != 2) {
451 log_debug("Failed to parse dual timestamp value \"%s\": %m", value
);
461 int timestamp_deserialize(const char *value
, usec_t
*timestamp
) {
466 r
= safe_atou64(value
, timestamp
);
468 return log_debug_errno(r
, "Failed to parse timestamp value \"%s\": %m", value
);
473 int parse_timestamp(const char *t
, usec_t
*usec
) {
474 static const struct {
498 usec_t x_usec
, plus
= 0, minus
= 0, ret
;
505 * 2012-09-22 16:34:22
506 * 2012-09-22 16:34 (seconds will be set to 0)
507 * 2012-09-22 (time will be set to 00:00:00)
508 * 16:34:22 (date will be set to today)
509 * 16:34 (date will be set to today, seconds to 0)
511 * yesterday (time is set to 00:00:00)
512 * today (time is set to 00:00:00)
513 * tomorrow (time is set to 00:00:00)
516 * @2147483647 (seconds since epoch)
524 return parse_sec(t
+ 1, usec
);
526 ret
= now(CLOCK_REALTIME
);
531 else if (t
[0] == '+') {
532 r
= parse_sec(t
+1, &plus
);
538 } else if (t
[0] == '-') {
539 r
= parse_sec(t
+1, &minus
);
545 } else if ((k
= endswith(t
, " ago"))) {
546 t
= strndupa(t
, k
- t
);
548 r
= parse_sec(t
, &minus
);
554 } else if ((k
= endswith(t
, " left"))) {
555 t
= strndupa(t
, k
- t
);
557 r
= parse_sec(t
, &plus
);
564 utc
= endswith_no_case(t
, " UTC");
566 t
= strndupa(t
, utc
- t
);
568 x
= ret
/ USEC_PER_SEC
;
571 assert_se(localtime_or_gmtime_r(&x
, &tm
, utc
));
574 if (streq(t
, "today")) {
575 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
578 } else if (streq(t
, "yesterday")) {
580 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
583 } else if (streq(t
, "tomorrow")) {
585 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
590 for (i
= 0; i
< ELEMENTSOF(day_nr
); i
++) {
593 if (!startswith_no_case(t
, day_nr
[i
].name
))
596 skip
= strlen(day_nr
[i
].name
);
600 weekday
= day_nr
[i
].nr
;
606 k
= strptime(t
, "%y-%m-%d %H:%M:%S", &tm
);
615 k
= strptime(t
, "%Y-%m-%d %H:%M:%S", &tm
);
624 k
= strptime(t
, "%y-%m-%d %H:%M", &tm
);
631 k
= strptime(t
, "%Y-%m-%d %H:%M", &tm
);
638 k
= strptime(t
, "%y-%m-%d", &tm
);
640 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
645 k
= strptime(t
, "%Y-%m-%d", &tm
);
647 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
652 k
= strptime(t
, "%H:%M:%S", &tm
);
661 k
= strptime(t
, "%H:%M", &tm
);
674 r
= parse_fractional_part_u(&k
, 6, &add
);
686 x
= mktime_or_timegm(&tm
, utc
);
687 if (x
== (time_t) -1)
690 if (weekday
>= 0 && tm
.tm_wday
!= weekday
)
693 ret
= (usec_t
) x
* USEC_PER_SEC
+ x_usec
;
707 static char* extract_multiplier(char *p
, usec_t
*multiplier
) {
708 static const struct {
712 { "seconds", USEC_PER_SEC
},
713 { "second", USEC_PER_SEC
},
714 { "sec", USEC_PER_SEC
},
715 { "s", USEC_PER_SEC
},
716 { "minutes", USEC_PER_MINUTE
},
717 { "minute", USEC_PER_MINUTE
},
718 { "min", USEC_PER_MINUTE
},
719 { "months", USEC_PER_MONTH
},
720 { "month", USEC_PER_MONTH
},
721 { "M", USEC_PER_MONTH
},
722 { "msec", USEC_PER_MSEC
},
723 { "ms", USEC_PER_MSEC
},
724 { "m", USEC_PER_MINUTE
},
725 { "hours", USEC_PER_HOUR
},
726 { "hour", USEC_PER_HOUR
},
727 { "hr", USEC_PER_HOUR
},
728 { "h", USEC_PER_HOUR
},
729 { "days", USEC_PER_DAY
},
730 { "day", USEC_PER_DAY
},
731 { "d", USEC_PER_DAY
},
732 { "weeks", USEC_PER_WEEK
},
733 { "week", USEC_PER_WEEK
},
734 { "w", USEC_PER_WEEK
},
735 { "years", USEC_PER_YEAR
},
736 { "year", USEC_PER_YEAR
},
737 { "y", USEC_PER_YEAR
},
743 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
746 e
= startswith(p
, table
[i
].suffix
);
748 *multiplier
= table
[i
].usec
;
756 int parse_time(const char *t
, usec_t
*usec
, usec_t default_unit
) {
759 bool something
= false;
763 assert(default_unit
> 0);
767 p
+= strspn(p
, WHITESPACE
);
768 s
= startswith(p
, "infinity");
770 s
+= strspn(s
, WHITESPACE
);
774 *usec
= USEC_INFINITY
;
782 usec_t multiplier
= default_unit
, k
;
784 p
+= strspn(p
, WHITESPACE
);
794 l
= strtoll(p
, &e
, 10);
804 z
= strtoll(b
, &e
, 10);
819 e
+= strspn(e
, WHITESPACE
);
820 p
= extract_multiplier(e
, &multiplier
);
824 k
= (usec_t
) z
* multiplier
;
829 r
+= (usec_t
) l
* multiplier
+ k
;
837 int parse_sec(const char *t
, usec_t
*usec
) {
838 return parse_time(t
, usec
, USEC_PER_SEC
);
841 int parse_nsec(const char *t
, nsec_t
*nsec
) {
842 static const struct {
846 { "seconds", NSEC_PER_SEC
},
847 { "second", NSEC_PER_SEC
},
848 { "sec", NSEC_PER_SEC
},
849 { "s", NSEC_PER_SEC
},
850 { "minutes", NSEC_PER_MINUTE
},
851 { "minute", NSEC_PER_MINUTE
},
852 { "min", NSEC_PER_MINUTE
},
853 { "months", NSEC_PER_MONTH
},
854 { "month", NSEC_PER_MONTH
},
855 { "msec", NSEC_PER_MSEC
},
856 { "ms", NSEC_PER_MSEC
},
857 { "m", NSEC_PER_MINUTE
},
858 { "hours", NSEC_PER_HOUR
},
859 { "hour", NSEC_PER_HOUR
},
860 { "hr", NSEC_PER_HOUR
},
861 { "h", NSEC_PER_HOUR
},
862 { "days", NSEC_PER_DAY
},
863 { "day", NSEC_PER_DAY
},
864 { "d", NSEC_PER_DAY
},
865 { "weeks", NSEC_PER_WEEK
},
866 { "week", NSEC_PER_WEEK
},
867 { "w", NSEC_PER_WEEK
},
868 { "years", NSEC_PER_YEAR
},
869 { "year", NSEC_PER_YEAR
},
870 { "y", NSEC_PER_YEAR
},
871 { "usec", NSEC_PER_USEC
},
872 { "us", NSEC_PER_USEC
},
875 { "", 1ULL }, /* default is nsec */
880 bool something
= false;
887 p
+= strspn(p
, WHITESPACE
);
888 s
= startswith(p
, "infinity");
890 s
+= strspn(s
, WHITESPACE
);
894 *nsec
= NSEC_INFINITY
;
903 p
+= strspn(p
, WHITESPACE
);
913 l
= strtoll(p
, &e
, 10);
925 z
= strtoll(b
, &e
, 10);
940 e
+= strspn(e
, WHITESPACE
);
942 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
943 if (startswith(e
, table
[i
].suffix
)) {
944 nsec_t k
= (nsec_t
) z
* table
[i
].nsec
;
949 r
+= (nsec_t
) l
* table
[i
].nsec
+ k
;
950 p
= e
+ strlen(table
[i
].suffix
);
956 if (i
>= ELEMENTSOF(table
))
966 bool ntp_synced(void) {
967 struct timex txc
= {};
969 if (adjtimex(&txc
) < 0)
972 if (txc
.status
& STA_UNSYNC
)
978 int get_timezones(char ***ret
) {
979 _cleanup_fclose_
FILE *f
= NULL
;
980 _cleanup_strv_free_
char **zones
= NULL
;
981 size_t n_zones
= 0, n_allocated
= 0;
985 zones
= strv_new("UTC", NULL
);
992 f
= fopen("/usr/share/zoneinfo/zone.tab", "re");
996 FOREACH_LINE(l
, f
, return -errno
) {
1002 if (isempty(p
) || *p
== '#')
1005 /* Skip over country code */
1006 p
+= strcspn(p
, WHITESPACE
);
1007 p
+= strspn(p
, WHITESPACE
);
1009 /* Skip over coordinates */
1010 p
+= strcspn(p
, WHITESPACE
);
1011 p
+= strspn(p
, WHITESPACE
);
1013 /* Found timezone name */
1014 k
= strcspn(p
, WHITESPACE
);
1022 if (!GREEDY_REALLOC(zones
, n_allocated
, n_zones
+ 2)) {
1027 zones
[n_zones
++] = w
;
1028 zones
[n_zones
] = NULL
;
1033 } else if (errno
!= ENOENT
)
1042 bool timezone_is_valid(const char *name
) {
1053 for (p
= name
; *p
; p
++) {
1054 if (!(*p
>= '0' && *p
<= '9') &&
1055 !(*p
>= 'a' && *p
<= 'z') &&
1056 !(*p
>= 'A' && *p
<= 'Z') &&
1057 !(*p
== '-' || *p
== '_' || *p
== '+' || *p
== '/'))
1073 t
= strjoina("/usr/share/zoneinfo/", name
);
1074 if (stat(t
, &st
) < 0)
1077 if (!S_ISREG(st
.st_mode
))
1083 bool clock_boottime_supported(void) {
1084 static int supported
= -1;
1086 /* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */
1088 if (supported
< 0) {
1091 fd
= timerfd_create(CLOCK_BOOTTIME
, TFD_NONBLOCK
|TFD_CLOEXEC
);
1103 clockid_t
clock_boottime_or_monotonic(void) {
1104 if (clock_boottime_supported())
1105 return CLOCK_BOOTTIME
;
1107 return CLOCK_MONOTONIC
;
1110 int get_timezone(char **tz
) {
1111 _cleanup_free_
char *t
= NULL
;
1116 r
= readlink_malloc("/etc/localtime", &t
);
1118 return r
; /* returns EINVAL if not a symlink */
1120 e
= path_startswith(t
, "/usr/share/zoneinfo/");
1122 e
= path_startswith(t
, "../usr/share/zoneinfo/");
1126 if (!timezone_is_valid(e
))
1137 time_t mktime_or_timegm(struct tm
*tm
, bool utc
) {
1138 return utc
? timegm(tm
) : mktime(tm
);
1141 struct tm
*localtime_or_gmtime_r(const time_t *t
, struct tm
*tm
, bool utc
) {
1142 return utc
? gmtime_r(t
, tm
) : localtime_r(t
, tm
);
1145 unsigned long usec_to_jiffies(usec_t u
) {
1146 static thread_local
unsigned long hz
= 0;
1150 r
= sysconf(_SC_CLK_TCK
);
1153 hz
= (unsigned long) r
;
1156 return DIV_ROUND_UP(u
, USEC_PER_SEC
/ hz
);