问题描述
我正在尝试将在 Windows 10 上运行的本地计算机时钟同步到 time.nist.gov
。我从服务器流式传输数据(假设是 NTP 同步的)并且我正在研究优化延迟,所以我首先需要确保我有一个参考点。
我让 W32Time
自动运行。即使在与 os.system('w32tm /resync/Nowait')
强制同步后,我也总是得到大约 50 毫秒的偏移量。这是否抵消了数据包随机路由的背景统计噪声?有没有办法强制准确同步?
基于 How to ntp server time down to millisecond precision using Python ntplib? 和 What are all the fields in a Python ntplib response,and how are they used?,这是我使用的:
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()
我明白了:
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
对于每个服务器,无论我运行多少次此代码,correction
都大致保持在 50-100 毫秒的范围内。
解决方法
50-100 毫秒的时间偏移在客户端和远程时间源之间的 Windows 环境中并不罕见。请注意,您的测量中的往返持续时间超过 50 毫秒。
此 Microsoft article 定义了如何为高精度环境配置 Windows 时间服务,以实现 1 秒、50 毫秒和 1 毫秒的精度目标。
时间精度需要端到端的准确时间分布 从高度准确的权威时间源到终端设备。 任何引入网络不对称的事物都会产生负面影响 准确性,例如物理网络设备或高 CPU 负载 目标系统。
此外,Microsoft 文章指出,1 毫秒的精度要求目标计算机在其时间源之间具有优于 0.1 毫秒的网络延迟,而 50 毫秒的精度需要 5 毫秒或更小的网络延迟。时间源的层级和时间源到目标计算机的网络跳数也是影响因素。
如果使用 NTP pool 中的某些本地 NTP 服务器,您可能会得到不同的结果。在家附近试试;例如1.us.pool.ntp.org。 2.us.pool.ntp.org等美国境内服务器。