From 261b3afa013595db663e25310a1598ef21749fa3 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 31 Jan 2017 21:43:45 +0100 Subject: [PATCH] Added better comments and cleaned up by making seperate converting functions and some small error handling. --- v1/uptime.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/v1/uptime.py b/v1/uptime.py index eadcfa7..e0a9591 100755 --- a/v1/uptime.py +++ b/v1/uptime.py @@ -3,34 +3,37 @@ # @Author: KevinMidboe # @Date: 2017-01-27 19:48:42 # @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 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(): - # Use psutil 'boot_time' to get seconds since start - bootTime = boot_time() - # Use 'time()' to get seconds currently - currTime = time() - delta = int(currTime-bootTime) + bootTime = boot_time() # Use psutil 'boot_time' to get seconds since start + currTime = time() # Use 'time()' to get seconds currently + deltaSeconds = int(currTime-bootTime) # Return in day format - if delta >= 86400: + if deltaSeconds >= 86400: # TODO error handling - rt = int(delta/86400) - if rt is 1: - return str(rt)+' day' - else: - return str(rt)+' days' + return secToDay(deltaSeconds) # Return in hour format - elif delta < 86400 and delta >= 0: + elif deltaSeconds < 86400 and deltaSeconds >= 0: # TODO error handling - hours = (delta)//3600 - minutes = (delta - hours*3600)//60 - rt = '%02d' % hours +':'+ '%02d' % minutes - return rt + return secToHour(deltaSeconds) else: # Throw error return 'Null'