-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetimemonitor.py
More file actions
53 lines (43 loc) · 1.46 KB
/
datetimemonitor.py
File metadata and controls
53 lines (43 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#! /bin/env python3
#datetimemonitor.py
'''
Library of functions that handle
various date and time purposes.
'''
#Imports
import psutil
import time
#Custom Imports
from strfmt import *
#END Custom Imports
'''
Gets the uptime as a string, formatted as
Days, Hours:Minutes:Seconds (Seconds)
'''
def get_uptime(arg=None):
time_now = (arg if (arg is not None and isinstance(arg, int)) else time.time()) #Current time
boot_time = psutil.boot_time() #Boot time
uptime = (time_now - boot_time)
m, s = divmod(uptime, 60) #Mins, Secs
h, m = divmod(m, 60) #Hours, Mins
d, h = divmod(h, 24) #Days, Hours
dt = d #Store total days
w, d = divmod(d, 7) #Weeks, Days
dp = ('' if d == 1 else 's') #Is days plural?
dtp = ('' if dt == 1 else 's') #Total days plural?
wp = ('' if w == 1 else 's') #Weeks plural?
return("{:,.0f} ".format(dt) +
("day%s, %02d:%02d:%02d\n " % (dtp, h, m, s)) +
("({:,.0f} seconds)".format(uptime)) +
("\n (") +
("{:,.0f} week".format(w)) +
("%s and %d day%s)" % (wp, d, dp)))
#Get the current time
def get_localtime(arg=None):
return time.localtime(arg if (arg != None and isinstance(arg, int)) else time.time())
#Get string-formatted current time
def fmt_localtime(arg=None):
return time.strftime("%H:%M:%S", (arg if arg != None and isinstance(arg, int) else get_localtime()))
#Get string-formatted current date
def fmt_date(arg=None):
return time.strftime("%Y-%b-%d, %a", (arg if arg != None and isinstance(arg, int) else get_localtime()))