I wrote a desktop application and was using datetime.datetime.utcnow()
for timestamping, however I've recently noticed that some people using the application get wildly different results than I do when we run the program at the same time. Is there any way to get the UTC time locally without using urllib to fetch it from a website?
Asked
Active
Viewed 1.3k times
13

tslocum
- 3,422
- 5
- 30
- 33
2 Answers
24
Python depends on the underlying operating system to provide an accurate time-of-day clock. If it isn't doing that, you don't have much choice other than to bypass the o/s. There's a pure-Python implementation of an NTP client here. A very simple-minded approach:
>>> import ntplib,datetime
>>> x = ntplib.NTPClient()
>>> datetime.datetime.utcfromtimestamp(x.request('europe.pool.ntp.org').tx_time)
datetime.datetime(2009, 10, 21, 7, 1, 54, 716657)
However, it would not be very nice to be continually hitting on other NTP servers out there. A good net citizen would use the ntp client library to keep track of the offset between the o/s system clock and that obtained from the server and only periodically poll to adjust the time.

Liran Orevi
- 4,755
- 7
- 47
- 64

Ned Deily
- 83,389
- 16
- 128
- 151
-
And with monkeypatching, you could record that offset, and change the datetime library method to apply the offset whenever some part of the application asks for the time. It would be nice if the time and datetime library would handle this kind of offset automatically. You could tell it that you want real world time, it would hit an NTP server, record the offset, and apply it every time that time is used. – Michael Dillon Oct 21 '09 at 08:33
-
2This doesn't account for "round-trip-time" of making the request. What you want to do is use `time.clock()` to measure how long `NTPClient()` took to respond, split that in half and add that to the given time. That's accurate. – S.Lott Oct 21 '09 at 10:32
7
Actually, ntplib computes this offset accounting for round-trip delay. It's available through the "offset" attribute of the NTP response. Therefore the result should not very wildly.

NoDataDumpNoContribution
- 10,591
- 9
- 64
- 104

neologix
- 71
- 1
-
1It is a useful comment but it is not an answer. You should move it into the comments above. – jfs Nov 26 '15 at 14:36
-
I think it provides kind of an answer in the way that the observation of the OP (wildly different results) may be wrong. And how does anyone wants to get UTC time locally without having an atomic clock? – NoDataDumpNoContribution Nov 26 '15 at 22:09
-
@Trilarion: I don't see anything unusual that different computers return different time. Even [if the time is synchronized using ntp; it may differ by tens of milliseconds on the internet](http://stackoverflow.com/a/97939/4279). You don't need a local atomic clock, to get UTC time (that is not known in real time anyway if you want tens of nanoseconds precision) with milli-,microseconds precision, you could use GPS (e.g., via the same ntp software). For scale: [relativistic effects are microseconds per day here](http://www.astronomy.ohio-state.edu/~pogge/Ast162/Unit5/gps.html) – jfs Nov 28 '15 at 10:06