Jason Baker
Jason Baker

Reputation: 198707

Converting datetime to POSIX time

How do I convert a datetime or date object into a POSIX timestamp in python? There are methods to create a datetime object out of a timestamp, but I don't seem to find any obvious ways to do the operation the opposite way.

Upvotes: 57

Views: 47797

Answers (6)

Marc
Marc

Reputation: 5518

It depends

Is your datetime object timezone aware or naive?

Timezone Aware

If it is aware it's simple (and recommended)

from datetime import datetime, timezone
aware_date = datetime.now(tz=timezone.utc)
posix_timestamp = aware_date.timestamp()

as date.timestamp() gives you "POSIX timestamp"

NOTE: more accurate to call it an epoch/unix timestamp as it may not be POSIX compliant

Timezone Naive

If it's not timezone aware (naive), then you'd need to know what timezone it was originally in so we can use replace() to convert it into a timezone aware date object. Let's assume that you've stored/retrieved it as UTC Naive. Here we create one, as an example:

from datetime import datetime, timezone
naive_date = datetime.utcnow()  # this date is naive, but is UTC based
aware_date = naive_date.replace(tzinfo=timezone.utc)  # this date is no longer naive

# now we do as we did with the last one

posix_timestamp = aware_date.timestamp()

It's always better to get to a timezone aware date as soon as you can to prevent issues that can arise with naive dates (as Python will often assume they are local times and can mess you up)

NOTE: also be careful with your understanding of the epoch as it is platform dependent

Upvotes: 2

Clément
Clément

Reputation: 12937

Note that Python now (3.5.2) includes a built-in method for this in datetime objects:

>>> import datetime
>>> now = datetime.datetime(2020, 11, 18, 18, 52, 47, 874766)
>>> now.timestamp() # Local time
1605743567.874766
>>> now.replace(tzinfo=datetime.timezone.utc).timestamp() # UTC
1605725567.874766 # 5 hours delta (I'm in UTC-5)

Upvotes: 15

vishal
vishal

Reputation: 905

Best conversion from posix/epoch to datetime timestamp and the reverse:

this_time = datetime.datetime.utcnow() # datetime.datetime type
epoch_time = this_time.timestamp()      # posix time or epoch time
this_time = datetime.datetime.fromtimestamp(epoch_time)

Upvotes: 0

gnu_lorien
gnu_lorien

Reputation: 3114

In python, time.time() can return seconds as a floating point number that includes a decimal component with the microseconds. In order to convert a datetime back to this representation, you have to add the microseconds component because the direct timetuple doesn't include it.

import time, datetime

posix_now = time.time()

d = datetime.datetime.fromtimestamp(posix_now)
no_microseconds_time = time.mktime(d.timetuple())
has_microseconds_time = time.mktime(d.timetuple()) + d.microsecond * 0.000001

print posix_now
print no_microseconds_time
print has_microseconds_time

Upvotes: 4

fixermark
fixermark

Reputation: 1260

For UTC calculations, calendar.timegm is the inverse of time.gmtime.

import calendar, datetime
d = datetime.datetime.utcnow()
print calendar.timegm(d.timetuple())

Upvotes: 23

kender
kender

Reputation: 87211

import time, datetime

d = datetime.datetime.now()
print time.mktime(d.timetuple())

Upvotes: 67

Related Questions