-
Notifications
You must be signed in to change notification settings - Fork 7
/
rtc.c
163 lines (136 loc) · 4.09 KB
/
rtc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* virt-p2v
* Copyright (C) 2017 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Try to calculate Real Time Clock (RTC) offset from UTC in seconds.
* For example if the RTC is 1 hour ahead of UTC, this will return
* C<3600>. This is stored in C<config-E<gt>rtc_offset>.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <errno.h>
#include <libintl.h>
#include <time.h>
#include <sys/ioctl.h>
#include <math.h>
#ifdef HAVE_LINUX_RTC_H
#include <linux/rtc.h>
#endif
#include "ignore-value.h"
#include "p2v.h"
#ifndef HAVE_LINUX_RTC_H
void
get_rtc_config (struct rtc_config *rtc)
{
fprintf (stderr, "%s: RTC: compiled without support for /dev/rtc\n",
g_get_prgname ());
rtc->offset = 0;
rtc->basis = BASIS_UTC;
}
#else /* HAVE_LINUX_RTC_H */
/**
* Return RTC offset from UTC in seconds, positive numbers meaning
* that the RTC is running ahead of UTC.
*
* In the error case, C<rtcE<gt>offset> is updated with 0 and
* C<rtcE<gt>basis> is set to C<BASIS_UNKNOWN>.
*/
void
get_rtc_config (struct rtc_config *rtc)
{
int fd;
struct rtc_time rtm;
struct tm tm;
time_t rtc_time;
time_t system_time;
double rf;
rtc->basis = BASIS_UNKNOWN;
rtc->offset = 0;
fd = open ("/dev/rtc", O_RDONLY);
if (fd == -1) {
perror ("/dev/rtc");
return;
}
if (ioctl (fd, RTC_RD_TIME, &rtm) == -1) {
perror ("ioctl: RTC_RD_TIME");
close (fd);
return;
}
close (fd);
#ifdef DEBUG_STDERR
fprintf (stderr, "%s: RTC: %04d-%02d-%02d %02d:%02d:%02d\n",
g_get_prgname (),
rtm.tm_year + 1900, rtm.tm_mon + 1, rtm.tm_mday,
rtm.tm_hour, rtm.tm_min, rtm.tm_sec);
#endif
/* Convert this to seconds since the epoch. */
tm.tm_sec = rtm.tm_sec;
tm.tm_min = rtm.tm_min;
tm.tm_hour = rtm.tm_hour;
tm.tm_mday = rtm.tm_mday;
tm.tm_mon = rtm.tm_mon;
tm.tm_year = rtm.tm_year;
tm.tm_isdst = 0; /* Ignore DST when calculating. */
rtc_time = timegm (&tm);
if (rtc_time == -1)
return; /* Not representable as a Unix time. */
/* Get system time in UTC. */
system_time = time (NULL);
/* Calculate the difference, rounded to the nearest 15 minutes. */
rf = rtc_time - system_time;
#ifdef DEBUG_STDERR
fprintf (stderr, "%s: RTC: %ld system time: %ld difference: %g\n",
g_get_prgname (),
(long) rtc_time, (long) system_time, rf);
#endif
rf /= 15*60;
rf = round (rf);
rf *= 15*60;
/* If it's obviously out of range then print an error and return. */
if (rf < -12*60*60 || rf > 14*60*60) {
fprintf (stderr,
"%s: RTC: offset of RTC from UTC is out of range (%g).\n",
g_get_prgname (), rf);
return;
}
rtc->offset = (int) rf;
#ifdef DEBUG_STDERR
fprintf (stderr, "%s: RTC: offset of RTC from UTC = %d secs\n",
g_get_prgname (), rtc->offset);
#endif
/* Is the hardware clock set to localtime?
*
* Unfortunately it's not possible to distinguish between UTC and
* localtime in timezones that lie along the Greenwich Meridian
* (obviously including the UK), when daylight savings time is not
* in effect. In that case, prefer UTC.
*/
localtime_r (&system_time, &tm);
if (tm.tm_gmtoff != 0 && tm.tm_gmtoff != rtc->offset)
rtc->basis = BASIS_UTC;
else {
rtc->basis = BASIS_LOCALTIME;
rtc->offset = 0;
#ifdef DEBUG_STDERR
fprintf (stderr, "%s: RTC time is localtime\n", g_get_prgname ());
#endif
}
return;
}
#endif /* HAVE_LINUX_RTC_H */