6.17. boing.net.ntp — NTP utilities

The module boing.net.ntp provides few functions for handling the Network Time Protocol (NTP).

boing.net.ntp.ntpEncode(t)

Return the bytes object obtained from encoding the POSIX timestamp t.

boing.net.ntp.ntpDecode(data)

Return the POSIX timestamp obtained from decoding the bytes object data.

boing.net.ntp.ntpFromServer(server)

Send an ntp time query to the server and return the obtained POSIX timestamp. The request is sent by using an UDP connection to the port 123 of the NTP server.

boing.net.ntp.ntp2datetime(t)

Return the datetime.datetime instance corresponding to the POSIX timestamp t.

boing.net.ntp.datetime2ntp(dt)

Return the POSIX timestamp corresponding to the datetime.datetime instance dt.

Usage example:

>>> import datetime
>>> import boing.net.ntp as ntp
>>> srvtime = ntp.ntpFromServer("europe.pool.ntp.org")
>>> srvdatetime = ntp.ntp2datetime(srvtime)
>>> now = datetime.datetime.now()
>>> print("Server time:", srvdatetime)
Server time: 2012-10-16 16:39:12.332479
>>> print("Local time:", now)
Local time: 2012-10-16 16:40:23.772048
>>> print("Delta:", now - srvdatetime)
Delta: 0:01:11.439569

Previous topic

6.16. boing.net.udp — UDP utilities

Next topic

6.18. boing.utils — Common utilities