-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3cae804
commit fa2f9c4
Showing
1 changed file
with
34 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |