-
Notifications
You must be signed in to change notification settings - Fork 0
/
Time.py
109 lines (87 loc) · 2.75 KB
/
Time.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
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
from multidispatch import *
class Time(metaclass=MultipleMeta):
def __init__(self, got_time : str):
self.hour = 0
self.minute = 0
self.type = ""
size = len(got_time)
thour = ""
tminute = ""
ttype = got_time[size-2: size]
self.set_type(ttype)
is_check = 0
for i in range(0, size - 2):
if got_time[i] == ':' or got_time[i] == ' ':
is_check = 1
continue
if is_check :
tminute += got_time[i]
else :
thour += got_time[i]
self.set_hour(int(thour))
self.set_minute(int(tminute))
def __init__(self):
self.hour = 0
self.minute = 0
self.type = "AM"
def __init__(self, hour:int, minute:int, type:str):
self.hour = 0
self.minute = 0
self.type = ""
self.set_type(type)
self.set_hour(hour)
self.set_minute(minute)
def set_hour(self,hour : int):
if hour <= 12 and hour >= 0 :
self.hour = hour;
else :
raise ValueError("Invalid hour value")
def set_minute(self, minute : int):
if minute <= 60 and minute >= 0 :
self.minute = minute;
else :
raise ValueError("Invalid minute value")
def set_type(self, type : str):
if type.lower() == "am" or type.lower() == "pm":
self.type = type;
else :
raise ValueError(f"{type} Invalid Type value")
def __iadd__(self, number : int):
if(not isinstance(number, int)):
try:
number = int(number)
except:
print(number)
raise ValueError("not an int")
self.minute += number
if self.minute >= 60 :
count = int(self.minute / 60)
self.minute %= 60
self.hour += count
if self.hour == 12 :
if self.type.lower() == "am":
self.type = "PM"
elif self.type.lower() == "pm":
self.type = "AM"
elif self.hour > 12 :
self.hour %= 12
return self
def __add__(self, min:int):
t1 = Time()
t1.set_hour(self.get_hour())
t1.set_minute(self.get_minute())
t1.set_type(self.get_type())
t1 += min
return t1
def __str__(self):
time = f"{self.hour}"
if self.minute != 0 :
time = time + f":{self.minute} "
time = time + f" {self.type}"
return time
def get_hour(self) :
return self.hour
def get_minute(self) :
return self.minute
def get_type(self):
return self.type