开发者

Find offset between system-time and Internet time using Python

开发者 https://www.devze.com 2023-03-18 00:25 出处:网络
How would y开发者_StackOverflowou find the time offset between the local OS system-time and Internet time from various Internet time sources using Python?Use ntplib. Right from the manual:

How would y开发者_StackOverflowou find the time offset between the local OS system-time and Internet time from various Internet time sources using Python?


Use ntplib. Right from the manual:

>>> import ntplib
>>> c = ntplib.NTPClient()
>>> response = c.request('europe.pool.ntp.org', version=3)
>>> response.offset
-0.143156766891


Just to save you some time. Here's the code I ended up with using phihag's answer. It prints the drift every interval_sec to screen and to a log file.
You'll need to easy_install ntplib for it to work.

import logging
logging.basicConfig(filename='time_shift.txt',level=logging.DEBUG)

import ntplib
import time
import datetime
c = ntplib.NTPClient()

interval_sec = 60
while True:
    try:
        response = c.request('europe.pool.ntp.org', version=3)
        txt = '%s     %.3f' % (datetime.datetime.now().isoformat(), response.offset)
        print txt
        logging.info(txt)
    except:
        pass
    time.sleep(interval_sec)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号