[Tutor] time calc

Don Arnold darnold02 at sprynet.com
Fri Jun 18 18:59:32 EDT 2004

Just convert each time to minutes, do the subtraction, then convert back.
Off the top of my head, but hopefully close to correct:

MINS_PER_DAY = 60 * 24

startTimes = ['00:23','8:23','17:45','00:17']

def convertToMins(timeStr):
    hh, mm = timeStr.split(':')
    return int(hh) * 60 + int(mm)

def convertFromMins(minutes):
    hh, mm = divmod(minutes,60)
    return '%02d:%02d' % (hh, mm)

for i in range(len(startTimes) - 1):
    begin = startTimes[i]
    end = startTimes[i + 1]

    beginAsMins = convertToMins(begin)
    endAsMins = convertToMins(end)

    if endAsMins < beginAsMins: #rolled over midnight
        endAsMins += MINS_PER_DAY

    print 'begin: %-6s  end: %-6s  duration: %-6s' % \
            (begin, end, convertFromMins(endAsMins - beginAsMins))

begin: 00:23   end: 8:23    duration: 08:00 
begin: 8:23    end: 17:45   duration: 09:22 
begin: 17:45   end: 00:17   duration: 06:32


-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On Behalf
Of kevin parks
Sent: Friday, June 18, 2004 12:36 PM
To: tutor at python.org
Cc: kevin parks
Subject: [Tutor] time calc

hi all.

I have a bit of a task that i have been doing by hand, but as it turns 
out there is pages and pages more of this so i would like to make a 
python script that does this work and have it handy.

I have a list that has several columns like so:

1    1    1    00:23
1    2    2    8:23
1    3    3    9:41
1    4    3    10:47
1    5    3     11:21

What this is a list that has tape number, program number, item number 
and start time for a recording.

What i want to do is append the duration to the end of this list by 
subtracting the start of the item from the start time of the next so 
that the first line would look like:

1    1    1    00:23    08:00
1    2    2    8:23      01:18


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 


kevin parks

Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list