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/>.
27 #include <sys/timerfd.h>
28 #include <sys/timex.h>
29 #include <sys/types.h>
32 #include "alloc-util.h"
38 #include "parse-util.h"
39 #include "path-util.h"
40 #include "string-util.h"
42 #include "time-util.h"
44 static clockid_t
map_clock_id(clockid_t c
) {
46 /* Some more exotic archs (s390, ppc, …) lack the "ALARM" flavour of the clocks. Thus, clock_gettime() will
47 * fail for them. Since they are essentially the same as their non-ALARM pendants (their only difference is
48 * when timers are set on them), let's just map them accordingly. This way, we can get the correct time even on
53 case CLOCK_BOOTTIME_ALARM
:
54 return CLOCK_BOOTTIME
;
56 case CLOCK_REALTIME_ALARM
:
57 return CLOCK_REALTIME
;
64 usec_t
now(clockid_t clock_id
) {
67 assert_se(clock_gettime(map_clock_id(clock_id
), &ts
) == 0);
69 return timespec_load(&ts
);
72 nsec_t
now_nsec(clockid_t clock_id
) {
75 assert_se(clock_gettime(map_clock_id(clock_id
), &ts
) == 0);
77 return timespec_load_nsec(&ts
);
80 dual_timestamp
* dual_timestamp_get(dual_timestamp
*ts
) {
83 ts
->realtime
= now(CLOCK_REALTIME
);
84 ts
->monotonic
= now(CLOCK_MONOTONIC
);
89 triple_timestamp
* triple_timestamp_get(triple_timestamp
*ts
) {
92 ts
->realtime
= now(CLOCK_REALTIME
);
93 ts
->monotonic
= now(CLOCK_MONOTONIC
);
94 ts
->boottime
= clock_boottime_supported() ? now(CLOCK_BOOTTIME
) : USEC_INFINITY
;
99 dual_timestamp
* dual_timestamp_from_realtime(dual_timestamp
*ts
, usec_t u
) {
103 if (u
== USEC_INFINITY
|| u
<= 0) {
104 ts
->realtime
= ts
->monotonic
= u
;
110 delta
= (int64_t) now(CLOCK_REALTIME
) - (int64_t) u
;
111 ts
->monotonic
= usec_sub_signed(now(CLOCK_MONOTONIC
), delta
);
116 triple_timestamp
* triple_timestamp_from_realtime(triple_timestamp
*ts
, usec_t u
) {
121 if (u
== USEC_INFINITY
|| u
<= 0) {
122 ts
->realtime
= ts
->monotonic
= ts
->boottime
= u
;
127 delta
= (int64_t) now(CLOCK_REALTIME
) - (int64_t) u
;
128 ts
->monotonic
= usec_sub_signed(now(CLOCK_MONOTONIC
), delta
);
129 ts
->boottime
= clock_boottime_supported() ? usec_sub_signed(now(CLOCK_BOOTTIME
), delta
) : USEC_INFINITY
;
134 dual_timestamp
* dual_timestamp_from_monotonic(dual_timestamp
*ts
, usec_t u
) {
138 if (u
== USEC_INFINITY
) {
139 ts
->realtime
= ts
->monotonic
= USEC_INFINITY
;
144 delta
= (int64_t) now(CLOCK_MONOTONIC
) - (int64_t) u
;
145 ts
->realtime
= usec_sub_signed(now(CLOCK_REALTIME
), delta
);
150 dual_timestamp
* dual_timestamp_from_boottime_or_monotonic(dual_timestamp
*ts
, usec_t u
) {
153 if (u
== USEC_INFINITY
) {
154 ts
->realtime
= ts
->monotonic
= USEC_INFINITY
;
158 dual_timestamp_get(ts
);
159 delta
= (int64_t) now(clock_boottime_or_monotonic()) - (int64_t) u
;
160 ts
->realtime
= usec_sub_signed(ts
->realtime
, delta
);
161 ts
->monotonic
= usec_sub_signed(ts
->monotonic
, delta
);
166 usec_t
triple_timestamp_by_clock(triple_timestamp
*ts
, clockid_t clock
) {
171 case CLOCK_REALTIME_ALARM
:
174 case CLOCK_MONOTONIC
:
175 return ts
->monotonic
;
178 case CLOCK_BOOTTIME_ALARM
:
182 return USEC_INFINITY
;
186 usec_t
timespec_load(const struct timespec
*ts
) {
189 if (ts
->tv_sec
< 0 || ts
->tv_nsec
< 0)
190 return USEC_INFINITY
;
192 if ((usec_t
) ts
->tv_sec
> (UINT64_MAX
- (ts
->tv_nsec
/ NSEC_PER_USEC
)) / USEC_PER_SEC
)
193 return USEC_INFINITY
;
196 (usec_t
) ts
->tv_sec
* USEC_PER_SEC
+
197 (usec_t
) ts
->tv_nsec
/ NSEC_PER_USEC
;
200 nsec_t
timespec_load_nsec(const struct timespec
*ts
) {
203 if (ts
->tv_sec
< 0 || ts
->tv_nsec
< 0)
204 return NSEC_INFINITY
;
206 if ((nsec_t
) ts
->tv_sec
>= (UINT64_MAX
- ts
->tv_nsec
) / NSEC_PER_SEC
)
207 return NSEC_INFINITY
;
209 return (nsec_t
) ts
->tv_sec
* NSEC_PER_SEC
+ (nsec_t
) ts
->tv_nsec
;
212 struct timespec
*timespec_store(struct timespec
*ts
, usec_t u
) {
215 if (u
== USEC_INFINITY
||
216 u
/ USEC_PER_SEC
>= TIME_T_MAX
) {
217 ts
->tv_sec
= (time_t) -1;
218 ts
->tv_nsec
= (long) -1;
222 ts
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
223 ts
->tv_nsec
= (long int) ((u
% USEC_PER_SEC
) * NSEC_PER_USEC
);
228 usec_t
timeval_load(const struct timeval
*tv
) {
231 if (tv
->tv_sec
< 0 || tv
->tv_usec
< 0)
232 return USEC_INFINITY
;
234 if ((usec_t
) tv
->tv_sec
> (UINT64_MAX
- tv
->tv_usec
) / USEC_PER_SEC
)
235 return USEC_INFINITY
;
238 (usec_t
) tv
->tv_sec
* USEC_PER_SEC
+
239 (usec_t
) tv
->tv_usec
;
242 struct timeval
*timeval_store(struct timeval
*tv
, usec_t u
) {
245 if (u
== USEC_INFINITY
||
246 u
/ USEC_PER_SEC
> TIME_T_MAX
) {
247 tv
->tv_sec
= (time_t) -1;
248 tv
->tv_usec
= (suseconds_t
) -1;
250 tv
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
251 tv
->tv_usec
= (suseconds_t
) (u
% USEC_PER_SEC
);
257 static char *format_timestamp_internal(
264 /* The weekdays in non-localized (English) form. We use this instead of the localized form, so that our
265 * generated timestamps may be parsed with parse_timestamp(), and always read the same. */
266 static const char * const weekdays
[] = {
284 1 + 10 + /* space and date */
285 1 + 8 + /* space and time */
286 (us
? 1 + 6 : 0) + /* "." and microsecond part */
287 1 + 1 + /* space and shortest possible zone */
289 return NULL
; /* Not enough space even for the shortest form. */
290 if (t
<= 0 || t
== USEC_INFINITY
)
291 return NULL
; /* Timestamp is unset */
293 /* Let's not format times with years > 9999 */
294 if (t
> USEC_TIMESTAMP_FORMATTABLE_MAX
)
297 sec
= (time_t) (t
/ USEC_PER_SEC
); /* Round down */
299 if (!localtime_or_gmtime_r(&sec
, &tm
, utc
))
302 /* Start with the week day */
303 assert((size_t) tm
.tm_wday
< ELEMENTSOF(weekdays
));
304 memcpy(buf
, weekdays
[tm
.tm_wday
], 4);
306 /* Add the main components */
307 if (strftime(buf
+ 3, l
- 3, " %Y-%m-%d %H:%M:%S", &tm
) <= 0)
308 return NULL
; /* Doesn't fit */
310 /* Append the microseconds part, if that's requested */
314 return NULL
; /* Microseconds part doesn't fit. */
316 sprintf(buf
+ n
, ".%06"PRI_USEC
, t
% USEC_PER_SEC
);
319 /* Append the timezone */
322 /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r() normally uses the
323 * obsolete "GMT" instead. */
325 return NULL
; /* "UTC" doesn't fit. */
327 strcpy(buf
+ n
, " UTC");
329 } else if (!isempty(tm
.tm_zone
)) {
332 /* An explicit timezone is specified, let's use it, if it fits */
333 tn
= strlen(tm
.tm_zone
);
334 if (n
+ 1 + tn
+ 1 > l
) {
335 /* The full time zone does not fit in. Yuck. */
337 if (n
+ 1 + _POSIX_TZNAME_MAX
+ 1 > l
)
338 return NULL
; /* Not even enough space for the POSIX minimum (of 6)? In that case, complain that it doesn't fit */
340 /* So the time zone doesn't fit in fully, but the caller passed enough space for the POSIX
341 * minimum time zone length. In this case suppress the timezone entirely, in order not to dump
342 * an overly long, hard to read string on the user. This should be safe, because the user will
343 * assume the local timezone anyway if none is shown. And so does parse_timestamp(). */
346 strcpy(buf
+ n
, tm
.tm_zone
);
353 char *format_timestamp(char *buf
, size_t l
, usec_t t
) {
354 return format_timestamp_internal(buf
, l
, t
, false, false);
357 char *format_timestamp_utc(char *buf
, size_t l
, usec_t t
) {
358 return format_timestamp_internal(buf
, l
, t
, true, false);
361 char *format_timestamp_us(char *buf
, size_t l
, usec_t t
) {
362 return format_timestamp_internal(buf
, l
, t
, false, true);
365 char *format_timestamp_us_utc(char *buf
, size_t l
, usec_t t
) {
366 return format_timestamp_internal(buf
, l
, t
, true, true);
369 char *format_timestamp_relative(char *buf
, size_t l
, usec_t t
) {
373 if (t
<= 0 || t
== USEC_INFINITY
)
376 n
= now(CLOCK_REALTIME
);
385 if (d
>= USEC_PER_YEAR
)
386 snprintf(buf
, l
, USEC_FMT
" years " USEC_FMT
" months %s",
388 (d
% USEC_PER_YEAR
) / USEC_PER_MONTH
, s
);
389 else if (d
>= USEC_PER_MONTH
)
390 snprintf(buf
, l
, USEC_FMT
" months " USEC_FMT
" days %s",
392 (d
% USEC_PER_MONTH
) / USEC_PER_DAY
, s
);
393 else if (d
>= USEC_PER_WEEK
)
394 snprintf(buf
, l
, USEC_FMT
" weeks " USEC_FMT
" days %s",
396 (d
% USEC_PER_WEEK
) / USEC_PER_DAY
, s
);
397 else if (d
>= 2*USEC_PER_DAY
)
398 snprintf(buf
, l
, USEC_FMT
" days %s", d
/ USEC_PER_DAY
, s
);
399 else if (d
>= 25*USEC_PER_HOUR
)
400 snprintf(buf
, l
, "1 day " USEC_FMT
"h %s",
401 (d
- USEC_PER_DAY
) / USEC_PER_HOUR
, s
);
402 else if (d
>= 6*USEC_PER_HOUR
)
403 snprintf(buf
, l
, USEC_FMT
"h %s",
404 d
/ USEC_PER_HOUR
, s
);
405 else if (d
>= USEC_PER_HOUR
)
406 snprintf(buf
, l
, USEC_FMT
"h " USEC_FMT
"min %s",
408 (d
% USEC_PER_HOUR
) / USEC_PER_MINUTE
, s
);
409 else if (d
>= 5*USEC_PER_MINUTE
)
410 snprintf(buf
, l
, USEC_FMT
"min %s",
411 d
/ USEC_PER_MINUTE
, s
);
412 else if (d
>= USEC_PER_MINUTE
)
413 snprintf(buf
, l
, USEC_FMT
"min " USEC_FMT
"s %s",
415 (d
% USEC_PER_MINUTE
) / USEC_PER_SEC
, s
);
416 else if (d
>= USEC_PER_SEC
)
417 snprintf(buf
, l
, USEC_FMT
"s %s",
418 d
/ USEC_PER_SEC
, s
);
419 else if (d
>= USEC_PER_MSEC
)
420 snprintf(buf
, l
, USEC_FMT
"ms %s",
421 d
/ USEC_PER_MSEC
, s
);
423 snprintf(buf
, l
, USEC_FMT
"us %s",
426 snprintf(buf
, l
, "now");
432 char *format_timespan(char *buf
, size_t l
, usec_t t
, usec_t accuracy
) {
433 static const struct {
437 { "y", USEC_PER_YEAR
},
438 { "month", USEC_PER_MONTH
},
439 { "w", USEC_PER_WEEK
},
440 { "d", USEC_PER_DAY
},
441 { "h", USEC_PER_HOUR
},
442 { "min", USEC_PER_MINUTE
},
443 { "s", USEC_PER_SEC
},
444 { "ms", USEC_PER_MSEC
},
450 bool something
= false;
455 if (t
== USEC_INFINITY
) {
456 strncpy(p
, "infinity", l
-1);
462 strncpy(p
, "0", l
-1);
467 /* The result of this function can be parsed with parse_sec */
469 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
478 if (t
< accuracy
&& something
)
481 if (t
< table
[i
].usec
)
487 a
= t
/ table
[i
].usec
;
488 b
= t
% table
[i
].usec
;
490 /* Let's see if we should shows this in dot notation */
491 if (t
< USEC_PER_MINUTE
&& b
> 0) {
496 for (cc
= table
[i
].usec
; cc
> 1; cc
/= 10)
499 for (cc
= accuracy
; cc
> 1; cc
/= 10) {
506 "%s"USEC_FMT
".%0*"PRI_USEC
"%s",
518 /* No? Then let's show it normally */
529 n
= MIN((size_t) k
, l
);
542 void dual_timestamp_serialize(FILE *f
, const char *name
, dual_timestamp
*t
) {
548 if (!dual_timestamp_is_set(t
))
551 fprintf(f
, "%s="USEC_FMT
" "USEC_FMT
"\n",
557 int dual_timestamp_deserialize(const char *value
, dual_timestamp
*t
) {
564 pos
= strspn(value
, WHITESPACE
);
565 if (value
[pos
] == '-')
567 pos
+= strspn(value
+ pos
, DIGITS
);
568 pos
+= strspn(value
+ pos
, WHITESPACE
);
569 if (value
[pos
] == '-')
572 r
= sscanf(value
, "%" PRIu64
"%" PRIu64
"%n", &a
, &b
, &pos
);
574 log_debug("Failed to parse dual timestamp value \"%s\".", value
);
578 if (value
[pos
] != '\0')
579 /* trailing garbage */
588 int timestamp_deserialize(const char *value
, usec_t
*timestamp
) {
593 r
= safe_atou64(value
, timestamp
);
595 return log_debug_errno(r
, "Failed to parse timestamp value \"%s\": %m", value
);
600 static int parse_timestamp_impl(const char *t
, usec_t
*usec
, bool with_tz
) {
601 static const struct {
621 const char *k
, *utc
= NULL
, *tzn
= NULL
;
624 usec_t x_usec
, plus
= 0, minus
= 0, ret
;
625 int r
, weekday
= -1, dst
= -1;
631 * 2012-09-22 16:34:22
632 * 2012-09-22 16:34 (seconds will be set to 0)
633 * 2012-09-22 (time will be set to 00:00:00)
634 * 16:34:22 (date will be set to today)
635 * 16:34 (date will be set to today, seconds to 0)
637 * yesterday (time is set to 00:00:00)
638 * today (time is set to 00:00:00)
639 * tomorrow (time is set to 00:00:00)
642 * @2147483647 (seconds since epoch)
649 if (t
[0] == '@' && !with_tz
)
650 return parse_sec(t
+ 1, usec
);
652 ret
= now(CLOCK_REALTIME
);
658 else if (t
[0] == '+') {
659 r
= parse_sec(t
+1, &plus
);
665 } else if (t
[0] == '-') {
666 r
= parse_sec(t
+1, &minus
);
672 } else if ((k
= endswith(t
, " ago"))) {
673 t
= strndupa(t
, k
- t
);
675 r
= parse_sec(t
, &minus
);
681 } else if ((k
= endswith(t
, " left"))) {
682 t
= strndupa(t
, k
- t
);
684 r
= parse_sec(t
, &plus
);
691 /* See if the timestamp is suffixed with UTC */
692 utc
= endswith_no_case(t
, " UTC");
694 t
= strndupa(t
, utc
- t
);
696 const char *e
= NULL
;
701 /* See if the timestamp is suffixed by either the DST or non-DST local timezone. Note that we only
702 * support the local timezones here, nothing else. Not because we wouldn't want to, but simply because
703 * there are no nice APIs available to cover this. By accepting the local time zone strings, we make
704 * sure that all timestamps written by format_timestamp() can be parsed correctly, even though we don't
705 * support arbitrary timezone specifications. */
707 for (j
= 0; j
<= 1; j
++) {
709 if (isempty(tzname
[j
]))
712 e
= endswith_no_case(t
, tzname
[j
]);
723 if (IN_SET(j
, 0, 1)) {
724 /* Found one of the two timezones specified. */
725 t
= strndupa(t
, e
- t
- 1);
732 x
= (time_t) (ret
/ USEC_PER_SEC
);
735 if (!localtime_or_gmtime_r(&x
, &tm
, utc
))
742 if (streq(t
, "today")) {
743 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
746 } else if (streq(t
, "yesterday")) {
748 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
751 } else if (streq(t
, "tomorrow")) {
753 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
757 for (i
= 0; i
< ELEMENTSOF(day_nr
); i
++) {
760 if (!startswith_no_case(t
, day_nr
[i
].name
))
763 skip
= strlen(day_nr
[i
].name
);
767 weekday
= day_nr
[i
].nr
;
773 k
= strptime(t
, "%y-%m-%d %H:%M:%S", &tm
);
782 k
= strptime(t
, "%Y-%m-%d %H:%M:%S", &tm
);
791 k
= strptime(t
, "%y-%m-%d %H:%M", &tm
);
798 k
= strptime(t
, "%Y-%m-%d %H:%M", &tm
);
805 k
= strptime(t
, "%y-%m-%d", &tm
);
807 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
812 k
= strptime(t
, "%Y-%m-%d", &tm
);
814 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
819 k
= strptime(t
, "%H:%M:%S", &tm
);
828 k
= strptime(t
, "%H:%M", &tm
);
841 r
= parse_fractional_part_u(&k
, 6, &add
);
852 if (weekday
>= 0 && tm
.tm_wday
!= weekday
)
855 x
= mktime_or_timegm(&tm
, utc
);
859 ret
= (usec_t
) x
* USEC_PER_SEC
+ x_usec
;
860 if (ret
> USEC_TIMESTAMP_FORMATTABLE_MAX
)
864 if (ret
+ plus
< ret
) /* overflow? */
867 if (ret
> USEC_TIMESTAMP_FORMATTABLE_MAX
)
880 typedef struct ParseTimestampResult
{
883 } ParseTimestampResult
;
885 int parse_timestamp(const char *t
, usec_t
*usec
) {
886 char *last_space
, *tz
= NULL
;
887 ParseTimestampResult
*shared
, tmp
;
891 last_space
= strrchr(t
, ' ');
892 if (last_space
!= NULL
&& timezone_is_valid(last_space
+ 1))
895 if (tz
== NULL
|| endswith_no_case(t
, " UTC"))
896 return parse_timestamp_impl(t
, usec
, false);
898 shared
= mmap(NULL
, sizeof *shared
, PROT_READ
|PROT_WRITE
, MAP_SHARED
|MAP_ANONYMOUS
, -1, 0);
899 if (shared
== MAP_FAILED
)
900 return negative_errno();
905 int fork_errno
= errno
;
906 (void) munmap(shared
, sizeof *shared
);
913 if (setenv("TZ", tz
, 1) != 0) {
914 shared
->return_value
= negative_errno();
920 /* If there is a timezone that matches the tzname fields, leave the parsing to the implementation.
921 * Otherwise just cut it off */
922 with_tz
= !STR_IN_SET(tz
, tzname
[0], tzname
[1]);
924 /*cut off the timezone if we dont need it*/
926 t
= strndupa(t
, last_space
- t
);
928 shared
->return_value
= parse_timestamp_impl(t
, &shared
->usec
, with_tz
);
933 r
= wait_for_terminate(pid
, NULL
);
935 (void) munmap(shared
, sizeof *shared
);
940 if (munmap(shared
, sizeof *shared
) != 0)
941 return negative_errno();
943 if (tmp
.return_value
== 0)
946 return tmp
.return_value
;
949 static char* extract_multiplier(char *p
, usec_t
*multiplier
) {
950 static const struct {
954 { "seconds", USEC_PER_SEC
},
955 { "second", USEC_PER_SEC
},
956 { "sec", USEC_PER_SEC
},
957 { "s", USEC_PER_SEC
},
958 { "minutes", USEC_PER_MINUTE
},
959 { "minute", USEC_PER_MINUTE
},
960 { "min", USEC_PER_MINUTE
},
961 { "months", USEC_PER_MONTH
},
962 { "month", USEC_PER_MONTH
},
963 { "M", USEC_PER_MONTH
},
964 { "msec", USEC_PER_MSEC
},
965 { "ms", USEC_PER_MSEC
},
966 { "m", USEC_PER_MINUTE
},
967 { "hours", USEC_PER_HOUR
},
968 { "hour", USEC_PER_HOUR
},
969 { "hr", USEC_PER_HOUR
},
970 { "h", USEC_PER_HOUR
},
971 { "days", USEC_PER_DAY
},
972 { "day", USEC_PER_DAY
},
973 { "d", USEC_PER_DAY
},
974 { "weeks", USEC_PER_WEEK
},
975 { "week", USEC_PER_WEEK
},
976 { "w", USEC_PER_WEEK
},
977 { "years", USEC_PER_YEAR
},
978 { "year", USEC_PER_YEAR
},
979 { "y", USEC_PER_YEAR
},
986 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
989 e
= startswith(p
, table
[i
].suffix
);
991 *multiplier
= table
[i
].usec
;
999 int parse_time(const char *t
, usec_t
*usec
, usec_t default_unit
) {
1002 bool something
= false;
1006 assert(default_unit
> 0);
1010 p
+= strspn(p
, WHITESPACE
);
1011 s
= startswith(p
, "infinity");
1013 s
+= strspn(s
, WHITESPACE
);
1017 *usec
= USEC_INFINITY
;
1025 usec_t multiplier
= default_unit
, k
;
1027 p
+= strspn(p
, WHITESPACE
);
1037 l
= strtoll(p
, &e
, 10);
1047 z
= strtoll(b
, &e
, 10);
1062 e
+= strspn(e
, WHITESPACE
);
1063 p
= extract_multiplier(e
, &multiplier
);
1067 k
= (usec_t
) z
* multiplier
;
1072 r
+= (usec_t
) l
* multiplier
+ k
;
1080 int parse_sec(const char *t
, usec_t
*usec
) {
1081 return parse_time(t
, usec
, USEC_PER_SEC
);
1084 int parse_sec_fix_0(const char *t
, usec_t
*usec
) {
1085 t
+= strspn(t
, WHITESPACE
);
1086 if (streq(t
, "0")) {
1087 *usec
= USEC_INFINITY
;
1091 return parse_sec(t
, usec
);
1094 int parse_nsec(const char *t
, nsec_t
*nsec
) {
1095 static const struct {
1099 { "seconds", NSEC_PER_SEC
},
1100 { "second", NSEC_PER_SEC
},
1101 { "sec", NSEC_PER_SEC
},
1102 { "s", NSEC_PER_SEC
},
1103 { "minutes", NSEC_PER_MINUTE
},
1104 { "minute", NSEC_PER_MINUTE
},
1105 { "min", NSEC_PER_MINUTE
},
1106 { "months", NSEC_PER_MONTH
},
1107 { "month", NSEC_PER_MONTH
},
1108 { "msec", NSEC_PER_MSEC
},
1109 { "ms", NSEC_PER_MSEC
},
1110 { "m", NSEC_PER_MINUTE
},
1111 { "hours", NSEC_PER_HOUR
},
1112 { "hour", NSEC_PER_HOUR
},
1113 { "hr", NSEC_PER_HOUR
},
1114 { "h", NSEC_PER_HOUR
},
1115 { "days", NSEC_PER_DAY
},
1116 { "day", NSEC_PER_DAY
},
1117 { "d", NSEC_PER_DAY
},
1118 { "weeks", NSEC_PER_WEEK
},
1119 { "week", NSEC_PER_WEEK
},
1120 { "w", NSEC_PER_WEEK
},
1121 { "years", NSEC_PER_YEAR
},
1122 { "year", NSEC_PER_YEAR
},
1123 { "y", NSEC_PER_YEAR
},
1124 { "usec", NSEC_PER_USEC
},
1125 { "us", NSEC_PER_USEC
},
1126 { "µs", NSEC_PER_USEC
},
1129 { "", 1ULL }, /* default is nsec */
1134 bool something
= false;
1141 p
+= strspn(p
, WHITESPACE
);
1142 s
= startswith(p
, "infinity");
1144 s
+= strspn(s
, WHITESPACE
);
1148 *nsec
= NSEC_INFINITY
;
1157 p
+= strspn(p
, WHITESPACE
);
1167 l
= strtoll(p
, &e
, 10);
1179 z
= strtoll(b
, &e
, 10);
1194 e
+= strspn(e
, WHITESPACE
);
1196 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
1197 if (startswith(e
, table
[i
].suffix
)) {
1198 nsec_t k
= (nsec_t
) z
* table
[i
].nsec
;
1203 r
+= (nsec_t
) l
* table
[i
].nsec
+ k
;
1204 p
= e
+ strlen(table
[i
].suffix
);
1210 if (i
>= ELEMENTSOF(table
))
1220 bool ntp_synced(void) {
1221 struct timex txc
= {};
1223 if (adjtimex(&txc
) < 0)
1226 if (txc
.status
& STA_UNSYNC
)
1232 int get_timezones(char ***ret
) {
1233 _cleanup_fclose_
FILE *f
= NULL
;
1234 _cleanup_strv_free_
char **zones
= NULL
;
1235 size_t n_zones
= 0, n_allocated
= 0;
1239 zones
= strv_new("UTC", NULL
);
1246 f
= fopen("/usr/share/zoneinfo/zone.tab", "re");
1250 FOREACH_LINE(l
, f
, return -errno
) {
1256 if (isempty(p
) || *p
== '#')
1259 /* Skip over country code */
1260 p
+= strcspn(p
, WHITESPACE
);
1261 p
+= strspn(p
, WHITESPACE
);
1263 /* Skip over coordinates */
1264 p
+= strcspn(p
, WHITESPACE
);
1265 p
+= strspn(p
, WHITESPACE
);
1267 /* Found timezone name */
1268 k
= strcspn(p
, WHITESPACE
);
1276 if (!GREEDY_REALLOC(zones
, n_allocated
, n_zones
+ 2)) {
1281 zones
[n_zones
++] = w
;
1282 zones
[n_zones
] = NULL
;
1287 } else if (errno
!= ENOENT
)
1296 bool timezone_is_valid(const char *name
) {
1307 for (p
= name
; *p
; p
++) {
1308 if (!(*p
>= '0' && *p
<= '9') &&
1309 !(*p
>= 'a' && *p
<= 'z') &&
1310 !(*p
>= 'A' && *p
<= 'Z') &&
1311 !IN_SET(*p
, '-', '_', '+', '/'))
1327 t
= strjoina("/usr/share/zoneinfo/", name
);
1328 if (stat(t
, &st
) < 0)
1331 if (!S_ISREG(st
.st_mode
))
1337 bool clock_boottime_supported(void) {
1338 static int supported
= -1;
1340 /* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */
1342 if (supported
< 0) {
1345 fd
= timerfd_create(CLOCK_BOOTTIME
, TFD_NONBLOCK
|TFD_CLOEXEC
);
1357 clockid_t
clock_boottime_or_monotonic(void) {
1358 if (clock_boottime_supported())
1359 return CLOCK_BOOTTIME
;
1361 return CLOCK_MONOTONIC
;
1364 bool clock_supported(clockid_t clock
) {
1369 case CLOCK_MONOTONIC
:
1370 case CLOCK_REALTIME
:
1373 case CLOCK_BOOTTIME
:
1374 return clock_boottime_supported();
1376 case CLOCK_BOOTTIME_ALARM
:
1377 if (!clock_boottime_supported())
1383 /* For everything else, check properly */
1384 return clock_gettime(clock
, &ts
) >= 0;
1388 int get_timezone(char **tz
) {
1389 _cleanup_free_
char *t
= NULL
;
1394 r
= readlink_malloc("/etc/localtime", &t
);
1396 return r
; /* returns EINVAL if not a symlink */
1398 e
= path_startswith(t
, "/usr/share/zoneinfo/");
1400 e
= path_startswith(t
, "../usr/share/zoneinfo/");
1404 if (!timezone_is_valid(e
))
1415 time_t mktime_or_timegm(struct tm
*tm
, bool utc
) {
1416 return utc
? timegm(tm
) : mktime(tm
);
1419 struct tm
*localtime_or_gmtime_r(const time_t *t
, struct tm
*tm
, bool utc
) {
1420 return utc
? gmtime_r(t
, tm
) : localtime_r(t
, tm
);
1423 unsigned long usec_to_jiffies(usec_t u
) {
1424 static thread_local
unsigned long hz
= 0;
1428 r
= sysconf(_SC_CLK_TCK
);
1434 return DIV_ROUND_UP(u
, USEC_PER_SEC
/ hz
);
1437 usec_t
usec_shift_clock(usec_t x
, clockid_t from
, clockid_t to
) {
1440 if (x
== USEC_INFINITY
)
1441 return USEC_INFINITY
;
1442 if (map_clock_id(from
) == map_clock_id(to
))
1449 /* x lies in the future */
1450 return usec_add(b
, usec_sub_unsigned(x
, a
));
1452 /* x lies in the past */
1453 return usec_sub_unsigned(b
, usec_sub_unsigned(a
, x
));