linux - detecting change of system time

2019-05-19 03:32发布

问题:

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 or my effort to optimize generating a value for something similar to SQL NOW() to an hour granularity, without using SQL.

回答1:

clock_gettime on most recent Linux systems is incredibly fast, and usually pretty amazingly precise as well; you can find out the precision using clock_getres. But for hour level timestamps, gettimeofday might be more convenient since it can do the timezone adjustment for you.

Simply call the appropriate system call and do the division into hours each time you need a timestamp; all the other time adjustments from NTP or whatever will already have been done for you.



回答2:

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.