Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96169
License: OTHER
1
"""This module contains a code example related to
2
3
Think Python, 2nd Edition
4
by Allen Downey
5
http://thinkpython2.com
6
7
Copyright 2015 Allen Downey
8
9
License: http://creativecommons.org/licenses/by/4.0/
10
"""
11
12
from __future__ import print_function, division
13
14
15
from datetime import datetime
16
17
# to avoid duplicating code, I'm importing everything from Time1
18
from Time1 import *
19
20
21
def is_after(t1, t2):
22
"""Returns True if t1 is after t2; false otherwise."""
23
return (t1.hour, t1.minute, t1.second) > (t2.hour, t2.minute, t2.second)
24
25
26
def increment(t1, seconds):
27
"""Adds seconds to a Time object."""
28
assert valid_time(t1)
29
seconds += time_to_int(t1)
30
return int_to_time(seconds)
31
32
33
def mul_time(t1, factor):
34
"""Multiplies a Time object by a factor."""
35
assert valid_time(t1)
36
seconds = time_to_int(t1) * factor
37
return int_to_time(seconds)
38
39
40
def days_until_birthday(birthday):
41
"""How long until my next birthday?"""
42
today = datetime.today()
43
# when is my birthday this year?
44
next_birthday = datetime(today.year, birthday.month, birthday.day)
45
46
# if it has gone by, when will it be next year
47
if today > next_birthday:
48
next_birthday = datetime(today.year+1, birthday.month, birthday.day)
49
50
# subtraction on datetime objects returns a timedelta object
51
delta = next_birthday - today
52
return delta.days
53
54
55
def double_day(b1, b2):
56
"""Compute the day when one person is twice as old as the other.
57
58
b1: datetime birthday of the younger person
59
b2: datetime birthday of the older person
60
"""
61
assert b1 > b2
62
delta = b1 - b2
63
dday = b1 + delta
64
return dday
65
66
67
def datetime_exercises():
68
"""Exercise solutions."""
69
70
# print today's day of the week
71
today = datetime.today()
72
print(today.weekday())
73
print(today.strftime('%A'))
74
75
# compute the number of days until the next birthday
76
# (note that it usually gets rounded down)
77
birthday = datetime(1967, 5, 2)
78
print('Days until birthday', end=' ')
79
print(days_until_birthday(birthday))
80
81
# compute the day one person is twice as old as another
82
b1 = datetime(2006, 12, 26)
83
b2 = datetime(2003, 10, 11)
84
print('Double Day', end=' ')
85
print(double_day(b1, b2))
86
87
88
def main():
89
# if a movie starts at noon...
90
noon_time = Time()
91
noon_time.hour = 12
92
noon_time.minute = 0
93
noon_time.second = 0
94
95
print('Starts at', end=' ')
96
print_time(noon_time)
97
98
# and the run time of the movie is 109 minutes...
99
movie_minutes = 109
100
run_time = int_to_time(movie_minutes * 60)
101
print('Run time', end=' ')
102
print_time(run_time)
103
104
# what time does the movie end?
105
end_time = add_times(noon_time, run_time)
106
print('Ends at', end=' ')
107
print_time(end_time)
108
109
print('Does it end after it begins?', end=' ')
110
print(is_after(end_time, noon_time))
111
112
print('Home by', end=' ')
113
travel_time = 600 # 10 minutes
114
home_time = increment(end_time, travel_time)
115
print_time(home_time)
116
117
race_time = Time()
118
race_time.hour = 1
119
race_time.minute = 34
120
race_time.second = 5
121
122
print('Half marathon time', end=' ')
123
print_time(race_time)
124
125
distance = 13.1 # miles
126
pace = mul_time(race_time, 1/distance)
127
128
print('Time per mile', end=' ')
129
print_time(pace)
130
131
datetime_exercises()
132
133
134
if __name__ == '__main__':
135
main()
136
137