-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha1.py
163 lines (124 loc) · 3.34 KB
/
a1.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
def seconds_difference(time_1, time_2):
""" (number, number) -> number
Return the number of seconds later that a time in seconds
time_2 is than a time in seconds time_1.
>>> seconds_difference(1800.0, 3600.0)
1800.0
>>> seconds_difference(3600.0, 1800.0)
-1800.0
>>> seconds_difference(1800.0, 2160.0)
360.0
>>> seconds_difference(1800.0, 1800.0)
0.0
"""
return time_2 - time_1
def hours_difference(time_1, time_2):
""" (number, number) -> float
Return the number of hours later that a time in seconds
time_2 is than a time in seconds time_1.
>>> hours_difference(1800.0, 3600.0)
0.5
>>> hours_difference(3600.0, 1800.0)
-0.5
>>> hours_difference(1800.0, 2160.0)
0.1
>>> hours_difference(1800.0, 1800.0)
0.0
"""
return (time_2 - time_1) /60/60
def to_float_hours(hours, minutes, seconds):
""" (int, int, int) -> float
Return the total number of hours in the specified number
of hours, minutes, and seconds.
Precondition: 0 <= minutes < 60 and 0 <= seconds < 60
>>> to_float_hours(0, 15, 0)
0.25
>>> to_float_hours(2, 45, 9)
2.7525
>>> to_float_hours(1, 0, 36)
1.01
"""
return hours + (minutes / 60) + (seconds/60/60)
def to_24_hour_clock(hours):
""" (number) -> number
hours is a number of hours since midnight. Return the
hour as seen on a 24-hour clock.
Precondition: hours >= 0
>>> to_24_hour_clock(24)
0
>>> to_24_hour_clock(48)
0
>>> to_24_hour_clock(25)
1
>>> to_24_hour_clock(4)
4
>>> to_24_hour_clock(28.5)
4.5
"""
return hours % 24
### Write your get_hours function definition here:
def get_hours (seconds):
""" (number) ->number
return value should be in the range of 0 to 23.
>>> get_hours(3600)
1
>>> get_hours(3800)
1
>>> get_hours(7200)
2
"""
return seconds// (60*60)
### Write your get_minutes function definition here:
def get_minutes(seconds):
""" (number) -> number
return value should be in the range of 0 to 59.
>>>get_minutes(3800)
3
"""
return seconds % (60*60)//60
### Write your get_seconds function definition here:
def get_seconds(seconds):
return ((seconds % (60*60)) % 60)
def time_to_utc(utc_offset, time):
""" (number, float) -> float
Return time at UTC+0, where utc_offset is the number of hours away from
UTC+0.
>>> time_to_utc(+0, 12.0)
12.0
>>> time_to_utc(+1, 12.0)
11.0
>>> time_to_utc(-1, 12.0)
13.0
>>> time_to_utc(-11, 18.0)
5.0
>>> time_to_utc(-1, 0.0)
1.0
>>> time_to_utc(-1, 23.0)
0.0
"""
utc0_actual_time = time - utc_offset
utc_time = to_24_hour_clock(utc0_actual_time)
return utc_time
def time_from_utc(utc_offset, time):
""" (number, float) -> float
Return UTC time in time zone utc_offset.
>>> time_from_utc(+0, 12.0)
12.0
>>> time_from_utc(+1, 12.0)
13.0
>>> time_from_utc(-1, 12.0)
11.0
>>> time_from_utc(+6, 6.0)
12.0
>>> time_from_utc(-7, 6.0)
23.0
>>> time_from_utc(-1, 0.0)
23.0
>>> time_from_utc(-1, 23.0)
22.0
>>> time_from_utc(+1, 23.0)
0.0
"""
local_time = time + utc_offset
local_time_display = to_24_hour_clock(local_time)
return local_time_display