summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-05-08 03:15:21 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2017-05-08 03:15:21 +0200
commit49ce1f6de1532fb2be4301f3a62699340cd06a2d (patch)
tree3967d443e73246839c789b669b9c8db2adfe074f
parent3e06eee00d3717592336191113f3f82c9832ddce (diff)
downloadpsutil-49ce1f6de1532fb2be4301f3a62699340cd06a2d.tar.gz
#1007 / boot_time() / win: consider 1 sec fluctuation between calls acceptable and return always the same value
-rw-r--r--HISTORY.rst3
-rw-r--r--psutil/_pswindows.py14
-rwxr-xr-xpsutil/tests/test_windows.py11
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()