Converting datetime to POSIX time

import time, datetime

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

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

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

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)

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