# time.py # Steve Marschner (srm2) and Lillian Lee (ljl2) # March 13, 2013 """Lecture example: object representing the time of day.""" class Time(object): """Instances represent times of day. Instance Attributes: hour: hour of day [int in 0..23] min: minute of hour [int in 0..59]""" def __init__(self, hour, min): """Initializer: Creates the time hour:min. Precondition: hour in 0..23; min in 0..59""" assert type(hour) == int, str(hour)+' is not an int' assert 0 <= hour and hour < 24, str(hour)+' is out of range 0..23' assert type(min) == int, str(min)+' is not an int' assert 0 <= min and min < 60, str(hour)+' is out of range 0..59' self.hour = hour self.min = min def increment(self, hours, mins): """Move this time hours and minutes into the future. Precondition: hours is an int >= 0; mins an int in 0..59""" assert type(hour) == int, str(hour)+' is not an int' assert 0 <= hour, str(hour)+' is not >= 0' assert type(min) == int, str(min)+' is not an int' assert 0 <= min and min < 60, str(hour)+' is out of range 0..59' self.hour = self.hour + hours self.min = self.min + mins self.hour = self.hour + self.min / 60 self.min = self.min % 60 self.hour = self.hour % 24 def isPM(self): """Returns: this time is noon or later.""" return self.hour >= 12 def __str__(self): """Format the time as a string in [H]H:MM 24-hour format.""" return str(self.hour) + (':0' if self.min < 10 else ':') + str(self.min) if __name__ == '__main__': t = Time(23,58) print t t.increment(0,3) print t t.increment(11,0) print t, t.is_pm() t.increment(1, 59) print t, t.is_pm()