Section: Linux Programmer's Manual (4)
Updated: 2005-12-05
Index Return to Main Contents


rtc - real-time clock  


#include <linux/rtc.h>  


This is the driver for the real-time clock (RTC).

Most computers have a built-in hardware clock, usually called the real-time clock. This clock is normally battery powered so that it keeps the time even while the computer is switched off. It represents the current time as year, month, day of month, hour, minute, and second.

The RTC is a chip that maintains the time and date and is able to generate interrupts at specified times. This chip typically used to be a Motorola MC146818, a Dallas DS12887, or similar, but today it is usually implemented in the mainboard's chipset.

The RTC should not be confused with the system time which is an independent, interrupt-driven software clock maintained by the kernel. The software clock is maintained by an interrupt routine that typically has a frequency of 100, 250, or 1000 Hz. The software clock counts seconds and microsecond since the POSIX Epoch, i.e., Jan 1, 1970, 0:00 UTC. This clock does not involve any special hardware.

The RTC can be read and set with hwclock(8).

The RTC is almost never used by the Linux kernel. Instead, the kernel uses the software clock time for time(2), gettimeofday(2), timestamps on files, etc. However, at boot time the kernel initializes its software clock by reading the RTC.

Besides counting the date and time, the RTC can also generate interrupts

on every clock update (i.e. once per second);
at periodic intervals with a frequency that can be set to any power-of-2 multiple in the range 2 Hz to 8192 Hz;
on reaching a previously specified alarm time.

Each of these interrupt sources can be enabled or disabled separately.

The /dev/rtc device can be opened only once simultaneously and it is read-only. On read(2) and select(2) the calling process is blocked until the next interrupt from the RTC is received. Following the interrupt, the process can read a long integer, of which the least significant byte contains the type of interrupt that occurred, while the remaining 3 bytes contain the number of interrupts since the last read(2).

The following ioctl(2) operations are provided:

Returns the RTC time in the following structure:

struct rtc_time {
    int tm_sec;
    int tm_min;
    int tm_hour;
    int tm_mday;
    int tm_mon;
    int tm_year;
    int tm_wday;     /* unused */
    int tm_yday;     /* unused */
    int tm_isdst;    /* unused */
The fields in this structure have the same meaning and ranges as for the tm structure described in gmtime(3). A pointer to this structure should be passed as the third ioctl() argument.
Sets the RTC time to the time specified by the rtc_time structure pointed to by the third ioctl() argument. To set the RTC time the process must be privileged (i.e., have the CAP_SYS_TIME capability).
Read and set the alarm time. The third ioctl() argument is a pointer to an rtc_time structure. Only the tm_sec, tm_min, and tm_hour fields of this structure are used.
Read and set the frequency for periodic interrupts. The third ioctl() argument is a long * or a long, respectively. The value is the frequency in interrupts per second. The set of allowable frequencies is the multiples of two in the range 2 to 8192. Only a privileged process (i.e., one having the CAP_SYS_RESOURCE capability) can set frequencies above the value specified in /proc/sys/dev/rtc/max-user-freq. (This file contains the value 64 by default.)
Enable or disable the alarm interrupt. The third ioctl() argument is ignored.
Enable or disable the interrupt on every clock update. The third ioctl() argument is ignored.
Enable or disable the periodic interrupt. The third ioctl() argument is ignored. Only a privileged process (i.e., one having the CAP_SYS_RESOURCE capability) can enable the periodic interrupt if the frequency is currently set above the value specified in /proc/sys/dev/rtc/max-user-freq.
The RTC encodes the year in an 8-bit register which is either interpreted as an 8-bit binary number or as a BCD number. In both cases, the number is interpreted relative to the RTC Epoch. The RTC Epoch is initialized to 1900 on most systems but on Alpha and Mips it might also be initialized to 1952, 1980, or 2000, depending on the value of RTC register for the year. These operations can be used to read or to set the RTC Epoch, respectively. To set the RTC Epoch the process must be privileged (i.e., have the CAP_SYS_TIME capability).


/dev/rtc: the RTC special character device file.

/proc/driver/rtc: status of the RTC.  


When the kernel's system time is synchronized with an external reference using adjtimex(2) it will update the RTC periodically every 11 minutes. To do so, the kernel has to briefly turn off periodic interrupts; this might affect programs using the RTC.

The RTC Epoch has nothing to do with the POSIX Epoch which is only used for the system clock.

If the year according to the RTC Epoch and the RTC's year register is less than 1970 it is assumed to be 100 years later, i.e. between 2000 and 2069.  


hwclock(8), date(1), time(2), stime(2), gettimeofday(2), settimeofday(2), adjtimex(2), gmtime(3), time(7), /usr/src/linux/Documentation/rtc.txt




linux.jgfs.net manual pages