fdzworkzone fdzworkzone - 1 year ago 107
Python Question

Getting startup time in Windows 10 doesn't give always the same time

I'm using this python (3.5) code to check windows startup time but it gives me a different time from the time I get using the shell command "systeminfo"

  • systeminfo: 9:15:23

  • python code: 9:14:42

Not happy with that, the python code is giving different times in itself!
It started giving me 9:14:42, and right now it gives me 9:15:19, it's been adding a second to the boot time every 10 seconds or so.

I give you the code:

import datetime
import win32api

s = win32api.GetTickCount()
t = datetime.datetime.now()
st = datetime.timedelta(milliseconds=s)
bt = t-st
boot = "Boot time was around {}:{}:{}".format(bt.hour, bt.minute, bt.second)

I'm starting to think about calling the systeminfo command from python and just show the time it gives me.

Answer Source

taken from comments and question edit

GetTickCount does not report the startup time. To get that you should be using WMI

import datetime
import wmi

wmiob = wmi.WMI()
sdata = wmiob.Win32_PerfFormattedData_PerfOS_System()
uptime = sdata[-1].SystemUpTime
tnow = datetime.datetime.now()
utime = datetime.timedelta(seconds=int(uptime))
boot = tnow-utime
bootime = "Boot time was around {}:{}:{}".format(boot.hour, boot.minute, boot.second)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download