Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
ThinkPython/code/Time1_soln.py at master · DataEdgeSystems/ThinkPython · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
DataEdgeSystems
ThinkPython
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
ThinkPython
/
code
/
Time1_soln.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
129 lines (93 loc) · 3.14 KB
master
Breadcrumbs
ThinkPython
/
code
/
Time1_soln.py
Copy path
Top
File metadata and controls
Code
Blame
129 lines (93 loc) · 3.14 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
Code example from Think Python, by Allen B. Downey.
Available from http://thinkpython.com
Copyright 2012 Allen B. Downey.
Distributed under the GNU General Public License at gnu.org/licenses/gpl.html.
"""
from
datetime
import
datetime
# to avoid duplicating code, I'm importing everything from Time1
from
Time1
import
*
def
is_after
(
t1
,
t2
):
"""Returns True if t1 is after t2; false otherwise."""
return
(
t1
.
hour
,
t1
.
minute
,
t1
.
second
)
>
(
t2
.
hour
,
t2
.
minute
,
t2
.
second
)
def
increment
(
t1
,
seconds
):
"""Adds seconds to a Time object."""
assert
valid_time
(
t1
)
seconds
+=
time_to_int
(
t1
)
return
int_to_time
(
seconds
)
def
mul_time
(
t1
,
factor
):
"""Multiplies a Time object by a factor."""
assert
valid_time
(
t1
)
seconds
=
time_to_int
(
t1
)
*
factor
return
int_to_time
(
seconds
)
def
days_until_birthday
(
birthday
):
"""How long until my next birthday?"""
today
=
datetime
.
today
()
# when is my birthday this year?
next_birthday
=
datetime
(
today
.
year
,
birthday
.
month
,
birthday
.
day
)
# if it has gone by, when will it be next year
if
today
>
next_birthday
:
next_birthday
=
datetime
(
today
.
year
+
1
,
birthday
.
month
,
birthday
.
day
)
# subtraction on datetime objects returns a timedelta object
delta
=
next_birthday
-
today
return
delta
.
days
def
double_day
(
b1
,
b2
):
"""Compute the day when one person is twice as old as the other.
b1: datetime birthday of the younger person
b2: datetime birthday of the older person
"""
assert
b1
>
b2
delta
=
b1
-
b2
double_day
=
b1
+
delta
return
double_day
def
datetime_exercises
():
# print today's day of the week
today
=
datetime
.
today
()
print
today
.
weekday
()
print
today
.
strftime
(
'%A'
)
# compute the number of days until the next birthday
# (note that it usually gets rounded down)
birthday
=
datetime
(
1967
,
5
,
2
)
print
'Days until birthday'
,
print
days_until_birthday
(
birthday
)
# compute the day one person is twice as old as another
b1
=
datetime
(
2006
,
12
,
26
)
b2
=
datetime
(
2003
,
10
,
11
)
print
'Double Day'
,
print
double_day
(
b1
,
b2
)
def
main
():
# if a movie starts at noon...
noon_time
=
Time
()
noon_time
.
hour
=
12
noon_time
.
minute
=
0
noon_time
.
second
=
0
print
'Starts at'
,
print_time
(
noon_time
)
# and the run time of the movie is 109 minutes...
movie_minutes
=
109
run_time
=
int_to_time
(
movie_minutes
*
60
)
print
'Run time'
,
print_time
(
run_time
)
# what time does the movie end?
end_time
=
add_times
(
noon_time
,
run_time
)
print
'Ends at'
,
print_time
(
end_time
)
print
'Does it end after it begins?'
,
print
is_after
(
end_time
,
noon_time
)
print
'Home by'
,
travel_time
=
600
# 10 minutes
home_time
=
increment
(
end_time
,
travel_time
)
print_time
(
home_time
)
race_time
=
Time
()
race_time
.
hour
=
1
race_time
.
minute
=
34
race_time
.
second
=
05
print
'Half marathon time'
,
print_time
(
race_time
)
distance
=
13.1
# miles
pace
=
mul_time
(
race_time
,
1
/
distance
)
print
'Time per mile'
,
print_time
(
pace
)
datetime_exercises
()
if
__name__
==
'__main__'
:
main
()
You can’t perform that action at this time.