擅长:python、mysql、java
<p>Python2:</p>
<pre><code>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)
</code></pre>
<hr/>
<p>Python3:</p>
<pre><code>def datetime_to_float(d):
return d.timestamp()
</code></pre>
<p>python 3版本的<code>float_to_datetime</code>与上面的python 2版本没有区别。</p>