adjust clock 20ms with current time

  • As for my pi5 i know the it has ~5PPM, so it drift ~20ms every hours
#include <stdio.h>
#include <time.h>

int main(void)
{
    struct timespec ts;

    if (clock_gettime(CLOCK_REALTIME, &ts) != 0)
        return 1;

    ts.tv_nsec += 20000000L;  // +20 ms

    if (ts.tv_nsec >= 1000000000L) {
        ts.tv_sec++;
        ts.tv_nsec -= 1000000000L;
    }

    if (clock_settime(CLOCK_REALTIME, &ts) != 0)
        return 1;

    return 0;
}
gcc add20ms.c -o add20ms
./add20ms
  • Other tool adjtimex that can update the frequency of current clock
@home:~$ adjtimex --print
         mode: 0
       offset: 0
    frequency: -1058757
     maxerror: 3752
     esterror: 1145
       status: 0
time_constant: 2
    precision: 1
    tolerance: 32768000
         tick: 10000
     raw time:  1787551740s 373129us = 1787551740.373129


frequency: -1058757
1 ppm = 65536
So +5.556 ppm corresponds to:
5.556 × 65536 ≈ 364446

add +5.556 ppm to the clock's existing frequency correction
-1058757 + 364446
= -694311


sudo adjtimex --freq -694311
root@lp-arm-5:/opt/docker# ntpdate 192.168.10.196
2026-08-24 13:32:32.978000 (+0530) +166337.977652 +/- 0.003123 192.168.10.196 s1 no-leap
CLOCK: time stepped by 166337.977652
CLOCK: time changed from 2026-08-22 to 2026-08-24

#### After reboot

root@lp-arm-5:~# date
Mon Aug 24 13:40:46 IST 2026

root@lp-arm-5:~# ntpdate -q 192.168.10.90
2026-08-24 14:18:12.673999 (+0530) +2234.752511 +/- 0.004031 192.168.10.90 s1 no-leap

root@lp-arm-5:~# ntpdate 192.168.10.90
2026-08-24 14:18:15.906000 (+0530) +2234.750451 +/- 0.005585 192.168.10.90 s1 no-leap
CLOCK: time stepped by 2234.750451

Published by

Leave a Reply

Your email address will not be published. Required fields are marked *