clock-util.c revision 021dd87bc055a5bfb2dcef83fc868fe24648b959
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering This file is part of systemd.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Copyright 2010-2012 Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering systemd is free software; you can redistribute it and/or modify it
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering under the terms of the GNU Lesser General Public License as published by
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering (at your option) any later version.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering systemd is distributed in the hope that it will be useful, but
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Lesser General Public License for more details.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering You should have received a copy of the GNU Lesser General Public License
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* This leaves the timezone fields of struct tm
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * uninitialized! */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* We don't know daylight saving, so we reset this in order not
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * to confuse mktime(). */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint clock_set_hwclock(const struct tm *tm) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * The third line of adjtime is "UTC" or "LOCAL" or nothing.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * If the RTC does not run in UTC but in local time, the very first
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * call to settimeofday() will set the kernel's timezone and will warp the
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * system clock, so that it runs in UTC instead of the local time we
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * have read from the RTC.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * The very first call to settimeofday() does time warp magic. Do a
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * dummy call here, so the time warping is sealed and all later calls
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * behave as expected.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#define TIME_EPOCH_USEC ((usec_t) TIME_EPOCH * USEC_PER_SEC)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (now(CLOCK_REALTIME) >= TIME_EPOCH_USEC)