]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/clock-util.c
edd2669d0373cf6717ed9ecc7c43731848ca9be0
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
7 #include "clock-util.h"
10 #include "string-util.h"
11 #include "time-util.h"
13 int clock_is_localtime(const char *adjtime_path
) {
17 adjtime_path
= "/etc/adjtime";
20 * The third line of adjtime is "UTC" or "LOCAL" or nothing.
26 _cleanup_fclose_
FILE *f
= fopen(adjtime_path
, "re");
31 /* adjtime_path not present → default to UTC */
35 _cleanup_free_
char *line
= NULL
;
36 for (unsigned i
= 0; i
< 2; i
++) { /* skip the first two lines */
37 r
= read_line(f
, LONG_LINE_MAX
, NULL
);
41 return false; /* less than three lines → default to UTC */
44 r
= read_line(f
, LONG_LINE_MAX
, &line
);
48 return false; /* less than three lines → default to UTC */
50 return streq(line
, "LOCAL");
53 int clock_set_timezone(int *ret_minutesdelta
) {
57 r
= localtime_or_gmtime_usec(now(CLOCK_REALTIME
), /* utc= */ false, &tm
);
61 int minutesdelta
= tm
.tm_gmtoff
/ 60;
63 struct timezone tz
= {
64 .tz_minuteswest
= -minutesdelta
,
65 .tz_dsttime
= 0, /* DST_NONE */
68 /* If the RTC does not run in UTC but in local time, the very first call to settimeofday() will set
69 * the kernel's timezone and will warp the system clock, so that it runs in UTC instead of the local
70 * time we have read from the RTC. */
71 if (settimeofday(NULL
, &tz
) < 0)
75 *ret_minutesdelta
= minutesdelta
;