-
Notifications
You must be signed in to change notification settings - Fork 50
/
MyTime.py
62 lines (48 loc) · 1.8 KB
/
MyTime.py
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
54
55
56
57
58
59
60
61
62
"""
This module will help you to work with time object.
"""
class MyTime:
def __init__(self, hrs=0, mins=0, secs=0):
""" Create a new MyTime object initialized to hrs, mins, secs.
The values of mins and secs may be outside the range 0-59,
but the resulting MyTime object will be normalized.
"""
# Calculate total seconds to represent
totalsecs = hrs * 3600 + mins * 60 + secs
self.hours = totalsecs // 3600 # Split in h, m, s
leftoversecs = totalsecs % 3600
self.minutes = leftoversecs // 60
self.seconds = leftoversecs % 60
def after(self, time2):
""" Return True if I am strictly greater than time2 """
if self.hours > time2.hours:
return True
if self.hours < time2.hours:
return False
if self.minutes > time2.minutes:
return True
if self.minutes < time2.minutes:
return False
if self.seconds > time2.seconds:
return True
return False
def between(self, time1, time2):
return True if time1 < self and self < time2 else False
def to_seconds(self):
""" Return the number of seconds represented
by this instance
"""
return self.hours * 3600 + self.minutes * 60 + self.seconds
def __str__(self):
return str(self.hours).zfill(2) + ":" + str(self.minutes).zfill(2) + ":" + str(self.seconds).zfill(2)
def __gt__(self, t):
t1 = self.to_seconds()
t2 = t.to_seconds()
return True if t1 > t2 else False
def __lt__(self, t):
t1 = self.to_seconds()
t2 = t.to_seconds()
return True if t1 < t2 else False
def add_time(t1, t2):
secs = t1.to_seconds() + t2.to_seconds()
return MyTime(0, 0, secs)