delta time = time stop - time start

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • engsol

    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(st art[0:2]) * 3600) + (string.atoi(st art[3:5]) * 60) + string.atoi(sta rt[6:8])
    t_stop = (string.atoi(st op [0:2]) * 3600) + (string.atoi(st op [3:5]) * 60) + string.atoi(sto p [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:4 5:00', '02:55:03')

    RETURNS

    3hr 10min 3sec

    Thanks.....Norm
    PS Please reply via email (engsolnorm@hot mail.com) until my ISP gets fixed.


  • Guilherme Salgado

    #2
    Re: delta time = time stop - time start

    On Mon, 2004-01-26 at 04:08, engsol wrote:
    [...][color=blue]
    > 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.
    >[/color]

    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.
    [color=blue]
    > 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(st art[0:2]) * 3600) + (string.atoi(st art[3:5]) * 60) + string.atoi(sta rt[6:8])
    > t_stop = (string.atoi(st op [0:2]) * 3600) + (string.atoi(st op [3:5]) * 60) + string.atoi(sto p [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[/color]

    []'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>


    Comment

    • Dan Bishop

      #3
      Re: delta time = time stop - time start

      engsol <engsolnorm@ipn s.com> wrote in message news:<cs9910pcg ktdrp1e8abctopt ue7braqvqu@4ax. com>...[color=blue]
      > 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.[/color]

      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.
      [color=blue]
      > Also, this will have to transition to Linux, if that makes a difference.[/color]

      In your code, it makes no difference. I'm using a Linux box right
      now.
      [color=blue]
      > START CODE:
      >
      > import string[/color]

      Unless you have a very old version of Python, this is no longer
      needed; string.atoi has been officially replaced with the "int"
      constructor.
      [color=blue]
      > def deltatime(start , stop):
      >
      > t_start = (string.atoi(st art[0:2]) * 3600) + (string.atoi(st art[3:5]) * 60) + string.atoi(sta rt[6:8])
      > t_stop = (string.atoi(st op [0:2]) * 3600) + (string.atoi(st op [3:5]) * 60) + string.atoi(sto p [6:8])[/color]

      Rather than count character positions, take advantage of the fact that
      the strings are colon-delimited.

      def toSeconds(timeS tring):
      hour, min, sec = map(int, timeString.spli t(':'))
      return (hour * 60 + min) * 60 + sec

      t_start = toSeconds(start )
      t_stop = toSeconds(stop)
      [color=blue]
      > if t_start < t_stop:
      > d_time = t_stop - t_start
      > else:
      > d_time = (86400 - t_start) + t_stop[/color]

      You can eliminate the if-else test by taking advantage of Python's %
      operator

      d_time = (t_stop - t_start) % 86400
      [color=blue]
      > 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)[/color]

      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_m in, 60)
      [color=blue]
      > return str(d_time_hr) + 'hr ' + str(d_time_min) + 'min ' + str(d_time_sec) + 'sec'[/color]

      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(timeS tring):
      hour, min, sec = map(int, timeString.spli t(':'))
      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_m in, 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(timeS tring):
      hour, min, sec = map(int, timeString.spli t(':'))
      return (hour * 60 + min) * 60 + sec
      d_time_min, d_time_sec = divmod(toSecond s(stop) - toSeconds(start ),
      60)
      d_time_hr, d_time_min = divmod(d_time_m in, 60)
      return '%dhr %dmin %ssec' % (d_time_hr % 24, d_time_min,
      d_time_sec)

      Comment

      Working...