]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/clock-util.c
2 This file is part of systemd.
4 Copyright 2010-2012 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/>.
25 #include <linux/rtc.h>
27 #include <sys/ioctl.h>
30 #include "clock-util.h"
33 #include "string-util.h"
36 int clock_get_hwclock(struct tm
*tm
) {
37 _cleanup_close_
int fd
= -1;
41 fd
= open("/dev/rtc", O_RDONLY
|O_CLOEXEC
);
45 /* This leaves the timezone fields of struct tm
47 if (ioctl(fd
, RTC_RD_TIME
, tm
) < 0)
50 /* We don't know daylight saving, so we reset this in order not
51 * to confuse mktime(). */
57 int clock_set_hwclock(const struct tm
*tm
) {
58 _cleanup_close_
int fd
= -1;
62 fd
= open("/dev/rtc", O_RDONLY
|O_CLOEXEC
);
66 if (ioctl(fd
, RTC_SET_TIME
, tm
) < 0)
72 int clock_is_localtime(const char* adjtime_path
) {
73 _cleanup_fclose_
FILE *f
;
75 if (adjtime_path
== NULL
)
76 adjtime_path
= "/etc/adjtime";
79 * The third line of adjtime is "UTC" or "LOCAL" or nothing.
85 f
= fopen(adjtime_path
, "re");
90 b
= fgets(line
, sizeof(line
), f
) &&
91 fgets(line
, sizeof(line
), f
) &&
92 fgets(line
, sizeof(line
), f
);
94 /* less than three lines -> default to UTC */
98 return streq(line
, "LOCAL");
100 } else if (errno
!= ENOENT
)
103 /* adjtime not present -> default to UTC */
107 int clock_set_timezone(int *min
) {
108 const struct timeval
*tv_null
= NULL
;
114 assert_se(clock_gettime(CLOCK_REALTIME
, &ts
) == 0);
115 assert_se(tm
= localtime(&ts
.tv_sec
));
116 minutesdelta
= tm
->tm_gmtoff
/ 60;
118 tz
.tz_minuteswest
= -minutesdelta
;
119 tz
.tz_dsttime
= 0; /* DST_NONE */
122 * If the RTC does not run in UTC but in local time, the very first
123 * call to settimeofday() will set the kernel's timezone and will warp the
124 * system clock, so that it runs in UTC instead of the local time we
125 * have read from the RTC.
127 if (settimeofday(tv_null
, &tz
) < 0)
128 return negative_errno();
135 int clock_reset_timewarp(void) {
136 const struct timeval
*tv_null
= NULL
;
139 tz
.tz_minuteswest
= 0;
140 tz
.tz_dsttime
= 0; /* DST_NONE */
143 * The very first call to settimeofday() does time warp magic. Do a
144 * dummy call here, so the time warping is sealed and all later calls
145 * behave as expected.
147 if (settimeofday(tv_null
, &tz
) < 0)
153 #define TIME_EPOCH_USEC ((usec_t) TIME_EPOCH * USEC_PER_SEC)
155 int clock_apply_epoch(void) {
158 if (now(CLOCK_REALTIME
) >= TIME_EPOCH_USEC
)
161 if (clock_settime(CLOCK_REALTIME
, timespec_store(&ts
, TIME_EPOCH_USEC
)) < 0)