Source code for Time
"""
Class for storing time related information. Even though it
is initialized with hour, minute and second information,
it keeps time in seconds as a member value.
"""
class Time(object):
[docs] def __init__(self, h, m, s):
self.seconds = s + m*60 + h*60*60
def convert(self):
"""Return the hour, minute and second corresponding to the time value of the object.
"""
h = self.seconds/(3600)
m = (self.seconds - h*3600)/60
s = self.seconds - h*3600 - m*60
return (h,m,s)
[docs] def __sub__(self, other):
"""Subtract two times from each other and return
a new time object.
"""
(h,m,s) = self.convert()
newt = Time(h,m,s)
newt.seconds -= other.seconds
if newt.seconds < 0 :
newt.seconds = 0
return newt
[docs] def timestr(self,val):
if val < 10:
return "0" + str(val)
else:
return str(val)
[docs] def __str__(self):
(h,m,s) = self.convert()
return '%s:%s:%s' %(self.timestr(h),\
self.timestr(m),\
self.timestr(s))
if __name__ == "__main__":
##Code to test the class
p1 = Time(10, 3, 4)
p2 = Time(12, 10, 3)
print "p1:", p1
print "p2:", p2
print "p1-p2:", p1 - p2
print "p2-p1:", p2 - p1