]>
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 triple_timestamp
* triple_timestamp_get(triple_timestamp
*ts
) {
93 ts
->realtime
= now(CLOCK_REALTIME
);
94 ts
->monotonic
= now(CLOCK_MONOTONIC
);
95 ts
->boottime
= clock_boottime_supported() ? now(CLOCK_BOOTTIME
) : USEC_INFINITY
;
100 dual_timestamp
* dual_timestamp_from_realtime(dual_timestamp
*ts
, usec_t u
) {
104 if (u
== USEC_INFINITY
|| u
<= 0) {
105 ts
->realtime
= ts
->monotonic
= u
;
111 delta
= (int64_t) now(CLOCK_REALTIME
) - (int64_t) u
;
112 ts
->monotonic
= usec_sub(now(CLOCK_MONOTONIC
), delta
);
117 triple_timestamp
* triple_timestamp_from_realtime(triple_timestamp
*ts
, usec_t u
) {
122 if (u
== USEC_INFINITY
|| u
<= 0) {
123 ts
->realtime
= ts
->monotonic
= ts
->boottime
= u
;
128 delta
= (int64_t) now(CLOCK_REALTIME
) - (int64_t) u
;
129 ts
->monotonic
= usec_sub(now(CLOCK_MONOTONIC
), delta
);
130 ts
->boottime
= clock_boottime_supported() ? usec_sub(now(CLOCK_BOOTTIME
), delta
) : USEC_INFINITY
;
135 dual_timestamp
* dual_timestamp_from_monotonic(dual_timestamp
*ts
, usec_t u
) {
139 if (u
== USEC_INFINITY
) {
140 ts
->realtime
= ts
->monotonic
= USEC_INFINITY
;
145 delta
= (int64_t) now(CLOCK_MONOTONIC
) - (int64_t) u
;
146 ts
->realtime
= usec_sub(now(CLOCK_REALTIME
), delta
);
151 dual_timestamp
* dual_timestamp_from_boottime_or_monotonic(dual_timestamp
*ts
, usec_t u
) {
154 if (u
== USEC_INFINITY
) {
155 ts
->realtime
= ts
->monotonic
= USEC_INFINITY
;
159 dual_timestamp_get(ts
);
160 delta
= (int64_t) now(clock_boottime_or_monotonic()) - (int64_t) u
;
161 ts
->realtime
= usec_sub(ts
->realtime
, delta
);
162 ts
->monotonic
= usec_sub(ts
->monotonic
, delta
);
167 usec_t
triple_timestamp_by_clock(triple_timestamp
*ts
, clockid_t clock
) {
172 case CLOCK_REALTIME_ALARM
:
175 case CLOCK_MONOTONIC
:
176 return ts
->monotonic
;
179 case CLOCK_BOOTTIME_ALARM
:
183 return USEC_INFINITY
;
187 usec_t
timespec_load(const struct timespec
*ts
) {
190 if (ts
->tv_sec
== (time_t) -1 && ts
->tv_nsec
== (long) -1)
191 return USEC_INFINITY
;
193 if ((usec_t
) ts
->tv_sec
> (UINT64_MAX
- (ts
->tv_nsec
/ NSEC_PER_USEC
)) / USEC_PER_SEC
)
194 return USEC_INFINITY
;
197 (usec_t
) ts
->tv_sec
* USEC_PER_SEC
+
198 (usec_t
) ts
->tv_nsec
/ NSEC_PER_USEC
;
201 static nsec_t
timespec_load_nsec(const struct timespec
*ts
) {
204 if (ts
->tv_sec
== (time_t) -1 && ts
->tv_nsec
== (long) -1)
205 return NSEC_INFINITY
;
207 if ((nsec_t
) ts
->tv_sec
>= (UINT64_MAX
- ts
->tv_nsec
) / NSEC_PER_SEC
)
208 return NSEC_INFINITY
;
210 return (nsec_t
) ts
->tv_sec
* NSEC_PER_SEC
+ (nsec_t
) ts
->tv_nsec
;
213 struct timespec
*timespec_store(struct timespec
*ts
, usec_t u
) {
216 if (u
== USEC_INFINITY
) {
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
== (time_t) -1 &&
232 tv
->tv_usec
== (suseconds_t
) -1)
233 return USEC_INFINITY
;
235 if ((usec_t
) tv
->tv_sec
> (UINT64_MAX
- tv
->tv_usec
) / USEC_PER_SEC
)
236 return USEC_INFINITY
;
239 (usec_t
) tv
->tv_sec
* USEC_PER_SEC
+
240 (usec_t
) tv
->tv_usec
;
243 struct timeval
*timeval_store(struct timeval
*tv
, usec_t u
) {
246 if (u
== USEC_INFINITY
) {
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 sec
= (time_t) (t
/ USEC_PER_SEC
); /* Round down */
294 if ((usec_t
) sec
!= (t
/ USEC_PER_SEC
))
295 return NULL
; /* overflow? */
297 if (!localtime_or_gmtime_r(&sec
, &tm
, utc
))
300 /* Start with the week day */
301 assert((size_t) tm
.tm_wday
< ELEMENTSOF(weekdays
));
302 memcpy(buf
, weekdays
[tm
.tm_wday
], 4);
304 /* Add the main components */
305 if (strftime(buf
+ 3, l
- 3, " %Y-%m-%d %H:%M:%S", &tm
) <= 0)
306 return NULL
; /* Doesn't fit */
308 /* Append the microseconds part, if that's requested */
312 return NULL
; /* Microseconds part doesn't fit. */
314 sprintf(buf
+ n
, ".%06llu", (unsigned long long) (t
% USEC_PER_SEC
));
317 /* Append the timezone */
320 /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r() normally uses the
321 * obsolete "GMT" instead. */
323 return NULL
; /* "UTC" doesn't fit. */
325 strcpy(buf
+ n
, " UTC");
327 } else if (!isempty(tm
.tm_zone
)) {
330 /* An explicit timezone is specified, let's use it, if it fits */
331 tn
= strlen(tm
.tm_zone
);
332 if (n
+ 1 + tn
+ 1 > l
) {
333 /* The full time zone does not fit in. Yuck. */
335 if (n
+ 1 + _POSIX_TZNAME_MAX
+ 1 > l
)
336 return NULL
; /* Not even enough space for the POSIX minimum (of 6)? In that case, complain that it doesn't fit */
338 /* So the time zone doesn't fit in fully, but the caller passed enough space for the POSIX
339 * minimum time zone length. In this case suppress the timezone entirely, in order not to dump
340 * an overly long, hard to read string on the user. This should be safe, because the user will
341 * assume the local timezone anyway if none is shown. And so does parse_timestamp(). */
344 strcpy(buf
+ n
, tm
.tm_zone
);
351 char *format_timestamp(char *buf
, size_t l
, usec_t t
) {
352 return format_timestamp_internal(buf
, l
, t
, false, false);
355 char *format_timestamp_utc(char *buf
, size_t l
, usec_t t
) {
356 return format_timestamp_internal(buf
, l
, t
, true, false);
359 char *format_timestamp_us(char *buf
, size_t l
, usec_t t
) {
360 return format_timestamp_internal(buf
, l
, t
, false, true);
363 char *format_timestamp_us_utc(char *buf
, size_t l
, usec_t t
) {
364 return format_timestamp_internal(buf
, l
, t
, true, true);
367 char *format_timestamp_relative(char *buf
, size_t l
, usec_t t
) {
371 if (t
<= 0 || t
== USEC_INFINITY
)
374 n
= now(CLOCK_REALTIME
);
383 if (d
>= USEC_PER_YEAR
)
384 snprintf(buf
, l
, USEC_FMT
" years " USEC_FMT
" months %s",
386 (d
% USEC_PER_YEAR
) / USEC_PER_MONTH
, s
);
387 else if (d
>= USEC_PER_MONTH
)
388 snprintf(buf
, l
, USEC_FMT
" months " USEC_FMT
" days %s",
390 (d
% USEC_PER_MONTH
) / USEC_PER_DAY
, s
);
391 else if (d
>= USEC_PER_WEEK
)
392 snprintf(buf
, l
, USEC_FMT
" weeks " USEC_FMT
" days %s",
394 (d
% USEC_PER_WEEK
) / USEC_PER_DAY
, s
);
395 else if (d
>= 2*USEC_PER_DAY
)
396 snprintf(buf
, l
, USEC_FMT
" days %s", d
/ USEC_PER_DAY
, s
);
397 else if (d
>= 25*USEC_PER_HOUR
)
398 snprintf(buf
, l
, "1 day " USEC_FMT
"h %s",
399 (d
- USEC_PER_DAY
) / USEC_PER_HOUR
, s
);
400 else if (d
>= 6*USEC_PER_HOUR
)
401 snprintf(buf
, l
, USEC_FMT
"h %s",
402 d
/ USEC_PER_HOUR
, s
);
403 else if (d
>= USEC_PER_HOUR
)
404 snprintf(buf
, l
, USEC_FMT
"h " USEC_FMT
"min %s",
406 (d
% USEC_PER_HOUR
) / USEC_PER_MINUTE
, s
);
407 else if (d
>= 5*USEC_PER_MINUTE
)
408 snprintf(buf
, l
, USEC_FMT
"min %s",
409 d
/ USEC_PER_MINUTE
, s
);
410 else if (d
>= USEC_PER_MINUTE
)
411 snprintf(buf
, l
, USEC_FMT
"min " USEC_FMT
"s %s",
413 (d
% USEC_PER_MINUTE
) / USEC_PER_SEC
, s
);
414 else if (d
>= USEC_PER_SEC
)
415 snprintf(buf
, l
, USEC_FMT
"s %s",
416 d
/ USEC_PER_SEC
, s
);
417 else if (d
>= USEC_PER_MSEC
)
418 snprintf(buf
, l
, USEC_FMT
"ms %s",
419 d
/ USEC_PER_MSEC
, s
);
421 snprintf(buf
, l
, USEC_FMT
"us %s",
424 snprintf(buf
, l
, "now");
430 char *format_timespan(char *buf
, size_t l
, usec_t t
, usec_t accuracy
) {
431 static const struct {
435 { "y", USEC_PER_YEAR
},
436 { "month", USEC_PER_MONTH
},
437 { "w", USEC_PER_WEEK
},
438 { "d", USEC_PER_DAY
},
439 { "h", USEC_PER_HOUR
},
440 { "min", USEC_PER_MINUTE
},
441 { "s", USEC_PER_SEC
},
442 { "ms", USEC_PER_MSEC
},
448 bool something
= false;
453 if (t
== USEC_INFINITY
) {
454 strncpy(p
, "infinity", l
-1);
460 strncpy(p
, "0", l
-1);
465 /* The result of this function can be parsed with parse_sec */
467 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
476 if (t
< accuracy
&& something
)
479 if (t
< table
[i
].usec
)
485 a
= t
/ table
[i
].usec
;
486 b
= t
% table
[i
].usec
;
488 /* Let's see if we should shows this in dot notation */
489 if (t
< USEC_PER_MINUTE
&& b
> 0) {
494 for (cc
= table
[i
].usec
; cc
> 1; cc
/= 10)
497 for (cc
= accuracy
; cc
> 1; cc
/= 10) {
504 "%s"USEC_FMT
".%0*llu%s",
508 (unsigned long long) b
,
516 /* No? Then let's show it normally */
527 n
= MIN((size_t) k
, l
);
540 void dual_timestamp_serialize(FILE *f
, const char *name
, dual_timestamp
*t
) {
546 if (!dual_timestamp_is_set(t
))
549 fprintf(f
, "%s="USEC_FMT
" "USEC_FMT
"\n",
555 int dual_timestamp_deserialize(const char *value
, dual_timestamp
*t
) {
556 unsigned long long a
, b
;
561 if (sscanf(value
, "%llu %llu", &a
, &b
) != 2) {
562 log_debug("Failed to parse dual timestamp value \"%s\": %m", value
);
572 int timestamp_deserialize(const char *value
, usec_t
*timestamp
) {
577 r
= safe_atou64(value
, timestamp
);
579 return log_debug_errno(r
, "Failed to parse timestamp value \"%s\": %m", value
);
584 int parse_timestamp(const char *t
, usec_t
*usec
) {
585 static const struct {
605 const char *k
, *utc
, *tzn
= NULL
;
608 usec_t x_usec
, plus
= 0, minus
= 0, ret
;
609 int r
, weekday
= -1, dst
= -1;
615 * 2012-09-22 16:34:22
616 * 2012-09-22 16:34 (seconds will be set to 0)
617 * 2012-09-22 (time will be set to 00:00:00)
618 * 16:34:22 (date will be set to today)
619 * 16:34 (date will be set to today, seconds to 0)
621 * yesterday (time is set to 00:00:00)
622 * today (time is set to 00:00:00)
623 * tomorrow (time is set to 00:00:00)
626 * @2147483647 (seconds since epoch)
634 return parse_sec(t
+ 1, usec
);
636 ret
= now(CLOCK_REALTIME
);
641 else if (t
[0] == '+') {
642 r
= parse_sec(t
+1, &plus
);
648 } else if (t
[0] == '-') {
649 r
= parse_sec(t
+1, &minus
);
655 } else if ((k
= endswith(t
, " ago"))) {
656 t
= strndupa(t
, k
- t
);
658 r
= parse_sec(t
, &minus
);
664 } else if ((k
= endswith(t
, " left"))) {
665 t
= strndupa(t
, k
- t
);
667 r
= parse_sec(t
, &plus
);
674 /* See if the timestamp is suffixed with UTC */
675 utc
= endswith_no_case(t
, " UTC");
677 t
= strndupa(t
, utc
- t
);
679 const char *e
= NULL
;
684 /* See if the timestamp is suffixed by either the DST or non-DST local timezone. Note that we only
685 * support the local timezones here, nothing else. Not because we wouldn't want to, but simply because
686 * there are no nice APIs available to cover this. By accepting the local time zone strings, we make
687 * sure that all timestamps written by format_timestamp() can be parsed correctly, even though we don't
688 * support arbitrary timezone specifications. */
690 for (j
= 0; j
<= 1; j
++) {
692 if (isempty(tzname
[j
]))
695 e
= endswith_no_case(t
, tzname
[j
]);
706 if (IN_SET(j
, 0, 1)) {
707 /* Found one of the two timezones specified. */
708 t
= strndupa(t
, e
- t
- 1);
714 x
= (time_t) (ret
/ USEC_PER_SEC
);
717 if (!localtime_or_gmtime_r(&x
, &tm
, utc
))
724 if (streq(t
, "today")) {
725 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
728 } else if (streq(t
, "yesterday")) {
730 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
733 } else if (streq(t
, "tomorrow")) {
735 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
739 for (i
= 0; i
< ELEMENTSOF(day_nr
); i
++) {
742 if (!startswith_no_case(t
, day_nr
[i
].name
))
745 skip
= strlen(day_nr
[i
].name
);
749 weekday
= day_nr
[i
].nr
;
755 k
= strptime(t
, "%y-%m-%d %H:%M:%S", &tm
);
764 k
= strptime(t
, "%Y-%m-%d %H:%M:%S", &tm
);
773 k
= strptime(t
, "%y-%m-%d %H:%M", &tm
);
780 k
= strptime(t
, "%Y-%m-%d %H:%M", &tm
);
787 k
= strptime(t
, "%y-%m-%d", &tm
);
789 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
794 k
= strptime(t
, "%Y-%m-%d", &tm
);
796 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
801 k
= strptime(t
, "%H:%M:%S", &tm
);
810 k
= strptime(t
, "%H:%M", &tm
);
823 r
= parse_fractional_part_u(&k
, 6, &add
);
834 x
= mktime_or_timegm(&tm
, utc
);
835 if (x
== (time_t) -1)
838 if (weekday
>= 0 && tm
.tm_wday
!= weekday
)
841 ret
= (usec_t
) x
* USEC_PER_SEC
+ x_usec
;
855 static char* extract_multiplier(char *p
, usec_t
*multiplier
) {
856 static const struct {
860 { "seconds", USEC_PER_SEC
},
861 { "second", USEC_PER_SEC
},
862 { "sec", USEC_PER_SEC
},
863 { "s", USEC_PER_SEC
},
864 { "minutes", USEC_PER_MINUTE
},
865 { "minute", USEC_PER_MINUTE
},
866 { "min", USEC_PER_MINUTE
},
867 { "months", USEC_PER_MONTH
},
868 { "month", USEC_PER_MONTH
},
869 { "M", USEC_PER_MONTH
},
870 { "msec", USEC_PER_MSEC
},
871 { "ms", USEC_PER_MSEC
},
872 { "m", USEC_PER_MINUTE
},
873 { "hours", USEC_PER_HOUR
},
874 { "hour", USEC_PER_HOUR
},
875 { "hr", USEC_PER_HOUR
},
876 { "h", USEC_PER_HOUR
},
877 { "days", USEC_PER_DAY
},
878 { "day", USEC_PER_DAY
},
879 { "d", USEC_PER_DAY
},
880 { "weeks", USEC_PER_WEEK
},
881 { "week", USEC_PER_WEEK
},
882 { "w", USEC_PER_WEEK
},
883 { "years", USEC_PER_YEAR
},
884 { "year", USEC_PER_YEAR
},
885 { "y", USEC_PER_YEAR
},
891 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
894 e
= startswith(p
, table
[i
].suffix
);
896 *multiplier
= table
[i
].usec
;
904 int parse_time(const char *t
, usec_t
*usec
, usec_t default_unit
) {
907 bool something
= false;
911 assert(default_unit
> 0);
915 p
+= strspn(p
, WHITESPACE
);
916 s
= startswith(p
, "infinity");
918 s
+= strspn(s
, WHITESPACE
);
922 *usec
= USEC_INFINITY
;
930 usec_t multiplier
= default_unit
, k
;
932 p
+= strspn(p
, WHITESPACE
);
942 l
= strtoll(p
, &e
, 10);
952 z
= strtoll(b
, &e
, 10);
967 e
+= strspn(e
, WHITESPACE
);
968 p
= extract_multiplier(e
, &multiplier
);
972 k
= (usec_t
) z
* multiplier
;
977 r
+= (usec_t
) l
* multiplier
+ k
;
985 int parse_sec(const char *t
, usec_t
*usec
) {
986 return parse_time(t
, usec
, USEC_PER_SEC
);
989 int parse_nsec(const char *t
, nsec_t
*nsec
) {
990 static const struct {
994 { "seconds", NSEC_PER_SEC
},
995 { "second", NSEC_PER_SEC
},
996 { "sec", NSEC_PER_SEC
},
997 { "s", NSEC_PER_SEC
},
998 { "minutes", NSEC_PER_MINUTE
},
999 { "minute", NSEC_PER_MINUTE
},
1000 { "min", NSEC_PER_MINUTE
},
1001 { "months", NSEC_PER_MONTH
},
1002 { "month", NSEC_PER_MONTH
},
1003 { "msec", NSEC_PER_MSEC
},
1004 { "ms", NSEC_PER_MSEC
},
1005 { "m", NSEC_PER_MINUTE
},
1006 { "hours", NSEC_PER_HOUR
},
1007 { "hour", NSEC_PER_HOUR
},
1008 { "hr", NSEC_PER_HOUR
},
1009 { "h", NSEC_PER_HOUR
},
1010 { "days", NSEC_PER_DAY
},
1011 { "day", NSEC_PER_DAY
},
1012 { "d", NSEC_PER_DAY
},
1013 { "weeks", NSEC_PER_WEEK
},
1014 { "week", NSEC_PER_WEEK
},
1015 { "w", NSEC_PER_WEEK
},
1016 { "years", NSEC_PER_YEAR
},
1017 { "year", NSEC_PER_YEAR
},
1018 { "y", NSEC_PER_YEAR
},
1019 { "usec", NSEC_PER_USEC
},
1020 { "us", NSEC_PER_USEC
},
1023 { "", 1ULL }, /* default is nsec */
1028 bool something
= false;
1035 p
+= strspn(p
, WHITESPACE
);
1036 s
= startswith(p
, "infinity");
1038 s
+= strspn(s
, WHITESPACE
);
1042 *nsec
= NSEC_INFINITY
;
1051 p
+= strspn(p
, WHITESPACE
);
1061 l
= strtoll(p
, &e
, 10);
1073 z
= strtoll(b
, &e
, 10);
1088 e
+= strspn(e
, WHITESPACE
);
1090 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
1091 if (startswith(e
, table
[i
].suffix
)) {
1092 nsec_t k
= (nsec_t
) z
* table
[i
].nsec
;
1097 r
+= (nsec_t
) l
* table
[i
].nsec
+ k
;
1098 p
= e
+ strlen(table
[i
].suffix
);
1104 if (i
>= ELEMENTSOF(table
))
1114 bool ntp_synced(void) {
1115 struct timex txc
= {};
1117 if (adjtimex(&txc
) < 0)
1120 if (txc
.status
& STA_UNSYNC
)
1126 int get_timezones(char ***ret
) {
1127 _cleanup_fclose_
FILE *f
= NULL
;
1128 _cleanup_strv_free_
char **zones
= NULL
;
1129 size_t n_zones
= 0, n_allocated
= 0;
1133 zones
= strv_new("UTC", NULL
);
1140 f
= fopen("/usr/share/zoneinfo/zone.tab", "re");
1144 FOREACH_LINE(l
, f
, return -errno
) {
1150 if (isempty(p
) || *p
== '#')
1153 /* Skip over country code */
1154 p
+= strcspn(p
, WHITESPACE
);
1155 p
+= strspn(p
, WHITESPACE
);
1157 /* Skip over coordinates */
1158 p
+= strcspn(p
, WHITESPACE
);
1159 p
+= strspn(p
, WHITESPACE
);
1161 /* Found timezone name */
1162 k
= strcspn(p
, WHITESPACE
);
1170 if (!GREEDY_REALLOC(zones
, n_allocated
, n_zones
+ 2)) {
1175 zones
[n_zones
++] = w
;
1176 zones
[n_zones
] = NULL
;
1181 } else if (errno
!= ENOENT
)
1190 bool timezone_is_valid(const char *name
) {
1201 for (p
= name
; *p
; p
++) {
1202 if (!(*p
>= '0' && *p
<= '9') &&
1203 !(*p
>= 'a' && *p
<= 'z') &&
1204 !(*p
>= 'A' && *p
<= 'Z') &&
1205 !(*p
== '-' || *p
== '_' || *p
== '+' || *p
== '/'))
1221 t
= strjoina("/usr/share/zoneinfo/", name
);
1222 if (stat(t
, &st
) < 0)
1225 if (!S_ISREG(st
.st_mode
))
1231 bool clock_boottime_supported(void) {
1232 static int supported
= -1;
1234 /* Note that this checks whether CLOCK_BOOTTIME is available in general as well as available for timerfds()! */
1236 if (supported
< 0) {
1239 fd
= timerfd_create(CLOCK_BOOTTIME
, TFD_NONBLOCK
|TFD_CLOEXEC
);
1251 clockid_t
clock_boottime_or_monotonic(void) {
1252 if (clock_boottime_supported())
1253 return CLOCK_BOOTTIME
;
1255 return CLOCK_MONOTONIC
;
1258 bool clock_supported(clockid_t clock
) {
1263 case CLOCK_MONOTONIC
:
1264 case CLOCK_REALTIME
:
1267 case CLOCK_BOOTTIME
:
1268 return clock_boottime_supported();
1270 case CLOCK_BOOTTIME_ALARM
:
1271 if (!clock_boottime_supported())
1274 /* fall through, after checking the cached value for CLOCK_BOOTTIME. */
1277 /* For everything else, check properly */
1278 return clock_gettime(clock
, &ts
) >= 0;
1282 int get_timezone(char **tz
) {
1283 _cleanup_free_
char *t
= NULL
;
1288 r
= readlink_malloc("/etc/localtime", &t
);
1290 return r
; /* returns EINVAL if not a symlink */
1292 e
= path_startswith(t
, "/usr/share/zoneinfo/");
1294 e
= path_startswith(t
, "../usr/share/zoneinfo/");
1298 if (!timezone_is_valid(e
))
1309 time_t mktime_or_timegm(struct tm
*tm
, bool utc
) {
1310 return utc
? timegm(tm
) : mktime(tm
);
1313 struct tm
*localtime_or_gmtime_r(const time_t *t
, struct tm
*tm
, bool utc
) {
1314 return utc
? gmtime_r(t
, tm
) : localtime_r(t
, tm
);
1317 unsigned long usec_to_jiffies(usec_t u
) {
1318 static thread_local
unsigned long hz
= 0;
1322 r
= sysconf(_SC_CLK_TCK
);
1325 hz
= (unsigned long) r
;
1328 return DIV_ROUND_UP(u
, USEC_PER_SEC
/ hz
);