Skip to content

Commit

Permalink
Print pretty uptime
Browse files Browse the repository at this point in the history
  • Loading branch information
aluminiumgeek committed Mar 29, 2016
1 parent 3cae804 commit fa2f9c4
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions modules/user_uptime.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
from datetime import timedelta
import os


def main(bot, *args, **kwargs):
"""
uptime
Print system uptime
Show system uptime
"""

with open('/proc/uptime', 'r') as f:
uptime_seconds = float(f.readline().split()[0])
return str(timedelta(seconds=uptime_seconds))
try:
f = open("/proc/uptime")
contents = f.read().split()
except:
return "Cannot open uptime file: /proc/uptime"
finally:
f.close()

total_seconds = float(contents[0])

# Helper vars:
MINUTE = 60
HOUR = MINUTE * 60
DAY = HOUR * 24

# Get the days, hours, etc:
days = int(total_seconds / DAY)
hours = int((total_seconds % DAY ) / HOUR)
minutes = int((total_seconds % HOUR ) / MINUTE)
seconds = int(total_seconds % MINUTE)

# Build up the pretty string (like this: "N days, N hours, N minutes, N seconds")
result = ''
if days > 0:
result += '{} {}, '.format(days, days == 1 and "day" or "days")
if result or hours > 0:
result += '{} {}, '.format(hours, hours == 1 and "hour" or "hours")
if result or minutes > 0:
result += '{} {}, '.format(minutes, minutes == 1 and "minute" or "minutes")
result += '{} {}'.format(seconds, seconds == 1 and "second" or "seconds")

return result

0 comments on commit fa2f9c4

Please sign in to comment.