Skip to content

Commit

Permalink
#989 / windows / boot_time(): try to return the correct C type in ord…
Browse files Browse the repository at this point in the history
…er to avoid negative values
  • Loading branch information
giampaolo committed Jun 7, 2017
1 parent 7eb1581 commit ec1d35e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Expand Up @@ -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.
Expand Down
12 changes: 8 additions & 4 deletions psutil/_psutil_windows.c
Expand Up @@ -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
Expand All @@ -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);
}


Expand Down
2 changes: 1 addition & 1 deletion psutil/_pswindows.py
Expand Up @@ -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:
Expand Down

0 comments on commit ec1d35e

Please sign in to comment.