# average time calculation??

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Jan 10 19:14:53 CET 2013

```Two quick corrections to what I wrote...

On 10 January 2013 18:13, Oscar Benjamin <oscar.j.benjamin at gmail.com> wrote:
> On 10 January 2013 17:50, pmec <pcurad01 at gmail.com> wrote:
>> Hi there guys i've got a script that's suppose to find the average of two times as strings. The times are in minutes:seconds:milliseconds
>> i'm doing ok in printing the right minutes and seconds my problem is with the milliseconds.
>
> You might find it easier to do this arithmetic if using datetime.time
> objects from the datetime module:
> http://docs.python.org/2/library/datetime.html

Actually  I meant datetime.timedelta objects.

>
>>
>> Example if i have 00:02:20 and 00:04:40 the average will be 00:03:30 or 00:02:00 and 00:03:00 will be 00:02:30
>>
>> Can anyone help me out with this please. Here is the code that i have so far:
>>
>> def lap_average(lap1, lap2):
>>
>>     t1 = lap1.replace(":",'')
>>     t2 = lap2.replace(":",'')
>>
>>     mins1, secs1, hundreths1 = t1[:2], t1[2:4], t1[4:]
>>     mins2, secs2, hundreths2 = t2[:2], t2[2:4], t2[4:]
>
> Are these really hundredths? At the top you said it goes
> minutes:seconds:milliseconds. A hundredth of a second is 10
> milliseconds so you need to be clear about which one you want.
>
>>
>>     total_seconds = int(secs1) + int(secs2) + int(mins1) * 60 + int(mins2) * 60
>
> What happened to the hundredths in the line above. Surely you wanted
> to add 0.01 * hundredths there.
>
>>
>>     millisec = (total_seconds * 1000)
>>     millisec = millisec / 2
>
> In Python 2 this division will always round down if millisec is an
> integer. Is that what you want?
>
>>
>>     micro_x = millisec
>
> Is the line above just there to confuse me? I thought millisec was a
> good descriptive name. In this context micro would suggest
> microseconds which would be 1000 times smaller.
>
>>     minutes = micro_x / (60*1000)
>
> Wouldn't it have been easier to just get this from total_seconds?
>
>>     micro_x = micro_x - minutes * (60*1000)
>>     seconds = micro_x / 1000
>
> This will behave differently in Python 3. Safest to use floor division // here:
> http://stackoverflow.com/questions/183853/in-python-what-is-the-difference-between-and-when-used-for-division
>
>>     micro_x = micro_x - seconds
>
> Surely that should be seconds*1000?
>
>>
>>    print '%02d:%02d:%s' % (minutes, seconds, micro_x)

micro_x is in units of microseconds. Did you want microseconds or
hundredths of a second here in the output?

>
> Until your done debugging, I suggest you print all of these values out
> directly (without the format string):
>
> print(minutes, seconds, microx)
>
>>
>> lap_average('03:40:00', '05:20:00')
>> lap_average('03:00:02', '02:00:00')
>> lap_average('02:25:50', '06:50:75')
>> lap_average('00:02:00', '00:03:00')
>> lap_average('00:02:20', '00:04:40')
>> lap_average('02:40:40', '03:30:30')
>> lap_average('02:60:30', '60:40:40')
>
>
> Oscar

```