get system boot time in milliseconds in python

You can use os.path.getmtime() to get the timestamp of the system boot time and then subtract it from the current timestamp to get the time elapsed since boot in seconds. You can then multiply it by 1000 to get the time elapsed in milliseconds.

Here's the code to get system boot time in milliseconds:

main.py
import datetime
import os

boot_time_timestamp = os.path.getmtime('/proc/stat')
boot_time = datetime.datetime.fromtimestamp(boot_time_timestamp)

uptime = datetime.datetime.now() - boot_time
milliseconds_since_boot = int(uptime.total_seconds() * 1000)

print(milliseconds_since_boot)
284 chars
11 lines

gistlibby LogSnag