]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/clock-util.c
e2499099b607a76f6dcab736c286fae7210ec57f
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010-2012 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <linux/rtc.h>
28 #include <sys/ioctl.h>
31 #include "clock-util.h"
34 #include "string-util.h"
37 int clock_get_hwclock(struct tm
*tm
) {
38 _cleanup_close_
int fd
= -1;
42 fd
= open("/dev/rtc", O_RDONLY
|O_CLOEXEC
);
46 /* This leaves the timezone fields of struct tm
48 if (ioctl(fd
, RTC_RD_TIME
, tm
) < 0)
51 /* We don't know daylight saving, so we reset this in order not
52 * to confuse mktime(). */
58 int clock_set_hwclock(const struct tm
*tm
) {
59 _cleanup_close_
int fd
= -1;
63 fd
= open("/dev/rtc", O_RDONLY
|O_CLOEXEC
);
67 if (ioctl(fd
, RTC_SET_TIME
, tm
) < 0)
73 int clock_is_localtime(const char* adjtime_path
) {
74 _cleanup_fclose_
FILE *f
;
77 adjtime_path
= "/etc/adjtime";
80 * The third line of adjtime is "UTC" or "LOCAL" or nothing.
86 f
= fopen(adjtime_path
, "re");
91 b
= fgets(line
, sizeof(line
), f
) &&
92 fgets(line
, sizeof(line
), f
) &&
93 fgets(line
, sizeof(line
), f
);
95 /* less than three lines -> default to UTC */
99 return streq(line
, "LOCAL");
101 } else if (errno
!= ENOENT
)
104 /* adjtime not present -> default to UTC */
108 int clock_set_timezone(int *min
) {
109 const struct timeval
*tv_null
= NULL
;
115 assert_se(clock_gettime(CLOCK_REALTIME
, &ts
) == 0);
116 assert_se(tm
= localtime(&ts
.tv_sec
));
117 minutesdelta
= tm
->tm_gmtoff
/ 60;
119 tz
.tz_minuteswest
= -minutesdelta
;
120 tz
.tz_dsttime
= 0; /* DST_NONE */
123 * If the RTC does not run in UTC but in local time, the very first
124 * call to settimeofday() will set the kernel's timezone and will warp the
125 * system clock, so that it runs in UTC instead of the local time we
126 * have read from the RTC.
128 if (settimeofday(tv_null
, &tz
) < 0)
129 return negative_errno();
136 int clock_reset_timewarp(void) {
137 const struct timeval
*tv_null
= NULL
;
140 tz
.tz_minuteswest
= 0;
141 tz
.tz_dsttime
= 0; /* DST_NONE */
144 * The very first call to settimeofday() does time warp magic. Do a
145 * dummy call here, so the time warping is sealed and all later calls
146 * behave as expected.
148 if (settimeofday(tv_null
, &tz
) < 0)
154 #define TIME_EPOCH_USEC ((usec_t) TIME_EPOCH * USEC_PER_SEC)
156 int clock_apply_epoch(void) {
159 if (now(CLOCK_REALTIME
) >= TIME_EPOCH_USEC
)
162 if (clock_settime(CLOCK_REALTIME
, timespec_store(&ts
, TIME_EPOCH_USEC
)) < 0)