diff --git a/HISTORY.rst b/HISTORY.rst index bc535643b..c61181a29 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -28,6 +28,7 @@ **Bug fixes** +- 989_: [Windows] boot_time() may return a negative value. - 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. diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index bec2d3aa9..74e7cfef0 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -235,8 +235,12 @@ psutil_boot_time(PyObject *self, PyObject *args) { and 01-01-1601, from time_t the divide by 1e+7 to get to the same base granularity. */ - ll = (((LONGLONG)(fileTime.dwHighDateTime)) << 32) \ - + fileTime.dwLowDateTime; +#if (_WIN32_WINNT >= 0x0600) // Windows Vista + ll = (((ULONGLONG) +#else + ll = (((LONGLONG) +#endif + (fileTime.dwHighDateTime)) << 32) + fileTime.dwLowDateTime; pt = (time_t)((ll - 116444736000000000ull) / 10000000ull); // GetTickCount64() is Windows Vista+ only. Dinamically load @@ -249,15 +253,15 @@ psutil_boot_time(PyObject *self, PyObject *args) { if (psutil_GetTickCount64 != NULL) { // Windows >= Vista uptime = psutil_GetTickCount64() / (ULONGLONG)1000.00f; + return Py_BuildValue("K", pt - uptime); } else { // Windows XP. // GetTickCount() time will wrap around to zero if the // system is run continuously for 49.7 days. uptime = GetTickCount() / (LONGLONG)1000.00f; + return Py_BuildValue("L", pt - uptime); } - - return Py_BuildValue("d", (double)pt - (double)uptime); } diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py index 52676183d..80225ee9e 100644 --- a/psutil/_pswindows.py +++ b/psutil/_pswindows.py @@ -410,7 +410,7 @@ def boot_time(): # value which may have a 1 second fluctuation, see: # https://github.com/giampaolo/psutil/issues/1007 global _last_btime - ret = cext.boot_time() + ret = float(cext.boot_time()) if abs(ret - _last_btime) <= 1: return _last_btime else: