diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-08 04:31:31 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-08 04:31:31 +0200 |
commit | 53acede12aa46e30d7893718d2383271f140b7a1 (patch) | |
tree | a712a153112d53b51e296a70eb1f4bb358b0b713 | |
parent | ef026ada97a4faadd5658472100c5e68b869d962 (diff) | |
parent | 49ce1f6de1532fb2be4301f3a62699340cd06a2d (diff) | |
download | psutil-53acede12aa46e30d7893718d2383271f140b7a1.tar.gz |
Merge branch 'master' of github.com:giampaolo/psutil
-rw-r--r-- | HISTORY.rst | 3 | ||||
-rw-r--r-- | psutil/_pswindows.py | 14 | ||||
-rwxr-xr-x | psutil/tests/test_windows.py | 11 |
3 files changed, 27 insertions, 1 deletions
diff --git a/HISTORY.rst b/HISTORY.rst index c1ad8d84..bf7f6367 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -20,6 +20,9 @@ **Bug fixes** +- 1007_: [Windows] boot_time() can have a 1 sec fluctuation between calls; the + value of the first call is now cached so that boot_time() always returns the + same value if fluctuation is <= 1 second. - 1014_: [Linux] Process class can mask legitimate ENOENT exceptions as NoSuchProcess. - 1016_: disk_io_counters() raises RuntimeError on a system with no disks. diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py index 4b27557c..bd6b091f 100644 --- a/psutil/_pswindows.py +++ b/psutil/_pswindows.py @@ -401,9 +401,21 @@ def sensors_battery(): # ===================================================================== +_last_btime = 0 + + def boot_time(): """The system boot time expressed in seconds since the epoch.""" - return cext.boot_time() + # This dirty hack is to adjust the precision of the returned + # value which may have a 1 second fluctuation, see: + # https://github.com/giampaolo/psutil/issues/1007 + global _last_btime + ret = cext.boot_time() + if abs(ret - _last_btime) <= 1: + return _last_btime + else: + _last_btime = ret + return ret def users(): diff --git a/psutil/tests/test_windows.py b/psutil/tests/test_windows.py index ac787283..a4f32315 100755 --- a/psutil/tests/test_windows.py +++ b/psutil/tests/test_windows.py @@ -7,6 +7,7 @@ """Windows specific tests.""" +import datetime import errno import glob import os @@ -179,6 +180,16 @@ class TestSystemAPIs(unittest.TestCase): self.assertTrue(ps_names & wmi_names, "no common entries in %s, %s" % (ps_names, wmi_names)) + def test_boot_time(self): + wmi_os = wmi.WMI().Win32_OperatingSystem() + wmi_btime_str = wmi_os[0].LastBootUpTime.split('.')[0] + wmi_btime_dt = datetime.datetime.strptime( + wmi_btime_str, "%Y%m%d%H%M%S") + psutil_dt = datetime.datetime.fromtimestamp(psutil.boot_time()) + diff = abs((wmi_btime_dt - psutil_dt).total_seconds()) + # Wmic time is 2 secs lower for some reason; that's OK. + self.assertLessEqual(diff, 2) + # =================================================================== # sensors_battery() |