This type of system information on Linux is often available through D-Bus so I listed all of the D-Bus connections with time in them:
pi@raspberrypi:~ $ busctl | grep time
:1.1 359 systemd-timesyn systemd-timesync :1.1 systemd-timesyncd.service - -
org.freedesktop.timedate1 - - - (activatable) - -
org.freedesktop.timesync1 359 systemd-timesyn systemd-timesync :1.1 systemd-timesyncd.service - -
And I can then do introspection on them to find methods, properties and their values:
Firstly .timedate1
:
pi@raspberrypi:~ $ busctl introspect org.freedesktop.timedate1 /org/freedesktop/timedate1
NAME TYPE SIGNATURE RESULT/VALUE FLAGS
org.freedesktop.DBus.Introspectable interface - - -
.Introspect method - s -
org.freedesktop.DBus.Peer interface - - -
.GetMachineId method - s -
.Ping method - - -
org.freedesktop.DBus.Properties interface - - -
.Get method ss v -
.GetAll method s a{sv} -
.Set method ssv - -
.PropertiesChanged signal sa{sv}as - -
org.freedesktop.timedate1 interface - - -
.ListTimezones method - as -
.SetLocalRTC method bbb - -
.SetNTP method bb - -
.SetTime method xbb - -
.SetTimezone method sb - -
.CanNTP property b true -
.LocalRTC property b false emits-change
.NTP property b true emits-change
.NTPSynchronized property b true -
.RTCTimeUSec property t 0 -
.TimeUSec property t 1601112272495265 -
.Timezone property s "Europe/London" emits-change
And then on .timesync1
:
pi@raspberrypi:~ $ busctl introspect org.freedesktop.timesync1 /org/freedesktop/timesync1
NAME TYPE SIGNATURE RESULT/VALUE FLAGS
org.freedesktop.DBus.Introspectable interface - - -
.Introspect method - s -
org.freedesktop.DBus.Peer interface - - -
.GetMachineId method - s -
.Ping method - - -
org.freedesktop.DBus.Properties interface - - -
.Get method ss v -
.GetAll method s a{sv} -
.Set method ssv - -
.PropertiesChanged signal sa{sv}as - -
org.freedesktop.timesync1.Manager interface - - -
.FallbackNTPServers property as 4 "0.debian.pool.ntp.org" "1.debian.poo… const
.Frequency property x -9162 -
.LinkNTPServers property as 0 -
.NTPMessage property (uuuuittayttttbtt) 0 4 4 2 -21 137 14465 4 129 215 32 239 … emits-c
.PollIntervalMaxUSec property t 2048000000 const
.PollIntervalMinUSec property t 32000000 const
.PollIntervalUSec property t 2048000000 -
.RootDistanceMaxUSec property t 5000000 const
.ServerAddress property (iay) 2 4 129 215 160 240 -
.ServerName property s "2.debian.pool.ntp.org" -
.SystemNTPServers property as 0 const
Looking at the outputs, I think timedate1
is the one that will give you the information you require.
To access this information from Python, I find the pydbus library to be very helpful. Here is an example of doing that:
(venv) pi@raspberrypi:~/stack_overflow $ python3
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pydbus
>>> timedated = pydbus.SystemBus().get(".timedate1")
>>> dir(timedated)
['CanNTP', 'Get', 'GetAll', 'GetMachineId', 'Introspect', 'ListTimezones', 'LocalRTC', 'NTP', 'NTPSynchronized', 'Ping', 'PropertiesChanged', 'RTCTimeUSec', 'Set', 'SetLocalRTC', 'SetNTP', 'SetTime', 'SetTimezone', 'TimeUSec', 'Timezone', '_Introspect', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_bus', '_bus_name', '_object', '_path', 'onPropertiesChanged']
>>> timedated.NTPSynchronized # Example of access properties
True
>>> timedated.ListTimezones() # Example of accessing methods
['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', 'Africa/Asmara', 'Africa/Bamako', 'Afr...
And for completeness, here are the OS details of the system I ran on:
pi@raspberrypi:~ $ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
Stretch
doesn't usentpd
/etc/os-release
will show release, but you should tag your question with OScat
returns Raspbian GNU/Linux 8 (jessie)os.system()
?lsb_release -irc
works nicely as well.