473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

delta time = time stop - time start


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 (en********@hotmail.com) until my ISP gets fixed.
Jul 18 '05 #1
2 5177
On Mon, 2004-01-26 at 04:08, engsol wrote:
[...]
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.

You can use mx.DateTime
(http://www.lemburg.com/files/python/mxDateTime.html).
Then, the code will look like this:

from mx import DateTime
start = DateTime.now()
# do something ...
stop = DateTime.now()
delta = (stop - start).strftime("%H:%M:%S")

hope it helps.
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


[]'s
Salgado
--
This email has been inspected by Hans Blix, who has reported that no
weapons of mass destruction were used in its construction.
Read his report here:
<http://www.un.org/apps/news/infocusnewsiraq.asp?NewsID=414&sID=6>
Jul 18 '05 #2
engsol <en********@ipns.com> wrote in message news:<cs********************************@4ax.com>. ..
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.
It also relies on -Qold. Please use the // operator for integer
division. Or you're going to regret it when you upgrade to Python
3.0.
Also, this will have to transition to Linux, if that makes a difference.
In your code, it makes no difference. I'm using a Linux box right
now.
START CODE:

import string
Unless you have a very old version of Python, this is no longer
needed; string.atoi has been officially replaced with the "int"
constructor.
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])
Rather than count character positions, take advantage of the fact that
the strings are colon-delimited.

def toSeconds(timeString):
hour, min, sec = map(int, timeString.split(':'))
return (hour * 60 + min) * 60 + sec

t_start = toSeconds(start)
t_stop = toSeconds(stop)
if t_start < t_stop:
d_time = t_stop - t_start
else:
d_time = (86400 - t_start) + t_stop
You can eliminate the if-else test by taking advantage of Python's %
operator

d_time = (t_stop - t_start) % 86400
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)
Rather than calculate remainders the hard way, use the % operator. Or
since you need both the quotient and remainder, use the divmod
function.

d_time_min, d_time_sec = divmod(d_time, 60)
d_time_hr, d_time_min = divmod(d_time_min, 60)
return str(d_time_hr) + 'hr ' + str(d_time_min) + 'min ' + str(d_time_sec) + 'sec'


A more concise way of writing this is

return '%dhr %dmin %ssec' % (d_time_hr, d_time_min, d_time_sec)

The complete rewritten function is

def deltatime(start, stop):
def toSeconds(timeString):
hour, min, sec = map(int, timeString.split(':'))
return (hour * 60 + min) * 60 + sec
t_start = toSeconds(start)
t_stop = toSeconds(stop)
d_time = (t_stop - t_start) % 86400
d_time_min, d_time_sec = divmod(d_time, 60)
d_time_hr, d_time_min = divmod(d_time_min, 60)
return '%dhr %dmin %ssec' % (d_time_hr, d_time_min, d_time_sec)

which is still too verbose for my tastes; I would rewrite it as:

def deltatime(start, stop):
def toSeconds(timeString):
hour, min, sec = map(int, timeString.split(':'))
return (hour * 60 + min) * 60 + sec
d_time_min, d_time_sec = divmod(toSeconds(stop) - toSeconds(start),
60)
d_time_hr, d_time_min = divmod(d_time_min, 60)
return '%dhr %dmin %ssec' % (d_time_hr % 24, d_time_min,
d_time_sec)
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dan glenn | last post by:
Say, I want to set a cookie and have it expire an hour after it's set. It's looking like this is only possible for browsers which are in the same time zone as my server?? In other words, if I...
2
by: Slide-O-Mix | last post by:
I am using the Process class to run an external application from my application. The first time I call the .Start() method it takes several seconds for the process to actually start. Subsequent...
8
by: peterbe | last post by:
What's the difference between time.clock() and time.time() (and please don't say clock() is the CPU clock and time() is the actual time because that doesn't help me at all :) I'm trying to...
0
by: Edward Diener | last post by:
In Borland's VCL it was possible to divide a component into design time and run time DLLs. The design time DLL would only be necessary when the programmer was setting a component's properties or...
1
by: marius | last post by:
Hi! I'm trying to make myself a simple webpage, enabling me to stop and start certain services on the server. I've been doing some reading, and have given permission for ASPNET account to the...
3
by: Shawn Yates | last post by:
I have a database that I am using as a Time clock where employees can clock in and out. Is there a way to make it so that when they clock out a form would open displaying their work day hour by...
7
by: shai | last post by:
I am working at .net 1.1, writing in c#. I have windows service with a COM object. Every unexpected time The COM object throw an error that make my service get stuck (do not respond). I can catch...
1
by: davelist | last post by:
I'm guessing there is an easy way to do this but I keep going around in circles in the documentation. I have a time stamp that looks like this (corresponding to UTC time): start_time =...
5
by: smahaboob | last post by:
Actually iam preparing a windows application. My winform duty is storing finding the start time and stop timings of the applications. when ever i opened the word my winform should catch this one and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.