How to read cmos clock - what is gettimeofday reading?

Phil Grundig wdef200 at yahoo.co.uk
Wed Aug 18 08:41:18 UTC 2010


I have C code for Linux that, among other things, caches the difference between 
the rtc and system time. I want to port this code to FreeBSD/Mac. Linux has the 
utility hwclock which reads /dev/rtc and anyway getting the cmos clock time 
directly from this interface can be done in a few lines of code.

I've been looking around for how to read the cmos/rtc on FreeBSD. There is no 
hwclock utility in FreeBSD that I can read sources for to see how it is done.
http://www.wraith.sf.ca.us/ntp/not-hwclock.c.txt is supposed to be C code to 
read the software time and then set the cmos clock to match. This code implies 
that, on FreeBSD, gettimeofday reads the software time and settimeofday sets the 
cmos clock. On Linux gettimeofday does indeed read the software time.

BUT the source to adjkerntz.c for FreeBSD says that gettimeofday reads the CMOS 
clock not the system time:

/* get local CMOS clock and possible kernel offset */
if (gettimeofday(&tv, &tz)) {
    syslog(LOG_ERR, "gettimeofday: %m");
    return 1;
}

Which is it? Does gettimeofday read the cmos clock/rtc on FreeBSD? If not, how 
do I read the battery-backed clock on FreeBSD?

To further confuse matters there appears to be a common misconception on the web 
that the cmos time is automatically synced to system time on FreeBSD. This is 
incorrect: see msg03414 on freebsd-hardware at freebsd.org.

Thanks



      


More information about the freebsd-hackers mailing list