mirror of
https://github.com/KevinMidboe/Node-Com-Handler.git
synced 2025-10-29 17:50:27 +00:00
Added better comments and cleaned up by making seperate converting functions and some small error handling.
This commit is contained in:
37
v1/uptime.py
37
v1/uptime.py
@@ -3,34 +3,37 @@
|
|||||||
# @Author: KevinMidboe
|
# @Author: KevinMidboe
|
||||||
# @Date: 2017-01-27 19:48:42
|
# @Date: 2017-01-27 19:48:42
|
||||||
# @Last Modified by: KevinMidboe
|
# @Last Modified by: KevinMidboe
|
||||||
# @Last Modified time: 2017-01-28 13:30:19
|
# @Last Modified time: 2017-01-31 21:02:21
|
||||||
|
|
||||||
from psutil import boot_time
|
from psutil import boot_time
|
||||||
from time import time
|
from time import time
|
||||||
|
|
||||||
|
def secToDay(seconds):
|
||||||
|
days = int(seconds/86400)
|
||||||
|
if days is 1:
|
||||||
|
return str(days)+' day'
|
||||||
|
else:
|
||||||
|
return str(days)+' days'
|
||||||
|
|
||||||
|
def secToHour(seconds):
|
||||||
|
hours = (seconds)//3600
|
||||||
|
minutes = (seconds - hours*3600)//60
|
||||||
|
hourMinutes = '%02d' % hours + +':'+ '%02d' % minutes
|
||||||
|
|
||||||
def timeSinceBoot():
|
def timeSinceBoot():
|
||||||
# Use psutil 'boot_time' to get seconds since start
|
bootTime = boot_time() # Use psutil 'boot_time' to get seconds since start
|
||||||
bootTime = boot_time()
|
currTime = time() # Use 'time()' to get seconds currently
|
||||||
# Use 'time()' to get seconds currently
|
deltaSeconds = int(currTime-bootTime)
|
||||||
currTime = time()
|
|
||||||
delta = int(currTime-bootTime)
|
|
||||||
|
|
||||||
# Return in day format
|
# Return in day format
|
||||||
if delta >= 86400:
|
if deltaSeconds >= 86400:
|
||||||
# TODO error handling
|
# TODO error handling
|
||||||
rt = int(delta/86400)
|
return secToDay(deltaSeconds)
|
||||||
if rt is 1:
|
|
||||||
return str(rt)+' day'
|
|
||||||
else:
|
|
||||||
return str(rt)+' days'
|
|
||||||
|
|
||||||
# Return in hour format
|
# Return in hour format
|
||||||
elif delta < 86400 and delta >= 0:
|
elif deltaSeconds < 86400 and deltaSeconds >= 0:
|
||||||
# TODO error handling
|
# TODO error handling
|
||||||
hours = (delta)//3600
|
return secToHour(deltaSeconds)
|
||||||
minutes = (delta - hours*3600)//60
|
|
||||||
rt = '%02d' % hours +':'+ '%02d' % minutes
|
|
||||||
return rt
|
|
||||||
else:
|
else:
|
||||||
# Throw error
|
# Throw error
|
||||||
return 'Null'
|
return 'Null'
|
||||||
|
|||||||
Reference in New Issue
Block a user