Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect change of system time in Linux?

Is there a way to get notified when there is update to the system time from a time-server or due to DST change? I am after an API/system call or equivalent.

It is part of my effort to optimise generating a value for something similar to SQL NOW() to an hour granularity, without using SQL.

like image 941
ϹοδεMεδιϲ Avatar asked Feb 12 '10 12:02

ϹοδεMεδιϲ


People also ask

How do I know what time my computer changes?

You can use the SystemEvents. TimeChanged event to detect the time changed. However, this will only fire if you have a messagepump running - this is fine for windows client applications, but not so much for most server applications (like services, web apps, etc). Otherwise you can use Environment.

How do I fix system time on Linux?

How to Adjust the Time on Linux. To set or change the time, use the timedatectl command together with the set-time subcommand. Note: You need to have elevated privileges to adjust the time or date. In the aforementioned command, the hh stands for hours, mm for minutes, and ss for seconds.

Where is system time stored in Linux?

Time zone and DST information is stored in /usr/share/zoneinfo (or /usr/lib/zoneinfo on older systems). The local time zone is determined by a symbolic link from /etc/localtime to one of these files. The way to change your timezone is to change the link.

What is system clock in Linux?

Two clocks are important in Linux: a 'hardware clock', also known as RTC, CMOS or BIOS clock. This is the battery-backed clock that keeps time even when the system is shut down. The second clock is called the 'system clock/tick' or 'kernel clock' and is maintained by the operating system.


2 Answers

You can use timerfd_create(2) to create a timer, then mark it with the TFD_TIMER_CANCEL_ON_SET option when setting it. Set it for an implausible time in the future and then block on it (with poll/select etc.) - if the system time changes then the timer will be cancelled, which you can detect.

(this is how systemd does it)

e.g.:

#include <sys/timerfd.h>
#include <limits.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>

int main(void) {
        int fd = timerfd_create(CLOCK_REALTIME, 0);
        timerfd_settime(fd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET,
                        &(struct itimerspec){ .it_value = { .tv_sec = INT_MAX } },
                        NULL);
        printf("Waiting\n");
        char buffer[10];
        if (-1 == read(fd, &buffer, 10)) {
                if (errno == ECANCELED)
                        printf("Timer cancelled - system clock changed\n");
                else
                        perror("error");
        }
        close(fd);
        return 0;
}
like image 171
Ben Avatar answered Sep 29 '22 10:09

Ben


I don't know if there is a way to be notified of a change in the system time, but

  • The system time is stored as UTC, so there is never a change due to DST change to be notified.

  • If my memory is correct, NTP deamon usually adjust the clock by changing its speed, again no change to be notified.

So the only times where you would be notified is after an uncommon manipulation.

like image 22
AProgrammer Avatar answered Sep 29 '22 08:09

AProgrammer