# 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 variables: hour [int]: hour of day, in 0..23 min [int]: minute of hour, in 0..59 """ def __init__(self, hour, min): """The time hour:min. Pre: hour in 0..23; min in 0..59 """ self.hour = hour self.min = min def increment(self, hours, mins): """Move this time hours and minutes into the future. Precondition: hours [int] >= 0; mins in 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 is_pm(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()