|
| 1 | +class Time: |
| 2 | + def __init__(self, hour, minute, second): |
| 3 | + self.hour = hour |
| 4 | + self.minute = minute |
| 5 | + self.second = second |
| 6 | + |
| 7 | + def __str__(self): |
| 8 | + return f"{str(self.hour).zfill(2)}:" \ |
| 9 | + f"{str(self.minute).zfill(2)}:" \ |
| 10 | + f"{str(self.second).zfill(2)}" |
| 11 | + |
| 12 | + def __gt__(self, other): |
| 13 | + if self.hour > other.hour: |
| 14 | + return True |
| 15 | + elif self.hour == other.hour: |
| 16 | + if self.minute > other.minute: |
| 17 | + return True |
| 18 | + elif self.minute == other.minute: |
| 19 | + if self.second > other.second: |
| 20 | + return True |
| 21 | + return False |
| 22 | + |
| 23 | + def __ge__(self, other): |
| 24 | + if self.hour > other.hour: |
| 25 | + return True |
| 26 | + elif self.hour == other.hour: |
| 27 | + if self.minute > other.minute: |
| 28 | + return True |
| 29 | + elif self.minute == other.minute: |
| 30 | + if self.second >= other.second: |
| 31 | + return True |
| 32 | + return False |
| 33 | + |
| 34 | + def __eq__(self, other): |
| 35 | + if isinstance(other, Time): |
| 36 | + if self.hour == other.hour and self.minute == other.minute and self.second == other.second: |
| 37 | + return True |
| 38 | + return False |
| 39 | + elif isinstance(other, int): |
| 40 | + print(Time(other,0,0)) |
| 41 | + return self == Time(other, 0, 0) |
| 42 | + |
| 43 | + def __add__(self, other): |
| 44 | + if isinstance(other, Time): |
| 45 | + carry = (self.second + other.second) // 60 |
| 46 | + new_second = (self.second + other.second) % 60 |
| 47 | + carry = (self.minute + other.minute + carry) // 60 |
| 48 | + new_minute = (self.minute + other.minute + carry) % 60 |
| 49 | + new_hour = (self.hour + other.hour + carry) % 24 |
| 50 | + return Time(new_hour, new_minute, new_second) |
| 51 | + elif isinstance(other, int): |
| 52 | + return Time((self.hour + other)%24, self.minute, self.second) |
| 53 | + |
| 54 | + def __radd__(self, other): |
| 55 | + return self.__add__(other) |
| 56 | + |
| 57 | + |
| 58 | +print(Time(7,44,55) + Time(10,22,5)) |
| 59 | +print(Time(7,44,55) + 19) |
| 60 | +print(19 + Time(7,44,55)) |
| 61 | + |
0 commit comments