forked from psounis/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise08.py
More file actions
55 lines (39 loc) · 1.34 KB
/
exercise08.py
File metadata and controls
55 lines (39 loc) · 1.34 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
class Time:
def __init__(self, hour, minute, second):
self.hour = self.__validate(hour,0,23)
self.minute = self.__validate(minute,0,59)
self.second = self.__validate(second,0,59)
def set_hour(self, hour):
self.hour = self.__validate(hour,0,23)
def set_minute(self, minute):
self.minute = self.__validate(minute,0,59)
def set_second(self, second):
self.second = self.__validate(second,0,59)
def __validate(self, val, low, upp):
if low <= val <= upp:
return val
else:
return 0
def total_seconds(self):
return self.hour * 3600 + self.minute * 60 + self.second
def print(self):
print(f"{str(self.hour).zfill(2)}:{str(self.minute).zfill(2)}:{str(self.second).zfill(2)}")
def next_second(self):
second = self.second + 1
carry = second // 60
second = second % 60
minute = self.minute + carry
carry = minute // 60
minute = minute % 60
hour = (self.hour + carry) % 24
return Time(hour, minute, second)
t = Time(1,45,110)
t.print()
t1 = Time(11,12,13)
t1.next_second().print()
t1 = Time(11,12,59)
t1.next_second().print()
t1 = Time(11,59,59)
t1.next_second().print()
t1 = Time(23,59,59)
t1.next_second().print()