Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 19 Aug 2010 13:09:46 +0300
From:      phil hefferan <wdef200@gmail.com>
To:        freebsd-hackers@freebsd.org
Subject:   Reading rtc on FreeBSD
Message-ID:  <AANLkTinu-7wxfHU3OweQ3rM9P6DvR10gTxNYys8OEpjq@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
 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
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. I read here
http://www.mail-archive.com/freebsd-hardware@freebsd.org/msg03414.html 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?



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?AANLkTinu-7wxfHU3OweQ3rM9P6DvR10gTxNYys8OEpjq>