4

I did not see any similar questions on this site. The manpage, while helpful in describing how to use date, did not have much background info. Even the info page (as prescribed in the manpage: info '(coreutils) date invocation'), had little more than how it operates based on the TZ variable.

I'm wondering how the date command line utility works. Specifically,

date +%s

which returns seconds since the epoch, e.g. 1467743297 (seconds since 1970-01-01 00:00:00 UTC).

For example, I'm thinking it doesn't require an internet connection. Then again, does it occasionally try to re-sync with a particular source? If so, how is that source specified? If not, then how much confidence on a given machine does one have in values reported by date?

Section 21.1.5 of the info page implies date only deals with the system/software clock, and then any sync with the hwclock is dependent upon the OS. So... maybe the question is more appropriately "which systems have more accurate date +%s reporting?" I suppose this would also imply the accuracy of date is limited to the accuracy of the machine's hardware clock, especially as influenced by the system/software clock? (e.g. the sw clock could interfere, skewing accuracy, even with a highly-accurate hw clock).

tniles
  • 510
  • 2
  • 14
  • `date` takes its time from the system clock, which takes its time (primarily) from the hardware clock. – Shadur Jul 06 '16 at 08:14

2 Answers2

4

date makes no effort to synchronize with anything at all, and merely makes some system call (that on linux a strace date ... may or may not show) to lookup the time since the epoch as known by the system.

The system itself may synchronize with the BIOS clock, or if a virtual machine may obtain the current time from the parent it runs under, or may use NTP (or other software that does more or less the same thing, e.g. the older rdate or instead the Precision Time Protocol (PTP)) to synchronize the time with other computers, or may use a hardware GPS or radio device to set the time from. None of these involve date, unless the admin is listening to, say, BBC Radio and manually setting the system time with date at the top of the hour (do they still do the doot doot doot dee thing at the top of the hour?).

Accuracy of the system clock depends on the NTP configuration (or whatever other software is used), whether NTP (or similar) is broken, the BIOS, whether the BIOS is broken (I've seen a system boot four years into the future and that host had a broken NTP setup for other reasons, good fun, this being a NFS server with Makefile...), whether the hardware clock is misbehaving, and various other details. If you're using NTP, the peers command to ntpq or the drift file might be worth looking at.

For the most accurate time (apart from putting an atomic clock on each host...) you might want to read up on PTP in "Toward Higher Precision" (and also learn that Time is an Illusion).

thrig
  • 34,333
  • 3
  • 63
  • 84
  • 2
    Fun factoid: on a current-ish Linux, `strace` might_not_ show the system calls used for fetching the current time (`time()` or `gettimeofday()`). Those are called often enough that there are optimizations for them: http://man7.org/linux/man-pages/man7/vdso.7.html – ilkkachu Jul 05 '16 at 19:50
  • @ilkkachu There is always `ltrace`, which shows you both lib- and system calls. If that's not sufficient, I'd read the source of `date`. – ott-- Jul 05 '16 at 21:15
2

Your assumption that date does not require an internet connection is correct. It simply outputs the current time stored in the system-clock. (More information on time and date commands)

The hardware clock is a battery-operated circuit on the motherboard that maintains the current time while the system is powered off or unplugged.

On system startup, the kernel pulls the hardware time and sets the system (software) clock. This is run based on the processor clock, which is far more precise than the low-power hardware clock. (More here)

The hardware and software clocks then run independently of each other, although they can be synced with the hwclock --systohc or hwclock --hctosys commands. The software clock is typically written to the hardware clock on system shutdown.

To synchronize with an external timekeeping authority, use rdate for one-time synchronization, or set up the ntp utility for continuous updating. Additionally, the ntp daemon will calculate the drift of the system clock relative to an internet source, and correct for it, even if the internet connection is lost.

strakcy
  • 120
  • 11