]>
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 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(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(now(CLOCK_MONOTONIC
), delta
);
128 ts
->boottime
= clock_boottime_supported() ? usec_sub(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(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(ts
->realtime
, delta
);
160 ts
->monotonic
= usec_sub(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
== (time_t) -1 && ts
->tv_nsec
== (long) -1)
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
== (time_t) -1 && ts
->tv_nsec
== (long) -1)
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 ts
->tv_sec
= (time_t) -1;
216 ts
->tv_nsec
= (long) -1;
220 ts
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
221 ts
->tv_nsec
= (long int) ((u
% USEC_PER_SEC
) * NSEC_PER_USEC
);
226 usec_t
timeval_load(const struct timeval
*tv
) {
229 if (tv
->tv_sec
== (time_t) -1 &&
230 tv
->tv_usec
== (suseconds_t
) -1)
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 tv
->tv_sec
= (time_t) -1;
246 tv
->tv_usec
= (suseconds_t
) -1;
248 tv
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
249 tv
->tv_usec
= (suseconds_t
) (u
% USEC_PER_SEC
);
255 static char *format_timestamp_internal(
262 /* The weekdays in non-localized (English) form. We use this instead of the localized form, so that our
263 * generated timestamps may be parsed with parse_timestamp(), and always read the same. */
264 static const char * const weekdays
[] = {
282 1 + 10 + /* space and date */
283 1 + 8 + /* space and time */
284 (us
? 1 + 6 : 0) + /* "." and microsecond part */
285 1 + 1 + /* space and shortest possible zone */
287 return NULL
; /* Not enough space even for the shortest form. */
288 if (t
<= 0 || t
== USEC_INFINITY
)
289 return NULL
; /* Timestamp is unset */
291 sec
= (time_t) (t
/ USEC_PER_SEC
); /* Round down */
292 if ((usec_t
) sec
!= (t
/ USEC_PER_SEC
))
293 return NULL
; /* overflow? */
295 if (!localtime_or_gmtime_r(&sec
, &tm
, utc
))
298 /* Start with the week day */
299 assert((size_t) tm
.tm_wday
< ELEMENTSOF(weekdays
));
300 memcpy(buf
, weekdays
[tm
.tm_wday
], 4);
302 /* Add the main components */
303 if (strftime(buf
+ 3, l
- 3, " %Y-%m-%d %H:%M:%S", &tm
) <= 0)
304 return NULL
; /* Doesn't fit */
306 /* Append the microseconds part, if that's requested */
310 return NULL
; /* Microseconds part doesn't fit. */
312 sprintf(buf
+ n
, ".%06"PRI_USEC
, t
% USEC_PER_SEC
);
315 /* Append the timezone */
318 /* If this is UTC then let's explicitly use the "UTC" string here, because gmtime_r() normally uses the
319 * obsolete "GMT" instead. */
321 return NULL
; /* "UTC" doesn't fit. */
323 strcpy(buf
+ n
, " UTC");
325 } else if (!isempty(tm
.tm_zone
)) {
328 /* An explicit timezone is specified, let's use it, if it fits */
329 tn
= strlen(tm
.tm_zone
);
330 if (n
+ 1 + tn
+ 1 > l
) {
331 /* The full time zone does not fit in. Yuck. */
333 if (n
+ 1 + _POSIX_TZNAME_MAX
+ 1 > l
)
334 return NULL
; /* Not even enough space for the POSIX minimum (of 6)? In that case, complain that it doesn't fit */
336 /* So the time zone doesn't fit in fully, but the caller passed enough space for the POSIX
337 * minimum time zone length. In this case suppress the timezone entirely, in order not to dump
338 * an overly long, hard to read string on the user. This should be safe, because the user will
339 * assume the local timezone anyway if none is shown. And so does parse_timestamp(). */
342 strcpy(buf
+ n
, tm
.tm_zone
);
349 char *format_timestamp(char *buf
, size_t l
, usec_t t
) {
350 return format_timestamp_internal(buf
, l
, t
, false, false);
353 char *format_timestamp_utc(char *buf
, size_t l
, usec_t t
) {
354 return format_timestamp_internal(buf
, l
, t
, true, false);
357 char *format_timestamp_us(char *buf
, size_t l
, usec_t t
) {
358 return format_timestamp_internal(buf
, l
, t
, false, true);
361 char *format_timestamp_us_utc(char *buf
, size_t l
, usec_t t
) {
362 return format_timestamp_internal(buf
, l
, t
, true, true);
365 char *format_timestamp_relative(char *buf
, size_t l
, usec_t t
) {
369 if (t
<= 0 || t
== USEC_INFINITY
)
372 n
= now(CLOCK_REALTIME
);
381 if (d
>= USEC_PER_YEAR
)
382 snprintf(buf
, l
, USEC_FMT
" years " USEC_FMT
" months %s",
384 (d
% USEC_PER_YEAR
) / USEC_PER_MONTH
, s
);
385 else if (d
>= USEC_PER_MONTH
)
386 snprintf(buf
, l
, USEC_FMT
" months " USEC_FMT
" days %s",
388 (d
% USEC_PER_MONTH
) / USEC_PER_DAY
, s
);
389 else if (d
>= USEC_PER_WEEK
)
390 snprintf(buf
, l
, USEC_FMT
" weeks " USEC_FMT
" days %s",
392 (d
% USEC_PER_WEEK
) / USEC_PER_DAY
, s
);
393 else if (d
>= 2*USEC_PER_DAY
)
394 snprintf(buf
, l
, USEC_FMT
" days %s", d
/ USEC_PER_DAY
, s
);
395 else if (d
>= 25*USEC_PER_HOUR
)
396 snprintf(buf
, l
, "1 day " USEC_FMT
"h %s",
397 (d
- USEC_PER_DAY
) / USEC_PER_HOUR
, s
);
398 else if (d
>= 6*USEC_PER_HOUR
)
399 snprintf(buf
, l
, USEC_FMT
"h %s",
400 d
/ USEC_PER_HOUR
, s
);
401 else if (d
>= USEC_PER_HOUR
)
402 snprintf(buf
, l
, USEC_FMT
"h " USEC_FMT
"min %s",
404 (d
% USEC_PER_HOUR
) / USEC_PER_MINUTE
, s
);
405 else if (d
>= 5*USEC_PER_MINUTE
)
406 snprintf(buf
, l
, USEC_FMT
"min %s",
407 d
/ USEC_PER_MINUTE
, s
);
408 else if (d
>= USEC_PER_MINUTE
)
409 snprintf(buf
, l
, USEC_FMT
"min " USEC_FMT
"s %s",
411 (d
% USEC_PER_MINUTE
) / USEC_PER_SEC
, s
);
412 else if (d
>= USEC_PER_SEC
)
413 snprintf(buf
, l
, USEC_FMT
"s %s",
414 d
/ USEC_PER_SEC
, s
);
415 else if (d
>= USEC_PER_MSEC
)
416 snprintf(buf
, l
, USEC_FMT
"ms %s",
417 d
/ USEC_PER_MSEC
, s
);
419 snprintf(buf
, l
, USEC_FMT
"us %s",
422 snprintf(buf
, l
, "now");
428 char *format_timespan(char *buf
, size_t l
, usec_t t
, usec_t accuracy
) {
429 static const struct {
433 { "y", USEC_PER_YEAR
},
434 { "month", USEC_PER_MONTH
},
435 { "w", USEC_PER_WEEK
},
436 { "d", USEC_PER_DAY
},
437 { "h", USEC_PER_HOUR
},
438 { "min", USEC_PER_MINUTE
},
439 { "s", USEC_PER_SEC
},
440 { "ms", USEC_PER_MSEC
},
446 bool something
= false;
451 if (t
== USEC_INFINITY
) {
452 strncpy(p
, "infinity", l
-1);
458 strncpy(p
, "0", l
-1);
463 /* The result of this function can be parsed with parse_sec */
465 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
474 if (t
< accuracy
&& something
)
477 if (t
< table
[i
].usec
)
483 a
= t
/ table
[i
].usec
;
484 b
= t
% table
[i
].usec
;
486 /* Let's see if we should shows this in dot notation */
487 if (t
< USEC_PER_MINUTE
&& b
> 0) {
492 for (cc
= table
[i
].usec
; cc
> 1; cc
/= 10)
495 for (cc
= accuracy
; cc
> 1; cc
/= 10) {
502 "%s"USEC_FMT
".%0*"PRI_USEC
"%s",
514 /* No? Then let's show it normally */
525 n
= MIN((size_t) k
, l
);
538 void dual_timestamp_serialize(FILE *f
, const char *name
, dual_timestamp
*t
) {
544 if (!dual_timestamp_is_set(t
))
547 fprintf(f
, "%s="USEC_FMT
" "USEC_FMT
"\n",
553 int dual_timestamp_deserialize(const char *value
, dual_timestamp
*t
) {
554 unsigned long long a
, b
;
559 if (sscanf(value
, "%llu %llu", &a
, &b
) != 2) {
560 log_debug("Failed to parse dual timestamp value \"%s\": %m", value
);
570 int timestamp_deserialize(const char *value
, usec_t
*timestamp
) {
575 r
= safe_atou64(value
, timestamp
);
577 return log_debug_errno(r
, "Failed to parse timestamp value \"%s\": %m", value
);
582 int parse_timestamp(const char *t
, usec_t
*usec
) {
583 static const struct {
603 const char *k
, *utc
, *tzn
= NULL
;
606 usec_t x_usec
, plus
= 0, minus
= 0, ret
;
607 int r
, weekday
= -1, dst
= -1;
613 * 2012-09-22 16:34:22
614 * 2012-09-22 16:34 (seconds will be set to 0)
615 * 2012-09-22 (time will be set to 00:00:00)
616 * 16:34:22 (date will be set to today)
617 * 16:34 (date will be set to today, seconds to 0)
619 * yesterday (time is set to 00:00:00)
620 * today (time is set to 00:00:00)
621 * tomorrow (time is set to 00:00:00)
624 * @2147483647 (seconds since epoch)
632 return parse_sec(t
+ 1, usec
);
634 ret
= now(CLOCK_REALTIME
);
639 else if (t
[0] == '+') {
640 r
= parse_sec(t
+1, &plus
);
646 } else if (t
[0] == '-') {
647 r
= parse_sec(t
+1, &minus
);
653 } else if ((k
= endswith(t
, " ago"))) {
654 t
= strndupa(t
, k
- t
);
656 r
= parse_sec(t
, &minus
);
662 } else if ((k
= endswith(t
, " left"))) {
663 t
= strndupa(t
, k
- t
);
665 r
= parse_sec(t
, &plus
);
672 /* See if the timestamp is suffixed with UTC */
673 utc
= endswith_no_case(t
, " UTC");
675 t
= strndupa(t
, utc
- t
);
677 const char *e
= NULL
;
682 /* See if the timestamp is suffixed by either the DST or non-DST local timezone. Note that we only
683 * support the local timezones here, nothing else. Not because we wouldn't want to, but simply because
684 * there are no nice APIs available to cover this. By accepting the local time zone strings, we make
685 * sure that all timestamps written by format_timestamp() can be parsed correctly, even though we don't
686 * support arbitrary timezone specifications. */
688 for (j
= 0; j
<= 1; j
++) {
690 if (isempty(tzname
[j
]))
693 e
= endswith_no_case(t
, tzname
[j
]);
704 if (IN_SET(j
, 0, 1)) {
705 /* Found one of the two timezones specified. */
706 t
= strndupa(t
, e
- t
- 1);
712 x
= (time_t) (ret
/ USEC_PER_SEC
);
715 if (!localtime_or_gmtime_r(&x
, &tm
, utc
))
722 if (streq(t
, "today")) {
723 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
726 } else if (streq(t
, "yesterday")) {
728 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
731 } else if (streq(t
, "tomorrow")) {
733 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
737 for (i
= 0; i
< ELEMENTSOF(day_nr
); i
++) {
740 if (!startswith_no_case(t
, day_nr
[i
].name
))
743 skip
= strlen(day_nr
[i
].name
);
747 weekday
= day_nr
[i
].nr
;
753 k
= strptime(t
, "%y-%m-%d %H:%M:%S", &tm
);
762 k
= strptime(t
, "%Y-%m-%d %H:%M:%S", &tm
);
771 k
= strptime(t
, "%y-%m-%d %H:%M", &tm
);
778 k
= strptime(t
, "%Y-%m-%d %H:%M", &tm
);
785 k
= strptime(t
, "%y-%m-%d", &tm
);
787 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
792 k
= strptime(t
, "%Y-%m-%d", &tm
);
794 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
799 k
= strptime(t
, "%H:%M:%S", &tm
);
808 k
= strptime(t
, "%H:%M", &tm
);
821 r
= parse_fractional_part_u(&k
, 6, &add
);
832 x
= mktime_or_timegm(&tm
, utc
);
833 if (x
== (time_t) -1)
836 if (weekday
>= 0 && tm
.tm_wday
!= weekday
)
839 ret
= (usec_t
) x
* USEC_PER_SEC
+ x_usec
;
853 static char* extract_multiplier(char *p
, usec_t
*multiplier
) {
854 static const struct {
858 { "seconds", USEC_PER_SEC
},
859 { "second", USEC_PER_SEC
},
860 { "sec", USEC_PER_SEC
},
861 { "s", USEC_PER_SEC
},
862 { "minutes", USEC_PER_MINUTE
},
863 { "minute", USEC_PER_MINUTE
},
864 { "min", USEC_PER_MINUTE
},
865 { "months", USEC_PER_MONTH
},
866 { "month", USEC_PER_MONTH
},
867 { "M", USEC_PER_MONTH
},
868 { "msec", USEC_PER_MSEC
},
869 { "ms", USEC_PER_MSEC
},
870 { "m", USEC_PER_MINUTE
},
871 { "hours", USEC_PER_HOUR
},
872 { "hour", USEC_PER_HOUR
},
873 { "hr", USEC_PER_HOUR
},
874 { "h", USEC_PER_HOUR
},
875 { "days", USEC_PER_DAY
},
876 { "day", USEC_PER_DAY
},
877 { "d", USEC_PER_DAY
},
878 { "weeks", USEC_PER_WEEK
},
879 { "week", USEC_PER_WEEK
},
880 { "w", USEC_PER_WEEK
},
881 { "years", USEC_PER_YEAR
},
882 { "year", USEC_PER_YEAR
},
883 { "y", USEC_PER_YEAR
},
890 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
893 e
= startswith(p
, table
[i
].suffix
);
895 *multiplier
= table
[i
].usec
;
903 int parse_time(const char *t
, usec_t
*usec
, usec_t default_unit
) {
906 bool something
= false;
910 assert(default_unit
> 0);
914 p
+= strspn(p
, WHITESPACE
);
915 s
= startswith(p
, "infinity");
917 s
+= strspn(s
, WHITESPACE
);
921 *usec
= USEC_INFINITY
;
929 usec_t multiplier
= default_unit
, k
;
931 p
+= strspn(p
, WHITESPACE
);
941 l
= strtoll(p
, &e
, 10);
951 z
= strtoll(b
, &e
, 10);
966 e
+= strspn(e
, WHITESPACE
);
967 p
= extract_multiplier(e
, &multiplier
);
971 k
= (usec_t
) z
* multiplier
;
976 r
+= (usec_t
) l
* multiplier
+ k
;
984 int parse_sec(const char *t
, usec_t
*usec
) {
985 return parse_time(t
, usec
, USEC_PER_SEC
);
988 int parse_nsec(const char *t
, nsec_t
*nsec
) {
989 static const struct {
993 { "seconds", NSEC_PER_SEC
},
994 { "second", NSEC_PER_SEC
},
995 { "sec", NSEC_PER_SEC
},
996 { "s", NSEC_PER_SEC
},
997 { "minutes", NSEC_PER_MINUTE
},
998 { "minute", NSEC_PER_MINUTE
},
999 { "min", NSEC_PER_MINUTE
},
1000 { "months", NSEC_PER_MONTH
},
1001 { "month", NSEC_PER_MONTH
},
1002 { "msec", NSEC_PER_MSEC
},
1003 { "ms", NSEC_PER_MSEC
},
1004 { "m", NSEC_PER_MINUTE
},
1005 { "hours", NSEC_PER_HOUR
},
1006 { "hour", NSEC_PER_HOUR
},
1007 { "hr", NSEC_PER_HOUR
},
1008 { "h", NSEC_PER_HOUR
},
1009 { "days", NSEC_PER_DAY
},
1010 { "day", NSEC_PER_DAY
},
1011 { "d", NSEC_PER_DAY
},
1012 { "weeks", NSEC_PER_WEEK
},
1013 { "week", NSEC_PER_WEEK
},
1014 { "w", NSEC_PER_WEEK
},
1015 { "years", NSEC_PER_YEAR
},
1016 { "year", NSEC_PER_YEAR
},
1017 { "y", NSEC_PER_YEAR
},
1018 { "usec", NSEC_PER_USEC
},
1019 { "us", NSEC_PER_USEC
},
1020 { "µs", 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
);
1328 return DIV_ROUND_UP(u
, USEC_PER_SEC
/ hz
);