Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python datetime to epoch

t='20180515102500'
d=datetime.strptime(t, "%Y%m%d%H%M%S")
millis_since_epoch = int(time.mktime(d.timetuple())) * 1000
print(millis_since_epoch)

gives me: 1526379900000 on repl.it(python 3.6.1) and on my local: 1526397900000 (python 2.7.14)

Why? What is the recommended way to convert datetime object to epoch?

like image 560
user1179299 Avatar asked May 15 '18 17:05

user1179299


People also ask

What is epoch time in Python?

Epoch time is also known as unix time or posix time or number of seconds since epoch. It is the number of seconds since 1st January, 1970 00:00:00 UTC. datetime module is used to work with different date-time related operations in Python.

How to convert Python datetime to epoch with strftime?

To convert python datetime to epoch with strftime you'd need to provide the strftime function with a formatting string that contains the code for formatting the date as seconds since epoch. The %s directive is used for this purpose. ... If you're on Python 3.3+, you can simply use the timestamp function to get the time since epoch. example

How to convert timestamp or epoch to human readble date?

We can convert timestamp or epoch to human readble date. A Unix timestamp is the number of seconds between a particular date and January 1, 1970 at UTC. You can convert a timestamp to a datetime or date using fromtimestamp () method. We can convert the date or datetime to a timestamp.

How to convert a Python datetime to seconds?

If you want to convert a python datetime to seconds since epoch you could do it explicitly: >>> (datetime.datetime(2012,04,01,0,0) - datetime.datetime(1970,1,1)).total_seconds() 1333238400.0 In Python 3.3+ you can use timestamp()instead:


2 Answers

Your problem is that you're using naive datetime (and struct_tm and so on) objects. So you're asking how far 15 May 2018 10:25:00 in "whatever the local timezone on this machine is" is from the Unix epoch. If you run it on two machines with timezones 5 hours apart, you will get results that are 5*3600*1000 milliseconds apart.

The recommended way to do it is either:

d.timestamp() # returns difference from epoch as float seconds

… or:

d - datetime.fromtimestamp(0) # returns difference as timedelta

(It should be obvious how to convert either of those to float millis, or whatever else you need.)

But neither of those will make any difference here, since you're still using naive datetime objects. (At least you aren't mixing in other types from the time module and throwing away precision, but you're not solving your problem.)


The solution to your problem depends on what you're trying to do. Are these UTC timestamps? Are they timestamps in some known timezone that you know out-of-band?

If everything is UTC, the best thing to do is to explicitly create UTC objects, but the simplest thing to do (especially if you need your code to run in 2.7) is:

d - datetime.utcfromtimestamp(0)

This gives you a naive datetime representing 0 UTC seconds since the epoch, and subtracts it from your naive datetime representing X UTC seconds since the epoch, so you get a timedelta representing X seconds (specifically, 17666 days, 37500 seconds), without regard to what timezone the system is using.

Or, the smallest change to your existing code is to use utctimetuple instead of timetuple, which does the same thing in a more convoluted way.

like image 148
abarnert Avatar answered Sep 29 '22 09:09

abarnert


in python 3.6:

datetime.timestamp()

for example:

import datetime

timestamp = datetime.datetime.now().timestamp()
like image 36
Shelef Avatar answered Sep 29 '22 11:09

Shelef