0

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.

Alfred Wallace
  • 1,741
  • 1
  • 14
  • 32

1 Answers1

0

50-100ms time offset is not unusual in a Windows environment between a client and a remote time source. Note the round trip duration in your measurements is over 50ms.

This Microsoft article defines how to configure the Windows Time service for high-accuracy environments for target goal of 1-second, 50-ms, and 1-ms accuracies.

Time accuracy entails the end-to-end distribution of accurate time from a highly accurate authoritative time source to the end device. Anything that introduces network asymmetry will negatively influence accuracy, for example physical network devices or high CPU load on the target system.

In addition, the Microsoft article states that a 1-ms accuracy would require the target computer have better than 0.1 ms of network latency between its time source while 50-ms accuracy requires a 5ms or smaller network latency. The stratum level of the time source and number of network hops from the time source to the target computer are also factors.

You might get different results if use some of the local NTP servers in the NTP pool. Try one close to home; e.g. 1.us.pool.ntp.org. 2.us.pool.ntp.org, etc. for servers inside the United States.

CodeMonkey
  • 22,825
  • 4
  • 35
  • 75