Technology Smiths Technology Smiths - 1 year ago 275
Python Question

Python 2.7 - how can I find the difference between two datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S.%f') timestamps in seconds/microseconds?

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.

Answer Source

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))


Time difference: 0:00:03.980000
Seconds: 3
Microseconds: 980000
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download