Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - Difference between two unix timestamps

Tags:

python-2.7

I have two timestamps in miliseconds and i want to compute the difference between the two in minutes:

d1 = 1502053449617 

current_time_utc = int(round(time.time() * 1000))

The values for d1 are dynamically generated by a third party API and are in UTC . I am trying to get the difference between the current time in UTC and d1.

fmt = '%Y-%m-%d %H:%M:%S'

 time1  = datetime.strptime(d1, fmt)
 time2  = datetime.strptime(current_time_utc, fmt)

I want to be able to find the difference between the two (time1 - time2) . If i do the below , i get an error saying "string expected, long given"

print(    time1-time2)

I want the difference between the two in minutes . Please help

like image 330
user1411837 Avatar asked Aug 10 '17 01:08

user1411837


People also ask

How do you find the difference between two timestamps in Python?

To get the difference between two-time, subtract time1 from time2. A result is a timedelta object. The timedelta represents a duration which is the difference between two-time to the microsecond resolution. To get a time difference in seconds, use the timedelta.

How do you find the difference between two timestamps?

If you'd like to calculate the difference between the timestamps in seconds, multiply the decimal difference in days by the number of seconds in a day, which equals 24 * 60 * 60 = 86400 , or the product of the number of hours in a day, the number of minutes in an hour, and the number of seconds in a minute.

How does Python compare UTC time?

Use the timezone() function(gets the time zone of a specific location) of the pytz module, to get the timezone of “CET”(local TimeZone) and store it in a variable. Get the UTC timezone using the timezone() function and store it in another variable. Convert the above time to another time zone using the datetime.

How do I get the difference between two dates and minutes in Python?

We created the two datetime objects from the two timestamps in the string format, by passing the date and it's format in the strptime() function. Then we subtracted these two datetime objects and got the datetime. timedelta object, which represents the duration between two dates.


2 Answers

You don't need to format the string, you just need to convert the timestamp directly, by first dividing it by 1000. Then its just a matter of printing out the differences (and calculating it in minutes):

from __future__ import division
import datetime

d1 = 1502053449617

converted_d1 = datetime.datetime.fromtimestamp(round(d1 / 1000))
current_time_utc = datetime.datetime.utcnow()

print((current_time_utc - converted_d1))
print((current_time_utc - converted_d1).total_seconds() / 60)

The above prints:

3 days, 5:08:14.087515
4628.234791916667
like image 115
Burhan Khalid Avatar answered Oct 22 '22 20:10

Burhan Khalid


I had to calculate the difference between two unix timestamps - but in days, as follows:

create two unix timestamps:

import datetime

timestamp1 = datetime.datetime(2017, 12, 1).strftime('%s')
timestamp2 = datetime.datetime(2017, 11, 14).strftime('%s')

print(timestamp1)
print(timestamp2)

1512079200
1510610400

calculate the day difference:

print((float(timestamp1)-float(timestamp2))/(60*60*24))

output:

17.0
like image 9
Grant Shannon Avatar answered Oct 22 '22 21:10

Grant Shannon