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>
43 #include "clock-util.h"
46 int clock_get_hwclock(struct tm *tm) {
47 _cleanup_close_ int fd = -1;
51 fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
55 /* This leaves the timezone fields of struct tm
57 if (ioctl(fd, RTC_RD_TIME, tm) < 0)
60 /* We don't know daylight saving, so we reset this in order not
61 * to confuse mktime(). */
67 int clock_set_hwclock(const struct tm *tm) {
68 _cleanup_close_ int fd = -1;
72 fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
76 if (ioctl(fd, RTC_SET_TIME, tm) < 0)
82 int clock_is_localtime(void) {
83 _cleanup_fclose_ FILE *f;
86 * The third line of adjtime is "UTC" or "LOCAL" or nothing.
92 f = fopen("/etc/adjtime", "re");
97 b = fgets(line, sizeof(line), f) &&
98 fgets(line, sizeof(line), f) &&
99 fgets(line, sizeof(line), f);
104 return streq(line, "LOCAL");
106 } else if (errno != ENOENT)
112 int clock_set_timezone(int *min) {
113 const struct timeval *tv_null = NULL;
119 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
120 assert_se(tm = localtime(&ts.tv_sec));
121 minutesdelta = tm->tm_gmtoff / 60;
123 tz.tz_minuteswest = -minutesdelta;
124 tz.tz_dsttime = 0; /* DST_NONE */
127 * If the RTC does not run in UTC but in local time, the very first
128 * call to settimeofday() will set the kernel's timezone and will warp the
129 * system clock, so that it runs in UTC instead of the local time we
130 * have read from the RTC.
132 if (settimeofday(tv_null, &tz) < 0)
139 int clock_reset_timewarp(void) {
140 const struct timeval *tv_null = NULL;
143 tz.tz_minuteswest = 0;
144 tz.tz_dsttime = 0; /* DST_NONE */
147 * The very first call to settimeofday() does time warp magic. Do a
148 * dummy call here, so the time warping is sealed and all later calls
149 * behave as expected.
151 if (settimeofday(tv_null, &tz) < 0)