How to ntp server time down to millisecond precision using Python ntplib?

2024/11/14 14:48:08

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))
Answer

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"))
https://en.xdnf.cn/q/72073.html

Related Q&A

Control 2 separate Excel instances by COM independently... can it be done?

Ive got a legacy application which is implemented in a number of Excel workbooks. Its not something that I have the authority to re-implement, however another application that I do maintain does need t…

nested Python numpy arrays dimension confusion

Suppose I have a numpy array c constructed as follows:a = np.zeros((2,4)) b = np.zeros((2,8)) c = np.array([a,b])I would have expected c.shape to be (2,1) or (2,) but instead it is (2,2). Additionally,…

how to reuse tests written using unittest.testcase

Ive written some tests using unittest as below and I want to reuse them in another class where Im stuck and need help.. Code snippets are as below.MyTestClass.pyClass MyTestClass(unittest.TestCase): …

Randomized stratified k-fold cross-validation in scikit-learn?

Is there any built-in way to get scikit-learn to perform shuffled stratified k-fold cross-validation? This is one of the most common CV methods, and I am surprised I couldnt find a built-in method to …

Find all paths through a tree (nested dicts) from top to bottom

EDIT: See below for a suggested answer and how its not quite right yet.There are many similar questions to this one on Stack Overflow, but none exactly like it in Python. Im a programming novice, so pl…

How to show process state (blocking, non-blocking) in Linux

Is there a way to query the state of processes in a Linux process table to be able to demonstrate if a process is running or blocked at the time the query is executed? My goal is to do this from outsi…

Removing quotation marks from list items

I am running this program:f = open( "animals.txt", "r") g = f.read() g1 = g.split(,) #turning the file into list print g1And I want this to come out:[ELDEN, DORSEY, DARELL, BRODERIC…

Handle multiple questions for Telegram bot in python

Im programming a telegram bot in Python using the Telegram bot API. Im facing the problem of managing questions that need an answer of the user. The problem arises when the program is waiting for an an…

Which GTK+ elements support which CSS properties?

While applying my own CSS to my GTK+ application, I noticed, that some elements ignore some CSS properties and others ignore others or dont ignore them, which leads me to search for an overview of whic…

Self import of subpackages or not?

Suppose you have the following b b/__init__.py b/c b/c/__init__.py b/c/d b/c/d/__init__.pyIn some python packages, if you import b, you only get the symbols defined in b. To access b.c, you have to exp…