📚 The CoCalc Library - books, templates and other resources
License: OTHER
"""This module contains a code example related to12Think Python, 2nd Edition3by Allen Downey4http://thinkpython2.com56Copyright 2015 Allen Downey78License: http://creativecommons.org/licenses/by/4.0/9"""1011from __future__ import print_function, division121314from datetime import datetime1516# to avoid duplicating code, I'm importing everything from Time117from Time1 import *181920def is_after(t1, t2):21"""Returns True if t1 is after t2; false otherwise."""22return (t1.hour, t1.minute, t1.second) > (t2.hour, t2.minute, t2.second)232425def increment(t1, seconds):26"""Adds seconds to a Time object."""27assert valid_time(t1)28seconds += time_to_int(t1)29return int_to_time(seconds)303132def mul_time(t1, factor):33"""Multiplies a Time object by a factor."""34assert valid_time(t1)35seconds = time_to_int(t1) * factor36return int_to_time(seconds)373839def days_until_birthday(birthday):40"""How long until my next birthday?"""41today = datetime.today()42# when is my birthday this year?43next_birthday = datetime(today.year, birthday.month, birthday.day)4445# if it has gone by, when will it be next year46if today > next_birthday:47next_birthday = datetime(today.year+1, birthday.month, birthday.day)4849# subtraction on datetime objects returns a timedelta object50delta = next_birthday - today51return delta.days525354def double_day(b1, b2):55"""Compute the day when one person is twice as old as the other.5657b1: datetime birthday of the younger person58b2: datetime birthday of the older person59"""60assert b1 > b261delta = b1 - b262dday = b1 + delta63return dday646566def datetime_exercises():67"""Exercise solutions."""6869# print today's day of the week70today = datetime.today()71print(today.weekday())72print(today.strftime('%A'))7374# compute the number of days until the next birthday75# (note that it usually gets rounded down)76birthday = datetime(1967, 5, 2)77print('Days until birthday', end=' ')78print(days_until_birthday(birthday))7980# compute the day one person is twice as old as another81b1 = datetime(2006, 12, 26)82b2 = datetime(2003, 10, 11)83print('Double Day', end=' ')84print(double_day(b1, b2))858687def main():88# if a movie starts at noon...89noon_time = Time()90noon_time.hour = 1291noon_time.minute = 092noon_time.second = 09394print('Starts at', end=' ')95print_time(noon_time)9697# and the run time of the movie is 109 minutes...98movie_minutes = 10999run_time = int_to_time(movie_minutes * 60)100print('Run time', end=' ')101print_time(run_time)102103# what time does the movie end?104end_time = add_times(noon_time, run_time)105print('Ends at', end=' ')106print_time(end_time)107108print('Does it end after it begins?', end=' ')109print(is_after(end_time, noon_time))110111print('Home by', end=' ')112travel_time = 600 # 10 minutes113home_time = increment(end_time, travel_time)114print_time(home_time)115116race_time = Time()117race_time.hour = 1118race_time.minute = 34119race_time.second = 5120121print('Half marathon time', end=' ')122print_time(race_time)123124distance = 13.1 # miles125pace = mul_time(race_time, 1/distance)126127print('Time per mile', end=' ')128print_time(pace)129130datetime_exercises()131132133if __name__ == '__main__':134main()135136137