def int_to_time(seconds): time = Time() minutes, time.second = divmod(seconds, 60) time.hour, time.minute = divmod(minutes, 60) return time
def increment(time,seconds): t = Time() t.hour = time.hour + int(seconds/3600) t.minute = time.minute + int((seconds-int(seconds/3600))/60) t.second = time.second + (seconds % 60) return t
# Exercise 16-5. # Rewrite increment using time_to_int and int_to_time. from time_class import Time def time_to_int(time): minutes = time.hour * 60 + time.minute seconds = minutes * 60 + time.second return seconds def int_to_time(seconds): time = Time() minutes, time.second = divmod(seconds, 60) time.hour, time.minute = divmod(minutes, 60) return time def increment(time,seconds): return int_to_time(time_to_int(time)+seconds) t = Time() t.hour = 12 t.minute = t.second = 0 t2 = increment(t,3610) print(t2.hour,t2.minute,t2.second,sep=":")
# Exercise 16-1. # Write a function called print_time that takes a Time object and prints it in the form # hour:minute:second. Hint: the format sequence '%.2d' prints an integer using at least # two digits, including a leading zero if necessary. from time_class import Time def print_time(time): print("%.2d:%.2d:%.2d" % (time.hour,time.minute,time.second)) noon = Time() noon.hour = 12 noon.minute = 0 noon.second = 0 print_time(noon)