Python 2:
def datetime_to_float(d):
epoch = datetime.datetime.utcfromtimestamp(0)
total_seconds = (d - epoch).total_seconds()
# total_seconds will be in decimals (millisecond precision)
return total_seconds
def float_to_datetime(fl):
return datetime.datetime.fromtimestamp(fl)
Python 3:
def datetime_to_float(d):
return d.timestamp()
The python 3 version of float_to_datetime
will be no different from the python 2 version above.