calculate difference between two timestamps [newbie]

N

nukeymusic

I'm trying to calculate the difference in seconds between two
timestamps, but I'm totally stuck:
date1="Dec-13-09:47:12"
date2="Dec-13-09:47:39"Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required

struct_date1=time.strptime(date1, "%b-%d-%H:%M:%S")
struct_date2=time.strptime(date2, "%b-%d-%H:%M:%S")Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required

thanks in advance
nukey
 
D

Dave Angel

I'm trying to calculate the difference in seconds between two
timestamps, but I'm totally stuck:
date1="Dec-13-09:47:12"
date2="Dec-13-09:47:39"
Traceback (most recent call last):
File "<stdin>", line 1, in<module>
TypeError: an integer is required

struct_date1=time.strptime(date1, "%b-%d-%H:%M:%S")
struct_date2=time.strptime(date2, "%b-%d-%H:%M:%S")
Traceback (most recent call last):
File "<stdin>", line 1, in<module>
TypeError: an integer is required

thanks in advance
nukey
You should post the full code; you omitted your import line(s)

That last approach was closer, try this one:

import datetime
struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S")
struct_date2=datetime.datetime.strptime(date2, "%b-%d-%H:%M:%S")
diff = struct_date2 - struct_date1
print diff

diff is of type datetime.timedelta
 
V

Vince

I'm trying to calculate the difference in seconds between two
timestamps, but I'm totally stuck:
date1="Dec-13-09:47:12"
date2="Dec-13-09:47:39"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required

struct_date1=time.strptime(date1, "%b-%d-%H:%M:%S")
struct_date2=time.strptime(date2, "%b-%d-%H:%M:%S")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required

You're trying to compare two time.struct_time types when they need to be datetime.datetime types.

This will do the conversion for you:

import datetime,time

date1="Dec-13-09:47:12"
date2="Dec-13-09:47:39"

struct_date1=datetime.datetime(*time.strptime(date1, "%b-%d-%H:%M:%S")[:6])
struct_date2=datetime.datetime(*time.strptime(date2, "%b-%d-%H:%M:%S")[:6])

print struct_date2 - struct_date1
 
G

Günther Dietrich

nukeymusic said:
I'm trying to calculate the difference in seconds between two
[...]

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
 
N

nukeymusic

nukeymusic said:
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()

thanks in advance
nukey
 
P

Peter Otten

nukeymusic said:
nukeymusic said:
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))
 
L

Lie Ryan

nukeymusic said:
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))
 
N

nukeymusic

nukeymusic 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 and also thanks to all the others who were so kind to help me
out with my first python-script.
I tested your alternatives and they work, the only a minor
inconvenience is that the first line of the inputfile gets lost i.e.
the first timestamp should become zero (seconds)

best regards,
nukey
 
N

nukeymusic

nukeymusic 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
 
P

Peter Otten

nukeymusic said:
thanks and also thanks to all the others who were so kind to help me
out with my first python-script.
I tested your alternatives and they work, the only a minor
inconvenience is that the first line of the inputfile gets lost i.e.
the first timestamp should become zero (seconds)

That should be easy to fix:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top