1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
15 #include <sys/timerfd.h>
16 #include <sys/timex.h>
17 #include <sys/types.h>
20 #include "alloc-util.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "process-util.h"
30 #include "stat-util.h"
31 #include "string-util.h"
33 #include "time-util.h"
35 static clockid_t
map_clock_id(clockid_t c
) {
37 /* Some more exotic archs (s390, ppc, …) lack the "ALARM" flavour of the clocks. Thus, clock_gettime() will
38 * fail for them. Since they are essentially the same as their non-ALARM pendants (their only difference is
39 * when timers are set on them), let's just map them accordingly. This way, we can get the correct time even on
44 case CLOCK_BOOTTIME_ALARM
:
45 return CLOCK_BOOTTIME
;
47 case CLOCK_REALTIME_ALARM
:
48 return CLOCK_REALTIME
;
55 usec_t
now(clockid_t clock_id
) {
58 assert_se(clock_gettime(map_clock_id(clock_id
), &ts
) == 0);
60 return timespec_load(&ts
);
63 nsec_t
now_nsec(clockid_t clock_id
) {
66 assert_se(clock_gettime(map_clock_id(clock_id
), &ts
) == 0);
68 return timespec_load_nsec(&ts
);
71 dual_timestamp
* dual_timestamp_get(dual_timestamp
*ts
) {
74 ts
->realtime
= now(CLOCK_REALTIME
);
75 ts
->monotonic
= now(CLOCK_MONOTONIC
);
80 triple_timestamp
* triple_timestamp_get(triple_timestamp
*ts
) {
83 ts
->realtime
= now(CLOCK_REALTIME
);
84 ts
->monotonic
= now(CLOCK_MONOTONIC
);
85 ts
->boottime
= clock_boottime_supported() ? now(CLOCK_BOOTTIME
) : USEC_INFINITY
;
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_signed(now(CLOCK_MONOTONIC
), delta
);
107 triple_timestamp
* triple_timestamp_from_realtime(triple_timestamp
*ts
, usec_t u
) {
112 if (u
== USEC_INFINITY
|| u
<= 0) {
113 ts
->realtime
= ts
->monotonic
= ts
->boottime
= u
;
118 delta
= (int64_t) now(CLOCK_REALTIME
) - (int64_t) u
;
119 ts
->monotonic
= usec_sub_signed(now(CLOCK_MONOTONIC
), delta
);
120 ts
->boottime
= clock_boottime_supported() ? usec_sub_signed(now(CLOCK_BOOTTIME
), delta
) : USEC_INFINITY
;
125 dual_timestamp
* dual_timestamp_from_monotonic(dual_timestamp
*ts
, usec_t u
) {
129 if (u
== USEC_INFINITY
) {
130 ts
->realtime
= ts
->monotonic
= USEC_INFINITY
;
135 delta
= (int64_t) now(CLOCK_MONOTONIC
) - (int64_t) u
;
136 ts
->realtime
= usec_sub_signed(now(CLOCK_REALTIME
), delta
);
141 dual_timestamp
* dual_timestamp_from_boottime_or_monotonic(dual_timestamp
*ts
, usec_t u
) {
144 if (u
== USEC_INFINITY
) {
145 ts
->realtime
= ts
->monotonic
= USEC_INFINITY
;
149 dual_timestamp_get(ts
);
150 delta
= (int64_t) now(clock_boottime_or_monotonic()) - (int64_t) u
;
151 ts
->realtime
= usec_sub_signed(ts
->realtime
, delta
);
152 ts
->monotonic
= usec_sub_signed(ts
->monotonic
, delta
);
157 usec_t
triple_timestamp_by_clock(triple_timestamp
*ts
, clockid_t clock
) {
162 case CLOCK_REALTIME_ALARM
:
165 case CLOCK_MONOTONIC
:
166 return ts
->monotonic
;
169 case CLOCK_BOOTTIME_ALARM
:
173 return USEC_INFINITY
;
177 usec_t
timespec_load(const struct timespec
*ts
) {
180 if (ts
->tv_sec
< 0 || ts
->tv_nsec
< 0)
181 return USEC_INFINITY
;
183 if ((usec_t
) ts
->tv_sec
> (UINT64_MAX
- (ts
->tv_nsec
/ NSEC_PER_USEC
)) / USEC_PER_SEC
)
184 return USEC_INFINITY
;
187 (usec_t
) ts
->tv_sec
* USEC_PER_SEC
+
188 (usec_t
) ts
->tv_nsec
/ NSEC_PER_USEC
;
191 nsec_t
timespec_load_nsec(const struct timespec
*ts
) {
194 if (ts
->tv_sec
< 0 || ts
->tv_nsec
< 0)
195 return NSEC_INFINITY
;
197 if ((nsec_t
) ts
->tv_sec
>= (UINT64_MAX
- ts
->tv_nsec
) / NSEC_PER_SEC
)
198 return NSEC_INFINITY
;
200 return (nsec_t
) ts
->tv_sec
* NSEC_PER_SEC
+ (nsec_t
) ts
->tv_nsec
;
203 struct timespec
*timespec_store(struct timespec
*ts
, usec_t u
) {
206 if (u
== USEC_INFINITY
||
207 u
/ USEC_PER_SEC
>= TIME_T_MAX
) {
208 ts
->tv_sec
= (time_t) -1;
209 ts
->tv_nsec
= (long) -1;
213 ts
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
214 ts
->tv_nsec
= (long int) ((u
% USEC_PER_SEC
) * NSEC_PER_USEC
);
219 usec_t
timeval_load(const struct timeval
*tv
) {
222 if (tv
->tv_sec
< 0 || tv
->tv_usec
< 0)
223 return USEC_INFINITY
;
225 if ((usec_t
) tv
->tv_sec
> (UINT64_MAX
- tv
->tv_usec
) / USEC_PER_SEC
)
226 return USEC_INFINITY
;
229 (usec_t
) tv
->tv_sec
* USEC_PER_SEC
+
230 (usec_t
) tv
->tv_usec
;
233 struct timeval
*timeval_store(struct timeval
*tv
, usec_t u
) {
236 if (u
== USEC_INFINITY
||
237 u
/ USEC_PER_SEC
> TIME_T_MAX
) {
238 tv
->tv_sec
= (time_t) -1;
239 tv
->tv_usec
= (suseconds_t
) -1;
241 tv
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
242 tv
->tv_usec
= (suseconds_t
) (u
% USEC_PER_SEC
);
248 static char *format_timestamp_internal(
255 /* The weekdays in non-localized (English) form. We use this instead of the localized form, so that our
256 * generated timestamps may be parsed with parse_timestamp(), and always read the same. */
257 static const char * const weekdays
[] = {
275 1 + 10 + /* space and date */
276 1 + 8 + /* space and time */
277 (us
? 1 + 6 : 0) + /* "." and microsecond part */
278 1 + 1 + /* space and shortest possible zone */
280 return NULL
; /* Not enough space even for the shortest form. */
281 if (t
<= 0 || t
== USEC_INFINITY
)
282 return NULL
; /* Timestamp is unset */
284 /* Let's not format times with years > 9999 */
285 if (t
> USEC_TIMESTAMP_FORMATTABLE_MAX
)
288 sec
= (time_t) (t
/ USEC_PER_SEC
); /* Round down */
290 if (!localtime_or_gmtime_r(&sec
, &tm
, utc
))
293 /* Start with the week day */
294 assert((size_t) tm
.tm_wday
< ELEMENTSOF(weekdays
));
295 memcpy(buf
, weekdays
[tm
.tm_wday
], 4);
297 /* Add the main components */
298 if (strftime(buf
+ 3, l
- 3, " %Y-%m-%d %H:%M:%S", &tm
) <= 0)
299 return NULL
; /* Doesn't fit */
301 /* Append the microseconds part, if that's requested */
305 return NULL
; /* Microseconds part doesn't fit. */
307 sprintf(buf
+ n
, ".%06"PRI_USEC
, t
% USEC_PER_SEC
);
310 /* Append the timezone */
313 /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r() normally uses the
314 * obsolete "GMT" instead. */
316 return NULL
; /* "UTC" doesn't fit. */
318 strcpy(buf
+ n
, " UTC");
320 } else if (!isempty(tm
.tm_zone
)) {
323 /* An explicit timezone is specified, let's use it, if it fits */
324 tn
= strlen(tm
.tm_zone
);
325 if (n
+ 1 + tn
+ 1 > l
) {
326 /* The full time zone does not fit in. Yuck. */
328 if (n
+ 1 + _POSIX_TZNAME_MAX
+ 1 > l
)
329 return NULL
; /* Not even enough space for the POSIX minimum (of 6)? In that case, complain that it doesn't fit */
331 /* So the time zone doesn't fit in fully, but the caller passed enough space for the POSIX
332 * minimum time zone length. In this case suppress the timezone entirely, in order not to dump
333 * an overly long, hard to read string on the user. This should be safe, because the user will
334 * assume the local timezone anyway if none is shown. And so does parse_timestamp(). */
337 strcpy(buf
+ n
, tm
.tm_zone
);
344 char *format_timestamp(char *buf
, size_t l
, usec_t t
) {
345 return format_timestamp_internal(buf
, l
, t
, false, false);
348 char *format_timestamp_utc(char *buf
, size_t l
, usec_t t
) {
349 return format_timestamp_internal(buf
, l
, t
, true, false);
352 char *format_timestamp_us(char *buf
, size_t l
, usec_t t
) {
353 return format_timestamp_internal(buf
, l
, t
, false, true);
356 char *format_timestamp_us_utc(char *buf
, size_t l
, usec_t t
) {
357 return format_timestamp_internal(buf
, l
, t
, true, true);
360 char *format_timestamp_relative(char *buf
, size_t l
, usec_t t
) {
364 if (t
<= 0 || t
== USEC_INFINITY
)
367 n
= now(CLOCK_REALTIME
);
376 if (d
>= USEC_PER_YEAR
)
377 snprintf(buf
, l
, USEC_FMT
" years " USEC_FMT
" months %s",
379 (d
% USEC_PER_YEAR
) / USEC_PER_MONTH
, s
);
380 else if (d
>= USEC_PER_MONTH
)
381 snprintf(buf
, l
, USEC_FMT
" months " USEC_FMT
" days %s",
383 (d
% USEC_PER_MONTH
) / USEC_PER_DAY
, s
);
384 else if (d
>= USEC_PER_WEEK
)
385 snprintf(buf
, l
, USEC_FMT
" weeks " USEC_FMT
" days %s",
387 (d
% USEC_PER_WEEK
) / USEC_PER_DAY
, s
);
388 else if (d
>= 2*USEC_PER_DAY
)
389 snprintf(buf
, l
, USEC_FMT
" days %s", d
/ USEC_PER_DAY
, s
);
390 else if (d
>= 25*USEC_PER_HOUR
)
391 snprintf(buf
, l
, "1 day " USEC_FMT
"h %s",
392 (d
- USEC_PER_DAY
) / USEC_PER_HOUR
, s
);
393 else if (d
>= 6*USEC_PER_HOUR
)
394 snprintf(buf
, l
, USEC_FMT
"h %s",
395 d
/ USEC_PER_HOUR
, s
);
396 else if (d
>= USEC_PER_HOUR
)
397 snprintf(buf
, l
, USEC_FMT
"h " USEC_FMT
"min %s",
399 (d
% USEC_PER_HOUR
) / USEC_PER_MINUTE
, s
);
400 else if (d
>= 5*USEC_PER_MINUTE
)
401 snprintf(buf
, l
, USEC_FMT
"min %s",
402 d
/ USEC_PER_MINUTE
, s
);
403 else if (d
>= USEC_PER_MINUTE
)
404 snprintf(buf
, l
, USEC_FMT
"min " USEC_FMT
"s %s",
406 (d
% USEC_PER_MINUTE
) / USEC_PER_SEC
, s
);
407 else if (d
>= USEC_PER_SEC
)
408 snprintf(buf
, l
, USEC_FMT
"s %s",
409 d
/ USEC_PER_SEC
, s
);
410 else if (d
>= USEC_PER_MSEC
)
411 snprintf(buf
, l
, USEC_FMT
"ms %s",
412 d
/ USEC_PER_MSEC
, s
);
414 snprintf(buf
, l
, USEC_FMT
"us %s",
417 snprintf(buf
, l
, "now");
423 char *format_timespan(char *buf
, size_t l
, usec_t t
, usec_t accuracy
) {
424 static const struct {
428 { "y", USEC_PER_YEAR
},
429 { "month", USEC_PER_MONTH
},
430 { "w", USEC_PER_WEEK
},
431 { "d", USEC_PER_DAY
},
432 { "h", USEC_PER_HOUR
},
433 { "min", USEC_PER_MINUTE
},
434 { "s", USEC_PER_SEC
},
435 { "ms", USEC_PER_MSEC
},
441 bool something
= false;
446 if (t
== USEC_INFINITY
) {
447 strncpy(p
, "infinity", l
-1);
453 strncpy(p
, "0", l
-1);
458 /* The result of this function can be parsed with parse_sec */
460 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
469 if (t
< accuracy
&& something
)
472 if (t
< table
[i
].usec
)
478 a
= t
/ table
[i
].usec
;
479 b
= t
% table
[i
].usec
;
481 /* Let's see if we should shows this in dot notation */
482 if (t
< USEC_PER_MINUTE
&& b
> 0) {
487 for (cc
= table
[i
].usec
; cc
> 1; cc
/= 10)
490 for (cc
= accuracy
; cc
> 1; cc
/= 10) {
497 "%s"USEC_FMT
".%0*"PRI_USEC
"%s",
509 /* No? Then let's show it normally */
520 n
= MIN((size_t) k
, l
);
533 void dual_timestamp_serialize(FILE *f
, const char *name
, dual_timestamp
*t
) {
539 if (!dual_timestamp_is_set(t
))
542 fprintf(f
, "%s="USEC_FMT
" "USEC_FMT
"\n",
548 int dual_timestamp_deserialize(const char *value
, dual_timestamp
*t
) {
555 pos
= strspn(value
, WHITESPACE
);
556 if (value
[pos
] == '-')
558 pos
+= strspn(value
+ pos
, DIGITS
);
559 pos
+= strspn(value
+ pos
, WHITESPACE
);
560 if (value
[pos
] == '-')
563 r
= sscanf(value
, "%" PRIu64
"%" PRIu64
"%n", &a
, &b
, &pos
);
565 log_debug("Failed to parse dual timestamp value \"%s\".", value
);
569 if (value
[pos
] != '\0')
570 /* trailing garbage */
579 int timestamp_deserialize(const char *value
, usec_t
*timestamp
) {
584 r
= safe_atou64(value
, timestamp
);
586 return log_debug_errno(r
, "Failed to parse timestamp value \"%s\": %m", value
);
591 static int parse_timestamp_impl(const char *t
, usec_t
*usec
, bool with_tz
) {
592 static const struct {
612 const char *k
, *utc
= NULL
, *tzn
= NULL
;
615 usec_t x_usec
, plus
= 0, minus
= 0, ret
;
616 int r
, weekday
= -1, dst
= -1;
621 * 2012-09-22 16:34:22
622 * 2012-09-22 16:34 (seconds will be set to 0)
623 * 2012-09-22 (time will be set to 00:00:00)
624 * 16:34:22 (date will be set to today)
625 * 16:34 (date will be set to today, seconds to 0)
627 * yesterday (time is set to 00:00:00)
628 * today (time is set to 00:00:00)
629 * tomorrow (time is set to 00:00:00)
632 * @2147483647 (seconds since epoch)
638 if (t
[0] == '@' && !with_tz
)
639 return parse_sec(t
+ 1, usec
);
641 ret
= now(CLOCK_REALTIME
);
647 else if (t
[0] == '+') {
648 r
= parse_sec(t
+1, &plus
);
654 } else if (t
[0] == '-') {
655 r
= parse_sec(t
+1, &minus
);
661 } else if ((k
= endswith(t
, " ago"))) {
662 t
= strndupa(t
, k
- t
);
664 r
= parse_sec(t
, &minus
);
670 } else if ((k
= endswith(t
, " left"))) {
671 t
= strndupa(t
, k
- t
);
673 r
= parse_sec(t
, &plus
);
680 /* See if the timestamp is suffixed with UTC */
681 utc
= endswith_no_case(t
, " UTC");
683 t
= strndupa(t
, utc
- t
);
685 const char *e
= NULL
;
690 /* See if the timestamp is suffixed by either the DST or non-DST local timezone. Note that we only
691 * support the local timezones here, nothing else. Not because we wouldn't want to, but simply because
692 * there are no nice APIs available to cover this. By accepting the local time zone strings, we make
693 * sure that all timestamps written by format_timestamp() can be parsed correctly, even though we don't
694 * support arbitrary timezone specifications. */
696 for (j
= 0; j
<= 1; j
++) {
698 if (isempty(tzname
[j
]))
701 e
= endswith_no_case(t
, tzname
[j
]);
712 if (IN_SET(j
, 0, 1)) {
713 /* Found one of the two timezones specified. */
714 t
= strndupa(t
, e
- t
- 1);
721 x
= (time_t) (ret
/ USEC_PER_SEC
);
724 if (!localtime_or_gmtime_r(&x
, &tm
, utc
))
731 if (streq(t
, "today")) {
732 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
735 } else if (streq(t
, "yesterday")) {
737 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
740 } else if (streq(t
, "tomorrow")) {
742 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
746 for (i
= 0; i
< ELEMENTSOF(day_nr
); i
++) {
749 if (!startswith_no_case(t
, day_nr
[i
].name
))
752 skip
= strlen(day_nr
[i
].name
);
756 weekday
= day_nr
[i
].nr
;
762 k
= strptime(t
, "%y-%m-%d %H:%M:%S", &tm
);
771 k
= strptime(t
, "%Y-%m-%d %H:%M:%S", &tm
);
780 k
= strptime(t
, "%y-%m-%d %H:%M", &tm
);
787 k
= strptime(t
, "%Y-%m-%d %H:%M", &tm
);
794 k
= strptime(t
, "%y-%m-%d", &tm
);
796 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
801 k
= strptime(t
, "%Y-%m-%d", &tm
);
803 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
808 k
= strptime(t
, "%H:%M:%S", &tm
);
817 k
= strptime(t
, "%H:%M", &tm
);
830 r
= parse_fractional_part_u(&k
, 6, &add
);
841 if (weekday
>= 0 && tm
.tm_wday
!= weekday
)
844 x
= mktime_or_timegm(&tm
, utc
);
848 ret
= (usec_t
) x
* USEC_PER_SEC
+ x_usec
;
849 if (ret
> USEC_TIMESTAMP_FORMATTABLE_MAX
)
853 if (ret
+ plus
< ret
) /* overflow? */
856 if (ret
> USEC_TIMESTAMP_FORMATTABLE_MAX
)
869 typedef struct ParseTimestampResult
{
872 } ParseTimestampResult
;
874 int parse_timestamp(const char *t
, usec_t
*usec
) {
875 char *last_space
, *tz
= NULL
;
876 ParseTimestampResult
*shared
, tmp
;
879 last_space
= strrchr(t
, ' ');
880 if (last_space
!= NULL
&& timezone_is_valid(last_space
+ 1, LOG_DEBUG
))
883 if (!tz
|| endswith_no_case(t
, " UTC"))
884 return parse_timestamp_impl(t
, usec
, false);
886 shared
= mmap(NULL
, sizeof *shared
, PROT_READ
|PROT_WRITE
, MAP_SHARED
|MAP_ANONYMOUS
, -1, 0);
887 if (shared
== MAP_FAILED
)
888 return negative_errno();
890 r
= safe_fork("(sd-timestamp)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_WAIT
, NULL
);
892 (void) munmap(shared
, sizeof *shared
);
898 if (setenv("TZ", tz
, 1) != 0) {
899 shared
->return_value
= negative_errno();
905 /* If there is a timezone that matches the tzname fields, leave the parsing to the implementation.
906 * Otherwise just cut it off. */
907 with_tz
= !STR_IN_SET(tz
, tzname
[0], tzname
[1]);
909 /* Cut off the timezone if we dont need it. */
911 t
= strndupa(t
, last_space
- t
);
913 shared
->return_value
= parse_timestamp_impl(t
, &shared
->usec
, with_tz
);
919 if (munmap(shared
, sizeof *shared
) != 0)
920 return negative_errno();
922 if (tmp
.return_value
== 0)
925 return tmp
.return_value
;
928 static char* extract_multiplier(char *p
, usec_t
*multiplier
) {
929 static const struct {
933 { "seconds", USEC_PER_SEC
},
934 { "second", USEC_PER_SEC
},
935 { "sec", USEC_PER_SEC
},
936 { "s", USEC_PER_SEC
},
937 { "minutes", USEC_PER_MINUTE
},
938 { "minute", USEC_PER_MINUTE
},
939 { "min", USEC_PER_MINUTE
},
940 { "months", USEC_PER_MONTH
},
941 { "month", USEC_PER_MONTH
},
942 { "M", USEC_PER_MONTH
},
943 { "msec", USEC_PER_MSEC
},
944 { "ms", USEC_PER_MSEC
},
945 { "m", USEC_PER_MINUTE
},
946 { "hours", USEC_PER_HOUR
},
947 { "hour", USEC_PER_HOUR
},
948 { "hr", USEC_PER_HOUR
},
949 { "h", USEC_PER_HOUR
},
950 { "days", USEC_PER_DAY
},
951 { "day", USEC_PER_DAY
},
952 { "d", USEC_PER_DAY
},
953 { "weeks", USEC_PER_WEEK
},
954 { "week", USEC_PER_WEEK
},
955 { "w", USEC_PER_WEEK
},
956 { "years", USEC_PER_YEAR
},
957 { "year", USEC_PER_YEAR
},
958 { "y", USEC_PER_YEAR
},
965 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
968 e
= startswith(p
, table
[i
].suffix
);
970 *multiplier
= table
[i
].usec
;
978 int parse_time(const char *t
, usec_t
*usec
, usec_t default_unit
) {
981 bool something
= false;
985 assert(default_unit
> 0);
989 p
+= strspn(p
, WHITESPACE
);
990 s
= startswith(p
, "infinity");
992 s
+= strspn(s
, WHITESPACE
);
996 *usec
= USEC_INFINITY
;
1004 usec_t multiplier
= default_unit
, k
;
1006 p
+= strspn(p
, WHITESPACE
);
1016 l
= strtoll(p
, &e
, 10);
1026 z
= strtoll(b
, &e
, 10);
1041 e
+= strspn(e
, WHITESPACE
);
1042 p
= extract_multiplier(e
, &multiplier
);
1046 k
= (usec_t
) z
* multiplier
;
1051 r
+= (usec_t
) l
* multiplier
+ k
;
1059 int parse_sec(const char *t
, usec_t
*usec
) {
1060 return parse_time(t
, usec
, USEC_PER_SEC
);
1063 int parse_sec_fix_0(const char *t
, usec_t
*usec
) {
1067 t
+= strspn(t
, WHITESPACE
);
1069 if (streq(t
, "0")) {
1070 *usec
= USEC_INFINITY
;
1074 return parse_sec(t
, usec
);
1077 int parse_nsec(const char *t
, nsec_t
*nsec
) {
1078 static const struct {
1082 { "seconds", NSEC_PER_SEC
},
1083 { "second", NSEC_PER_SEC
},
1084 { "sec", NSEC_PER_SEC
},
1085 { "s", NSEC_PER_SEC
},
1086 { "minutes", NSEC_PER_MINUTE
},
1087 { "minute", NSEC_PER_MINUTE
},
1088 { "min", NSEC_PER_MINUTE
},
1089 { "months", NSEC_PER_MONTH
},
1090 { "month", NSEC_PER_MONTH
},
1091 { "msec", NSEC_PER_MSEC
},
1092 { "ms", NSEC_PER_MSEC
},
1093 { "m", NSEC_PER_MINUTE
},
1094 { "hours", NSEC_PER_HOUR
},
1095 { "hour", NSEC_PER_HOUR
},
1096 { "hr", NSEC_PER_HOUR
},
1097 { "h", NSEC_PER_HOUR
},
1098 { "days", NSEC_PER_DAY
},
1099 { "day", NSEC_PER_DAY
},
1100 { "d", NSEC_PER_DAY
},
1101 { "weeks", NSEC_PER_WEEK
},
1102 { "week", NSEC_PER_WEEK
},
1103 { "w", NSEC_PER_WEEK
},
1104 { "years", NSEC_PER_YEAR
},
1105 { "year", NSEC_PER_YEAR
},
1106 { "y", NSEC_PER_YEAR
},
1107 { "usec", NSEC_PER_USEC
},
1108 { "us", NSEC_PER_USEC
},
1109 { "µs", NSEC_PER_USEC
},
1112 { "", 1ULL }, /* default is nsec */
1117 bool something
= false;
1124 p
+= strspn(p
, WHITESPACE
);
1125 s
= startswith(p
, "infinity");
1127 s
+= strspn(s
, WHITESPACE
);
1131 *nsec
= NSEC_INFINITY
;
1140 p
+= strspn(p
, WHITESPACE
);
1150 l
= strtoll(p
, &e
, 10);
1162 z
= strtoll(b
, &e
, 10);
1177 e
+= strspn(e
, WHITESPACE
);
1179 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
1180 if (startswith(e
, table
[i
].suffix
)) {
1181 nsec_t k
= (nsec_t
) z
* table
[i
].nsec
;
1186 r
+= (nsec_t
) l
* table
[i
].nsec
+ k
;
1187 p
= e
+ strlen(table
[i
].suffix
);
1193 if (i
>= ELEMENTSOF(table
))
1203 bool ntp_synced(void) {
1204 struct timex txc
= {};
1206 if (adjtimex(&txc
) < 0)
1209 if (txc
.status
& STA_UNSYNC
)
1215 int get_timezones(char ***ret
) {
1216 _cleanup_fclose_
FILE *f
= NULL
;
1217 _cleanup_strv_free_
char **zones
= NULL
;
1218 size_t n_zones
= 0, n_allocated
= 0;
1222 zones
= strv_new("UTC", NULL
);
1229 f
= fopen("/usr/share/zoneinfo/zone.tab", "re");
1233 FOREACH_LINE(l
, f
, return -errno
) {
1239 if (isempty(p
) || *p
== '#')
1242 /* Skip over country code */
1243 p
+= strcspn(p
, WHITESPACE
);
1244 p
+= strspn(p
, WHITESPACE
);
1246 /* Skip over coordinates */
1247 p
+= strcspn(p
, WHITESPACE
);
1248 p
+= strspn(p
, WHITESPACE
);
1250 /* Found timezone name */
1251 k
= strcspn(p
, WHITESPACE
);
1259 if (!GREEDY_REALLOC(zones
, n_allocated
, n_zones
+ 2)) {
1264 zones
[n_zones
++] = w
;
1265 zones
[n_zones
] = NULL
;
1270 } else if (errno
!= ENOENT
)
1273 *ret
= TAKE_PTR(zones
);
1278 bool timezone_is_valid(const char *name
, int log_level
) {
1281 _cleanup_close_
int fd
= -1;
1291 for (p
= name
; *p
; p
++) {
1292 if (!(*p
>= '0' && *p
<= '9') &&
1293 !(*p
>= 'a' && *p
<= 'z') &&
1294 !(*p
>= 'A' && *p
<= 'Z') &&
1295 !IN_SET(*p
, '-', '_', '+', '/'))
1311 t
= strjoina("/usr/share/zoneinfo/", name
);
1313 fd
= open(t
, O_RDONLY
|O_CLOEXEC
);
1315 log_full_errno(log_level
, errno
, "Failed to open timezone file '%s': %m", t
);
1319 r
= fd_verify_regular(fd
);
1321 log_full_errno(log_level
, r
, "Timezone file '%s' is not a regular file: %m", t
);
1325 r
= loop_read_exact(fd
, buf
, 4, false);
1327 log_full_errno(log_level
, r
, "Failed to read from timezone file '%s': %m", t
);
1331 /* Magic from tzfile(5) */
1332 if (memcmp(buf
, "TZif", 4) != 0) {
1333 log_full(log_level
, "Timezone file '%s' has wrong magic bytes", t
);
1340 bool clock_boottime_supported(void) {
1341 static int supported
= -1;
1343 /* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */
1345 if (supported
< 0) {
1348 fd
= timerfd_create(CLOCK_BOOTTIME
, TFD_NONBLOCK
|TFD_CLOEXEC
);
1360 clockid_t
clock_boottime_or_monotonic(void) {
1361 if (clock_boottime_supported())
1362 return CLOCK_BOOTTIME
;
1364 return CLOCK_MONOTONIC
;
1367 bool clock_supported(clockid_t clock
) {
1372 case CLOCK_MONOTONIC
:
1373 case CLOCK_REALTIME
:
1376 case CLOCK_BOOTTIME
:
1377 return clock_boottime_supported();
1379 case CLOCK_BOOTTIME_ALARM
:
1380 if (!clock_boottime_supported())
1385 /* For everything else, check properly */
1386 return clock_gettime(clock
, &ts
) >= 0;
1390 int get_timezone(char **tz
) {
1391 _cleanup_free_
char *t
= NULL
;
1396 r
= readlink_malloc("/etc/localtime", &t
);
1398 return r
; /* returns EINVAL if not a symlink */
1400 e
= path_startswith(t
, "/usr/share/zoneinfo/");
1402 e
= path_startswith(t
, "../usr/share/zoneinfo/");
1406 if (!timezone_is_valid(e
, LOG_DEBUG
))
1417 time_t mktime_or_timegm(struct tm
*tm
, bool utc
) {
1418 return utc
? timegm(tm
) : mktime(tm
);
1421 struct tm
*localtime_or_gmtime_r(const time_t *t
, struct tm
*tm
, bool utc
) {
1422 return utc
? gmtime_r(t
, tm
) : localtime_r(t
, tm
);
1425 unsigned long usec_to_jiffies(usec_t u
) {
1426 static thread_local
unsigned long hz
= 0;
1430 r
= sysconf(_SC_CLK_TCK
);
1436 return DIV_ROUND_UP(u
, USEC_PER_SEC
/ hz
);
1439 usec_t
usec_shift_clock(usec_t x
, clockid_t from
, clockid_t to
) {
1442 if (x
== USEC_INFINITY
)
1443 return USEC_INFINITY
;
1444 if (map_clock_id(from
) == map_clock_id(to
))
1451 /* x lies in the future */
1452 return usec_add(b
, usec_sub_unsigned(x
, a
));
1454 /* x lies in the past */
1455 return usec_sub_unsigned(b
, usec_sub_unsigned(a
, x
));
1458 bool in_utc_timezone(void) {
1461 return timezone
== 0 && daylight
== 0;