aboutsummaryrefslogtreecommitdiff
path: root/util-linux/hwclock.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-01-06 22:43:39 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2010-01-06 22:43:39 +0100
commit5e3b14069e3fe25ec2595124e6e0103c7be4f813 (patch)
treed818305a88168d4aff500e2dc9a5fb590763375a /util-linux/hwclock.c
parent695fa51c80047eb25cc82e6e1630b4545a6bc0b6 (diff)
downloadbusybox-w32-5e3b14069e3fe25ec2595124e6e0103c7be4f813.tar.gz
busybox-w32-5e3b14069e3fe25ec2595124e6e0103c7be4f813.tar.bz2
busybox-w32-5e3b14069e3fe25ec2595124e6e0103c7be4f813.zip
hwclock: make it report system/rtc clock difference
function old new delta rtc_tm2time - 89 +89 read_rtc 23 86 +63 rtc_read_tm - 49 +49 hwclock_main 428 466 +38 rtcwake_main 453 477 +24 rtc_read_time 142 - -142 ------------------------------------------------------------------------------ (add/remove: 2/1 grow/shrink: 3/0 up/down: 263/-142) Total: 121 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'util-linux/hwclock.c')
-rw-r--r--util-linux/hwclock.c44
1 files changed, 32 insertions, 12 deletions
diff --git a/util-linux/hwclock.c b/util-linux/hwclock.c
index 08e5bd701..606721e2c 100644
--- a/util-linux/hwclock.c
+++ b/util-linux/hwclock.c
@@ -20,34 +20,54 @@
20 20
21static const char *rtcname; 21static const char *rtcname;
22 22
23static time_t read_rtc(int utc) 23static time_t read_rtc(struct timeval *sys_tv, int utc)
24{ 24{
25 time_t ret; 25 struct tm tm;
26 int fd; 26 int fd;
27 int before;
27 28
28 fd = rtc_xopen(&rtcname, O_RDONLY); 29 fd = rtc_xopen(&rtcname, O_RDONLY);
29 ret = rtc_read_time(fd, utc); 30
31 rtc_read_tm(&tm, fd);
32 before = tm.tm_sec;
33 while (1) {
34 rtc_read_tm(&tm, fd);
35 gettimeofday(sys_tv, NULL);
36 if (before != tm.tm_sec)
37 break;
38 }
39
30 if (ENABLE_FEATURE_CLEAN_UP) 40 if (ENABLE_FEATURE_CLEAN_UP)
31 close(fd); 41 close(fd);
32 42
33 return ret; 43 return rtc_tm2time(&tm, utc);
34} 44}
35 45
36static void show_clock(int utc) 46static void show_clock(int utc)
37{ 47{
38 //struct tm *ptm; 48 struct timeval sys_tv;
39 time_t t; 49 time_t t;
50 long diff;
40 char *cp; 51 char *cp;
41 52
42 t = read_rtc(utc); 53 t = read_rtc(&sys_tv, utc);
43 //ptm = localtime(&t); /* Sets 'tzname[]' */
44 54
45 cp = ctime(&t); 55 cp = ctime(&t);
46 strchrnul(cp, '\n')[0] = '\0'; 56 strchrnul(cp, '\n')[0] = '\0';
47 57
48 //printf("%s 0.000000 seconds %s\n", cp, utc ? "" : (ptm->tm_isdst ? tzname[1] : tzname[0])); 58 //printf("%s 0.000000 seconds %s\n", cp, utc ? "" : (ptm->tm_isdst ? tzname[1] : tzname[0]));
49 /* 0.000000 stand for unimplemented difference between RTC and system clock */ 59 diff = sys_tv.tv_sec - t;
50 printf("%s 0.000000 seconds\n", cp); 60 if (diff < 0 /*&& tv.tv_usec != 0*/) {
61 /* Why? */
62 /* diff >= 0 is ok: diff < 0, can't just use tv.tv_usec: */
63 /* 45.520820 43.520820 */
64 /* - 44.000000 - 45.000000 */
65 /* = 0.520820 = -1.479180, not -2.520820! */
66 diff++;
67 /* should be 1000000 - tv.tv_usec, but then we must check tv.tv_usec != 0 */
68 sys_tv.tv_usec = 999999 - sys_tv.tv_usec;
69 }
70 printf("%s %ld.%06lu seconds\n", cp, diff, (unsigned long)sys_tv.tv_usec);
51} 71}
52 72
53static void to_sys_clock(int utc) 73static void to_sys_clock(int utc)
@@ -58,7 +78,7 @@ static void to_sys_clock(int utc)
58 tz.tz_minuteswest = timezone/60 - 60*daylight; 78 tz.tz_minuteswest = timezone/60 - 60*daylight;
59 tz.tz_dsttime = 0; 79 tz.tz_dsttime = 0;
60 80
61 tv.tv_sec = read_rtc(utc); 81 tv.tv_sec = read_rtc(NULL, utc);
62 tv.tv_usec = 0; 82 tv.tv_usec = 0;
63 if (settimeofday(&tv, &tz)) 83 if (settimeofday(&tv, &tz))
64 bb_perror_msg_and_die("settimeofday() failed"); 84 bb_perror_msg_and_die("settimeofday() failed");
@@ -79,15 +99,15 @@ static void from_sys_clock(int utc)
79 99
80 gettimeofday(&tv, NULL); 100 gettimeofday(&tv, NULL);
81 101
102 t = tv.tv_sec;
82 rem_usec = 1000000 - tv.tv_usec; 103 rem_usec = 1000000 - tv.tv_usec;
83 if (rem_usec < 1024) { 104 if (rem_usec < 1024) {
84 /* Less than 1ms to next second. Good enough */ 105 /* Less than 1ms to next second. Good enough */
85 small_rem: 106 small_rem:
86 tv.tv_sec++; 107 t++;
87 } 108 }
88 109
89 /* Prepare tm */ 110 /* Prepare tm */
90 t = tv.tv_sec;
91 if (utc) 111 if (utc)
92 gmtime_r(&t, &tm); /* may read /etc/xxx (it takes time) */ 112 gmtime_r(&t, &tm); /* may read /etc/xxx (it takes time) */
93 else 113 else