User Tools

Site Tools

한국어

comfilepi:set_date_and_time

Set the Date and Time

Run the following commands in a terminal to set the ComfilePi's date and time and save it to the ComfilePi's RTC.

# Disable synchronization with the time server
sudo timedatectl set-ntp false
 
# Set the date and time (e.g. January 2nd, 2026 5:30am).  This will set both the system time and the RTC's time.
sudo timedatectl set-time '2026-1-2 5:30:00'

NOTE: Modern Linux has a built-in safety feature that checks system's date and time is prior to the date and time that the operating system was built, and if so, uses the build date and time instead of the set date and time. So, if manually setting the date and time for testing, be sure to use a date and time after the operating system's build date and time.

Drift Compensation

There are two clocks that have the potential to drift: The RTC and the operating system's system clock. Both will drift independently, so the Linux operating system provides utilities to configure compensation for both.

RTC Drift Compensation

If you notice the RTC drifting over time, and the drift is consistent and predictable, it is possible to configure the OS to compensate for the drift using the following procedure:

  1. With Internet time synchronization disabled, but with an Internet connection, run sudo ntpdate pool.ntp.org to manually synchronize the system time to an Internet time server.
  2. Run sudo hwclock --systohc --utc --update-drift to synchronize the RTC with the system time, and to ensure the /etc/adjtime records the last calibration time.
  3. Power off the panel PC for a few days; the longer the better.
  4. Boot the panel PC
  5. Run sudo ntpdate pool.ntp.org to manually synchronize the system time to an Internet time server.
  6. Run sudo hwclock --systohc --utc --update-drift to synchronize the RTC with the system time, and to record the drift compensation, as measured over the idle time period, to the /etc/adjtime file.

That procedure will update the /etc/adjtime file with a drift compensation measurement determined from the above procedure.

Finally, to ensure the system time is updated from the RTC, with drift compensation, create the following udev rule file at /etc/udev/rules.d/85-hwclock.rules:

ACTION=="add", SUBSYSTEM=="rtc", KERNEL=="rtc0", TAG+="systemd", ENV{SYSTEMD_WANTS}+="hwclock.service"

And create the following service at /etc/systemd/system/hwclock.service:

[Unit]
Description=Set system clock from RTC with adjtime compensation
ConditionPathExists=/dev/rtc0
ConditionPathExists=/etc/adjtime

[Service]
Type=oneshot
ExecCondition=/bin/sh -ec 'timedatectl show --property=NTP 2>/dev/null | grep -qx "NTP=no"'
ExecStart=/sbin/hwclock --rtc=/dev/rtc0 --hctosys

Reboot, and after booting, the ComfilePi should have its system time synchronized with the RTC, and adjusted to compensate for the RTC's drift. Confirm with the date command.

System Clock Compensation

After configuring the RTC's drift compensation, the system clock's drift compensation can be configured. For that we need adjtimexconfig.

Run the following commands in a terminal.

sudo apt update
sudo apt install adjtimex
sudo adjtimexconfig
sudo systemctl enable adjtimex.service

The adjustimexconfig command will measure the amount of drift the system clock has relative to the RTC, save it to /etc/default/adjtimex and the adjtimex.service will load that at every boot to compensate.

ComfilePi - Industrial Raspberry Pi Panel PC

comfilepi/set_date_and_time.txt · Last modified: by mfranklin