0

I am a python (2.7) newbie and I have two variables which contain timestamps - I need to find the difference between them in seconds/microseconds.

I have spent nearly a whole day trying to work out how to do this and am stumped - so can you please help point me in the right direction, please keep in mind that I am new to python so I need things explained simply.

I have searched Stackoverflow and have not seen any questions/answers which help me resolve this problem or if they do, I do not understand how... as they either use different versions of python or work with known timestamp values where as the variables in mine are generated as the program runs...

Firstly, I import the below modules to work with date and time values:

from datetime import *
from time import *

I then receive one time stamp value from an amqp message which is in posix (utc) format so I convert it using:

== t_timestamp is acquired from an amqp message== 
ts = int(t_timestamp) / 1000.0
formatted_timestamp = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S.%f')

I then assign the local system time to a variable and convert it so that it has the same format as the above using:

local_time = datetime.utcnow()
local_time = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S.%f')

If I print the formatted_timestamp and local_time variables they appear exactly the same in terms of formatting...

2016-11-03 21:05:37.512000 and 2016-11-03 21:05:38.045000 

if I then try to subtract the two variables from each other so that I can view the time difference between the formatted_timestamp and local_time in seconds/microseconds, as shown below:

tstamp_age = (local_time - formatted_timestamp)

I get the below error:

TypeError: unsupported operand type(s) for -: 'str' and 'str'

If I don't convert the local_time timestamp, I get the below error

TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str'

But strangely enough, if I print the two variables, I get the same formatted timestamps as shown below:

2016-11-03 21:12:08.319000 and 2016-11-03 21:12:12.299000

So I suppose my question is - how can I subtract two str formatted dates or convert the timestamp which is formatted as shown below as a datetime type so that I can use this in a subtraction operation to show the remainder in seconds\microseconds?

== t_timestamp is acquired from an amqp message== 
ts = int(t_timestamp) / 1000.0
formatted_timestamp = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S.%f')

Please keep in mind that I receive the t_timestamp from an amqp (RabbitMQ) message via pika and I use 'split' to assign the utc epoc/posix timestamp received to a variable in my program and then work with it as shown directly above.

I have searched for hours before posting this question without finding a solution or understanding why I cant do this, given that the print statements produce the exact same results - any help will be much appreciated - thank you.

  • Possible duplicate of [How do I check the difference, in seconds, between two dates?](http://stackoverflow.com/questions/4362491/how-do-i-check-the-difference-in-seconds-between-two-dates) – Peter Wood Nov 03 '16 at 21:38
  • You need to subtract two `datetime` objects, not their string representations. When you put an object in `print` it implicitly calls `str(obj)`, which is why they print the same. – juanpa.arrivillaga Nov 03 '16 at 21:42
  • if you have timestamps then do `timestamp1 - timestamp2` to get difference in miliseconds. BTW: don't convert object `datetime` into `string` - do `datetime1 - datetime2`. – furas Nov 03 '16 at 21:43
  • 1
    Yea, fundamentally, when you use `strftime` the result is no longer a `timestamp` in the sense that it is a `datetime` object. It's just a plain-old string with no knowledge of dates and times. – juanpa.arrivillaga Nov 03 '16 at 21:44
  • thanks to all - but @juanpa.arrivillaga, I think you may have helped me find the solution - I changed the code to 'ts = int(t_timestamp) tick_timestamp = datetime.fromtimestamp(ts / 1e3)' and I am now able to subtract the timestamps - many thanks to all! –  Nov 03 '16 at 22:05

2 Answers2

1

Alternate solution in python3.5

#!/usr/bin/env python3
import datetime as dt

t1 = dt.datetime.strptime("2016-11-03 21:12:08.319000", "%Y-%m-%d %H:%M:%S.%f")
t2 = dt.datetime.strptime("2016-11-03 21:12:12.299000", "%Y-%m-%d %H:%M:%S.%f")

delta = t2 - t1
print("Time difference: {}".format(delta))
print("Seconds: {}".format(delta.seconds))
print("Microseconds: {}".format(delta.microseconds))

Output

Time difference: 0:00:03.980000
Seconds: 3
Microseconds: 980000
r0xette
  • 898
  • 3
  • 11
  • 24
  • thanks @r0xette - I found a simple solution but I will experiment with what you have replied with as this does seem a more elegant solution - thank you! –  Nov 03 '16 at 22:22
  • @TechnologySmiths. Please feel free to click on arrow to accept the answer if it helped. Good luck. – r0xette Nov 03 '16 at 22:29
  • @r0xette I'm afraid this isn't the solution as this gives the result as a printed string and I need to work with it as a variable of datetime format so that I can perform arithmetic with the value - I do however appreciate your time and effort - thank you. The solution I found below allows me to reference the **tstamp_age** variable in code and work with it as a time value where as the solution you provided only allows these values to be printed as strings - but please forgive me if I am missing something... –  Nov 04 '16 at 22:32
0

Thanks to those who commented or posted an answer however something juanpa.arrivillaga commented on made me think so I did a search and ended up changing the code as shown below and I can now do what's needed - many thanks to all - I was able to very quickly resolve this with your help!

== t_timestamp is acquired from an amqp message== 
`ts = int(t_timestamp)
tick_timestamp = datetime.fromtimestamp(ts / 1e3)

local_time = datetime.utcnow()

tstamp_age = (local_time - tick_timestamp)

this now prints as

 21:59:57.001000 1478210397001 0:00:00.060000

which is fine for my purpose as I only really want to store the tstamp_age in a variable to be used as a comparison.

thank you all once again