[Tutor] time calc

Terry Carroll carroll at tjc.com
Sun Jun 20 03:26:13 EDT 2004


On Fri, 18 Jun 2004, kevin parks wrote:

> So the task is really two part : reading in a list of numbers and 
> adding one more piece of data and (2) doing time (base 60) math. It is 
> the time math part i simply can't get my head around. Anyone know if 
> there is a module that does time math? or a method for doing this by 
> hand?

This is really an ugly-looking hack, but maybe someone can improve on it:

import time
import datetime
time1 = "00:57"
dur1 = "08:05"

# get the time; give a year, etc. just to keep strptime happy
ttup1 = time.strptime(time1,"%M:%S")
t1=datetime.datetime(1970,1,1,minute=ttup1[4],second=ttup1[5])
print t1

# get the duration
dtup1 = time.strptime(dur1,"%M:%S")
d1=datetime.timedelta(minutes=dtup1[4],seconds=dtup1[5])
print d1

# you can add the time and duration
t2=t1+d1

print t2
time2printable = str(t2)[-5:]
print time2printable






More information about the Tutor mailing list