User Tools

Site Tools

한국어

comfilepi:set_date_and_time

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
comfilepi:set_date_and_time [2026/03/27 00:17] – [Drift Compensation] mfranklincomfilepi:set_date_and_time [2026/04/05 23:54] (current) – [System Clock Compensation] mfranklin
Line 14: Line 14:
  
 ===== Drift Compensation ===== ===== 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: 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:
  
-  - Boot the panel PC with Internet time server synchronization enabled (i.e''sudo timedatectl set-ntp true'') so the system time and RTC are both synchronized with the time from the Internet time server. +  - With Internet time synchronization disabled, but with an Internet connection, run <html><code>sudo ntpdate pool.ntp.org</code></html> to manually synchronize the system time to an Internet time server. 
-  - Run ''sudo hwclock systohc utc update-drift'' to ensure the ''/etc/adjtime'' records the last calibration time. +  - Run <html><code>sudo hwclock --systohc --utc --update-drift</code></html> to synchronize the RTC with the system time, and to ensure the ''/etc/adjtime'' records the last calibration time.
-  - Disable Internet time server synchronization with ''sudo timedatectl set-ntp false''+
   - Power off the panel PC for a few days; the longer the better.   - Power off the panel PC for a few days; the longer the better.
-  - Boot the panel PC and run the following commands in order +  - Boot the panel PC 
-    - <html><code>sudo ntpdate pool.ntp.org</code></html> +  Run <html><code>sudo ntpdate pool.ntp.org</code></html> to manually synchronize the system time to an Internet time server. 
-    - <html><code>sudo hwclock --systohc --utc --update-drift</code></html>+  Run <html><code>sudo hwclock --systohc --utc --update-drift</code></html> 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 [[https://manpages.debian.org/trixie/util-linux/adjtime_config.5.en.html|/etc/adjtime]] file with a drift compensation measurement determined from the above procedure.  That procedure will update the [[https://manpages.debian.org/trixie/util-linux/adjtime_config.5.en.html|/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//:
 +
 +<code>
 +ACTION=="add", SUBSYSTEM=="rtc", KERNEL=="rtc0", TAG+="systemd", ENV{SYSTEMD_WANTS}+="hwclock.service"
 +</code>
 +
 +And create the following service at ///etc/systemd/system/hwclock.service//:
 +
 +<code>
 +[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
 +</code>
 +
 +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 [[https://manpages.debian.org/trixie/adjtimex/adjtimexconfig.8.en.html|adjtimexconfig]].
 +
 +Run the following commands in a terminal.
 +<code>
 +sudo apt update
 +sudo apt install adjtimex
 +sudo adjtimexconfig
 +sudo systemctl enable adjtimex.service
 +</code>
 +
 +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:index|ComfilePi - Industrial Raspberry Pi Panel PC]] [[comfilepi:index|ComfilePi - Industrial Raspberry Pi Panel PC]]
comfilepi/set_date_and_time.1774570629.txt.gz · Last modified: by mfranklin