-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_class.py
More file actions
123 lines (109 loc) · 4.08 KB
/
time_class.py
File metadata and controls
123 lines (109 loc) · 4.08 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from math import floor
class Time:
def __init__(self, hours=0, minutes=0, seconds=0, time_reference=None):
self.hours = int(hours)
self.minutes = int(minutes)
self.seconds = int(seconds)
# If time reference was given, adjust
if time_reference is not None:
self += time_reference
self.update_time()
def __eq__(self, other):
"""For == comparison support"""
if isinstance(other, Time):
return True if self.convert_to_seconds() == other.convert_to_seconds() else False
else:
return NotImplemented
def __gt__(self, other):
"""For > comparison support"""
if isinstance(other, Time):
return True if self.convert_to_seconds() > other.convert_to_seconds() else False
else:
return NotImplemented
def __lt__(self, other):
"""For < comparison support"""
if isinstance(other, Time):
return True if self.convert_to_seconds() < other.convert_to_seconds() else False
else:
return NotImplemented
def __ge__(self, other):
"""For >= comparison support"""
if isinstance(other, Time):
return True if self.convert_to_seconds() >= other.convert_to_seconds() else False
else:
return NotImplemented
def __le__(self, other):
"""For <= comparison support"""
if isinstance(other, Time):
return True if self.convert_to_seconds() <= other.convert_to_seconds() else False
else:
return NotImplemented
def __hash__(self):
"""Make object hashable for use as dictionary key"""
return hash(str(self.convert_to_seconds()))
def __add__(self, other):
"""For + operation support"""
new_time = Time(time_reference=self)
if isinstance(other, Time):
new_time.hours += other.hours
new_time.minutes += other.minutes
new_time.seconds += other.seconds
new_time.update_time()
return new_time
elif isinstance(other, int):
new_time.seconds += other
new_time.update_time()
return new_time
else:
return NotImplemented
def __iadd__(self, other):
"""For += operation support"""
if isinstance(other, Time):
self.hours += other.hours
self.minutes += other.minutes
self.seconds += other.seconds
self.update_time()
return self
elif isinstance(other, int):
self.seconds += other
self.update_time()
return self
else:
return NotImplemented
def __sub__(self, other):
new_time = Time(time_reference=self)
if isinstance(other, Time):
new_time.hours -= other.hours
new_time.minutes -= other.minutes
new_time.seconds -= other.seconds
new_time.update_time()
return new_time
elif isinstance(other, int):
new_time.seconds -= other
new_time.update_time()
return new_time
else:
return NotImplemented
def update_time(self):
"""Function updates time to keep minutes and seconds under 60"""
new_minutes = floor(self.seconds/60)
self.seconds -= new_minutes * 60
self.minutes += new_minutes
new_hours = floor(self.minutes/60)
self.minutes -= new_hours * 60
self.hours += new_hours
return self
def convert_to_seconds(self, time=None):
"""Converts given time to seconds"""
if time is None:
time = self
return time.hours * 3600 + time.minutes * 60 + time.seconds
def convert_to_time(seconds):
"""Converts given seconds to time"""
hours = floor(seconds/3600)
minutes = floor((seconds - hours * 3600)/60)
seconds -= (hours * 3600 + minutes * 60)
return Time(hours, minutes, seconds)
def convert_to_text(self):
"""Method prints time"""
return (f"{self.hours}:{self.minutes}:{self.seconds}")