I am working on a sensor system, which runs on an embedded Linux platform and doesn't have access to the internet. An external GPS time server device is connected to provide time synchronization using the chrony daemon.
My c++ application reads measurements at 50Hz, with an associated timestamp, however, I would like to set a flag in the data to report if the system time is correct.
By command line timedatectl can be used to report the "NTP Synchronised" status.
:~$ timedatectl
Local time: Wed 2020-08-19 17:03:01 BST
Universal time: Wed 2020-08-19 16:03:01 UTC
RTC time: Wed 2020-08-19 16:03:01
Time zone: Europe/London (BST, +0100)
Network time on: yes
NTP synchronized: yes
RTC in local TZ: no
And the status of the chrony daemon will report system clock adjustments (note that the large time adjustment was caused by some phony date set for test purposes).
sudo systemctl status chrony
● chrony.service - LSB: Controls chronyd NTP time daemon
Loaded: loaded (/etc/init.d/chrony; bad; vendor preset: enabled)
Active: active (running) since Mon 2006-10-02 18:00:28 BST; 13 years 10 months ago
Docs: man:systemd-sysv-generator(8)
Process: 3201 ExecStop=/etc/init.d/chrony stop (code=exited, status=0/SUCCESS)
Process: 3240 ExecStart=/etc/init.d/chrony start (code=exited, status=0/SUCCESS)
Tasks: 1
Memory: 340.0K
CPU: 90ms
CGroup: /system.slice/chrony.service
└─3247 /usr/sbin/chronyd
Oct 02 18:00:26 systemd[1]: Starting LSB: Controls chronyd NTP time daemon...
Oct 02 18:00:26 chronyd[3247]: chronyd version 2.1.1 starting (+CMDMON +NTP +REFCLOCK +RTC +PR
Oct 02 18:00:26 chronyd[3247]: Frequency -5.049 +/- 2.705 ppm read from /var/lib/chrony/chrony
Oct 02 18:00:28 chrony[3240]: chronyd is running and online.
Oct 02 18:00:28 systemd[1]: Started LSB: Controls chronyd NTP time daemon.
Oct 02 18:00:30 chronyd[3247]: Selected source 10.10.11.20
Oct 02 18:00:30 chronyd[3247]: System clock wrong by 438114471.141858 seconds, adjustment star
Aug 20 12:28:21 chronyd[3247]: System clock was stepped by 438114471.141858 seconds
Naturally, these commands can be observed by eye and the reported time can be compared against what expect expected time, however I would like for my software application to determine it the reported time is correct.
I would therefore like to know how to programmatically detect if the current time has been synchronization and ideally detect the last time sync adjustment.
Edit: I simply want to be able to detect if the current time is synchronised within my c++ application. I have an external synchronisation source and so would like to know if this is operational and providing sync so that I can report in the data that the timestamps associated with the data has been synchronised. The system that my software application runs on will be used in a boat without internet access.