forked from AllenDowney/ThinkPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Time1.py
80 lines (58 loc) · 1.78 KB
/
Time1.py
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
"""
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.
"""
class Time(object):
"""Represents the time of day.
attributes: hour, minute, second
"""
def print_time(t):
print '%.2d:%.2d:%.2d' % (t.hour, t.minute, t.second)
def int_to_time(seconds):
"""Makes a new Time object.
seconds: int seconds since midnight.
"""
time = Time()
minutes, time.second = divmod(seconds, 60)
time.hour, time.minute = divmod(minutes, 60)
return time
def time_to_int(time):
"""Computes the number of seconds since midnight.
time: Time object.
"""
minutes = time.hour * 60 + time.minute
seconds = minutes * 60 + time.second
return seconds
def add_times(t1, t2):
"""Adds two time objects."""
assert valid_time(t1) and valid_time(t2)
seconds = time_to_int(t1) + time_to_int(t2)
return int_to_time(seconds)
def valid_time(time):
"""Checks whether a Time object satisfies the invariants."""
if time.hour < 0 or time.minute < 0 or time.second < 0:
return False
if time.minute >= 60 or time.second >= 60:
return False
return True
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)
if __name__ == '__main__':
main()