ntpdig/sntp sometimes output "weird" correct time

30 Views Asked by At

I am trying to get correct estimated time from an ntp server. Every once in a while ntpdig returns "weird" output.

# Weird, it contains 5 or even less digits in a fractional part of the output.
:~$ ntpdig -4 time.google.com
2023-12-18 16:39:36.74198 (+0100) +0.779136 +/- 0.013945 time.google.com 216.239.35.0 s1 no-leap

# Normal, it contains 6 digits in a fractional part of the output, even has trailing zeroes.
:~$ ntpdig -4 time.google.com
2023-12-18 16:40:08.970600 (+0100) +0.779832 +/- 0.014195 time.google.com 216.239.35.12 s1 no-leap

Command finished successfully in both cases, i.e. returns 0 exit code.

Command is supposed to output correct synced clock with options as in example above. If I compare its output clock values to the local clock of my machine with the following simple script:

# script.sh
ntp_output=`ntpdig -4 time.google.com`
t1_ms=`echo "$ntp_output" | cut -c-26 | xargs -i date -d '{}' +%s%3N`
t2_ms=`date '+%s%3N'`
t_diff_ms=$(($t2_ms-$t1_ms))
echo "ntpdig $ntp_output >>> vs local clock: $t_diff_ms ms"

Exactly on weird outputs time difference from local clock will "deviate":

$ ./script.sh 
ntpdig 2023-12-18 16:57:05.550649 (+0100) +0.773842 +/- 0.013840 time.google.com 216.239.35.0 s1 no-leap >>> clock diff: -685 ms
$ ./script.sh 
ntpdig 2023-12-18 16:57:06.309844 (+0100) +0.774098 +/- 0.006148 time.google.com 216.239.35.8 s1 no-leap >>> clock diff: -687 ms
$ ./script.sh 
ntpdig 2023-12-18 16:57:06.910969 (+0100) +0.774642 +/- 0.013711 time.google.com 216.239.35.12 s1 no-leap >>> clock diff: -688 ms
$ ./script.sh 
ntpdig 2023-12-18 16:57:07.462092 (+0100) +0.775254 +/- 0.006074 time.google.com 216.239.35.4 s1 no-leap >>> clock diff: -684 ms
$ ./script.sh 
ntpdig 2023-12-18 16:57:08.49674 (+0100) +0.772464 +/- 0.017524 time.google.com 216.239.35.0 s1 no-leap >>> clock diff: -1130 ms  <--- where only 5 fractional digits are output estimated correct time is significantly further away from local clock reading 

Which makes me think that whatever time command outputs in "weird" cases is incorrect. What is up with these "weird" ntpdig outputs? Can they be trusted? I could not find anything about them.

I've bundled ntpdig/sntp since in some distros (Debian 12) sntp is just an alias to ntpdig.

I do not know what to try. :) I would expect ntpdig/sntp to output data in constant format, i.e. with 6 digits in fractional part of the time. And if this is not possible for some reason I would expect command to fail with some error code.

Did anyone ran into anything like that?

0

There are 0 best solutions below