How can you detect large clock jumps?
Linux maintains at least two internal system clocks:
- The "real time clock" commonly kept in sync with an NTP server
- The "monotonic clock" never goes backwards, just ticks forwards
A jump back of approximately 4 years is very likely to be caused by something resetting the real time clock. The monotonic clock should never jump backwards, that's the whole point of it.
So if you monitor the monotonic clock compared to the system clock, you can discover times when the system clock has suddenly jumped back a very long way (few clocks should jump back more than a month).
If you have python, you can get the difference in seconds trivially:
python3 << EOF
import time
print(time.time() - time.monotonic())
EOF
This gives a number (in seconds) that's meaningless in it's own right, but any large changes will infer a system clock jump. Any change of -2592000 or worse shows a jump larger than 30 days.
Commands like sleep should be unaffected by clock jumps like this so you should be able to run this periodically.
Diagnosing the real problem
I went down a very deep rabbit hole with a near identical sounding issue.
It's a month of my professional life I won't be getting back. So I'll highlight this answer. It may help you fix the root cause.
It's quite unlikely your system clock is spontaneously corrupting itself.
The system clock is maintained by software not hardware. The RTC, if your device has one, is normally just read on startup to recover after reboot. Unless you have a quirky ntpd configuration, you can safely rule out the RTC.
Typically the only thing spontaneously changing you system clock is either an NTP daemon or SNTP daemon. My money would be on an SNTP daemon causing it because NTP daemons check multiple servers and so are pretty fault tolerant.
Some home routers do something very dirty. They act as an NTP or SNTP server. But when the router reboots, without an RTC, the router software just uses a fixed date/time (eg: a software patch build date?). Despite the date/time clearly being wrong, their NTP server carries on issuing the wrong date, and only issues the right date when the router itself has updated with NTP or SNTP.
In the case of the BT Home hub I dealt with a few years back, the router reconfigured every device it could using DHCP. It even declared itself a "Stratum 1" NTP server meaning it claimed to be directly connected to a time source (atomic clock).
Try rebooting your router a few times to see if this triggers your IOT device time to jump.