I am trying to sync my local computer clock running on Windows 10 to time.nist.gov
. I stream data from a server (assumed to be NTP synced) and I'm looking into optimizing latency, so I need to make sure first that I have a reference point.
I have W32Time
running automatically. Even after forcing sync with os.system('w32tm /resync/nowait')
, I always get a ~50ms offset. Is that offset a background statistical noise from packet random routing? Is there a way to force accurate syncing?
Building upon How to ntp server time down to millisecond precision using Python ntplib? and What are all the fields in a Python ntplib response, and how are they used?, this is what I use:
import ntplib
from datetime import datetime, timezone
def get_ntp_time():
ntp_pool = ['pool.ntp.org', 'time.nist.gov']
def call_ntp(serverAddress):
call = ntplib.NTPClient()
return call.request(server, version=3)
for server in ntp_pool:
response = call_ntp(server)
print(f"server: {server}")
print(f"request packet sent (as LOCAL client time, orig_time): {datetime.fromtimestamp(response.orig_time, timezone.utc)}")
print(f"request packet received (as NTP server time, recv_time): {datetime.fromtimestamp(response.recv_time, timezone.utc)}")
print(f"response packet sent (as NTP server time, tx_time): {datetime.fromtimestamp(response.tx_time, timezone.utc)}")
print(f"response packet received (as LOCAL client time, dest_time): {datetime.fromtimestamp(response.dest_time, timezone.utc)}")
print(f'round trip duration: {response.delay} s')
print(f'* adjusted time, tx_time + delay/2: {datetime.fromtimestamp(response.tx_time + response.delay/2, timezone.utc)}')
print(f'* adjusted time, dest_time + offset: {datetime.fromtimestamp(response.dest_time + response.offset, timezone.utc)}')
print(f'correction to client: {response.delay/2 - response.offset} s\n')
# for attr in dir(response):
# if not attr .startswith('_'):
# print("response.%s = %r" % (attr, getattr(response, attr)))
print('-')
get_ntp_time()
I get this:
server: pool.ntp.org
request packet sent (as LOCAL client time, orig_time): 2021-04-23 16:43:16.410470+00:00
request packet received (as NTP server time, recv_time): 2021-04-23 16:43:16.407630+00:00
response packet sent (as NTP server time, tx_time): 2021-04-23 16:43:16.407707+00:00
response packet received (as LOCAL client time, dest_time): 2021-04-23 16:43:16.469487+00:00
round trip duration: 0.05893993377685547 s
* adjusted time, tx_time + delay/2: 2021-04-23 16:43:16.437177+00:00
* adjusted time, dest_time + offset: 2021-04-23 16:43:16.437177+00:00
correction to client: 0.06177997589111328 s
-
server: time.nist.gov
request packet sent (as LOCAL client time, orig_time): 2021-04-23 16:43:16.528863+00:00
request packet received (as NTP server time, recv_time): 2021-04-23 16:43:16.514034+00:00
response packet sent (as NTP server time, tx_time): 2021-04-23 16:43:16.514035+00:00
response packet received (as LOCAL client time, dest_time): 2021-04-23 16:43:16.582343+00:00
round trip duration: 0.05347871780395508 s
* adjusted time, tx_time + delay/2: 2021-04-23 16:43:16.540774+00:00
* adjusted time, dest_time + offset: 2021-04-23 16:43:16.540774+00:00
correction to client: 0.06830835342407227 s
For each server, correction
remains roughly in the 50-100ms range, no matter how many times I run this code.