Converting datetime to POSIX time

PythonDatetimePosix

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

import time, datetime

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

Solution 2 - Python

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

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

Solution 3 - Python

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)

Solution 4 - Python

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

Solution 5 - Python

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)

Solution 6 - Python

It depends

Is your datetime object timezone aware or naive?

Timezone Aware

If it is aware it's simple

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJason BakerView Question on Stackoverflow
Solution 1 - PythonkenderView Answer on Stackoverflow
Solution 2 - PythonfixermarkView Answer on Stackoverflow
Solution 3 - PythonClémentView Answer on Stackoverflow
Solution 4 - Pythongnu_lorienView Answer on Stackoverflow
Solution 5 - PythonvishalView Answer on Stackoverflow
Solution 6 - PythonMarcView Answer on Stackoverflow