delta time = time stop - time start
engsol
engsolnorm at ipns.com
Mon Jan 26 01:08:05 EST 2004
I'm using Python to parse a bunch of s/w test files and make csv files for later report generation by MS ACCESS....(my boss
loves the quick turn-around compared to C). Each log file may contain one or more 'sessions', and each session may contain
one or more 'nodes'.
Each session in the log has an ASCII start and stop time, as does each node.
I have the basic parse part done for parameters, errors, etc., but noticed my routine for determining how long each
session/node took (delta time) was a bit repetitive, so decided to make a 'stand-alone' routine to handle this.
The time format from the log is in the format of:
hh:mm:ss
and is a string with leading zeros where appropiate. Date is never a factor. The longest "delta" is maybe 5 hours.
The routine I came up with is below, but seems a bit clunky.
Is there a better way of doing this? I think it relies too much on integers rounding off in the proper direction, a la
d_time_hr = d_time / 3600 below.
Also, this will have to transition to Linux, if that makes a difference.
START CODE:
import string
def deltatime(start, stop):
t_start = (string.atoi(start[0:2]) * 3600) + (string.atoi(start[3:5]) * 60) + string.atoi(start[6:8])
t_stop = (string.atoi(stop [0:2]) * 3600) + (string.atoi(stop [3:5]) * 60) + string.atoi(stop [6:8])
if t_start < t_stop:
d_time = t_stop - t_start
else:
d_time = (86400 - t_start) + t_stop
d_time_hr = d_time / 3600
d_time_min = (d_time - d_time_hr * 3600) / 60
d_time_sec = (d_time - d_time_hr * 3600) - (d_time_min * 60)
return str(d_time_hr) + 'hr ' + str(d_time_min) + 'min ' + str(d_time_sec) + 'sec'
END CODE
TRY IT
print deltatime('23:45:00', '02:55:03')
RETURNS
3hr 10min 3sec
Thanks.....Norm
PS Please reply via email (engsolnorm at hotmail.com) until my ISP gets fixed.
More information about the Python-list
mailing list