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 clockid_t
map_clock_id(clockid_t c
) {
45 /* Some more exotic archs (s390, ppc, …) lack the "ALARM" flavour of the clocks. Thus, clock_gettime() will
46 * fail for them. Since they are essentially the same as their non-ALARM pendants (their only difference is
47 * when timers are set on them), let's just map them accordingly. This way, we can get the correct time even on
52 case CLOCK_BOOTTIME_ALARM
:
53 return CLOCK_BOOTTIME
;
55 case CLOCK_REALTIME_ALARM
:
56 return CLOCK_REALTIME
;
63 usec_t
now(clockid_t clock_id
) {
66 assert_se(clock_gettime(map_clock_id(clock_id
), &ts
) == 0);
68 return timespec_load(&ts
);
71 nsec_t
now_nsec(clockid_t clock_id
) {
74 assert_se(clock_gettime(map_clock_id(clock_id
), &ts
) == 0);
76 return timespec_load_nsec(&ts
);
79 dual_timestamp
* dual_timestamp_get(dual_timestamp
*ts
) {
82 ts
->realtime
= now(CLOCK_REALTIME
);
83 ts
->monotonic
= now(CLOCK_MONOTONIC
);
88 triple_timestamp
* triple_timestamp_get(triple_timestamp
*ts
) {
91 ts
->realtime
= now(CLOCK_REALTIME
);
92 ts
->monotonic
= now(CLOCK_MONOTONIC
);
93 ts
->boottime
= clock_boottime_supported() ? now(CLOCK_BOOTTIME
) : USEC_INFINITY
;
98 dual_timestamp
* dual_timestamp_from_realtime(dual_timestamp
*ts
, usec_t u
) {
102 if (u
== USEC_INFINITY
|| u
<= 0) {
103 ts
->realtime
= ts
->monotonic
= u
;
109 delta
= (int64_t) now(CLOCK_REALTIME
) - (int64_t) u
;
110 ts
->monotonic
= usec_sub_signed(now(CLOCK_MONOTONIC
), delta
);
115 triple_timestamp
* triple_timestamp_from_realtime(triple_timestamp
*ts
, usec_t u
) {
120 if (u
== USEC_INFINITY
|| u
<= 0) {
121 ts
->realtime
= ts
->monotonic
= ts
->boottime
= u
;
126 delta
= (int64_t) now(CLOCK_REALTIME
) - (int64_t) u
;
127 ts
->monotonic
= usec_sub_signed(now(CLOCK_MONOTONIC
), delta
);
128 ts
->boottime
= clock_boottime_supported() ? usec_sub_signed(now(CLOCK_BOOTTIME
), delta
) : USEC_INFINITY
;
133 dual_timestamp
* dual_timestamp_from_monotonic(dual_timestamp
*ts
, usec_t u
) {
137 if (u
== USEC_INFINITY
) {
138 ts
->realtime
= ts
->monotonic
= USEC_INFINITY
;
143 delta
= (int64_t) now(CLOCK_MONOTONIC
) - (int64_t) u
;
144 ts
->realtime
= usec_sub_signed(now(CLOCK_REALTIME
), delta
);
149 dual_timestamp
* dual_timestamp_from_boottime_or_monotonic(dual_timestamp
*ts
, usec_t u
) {
152 if (u
== USEC_INFINITY
) {
153 ts
->realtime
= ts
->monotonic
= USEC_INFINITY
;
157 dual_timestamp_get(ts
);
158 delta
= (int64_t) now(clock_boottime_or_monotonic()) - (int64_t) u
;
159 ts
->realtime
= usec_sub_signed(ts
->realtime
, delta
);
160 ts
->monotonic
= usec_sub_signed(ts
->monotonic
, delta
);
165 usec_t
triple_timestamp_by_clock(triple_timestamp
*ts
, clockid_t clock
) {
170 case CLOCK_REALTIME_ALARM
:
173 case CLOCK_MONOTONIC
:
174 return ts
->monotonic
;
177 case CLOCK_BOOTTIME_ALARM
:
181 return USEC_INFINITY
;
185 usec_t
timespec_load(const struct timespec
*ts
) {
188 if (ts
->tv_sec
< 0 || ts
->tv_nsec
< 0)
189 return USEC_INFINITY
;
191 if ((usec_t
) ts
->tv_sec
> (UINT64_MAX
- (ts
->tv_nsec
/ NSEC_PER_USEC
)) / USEC_PER_SEC
)
192 return USEC_INFINITY
;
195 (usec_t
) ts
->tv_sec
* USEC_PER_SEC
+
196 (usec_t
) ts
->tv_nsec
/ NSEC_PER_USEC
;
199 nsec_t
timespec_load_nsec(const struct timespec
*ts
) {
202 if (ts
->tv_sec
< 0 || ts
->tv_nsec
< 0)
203 return NSEC_INFINITY
;
205 if ((nsec_t
) ts
->tv_sec
>= (UINT64_MAX
- ts
->tv_nsec
) / NSEC_PER_SEC
)
206 return NSEC_INFINITY
;
208 return (nsec_t
) ts
->tv_sec
* NSEC_PER_SEC
+ (nsec_t
) ts
->tv_nsec
;
211 struct timespec
*timespec_store(struct timespec
*ts
, usec_t u
) {
214 if (u
== USEC_INFINITY
||
215 u
/ USEC_PER_SEC
>= TIME_T_MAX
) {
216 ts
->tv_sec
= (time_t) -1;
217 ts
->tv_nsec
= (long) -1;
221 ts
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
222 ts
->tv_nsec
= (long int) ((u
% USEC_PER_SEC
) * NSEC_PER_USEC
);
227 usec_t
timeval_load(const struct timeval
*tv
) {
230 if (tv
->tv_sec
< 0 || tv
->tv_usec
< 0)
231 return USEC_INFINITY
;
233 if ((usec_t
) tv
->tv_sec
> (UINT64_MAX
- tv
->tv_usec
) / USEC_PER_SEC
)
234 return USEC_INFINITY
;
237 (usec_t
) tv
->tv_sec
* USEC_PER_SEC
+
238 (usec_t
) tv
->tv_usec
;
241 struct timeval
*timeval_store(struct timeval
*tv
, usec_t u
) {
244 if (u
== USEC_INFINITY
||
245 u
/ USEC_PER_SEC
> TIME_T_MAX
) {
246 tv
->tv_sec
= (time_t) -1;
247 tv
->tv_usec
= (suseconds_t
) -1;
249 tv
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
250 tv
->tv_usec
= (suseconds_t
) (u
% USEC_PER_SEC
);
256 static char *format_timestamp_internal(
263 /* The weekdays in non-localized (English) form. We use this instead of the localized form, so that our
264 * generated timestamps may be parsed with parse_timestamp(), and always read the same. */
265 static const char * const weekdays
[] = {
283 1 + 10 + /* space and date */
284 1 + 8 + /* space and time */
285 (us
? 1 + 6 : 0) + /* "." and microsecond part */
286 1 + 1 + /* space and shortest possible zone */
288 return NULL
; /* Not enough space even for the shortest form. */
289 if (t
<= 0 || t
== USEC_INFINITY
)
290 return NULL
; /* Timestamp is unset */
292 /* Let's not format times with years > 9999 */
293 if (t
> USEC_TIMESTAMP_FORMATTABLE_MAX
)
296 sec
= (time_t) (t
/ USEC_PER_SEC
); /* Round down */
298 if (!localtime_or_gmtime_r(&sec
, &tm
, utc
))
301 /* Start with the week day */
302 assert((size_t) tm
.tm_wday
< ELEMENTSOF(weekdays
));
303 memcpy(buf
, weekdays
[tm
.tm_wday
], 4);
305 /* Add the main components */
306 if (strftime(buf
+ 3, l
- 3, " %Y-%m-%d %H:%M:%S", &tm
) <= 0)
307 return NULL
; /* Doesn't fit */
309 /* Append the microseconds part, if that's requested */
313 return NULL
; /* Microseconds part doesn't fit. */
315 sprintf(buf
+ n
, ".%06"PRI_USEC
, t
% USEC_PER_SEC
);
318 /* Append the timezone */
321 /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r() normally uses the
322 * obsolete "GMT" instead. */
324 return NULL
; /* "UTC" doesn't fit. */
326 strcpy(buf
+ n
, " UTC");
328 } else if (!isempty(tm
.tm_zone
)) {
331 /* An explicit timezone is specified, let's use it, if it fits */
332 tn
= strlen(tm
.tm_zone
);
333 if (n
+ 1 + tn
+ 1 > l
) {
334 /* The full time zone does not fit in. Yuck. */
336 if (n
+ 1 + _POSIX_TZNAME_MAX
+ 1 > l
)
337 return NULL
; /* Not even enough space for the POSIX minimum (of 6)? In that case, complain that it doesn't fit */
339 /* So the time zone doesn't fit in fully, but the caller passed enough space for the POSIX
340 * minimum time zone length. In this case suppress the timezone entirely, in order not to dump
341 * an overly long, hard to read string on the user. This should be safe, because the user will
342 * assume the local timezone anyway if none is shown. And so does parse_timestamp(). */
345 strcpy(buf
+ n
, tm
.tm_zone
);
352 char *format_timestamp(char *buf
, size_t l
, usec_t t
) {
353 return format_timestamp_internal(buf
, l
, t
, false, false);
356 char *format_timestamp_utc(char *buf
, size_t l
, usec_t t
) {
357 return format_timestamp_internal(buf
, l
, t
, true, false);
360 char *format_timestamp_us(char *buf
, size_t l
, usec_t t
) {
361 return format_timestamp_internal(buf
, l
, t
, false, true);
364 char *format_timestamp_us_utc(char *buf
, size_t l
, usec_t t
) {
365 return format_timestamp_internal(buf
, l
, t
, true, true);
368 char *format_timestamp_relative(char *buf
, size_t l
, usec_t t
) {
372 if (t
<= 0 || t
== USEC_INFINITY
)
375 n
= now(CLOCK_REALTIME
);
384 if (d
>= USEC_PER_YEAR
)
385 snprintf(buf
, l
, USEC_FMT
" years " USEC_FMT
" months %s",
387 (d
% USEC_PER_YEAR
) / USEC_PER_MONTH
, s
);
388 else if (d
>= USEC_PER_MONTH
)
389 snprintf(buf
, l
, USEC_FMT
" months " USEC_FMT
" days %s",
391 (d
% USEC_PER_MONTH
) / USEC_PER_DAY
, s
);
392 else if (d
>= USEC_PER_WEEK
)
393 snprintf(buf
, l
, USEC_FMT
" weeks " USEC_FMT
" days %s",
395 (d
% USEC_PER_WEEK
) / USEC_PER_DAY
, s
);
396 else if (d
>= 2*USEC_PER_DAY
)
397 snprintf(buf
, l
, USEC_FMT
" days %s", d
/ USEC_PER_DAY
, s
);
398 else if (d
>= 25*USEC_PER_HOUR
)
399 snprintf(buf
, l
, "1 day " USEC_FMT
"h %s",
400 (d
- USEC_PER_DAY
) / USEC_PER_HOUR
, s
);
401 else if (d
>= 6*USEC_PER_HOUR
)
402 snprintf(buf
, l
, USEC_FMT
"h %s",
403 d
/ USEC_PER_HOUR
, s
);
404 else if (d
>= USEC_PER_HOUR
)
405 snprintf(buf
, l
, USEC_FMT
"h " USEC_FMT
"min %s",
407 (d
% USEC_PER_HOUR
) / USEC_PER_MINUTE
, s
);
408 else if (d
>= 5*USEC_PER_MINUTE
)
409 snprintf(buf
, l
, USEC_FMT
"min %s",
410 d
/ USEC_PER_MINUTE
, s
);
411 else if (d
>= USEC_PER_MINUTE
)
412 snprintf(buf
, l
, USEC_FMT
"min " USEC_FMT
"s %s",
414 (d
% USEC_PER_MINUTE
) / USEC_PER_SEC
, s
);
415 else if (d
>= USEC_PER_SEC
)
416 snprintf(buf
, l
, USEC_FMT
"s %s",
417 d
/ USEC_PER_SEC
, s
);
418 else if (d
>= USEC_PER_MSEC
)
419 snprintf(buf
, l
, USEC_FMT
"ms %s",
420 d
/ USEC_PER_MSEC
, s
);
422 snprintf(buf
, l
, USEC_FMT
"us %s",
425 snprintf(buf
, l
, "now");
431 char *format_timespan(char *buf
, size_t l
, usec_t t
, usec_t accuracy
) {
432 static const struct {
436 { "y", USEC_PER_YEAR
},
437 { "month", USEC_PER_MONTH
},
438 { "w", USEC_PER_WEEK
},
439 { "d", USEC_PER_DAY
},
440 { "h", USEC_PER_HOUR
},
441 { "min", USEC_PER_MINUTE
},
442 { "s", USEC_PER_SEC
},
443 { "ms", USEC_PER_MSEC
},
449 bool something
= false;
454 if (t
== USEC_INFINITY
) {
455 strncpy(p
, "infinity", l
-1);
461 strncpy(p
, "0", l
-1);
466 /* The result of this function can be parsed with parse_sec */
468 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
477 if (t
< accuracy
&& something
)
480 if (t
< table
[i
].usec
)
486 a
= t
/ table
[i
].usec
;
487 b
= t
% table
[i
].usec
;
489 /* Let's see if we should shows this in dot notation */
490 if (t
< USEC_PER_MINUTE
&& b
> 0) {
495 for (cc
= table
[i
].usec
; cc
> 1; cc
/= 10)
498 for (cc
= accuracy
; cc
> 1; cc
/= 10) {
505 "%s"USEC_FMT
".%0*"PRI_USEC
"%s",
517 /* No? Then let's show it normally */
528 n
= MIN((size_t) k
, l
);
541 void dual_timestamp_serialize(FILE *f
, const char *name
, dual_timestamp
*t
) {
547 if (!dual_timestamp_is_set(t
))
550 fprintf(f
, "%s="USEC_FMT
" "USEC_FMT
"\n",
556 int dual_timestamp_deserialize(const char *value
, dual_timestamp
*t
) {
563 pos
= strspn(value
, WHITESPACE
);
564 if (value
[pos
] == '-')
566 pos
+= strspn(value
+ pos
, DIGITS
);
567 pos
+= strspn(value
+ pos
, WHITESPACE
);
568 if (value
[pos
] == '-')
571 r
= sscanf(value
, "%" PRIu64
"%" PRIu64
"%n", &a
, &b
, &pos
);
573 log_debug("Failed to parse dual timestamp value \"%s\".", value
);
577 if (value
[pos
] != '\0')
578 /* trailing garbage */
587 int timestamp_deserialize(const char *value
, usec_t
*timestamp
) {
592 r
= safe_atou64(value
, timestamp
);
594 return log_debug_errno(r
, "Failed to parse timestamp value \"%s\": %m", value
);
599 int parse_timestamp(const char *t
, usec_t
*usec
) {
600 static const struct {
620 const char *k
, *utc
, *tzn
= NULL
;
623 usec_t x_usec
, plus
= 0, minus
= 0, ret
;
624 int r
, weekday
= -1, dst
= -1;
630 * 2012-09-22 16:34:22
631 * 2012-09-22 16:34 (seconds will be set to 0)
632 * 2012-09-22 (time will be set to 00:00:00)
633 * 16:34:22 (date will be set to today)
634 * 16:34 (date will be set to today, seconds to 0)
636 * yesterday (time is set to 00:00:00)
637 * today (time is set to 00:00:00)
638 * tomorrow (time is set to 00:00:00)
641 * @2147483647 (seconds since epoch)
649 return parse_sec(t
+ 1, usec
);
651 ret
= now(CLOCK_REALTIME
);
656 else if (t
[0] == '+') {
657 r
= parse_sec(t
+1, &plus
);
663 } else if (t
[0] == '-') {
664 r
= parse_sec(t
+1, &minus
);
670 } else if ((k
= endswith(t
, " ago"))) {
671 t
= strndupa(t
, k
- t
);
673 r
= parse_sec(t
, &minus
);
679 } else if ((k
= endswith(t
, " left"))) {
680 t
= strndupa(t
, k
- t
);
682 r
= parse_sec(t
, &plus
);
689 /* See if the timestamp is suffixed with UTC */
690 utc
= endswith_no_case(t
, " UTC");
692 t
= strndupa(t
, utc
- t
);
694 const char *e
= NULL
;
699 /* See if the timestamp is suffixed by either the DST or non-DST local timezone. Note that we only
700 * support the local timezones here, nothing else. Not because we wouldn't want to, but simply because
701 * there are no nice APIs available to cover this. By accepting the local time zone strings, we make
702 * sure that all timestamps written by format_timestamp() can be parsed correctly, even though we don't
703 * support arbitrary timezone specifications. */
705 for (j
= 0; j
<= 1; j
++) {
707 if (isempty(tzname
[j
]))
710 e
= endswith_no_case(t
, tzname
[j
]);
721 if (IN_SET(j
, 0, 1)) {
722 /* Found one of the two timezones specified. */
723 t
= strndupa(t
, e
- t
- 1);
729 x
= (time_t) (ret
/ USEC_PER_SEC
);
732 if (!localtime_or_gmtime_r(&x
, &tm
, utc
))
739 if (streq(t
, "today")) {
740 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
743 } else if (streq(t
, "yesterday")) {
745 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
748 } else if (streq(t
, "tomorrow")) {
750 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
754 for (i
= 0; i
< ELEMENTSOF(day_nr
); i
++) {
757 if (!startswith_no_case(t
, day_nr
[i
].name
))
760 skip
= strlen(day_nr
[i
].name
);
764 weekday
= day_nr
[i
].nr
;
770 k
= strptime(t
, "%y-%m-%d %H:%M:%S", &tm
);
779 k
= strptime(t
, "%Y-%m-%d %H:%M:%S", &tm
);
788 k
= strptime(t
, "%y-%m-%d %H:%M", &tm
);
795 k
= strptime(t
, "%Y-%m-%d %H:%M", &tm
);
802 k
= strptime(t
, "%y-%m-%d", &tm
);
804 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
809 k
= strptime(t
, "%Y-%m-%d", &tm
);
811 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
816 k
= strptime(t
, "%H:%M:%S", &tm
);
825 k
= strptime(t
, "%H:%M", &tm
);
838 r
= parse_fractional_part_u(&k
, 6, &add
);
849 x
= mktime_or_timegm(&tm
, utc
);
853 if (weekday
>= 0 && tm
.tm_wday
!= weekday
)
856 ret
= (usec_t
) x
* USEC_PER_SEC
+ x_usec
;
857 if (ret
> USEC_TIMESTAMP_FORMATTABLE_MAX
)
861 if (ret
+ plus
< ret
) /* overflow? */
864 if (ret
> USEC_TIMESTAMP_FORMATTABLE_MAX
)
877 static char* extract_multiplier(char *p
, usec_t
*multiplier
) {
878 static const struct {
882 { "seconds", USEC_PER_SEC
},
883 { "second", USEC_PER_SEC
},
884 { "sec", USEC_PER_SEC
},
885 { "s", USEC_PER_SEC
},
886 { "minutes", USEC_PER_MINUTE
},
887 { "minute", USEC_PER_MINUTE
},
888 { "min", USEC_PER_MINUTE
},
889 { "months", USEC_PER_MONTH
},
890 { "month", USEC_PER_MONTH
},
891 { "M", USEC_PER_MONTH
},
892 { "msec", USEC_PER_MSEC
},
893 { "ms", USEC_PER_MSEC
},
894 { "m", USEC_PER_MINUTE
},
895 { "hours", USEC_PER_HOUR
},
896 { "hour", USEC_PER_HOUR
},
897 { "hr", USEC_PER_HOUR
},
898 { "h", USEC_PER_HOUR
},
899 { "days", USEC_PER_DAY
},
900 { "day", USEC_PER_DAY
},
901 { "d", USEC_PER_DAY
},
902 { "weeks", USEC_PER_WEEK
},
903 { "week", USEC_PER_WEEK
},
904 { "w", USEC_PER_WEEK
},
905 { "years", USEC_PER_YEAR
},
906 { "year", USEC_PER_YEAR
},
907 { "y", USEC_PER_YEAR
},
914 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
917 e
= startswith(p
, table
[i
].suffix
);
919 *multiplier
= table
[i
].usec
;
927 int parse_time(const char *t
, usec_t
*usec
, usec_t default_unit
) {
930 bool something
= false;
934 assert(default_unit
> 0);
938 p
+= strspn(p
, WHITESPACE
);
939 s
= startswith(p
, "infinity");
941 s
+= strspn(s
, WHITESPACE
);
945 *usec
= USEC_INFINITY
;
953 usec_t multiplier
= default_unit
, k
;
955 p
+= strspn(p
, WHITESPACE
);
965 l
= strtoll(p
, &e
, 10);
975 z
= strtoll(b
, &e
, 10);
990 e
+= strspn(e
, WHITESPACE
);
991 p
= extract_multiplier(e
, &multiplier
);
995 k
= (usec_t
) z
* multiplier
;
1000 r
+= (usec_t
) l
* multiplier
+ k
;
1008 int parse_sec(const char *t
, usec_t
*usec
) {
1009 return parse_time(t
, usec
, USEC_PER_SEC
);
1012 int parse_sec_fix_0(const char *t
, usec_t
*usec
) {
1013 t
+= strspn(t
, WHITESPACE
);
1014 if (streq(t
, "0")) {
1015 *usec
= USEC_INFINITY
;
1019 return parse_sec(t
, usec
);
1022 int parse_nsec(const char *t
, nsec_t
*nsec
) {
1023 static const struct {
1027 { "seconds", NSEC_PER_SEC
},
1028 { "second", NSEC_PER_SEC
},
1029 { "sec", NSEC_PER_SEC
},
1030 { "s", NSEC_PER_SEC
},
1031 { "minutes", NSEC_PER_MINUTE
},
1032 { "minute", NSEC_PER_MINUTE
},
1033 { "min", NSEC_PER_MINUTE
},
1034 { "months", NSEC_PER_MONTH
},
1035 { "month", NSEC_PER_MONTH
},
1036 { "msec", NSEC_PER_MSEC
},
1037 { "ms", NSEC_PER_MSEC
},
1038 { "m", NSEC_PER_MINUTE
},
1039 { "hours", NSEC_PER_HOUR
},
1040 { "hour", NSEC_PER_HOUR
},
1041 { "hr", NSEC_PER_HOUR
},
1042 { "h", NSEC_PER_HOUR
},
1043 { "days", NSEC_PER_DAY
},
1044 { "day", NSEC_PER_DAY
},
1045 { "d", NSEC_PER_DAY
},
1046 { "weeks", NSEC_PER_WEEK
},
1047 { "week", NSEC_PER_WEEK
},
1048 { "w", NSEC_PER_WEEK
},
1049 { "years", NSEC_PER_YEAR
},
1050 { "year", NSEC_PER_YEAR
},
1051 { "y", NSEC_PER_YEAR
},
1052 { "usec", NSEC_PER_USEC
},
1053 { "us", NSEC_PER_USEC
},
1054 { "µs", NSEC_PER_USEC
},
1057 { "", 1ULL }, /* default is nsec */
1062 bool something
= false;
1069 p
+= strspn(p
, WHITESPACE
);
1070 s
= startswith(p
, "infinity");
1072 s
+= strspn(s
, WHITESPACE
);
1076 *nsec
= NSEC_INFINITY
;
1085 p
+= strspn(p
, WHITESPACE
);
1095 l
= strtoll(p
, &e
, 10);
1107 z
= strtoll(b
, &e
, 10);
1122 e
+= strspn(e
, WHITESPACE
);
1124 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
1125 if (startswith(e
, table
[i
].suffix
)) {
1126 nsec_t k
= (nsec_t
) z
* table
[i
].nsec
;
1131 r
+= (nsec_t
) l
* table
[i
].nsec
+ k
;
1132 p
= e
+ strlen(table
[i
].suffix
);
1138 if (i
>= ELEMENTSOF(table
))
1148 bool ntp_synced(void) {
1149 struct timex txc
= {};
1151 if (adjtimex(&txc
) < 0)
1154 if (txc
.status
& STA_UNSYNC
)
1160 int get_timezones(char ***ret
) {
1161 _cleanup_fclose_
FILE *f
= NULL
;
1162 _cleanup_strv_free_
char **zones
= NULL
;
1163 size_t n_zones
= 0, n_allocated
= 0;
1167 zones
= strv_new("UTC", NULL
);
1174 f
= fopen("/usr/share/zoneinfo/zone.tab", "re");
1178 FOREACH_LINE(l
, f
, return -errno
) {
1184 if (isempty(p
) || *p
== '#')
1187 /* Skip over country code */
1188 p
+= strcspn(p
, WHITESPACE
);
1189 p
+= strspn(p
, WHITESPACE
);
1191 /* Skip over coordinates */
1192 p
+= strcspn(p
, WHITESPACE
);
1193 p
+= strspn(p
, WHITESPACE
);
1195 /* Found timezone name */
1196 k
= strcspn(p
, WHITESPACE
);
1204 if (!GREEDY_REALLOC(zones
, n_allocated
, n_zones
+ 2)) {
1209 zones
[n_zones
++] = w
;
1210 zones
[n_zones
] = NULL
;
1215 } else if (errno
!= ENOENT
)
1224 bool timezone_is_valid(const char *name
) {
1235 for (p
= name
; *p
; p
++) {
1236 if (!(*p
>= '0' && *p
<= '9') &&
1237 !(*p
>= 'a' && *p
<= 'z') &&
1238 !(*p
>= 'A' && *p
<= 'Z') &&
1239 !(*p
== '-' || *p
== '_' || *p
== '+' || *p
== '/'))
1255 t
= strjoina("/usr/share/zoneinfo/", name
);
1256 if (stat(t
, &st
) < 0)
1259 if (!S_ISREG(st
.st_mode
))
1265 bool clock_boottime_supported(void) {
1266 static int supported
= -1;
1268 /* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */
1270 if (supported
< 0) {
1273 fd
= timerfd_create(CLOCK_BOOTTIME
, TFD_NONBLOCK
|TFD_CLOEXEC
);
1285 clockid_t
clock_boottime_or_monotonic(void) {
1286 if (clock_boottime_supported())
1287 return CLOCK_BOOTTIME
;
1289 return CLOCK_MONOTONIC
;
1292 bool clock_supported(clockid_t clock
) {
1297 case CLOCK_MONOTONIC
:
1298 case CLOCK_REALTIME
:
1301 case CLOCK_BOOTTIME
:
1302 return clock_boottime_supported();
1304 case CLOCK_BOOTTIME_ALARM
:
1305 if (!clock_boottime_supported())
1311 /* For everything else, check properly */
1312 return clock_gettime(clock
, &ts
) >= 0;
1316 int get_timezone(char **tz
) {
1317 _cleanup_free_
char *t
= NULL
;
1322 r
= readlink_malloc("/etc/localtime", &t
);
1324 return r
; /* returns EINVAL if not a symlink */
1326 e
= path_startswith(t
, "/usr/share/zoneinfo/");
1328 e
= path_startswith(t
, "../usr/share/zoneinfo/");
1332 if (!timezone_is_valid(e
))
1343 time_t mktime_or_timegm(struct tm
*tm
, bool utc
) {
1344 return utc
? timegm(tm
) : mktime(tm
);
1347 struct tm
*localtime_or_gmtime_r(const time_t *t
, struct tm
*tm
, bool utc
) {
1348 return utc
? gmtime_r(t
, tm
) : localtime_r(t
, tm
);
1351 unsigned long usec_to_jiffies(usec_t u
) {
1352 static thread_local
unsigned long hz
= 0;
1356 r
= sysconf(_SC_CLK_TCK
);
1362 return DIV_ROUND_UP(u
, USEC_PER_SEC
/ hz
);
1365 usec_t
usec_shift_clock(usec_t x
, clockid_t from
, clockid_t to
) {
1368 if (x
== USEC_INFINITY
)
1369 return USEC_INFINITY
;
1370 if (map_clock_id(from
) == map_clock_id(to
))
1377 /* x lies in the future */
1378 return usec_add(b
, usec_sub_unsigned(x
, a
));
1380 /* x lies in the past */
1381 return usec_sub_unsigned(b
, usec_sub_unsigned(a
, x
));