# time.py # Steve Marschner (srm2), Lillian Lee (ljl2), and Walker White (wmw2) # October 15, 2015 """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. Parameter hour: hour of day Precondition: hour is an int in 0.23 Parameter min: minute of hour Precondition: min is an int 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 hours and minutes into the future. Parameter hours: number of hours to move Precondition: hours is an int >= 0 Parameter mins: number of minutes to move Precondition: mins an int in 0..59""" assert type(hours) == int, str(hours)+' is not an int' assert 0 <= hours, str(hours)+' is not >= 0' assert type(mins) == int, str(mins)+' is not an int' assert 0 <= mins and mins < 60, str(mins)+' 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: True if this time is noon or later; False otherwise.""" return self.hour >= 12 def __str__(self): """Returns: 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) def demo_time(): """Show off time in action""" t = Time(23,58) print t t.increment(0,3) print t t.increment(11,0) print t, t.isPM() t.increment(1, 59) print t, t.isPM() if __name__ == '__main__': demo_time()