How to work with RTC (Real Time Clock)#

There are two important clocks in a Linux system:

  • The hardware clock, which is also known as the RTC (Real Time Clock). It is the battery clock which keeps time even when the system is down.

  • The system clock, which is also known as the kernel clock. It is the clock which is handled by the operating system.

When the system boots, the hardware clock is read and used to set the system clock and after that, it is the system clock that is used to track time.

Use timedatectl command#

When using the timedatectl command to set a new date or time, the time is stored directly into the the hardware clock and the system clock is not stored into the hardware clock when the device is shutdown.

The timedatectl command can be used to display the current time, the hardware clock and the system clock:

root@sm2s-imx8plus:~# timedatectl
                Local time: Mon 2025-12-01 08:55:05 UTC
            Universal time: Mon 2025-12-01 08:55:05 UTC
                  RTC time: Mon 2025-12-01 08:55:05
                 Time zone: UTC (UTC, +0000)
 System clock synchronized: yes
               NTP service: active
           RTC in local TZ: no

If there is an Internet connection on the device (using Ethernet or Wifi), the service systemd-timesyncd automatically synchronizes the system clock with a NTP server. And the service systemd-timedated synchronizes the system clock with the hardware clock immediately.

If there is no Internet connection, it is possible to set the date or time and the service systemd-timedated synchronizes the system clock with the hardware clock immediately:

root@sm2s-imx8plus:~# timedatectl set-time "2025-12-01 09:58:00"
root@sm2s-imx8plus:~# timedatectl
                Local time: Mon 2025-12-01 09:58:01 UTC
            Universal time: Mon 2025-12-01 09:58:01 UTC
                  RTC time: Mon 2025-12-01 09:58:01
                 Time zone: UTC (UTC, +0000)
 System clock synchronized: no
               NTP service: inactive
           RTC in local TZ: no

Use hwclock command#

It is also possible to use the hwclock tool to access the hardware clock directly. It provides several commands:

  • hwclock -r: display the hardware clock time

  • hwclock -w: set the hardware clock from the system clock

  • hwclock -s: set the system clock from the hardware clock

For example, if there is no Internet connection, the date or time can be set using the date command but it does not synchronize the hardware clock automatically from the system clock so the command hwclock -w has to be used:

root@sm2s-imx8plus:~# date -s "2025-12-01 10:06:30"
Mon Dec  1 10:06:30 UTC 2025
root@sm2s-imx8plus:~# hwclock -r
Mon Dec  1 10:11:43 2025  0.000000 seconds
root@sm2s-imx8plus:~# hwclock -w
root@sm2s-imx8plus:~# hwclock -r
Mon Dec  1 10:06:36 2025  0.000000 seconds

Available RTCs on SOCs#

On the i.MX8 SOCs and the Intel SOCs, there is only one RTC.

For example, on sm2s-imx8plus and sm2s-al, there is only rtc0:

root@sm2s-imx8plus:~# dmesg | grep rtc
[    5.429254] rtc-rs5c372 5-0032: registered as rtc0
[    5.448176] rtc-rs5c372 5-0032: setting system clock to 2025-12-01T10:12:53 UTC (1764583973)
root@sm2s-intel-all:~# dmesg | grep rtc
rtc_cmos 00:02: RTC can wake from S4
rtc_cmos 00:02: registered as rtc0
rtc_cmos 00:02: setting system clock to 2025-12-01T09:27:47 UTC (1764581267)
rtc_cmos 00:02: alarms up to one month, y3k, 242 bytes nvram

Using the alarm to wake up the device#

The alarm feature is only available on Intel SOCs.

To check if the alarm is available it is necessary to check the presence of the file /sys/class/rtc/rtc0/wakealarm.

So when the feature is there, it is possible to set up an alarm and then put the device in sleep mode. And the device wakes up when the alarm is triggered.

For example, it is possible to enable the wake up in 20 seconds from now, and then put the device to sleep to check if it is waking up after 20 seconds:

root@sm2s-intel-all:/sys/class/rtc/rtc0# cat /sys/class/rtc/rtc0/wakealarm
root@sm2s-intel-all:/sys/class/rtc/rtc0# echo +20 > /sys/class/rtc/rtc0/wakealarm
root@sm2s-intel-all:/sys/class/rtc/rtc0# cat /sys/class/rtc/rtc0/wakealarm
1764581252
root@sm2s-intel-all:/sys/class/rtc/rtc0# shutdown -h now