应该使用 NTP 时间服务器的什么时间来设置我的时钟? [英] What time from an NTP time server should be used to set my clock?

查看:36
本文介绍了应该使用 NTP 时间服务器的什么时间来设置我的时钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Python3 和 ntplib

'Ambient' 是 ntplib 报告的偏移量,以毫秒为单位.

看起来我的电脑设置得很好,它的时间大多在 NTP 时间的 5 毫秒内.但是有一些长达 70 毫秒的显着偏移!在一个程序中,我本可以选择这个 - 显然是错误的 - 时间.

明显的问题:如何确保在我的时钟调整中不会出现异常值?

代码的基本部分:

 for i, ntpserver in enumerate(NTP_SERVERS):ntps = []客户端 = ntplib.NTPClient()尝试:response = client.request(ntpserver, version=4, timeout=0.5) # 最新的NTP版本= 4orig = response.orig_timerecv = response.recv_timetx = response.tx_timedest = response.dest_timeoffs = response.offsetntps.append( [orig", orig - orig])ntps.append( [recv", recv - orig])ntps.append( ["tx ", tx - orig])ntps.append( [dest", dest - orig])ntps.append( [偏移量", 偏移量])对于 ntps:打印("{:20s} {:15s} {:10.3f} ".format(ntpserver, a[0], a[1]))除了作为 e 的例外:msg = fncname + "FAILED with Exception: {}".format(e)edprint(味精)

I am using Python3 with the ntplib https://pypi.org/project/ntplib/ to query an NTP time server. Works well, but I am confused as to which of the offered time variables to use to correctly set the clock on my computer.

Reading the IETF document for NTP https://tools.ietf.org/html/rfc5905 on pages 23ff I settled for either:

Transmit Timestamp (xmt): Time at the server when the response left for the client, in NTP timestamp format.

Destination Timestamp (dst): Time at the client when the reply arrived from the server, in NTP timestamp format.

My interpretation is that the xmt is the correct time at the moment the server sent it, which seems to suggest that I would still have to add the time delay for the transmission time from server to my computer?

The dst time definition is unclear to me. It could mean either:

  1. it is the xmt time with the transmission time already added and so is the proper time to use to set my clock
  2. or it is the time of my clock at the arrival of the NTP packet. If my clock were wrong, it would NOT be the proper time to use

Which one is it?

I think that #1 (using dst) makes more sense, but most scripts found on the net use plain xmt. In terms of code for ntplib this means:

    client = ntplib.NTPClient()
    resp   = client.request(server, version=3)
    xmt    = resp.tx_time     # for the use of xmt
    # or:
    dst    = resp.dest_time   # for the use of dst

In some test runs dst was always later than xmt by 3 ... 30 ms, with no obvious pattern on the use of a local, regional, or global NTP server.

So it is not much, but I don't want to make an illogical choice.

解决方案

The knowledge of all the various timestamps is not very helpful for the understanding of how to change my computer's time to agree with the NTP time server.

After some days of fiddling I believe I found the solution. And the answer is: Don't use any of the time stamps - instead use only the offset! And the offset is already calculated for you by the ntplib.

This is surprising, since most people use one of the time stamps (mostly xmt), and, I may have missed it, but I have never seen anyone using the offset.

Let's look at an example. I have used 3 NTP server: one in Asia (most remote), one in Europe (medium distance), and one in Germany (closest). The round trip times for the data packages are quite variable, ranging from a few ms to 200ms, independent of close or distant targets. I picked one example to demonstrate my point (code at the end). From the timestamps I subtracted the orig, so all times are relative to their respective orig.

    NTP Server            Type t-stamp          sec 
    asia.pool.ntp.org     orig                  0.000
    asia.pool.ntp.org     recv                  0.004
    asia.pool.ntp.org     tx                    0.004
    asia.pool.ntp.org     dest                  0.014
    asia.pool.ntp.org     offset               -0.003

    europe.pool.ntp.org   orig                  0.000
    europe.pool.ntp.org   recv                  0.008
    europe.pool.ntp.org   tx                    0.008
    europe.pool.ntp.org   dest                  0.020
    europe.pool.ntp.org   offset               -0.002

    de.pool.ntp.org       orig                  0.000
    de.pool.ntp.org       recv                  0.008
    de.pool.ntp.org       tx                    0.008
    de.pool.ntp.org       dest                  0.019
    de.pool.ntp.org       offset               -0.002

In the Asia example total round trip takes 14 ms. The clock difference is 4ms in sending, and 10ms in receiving. Thus the offset is (4 - 10)/2 = -3ms.

The all important assumption to make now is that the travel times to and from are the same! Then I can say if I added this offset to my computer times, both times would become the same!

Same works for Europe: round trip 20ms, offset: (8 - 12)/2 = -2ms, and Germany: round trip 19ms, offset:(8 - 11)/2 = -1.5ms.

As the offset is already given by the lib, so all you do is add(!, not subtract) this signed offset to your computer's time.

I have then recorded the ntplib-offsets while using the German (the closest) NTP server for about half hour:

'Ambient' is the offset reported by the ntplib in ms.

It looks like my computer is well set with its time mostly within 5ms of NTP time. But there are some significant excursions up to 70ms! In a program I could have chosen this - obviously wrong - time.

Obvious question: how can I make sure to not get an outlier in my clock adjustment?

The essential part of the code:

    for i, ntpserver in enumerate(NTP_SERVERS):
    ntps     = []
    client   = ntplib.NTPClient()
    try:
        response = client.request(ntpserver, version=4, timeout=0.5) # latest NTP version = 4

        orig = response.orig_time
        recv = response.recv_time
        tx   = response.tx_time
        dest = response.dest_time
        offs = response.offset
        ntps.append( ["orig"     , orig - orig])
        ntps.append( ["recv"     , recv - orig])
        ntps.append( ["tx  "     , tx   - orig])
        ntps.append( ["dest"     , dest - orig])
        ntps.append( ["offset"   , offs])
       
        for  a in ntps:
            print("{:20s}  {:15s}  {:10.3f} ".format(ntpserver, a[0], a[1]))
       
    except Exception as e:
        msg = fncname + "FAILED with Exception: {}".format(e)
        edprint(msg)

这篇关于应该使用 NTP 时间服务器的什么时间来设置我的时钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆