Reading rtc on FreeBSD

phil hefferan wdef200 at
Thu Aug 19 10:40:02 UTC 2010

 I have C code for Linux that, among other things, caches the difference
between the rtc and system time, so that the program can detect if the
system time has changed more than a threshold between runs.  I want to port
this code to FreeBSD/Mac.

I'm trying to clarify the relationship between rtc and system time on
FreeBSD and how the rtc is read.

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

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. 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. I read here that
settimeofday in fact sets both rtc and system time together.

On Linux both gettimeofday and settimeofday apply to the software time only.

BUT the source to adjkerntz.c for FreeBSD seems to say 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?

More information about the freebsd-hackers mailing list