Python: clock

This commit is contained in:
Dmitry Kokorin 2021-06-11 16:54:11 +03:00
parent c3f7675134
commit 399d240948
4 changed files with 250 additions and 0 deletions

24
python/clock/clock.py Normal file
View file

@ -0,0 +1,24 @@
HOURS_IN_DAY = 24
MINUTES_IN_HOUR = 60
MINUTES_IN_DAY = HOURS_IN_DAY*MINUTES_IN_HOUR
class Clock:
def __init__(self, hour=0, minute=0):
self.minutes = (MINUTES_IN_HOUR*hour + minute) % MINUTES_IN_DAY
def __repr__(self):
hours = self.minutes // MINUTES_IN_HOUR
minutes = self.minutes % MINUTES_IN_HOUR
return f'{hours:02}:{minutes:02}'
def __eq__(self, other):
return self.minutes == other.minutes
def __add__(self, minutes):
clock = Clock()
clock.minutes = (self.minutes + minutes) % MINUTES_IN_DAY
return clock
def __sub__(self, minutes):
return self.__add__(-minutes)