calculate difference between two timestamps [newbie]

nukeymusic nukeymusic at gmail.com
Sun Dec 18 09:26:25 EST 2011


On 18 dec, 13:58, Lie Ryan <lie.1... at gmail.com> wrote:
> On 12/18/2011 10:43 PM, Peter Otten wrote:
>
>
>
>
>
>
>
>
>
> > nukeymusic wrote:
>
> >> On 17 dec, 12:20, "Günther Dietrich"<gd.use... at spamfence.net>  wrote:
> >>> nukeymusic<nukeymu... at gmail.com>  wrote:
> >>>> I'm trying to calculate the difference in seconds between two
>
> >>> [...]
>
> >>>>>> import datetime
> >>>>>> date1 = datetime.datetime.strptime("Dec-13-09:47:12",
> >>>>>> "%b-%d-%H:%M:%S") date2 =
> >>>>>> datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") delta
> >>>>>> = date2 - date1 delta_seconds = (delta.days * 60 * 60 * 24) +
> >>>>>> delta.seconds + ((delta.microseconds + 500000) / 1000000)
>
> >>> For very big time differences you should consider to use the Decimal
> >>> arithmetics (standard module Decimal) instead of integer arithmetics
> >>> for the last line.
> >>> If you are sure, that you don't use fractional seconds, you can omit
> >>> the part with 'delta.microseconds'.
>
> >>> Best regards,
>
> >>> Günther
> >> That can very much Günther, this helped me a lot further, I'm only
> >> struggling with one more problem to finish my first python-program.
> >> Could you
> >> tell me why I can't write to the outputfile as I do in the code
> >> below:?
> >> #!/usr/bin/python
> >> #version 16/12/2011
> >> #Example of testfile
> >> #Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00
> >> #Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00
> >> #Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00
> >> import datetime
> >> f = open('testfile','r')
> >> g = open('outputfile','w')
> >> #get line 1 from input file:
> >> line1=f.readline()
> >> #get first element in line 1:
> >> date1=line1.rsplit()[0]
> >> #convert first element tot structured date time
> >> struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S")
> >> for line in f:
> >>   temp=line.rsplit()
> >>   delta=datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")-
> >> datetime.datetime.strptime(date1,  "%b-%d-%H:%M:%S")
> >>   delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds +
> >> ((delta.microseconds + 500000) / 1000000)
> >>   temp[0]=delta_seconds
> >> #the following line is wrong, but I don't know how to fix it:
> >>   g.write(temp)
> >> #Close files
> >> f.close()
> >> g.close()
>
> > The write() method only accepts strings; you have to convert the temp list
> > to a string before passing it on. The minimal change:
>
> > for line in f:
> >       temp = line.rsplit()
> >       delta = (datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")
> >                -datetime.datetime.strptime(date1,  "%b-%d-%H:%M:%S"))
> >       delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds
> >                        + ((delta.microseconds + 500000) / 1000000))
> >       temp[0] = str(delta_seconds)
> >       g.write(" ".join(temp) + "\n")
>
> > Other observations:
>
> > - you are repeating calculations in the loop that you can do (and did)
> > outside.
>
> > - use four-space indent for better readability
>
> > - there's no need to use rsplit(); use split()
>
> > After a few other modifications:
>
> > import datetime
>
> > def parse_line(line):
> >      date, rest = line.split(None, 1)
> >      date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S")
> >      return date, rest
>
> > with open('testfile','r') as f:
> >      with open('outputfile','w') as g:
> >          first_date, first_rest = parse_line(next(f))
> >          for line in f:
> >              cur_date, rest = parse_line(line)
> >              delta = cur_date - first_date
> >              delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds
> >                               + ((delta.microseconds + 500000) / 1000000))
> >              g.write("%s %s" % (delta_seconds, rest))
>
> minor improvement, you can do:
>
> with open('testfile','r') as f, open('outputfile','w') as g:
>      ...
>
> instead of the nested with-block.
>
> Also, you can use `delta.total_seconds()` instead of `delta_seconds =
> ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds +
> 500000) / 1000000))`
>
> Therefore (untested):
>
> import datetime
>
> def parse_line(line):
>      date, rest = line.split(None, 1)
>      date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S")
>      return date, rest
>
> with open('testfile','r') as f, open('outputfile','w') as g:
>      first_date, first_rest = parse_line(next(f))
>      for line in f:
>          cur_date, rest = parse_line(line)
>          delta = cur_date - first_date
>          g.write("%s %s" % (int(round(delta.total_seconds())), rest))

thanks to you and all the others for helping me out with my first
python-script. I tested your alternatives and they do work.
The only minor inconvenience with the scripts is that the output-file
misses the first line i.e. the first timestamp should be reset to zero
(seconds),
but as these log-files are very large, missing one measurement is not
too bad

best regards
nukey



More information about the Python-list mailing list