[Tutor] time calc
Glen Wheeler
gew75 at hotmail.com
Fri Jun 18 20:24:49 EDT 2004
Howdy Kevin,
The time math shouldn't be too hard. What format is your duration in?
Hours:Mins? Minutes? Hours.<percentage_of_hour>?
To get around the time math, I would use a single intermediary
calculation; convert all pairs first into minutes. This would be different
if you have the duration in a different format, but on the back of the tapes
that I have it says minutes, so I'll use that for the time being :).
For each time pair, (hh:mm) simply think of the (hh) part as mm*60. Then,
to obtain total minutes, we have an easy operation -- totalMinutes = hh*60 +
mm. You can perform the conversion, etc yourself. To get the original
tuple, use the string.split(':') method.
Once that's finished we can add the duration on, then convert back to time
form again. The easiest way in my mind for this is to use modulus. As a
quick reminder/introduction :
x (mod y) = remainder of x upon repeated division of y
So that we want
total_minutes (mod 60) = new_minutes
Also, new_hours = total_minutes / 60, which is regular integer division.
Python has an incredibly useful builtin with divmod(..), which performs
integer division and modulus on the two arguments. Modulus in python is
with the % operator.
So, recapping in code:
>>> hhmm = (2,45)
>>> total_minutes = hhmm[0]*60 + hhmm[1]
>>> total_minutes += 120
>>> divmod(total_minutes, 60)
(4, 45)
HTH,
Glen
----- Original Message -----
From: "kevin parks" <kp8 at mac.com>
To: <tutor at python.org>
Cc: "kevin parks" <kp8 at mac.com>
Sent: Saturday, June 19, 2004 3:36 AM
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
>
> etc.
>
> 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?
>
> cheers,
>
> kevin parks
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list