在 Debian 机器上获取 TAI 时间

  • Publication and Update Information: The article was first published on 16 April 2024 and last updated on 17 April 2024.
  • Default Functioning and Issues on Some OS: It should work by default, but on some operating systems like Debian, it doesn't. To get TAI time, a small configuration change is needed.
  • Difference between TAI and UTC: TAI never adds or misses an extra second like UTC due to leap seconds.
  • Problem with clock_gettime on Debian: When using clock_gettime in C or time.clock_gettime in Python on a Debian stable machine (version 12.5), passing CLOCK_TAI doesn't work, and it gives the same value as CLOCK_REALTIME (set on UTC), with no error code indicating a problem.
  • Role of Kernel and ntpd: The kernel only knows UTC and needs to know the offset (currently 37 seconds) to convert to TAI. Debian has a leap seconds table in /usr/share/zoneinfo/leap-seconds.list, but ntpd needs to be configured to load this file. By adding leapfile /usr/share/zoneinfo/leap-seconds.list to /etc/ntpsec/ntp.conf (or /etc/ntp.conf) and restarting ntpd, the kernel can synchronize.
  • Log File and Permissions: If there is an error like "open failed: Permission denied" in the log file when ntpd tries to read the leap seconds file, it means there might be permission issues or restrictions by systemd or AppArmor.
  • Checking Kernel Knowledge: After configuring ntpd, one can check that the kernel knows the truth by using a simple Python session and seeing the difference between time.clock_gettime(time.CLOCK_TAI) and time.clock_gettime(time.CLOCK_REALTIME), which is 37 seconds plus a small delay.
  • Handling on Slackware: On a Slackware system, the leap-seconds.list file is not provided by default, and a cron job might be needed to download the proper file.
  • Alternative Library: An alternative is to use a library like hifitime for Rust and Python to handle time.
  • Additional Thanks: Thanks to Nicolas Sapa, Matthieu Herrb, and Kim Minh Kaplan for helpful help.
  • PDF Version: There is a PDF version of this page available at [https://www.bortzmeyer.org/ta...], and one can also print from the browser with a specific style sheet.
阅读 14
0 条评论