I am creating a python module that will output the time from a selection of NTP Pool servers to millisecond precision as an exercise in showing how server timestamps vary. Thus far I have been able to print out the origin server timestamp to within a second precision but how can I get the millisecond precision?
ntp_pool = '0.pool.ntp.org', \'uk.pool.ntp.org', \'ie.pool.ntp.org'def get_ntp_time():for item in ntp_pool:call = ntplib.NTPClient()response = call.request(item, version=3)print(time.ctime(response.orig_time))
The for loop is likely to colour your results since there is time passing in each iteration.
In any case, the ntp response is a timestamp with microsecond accuracy, so the limitation seems to be within time.ctime
, which only goes down to second-accuracy
You could use datetime.fromtimestamp
instead, and optionally also strftime
to make it prettier. My example half-heartedly mimics the output of your existing code.
from datetime import datetimedef get_ntp_time():
for item in ntp_pool:call = ntplib.NTPClient()response = call.request(item, version=3)t = datetime.fromtimestamp(response.orig_time)print(t.strftime("%a %b %d %H:%M:%S.%f"))