Changes

Jump to: navigation, search

Rchan sandbox

1,443 bytes added, 17:43, 2 November 2019
no edit summary
:8. Before proceeding, make certain that you identify all errors in lab7b.py. When the checking script tells you everything is OK - proceed to the next step.
<br><br>
 
==Part 3 - Another approach to perform operation on time objects ==
In the previous parts, we recognise that fact that a Time object is essentially a three-digit number in base 60. The 'second' attribute is the "ones colume", the 'minute' attribute is the "sixties column", and the hour attribute is the '60 x 60' column. This is why we have to carry over second to minute when the value of 'second' exceeds 60.
 
In this part, we are going to use a different approach to manage calculation on Time object. We know that there are 86400 (24 x 60 x 60) seconds in a day. It is relatively easy to convert a Time object in the form of hour:minute:second to be represent by a single integer value in seconds. It is equally straight forward to convert a integer number of seconds between 0 and 86399 into hour:minute:second format. Here are two functions: sec_to_time(seconds) and time_to_sec(time) that will do just that:<source lang="python">
...
...
def time_to_sec(time):
'''convert a time object to a single integer representing the number of seconds from mid-night'''
minutes = time.hour * 60 + time.minute
seconds = minutes * 60 + time.second
return seconds
 
def sec_to_time(seconds):
'''convert a given number of seconds to a time object in hour,minute,second format'''
time = Time()
minutes, time.second = divmod(seconds, 60)
time.hour, time.minute = divmod(minutes,60)
return time
...
...
</source>
1,760
edits

Navigation menu