<p><a href="https://stackoverflow.com/q/11743019/190597">Don't use ^{<cd1>}. It is not supported, and may silently fail.</a>相反,要将UTC日期时间转换为时间戳,请使用<a href="https://stackoverflow.com/a/8778548/190597">one of the methods shown here</a>,具体取决于您的Python版本:</p>
<h3>Python 3.3+:</h3>
<pre><code>timestamp = dt.timestamp()
</code></pre>
<h3>Python3(<;3.3):</h3>
^{pr2}$
<h3>Python 2.7+:</h3>
<pre><code>timestamp = (dt.replace(tzinfo=None) - datetime(1970, 1, 1)).total_seconds()
</code></pre>
<h3>Python2号(<;2.7):</h3>
<pre><code>def totimestamp(dt, epoch=datetime(1970,1,1)):
td = dt - epoch
# return td.total_seconds()
return (td.microseconds + (td.seconds + td.days * 86400) * 10**6) / 10**6
timestamp = totimestamp(dt.replace(tzinfo=None))
</code></pre>
<hr/>
<p>因此,您的<code>convert_mills_GMT</code>应该是</p>
<pre><code>def convert_mills_GMT(milliseconds,
utc=pytz.utc,
eastern=pytz.timezone('US/Eastern')
):
converted_raw = DT.datetime.fromtimestamp(milliseconds/1000.0)
date_eastern = eastern.localize(converted_raw, is_dst=True)
date_utc = date_eastern.astimezone(utc)
timestamp = ...
return int(timestamp) * 1000
</code></pre>
<hr/>
<p>例如,对于Python2.7</p>
<pre><code>import datetime as DT
import pytz
def convert_mills_GMT(milliseconds,
utc=pytz.utc,
eastern=pytz.timezone('US/Eastern')
):
converted_raw = DT.datetime.fromtimestamp(milliseconds/1000.0)
date_eastern = eastern.localize(converted_raw, is_dst=True)
date_utc = date_eastern.astimezone(utc)
timestamp = ((date_utc.replace(tzinfo=None) - DT.datetime(1970, 1, 1))
.total_seconds())
return int(timestamp) * 1000
print(DT.datetime.utcfromtimestamp(convert_mills_GMT(1432202088224)/1000.0))
</code></pre>
<p>印刷品</p>
<pre><code>2015-05-21 09:54:48
</code></pre>