python - How should negative time work? -
i'm trying create time
-class can handle times of format hh:mm:ss
. have:
class time(object): def __init__(self, h=0, m=0, s=0): #private self.__hours = 0 self.__minutes = 0 self.__seconds = 0 #public self.hours = h self.minutes = m self.seconds = s @property def hours(self): return self.__hours @hours.setter def hours(self, value): if value < 0: raise valueerror self.__hours = value @property def minutes(self): return self.__minutes @minutes.setter def minutes(self, value): if value < 0: raise valueerror self.hours += int(value / 60) self.__minutes = value % 60 @property def seconds(self): return self.__seconds @seconds.setter def seconds(self, value): if value < 0: raise valueerror self.minutes += int(value / 60) self.__seconds = value % 60
i'm having problems negative time values. check if time value (e.g. minutes
) being set negative , raise valueerror
if is. thing is, work negative time formats too, i'm not sure how.
how should negative time values behave? possible achieve setters? can't started, don't need code me, atleast give tips , explain how negative time works.
there 2 concepts time:
- a moment in time (instant)
- a length between 2 instants (timespan or time delta).
we measure timespan in seconds , multiple of seconds.
a moment of time described using same unit, understood timespan between reference time (like 0 bc, or unix epoch). (the units more complex though - instead of regular multiples of seconds, use calendars assign nice names "july 16th" particular moments in time).
so:
- timespan or can negative (as whole)
- an instant can negative too, because there's timespan inside (like year -3000 bc), actual formats don't have negative coefficients (it makes no sense "14:-3" or "june -5th").
so:
- setting
minutes
negative on instant, shouldvalueerror
, - there method
.later(minutes=10)
equivalent.earlier(minutes=-10)
.
Comments
Post a Comment