1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010-2012 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/types.h>
32 #include <sys/ioctl.h>
35 #include <sys/prctl.h>
37 #include <linux/rtc.h>
46 int hwclock_get_time(struct tm *tm) {
52 fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
56 /* This leaves the timezone fields of struct tm
58 if (ioctl(fd, RTC_RD_TIME, tm) < 0)
61 /* We don't know daylight saving, so we reset this in order not
62 * to confused mktime(). */
65 close_nointr_nofail(fd);
70 int hwclock_set_time(const struct tm *tm) {
76 fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
80 if (ioctl(fd, RTC_SET_TIME, tm) < 0)
83 close_nointr_nofail(fd);
88 int hwclock_is_localtime(void) {
89 _cleanup_fclose_ FILE *f;
92 * The third line of adjtime is "UTC" or "LOCAL" or nothing.
98 f = fopen("/etc/adjtime", "re");
103 b = fgets(line, sizeof(line), f) &&
104 fgets(line, sizeof(line), f) &&
105 fgets(line, sizeof(line), f);
110 return streq(line, "LOCAL");
112 } else if (errno != ENOENT)
118 int hwclock_set_timezone(int *min) {
119 const struct timeval *tv_null = NULL;
125 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
126 assert_se(tm = localtime(&ts.tv_sec));
127 minutesdelta = tm->tm_gmtoff / 60;
129 tz.tz_minuteswest = -minutesdelta;
130 tz.tz_dsttime = 0; /* DST_NONE*/
133 * If the hardware clock does not run in UTC, but in local time:
134 * The very first time we set the kernel's timezone, it will warp
135 * the clock so that it runs in UTC instead of local time.
137 if (settimeofday(tv_null, &tz) < 0)
144 int hwclock_reset_timezone(void) {
145 const struct timeval *tv_null = NULL;
148 tz.tz_minuteswest = 0;
149 tz.tz_dsttime = 0; /* DST_NONE*/
152 * The very first time we set the kernel's timezone, it will warp
153 * the clock. Do a dummy call here, so the time warping is sealed
154 * and we set only the time zone with next call.
156 if (settimeofday(tv_null, &tz) < 0)