擅长:python、mysql、java
<p>一般来说,要将任意时区感知的日期时间转换为原始(本地)日期时间,我将使用<code>pytz</code>模块和<code>astimezone</code>转换为本地时间,并使用<code>replace</code>使日期时间变为原始:</p>
<pre><code>In [76]: import pytz
In [77]: est=pytz.timezone('US/Eastern')
In [78]: d.astimezone(est)
Out[78]: datetime.datetime(2010, 10, 30, 13, 21, 12, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)
In [79]: d.astimezone(est).replace(tzinfo=None)
Out[79]: datetime.datetime(2010, 10, 30, 13, 21, 12)
</code></pre>
<p>但是,由于您的特定日期时间似乎位于UTC时区,您可以这样做:</p>
<pre><code>In [65]: d
Out[65]: datetime.datetime(2010, 10, 30, 17, 21, 12, tzinfo=tzutc())
In [66]: import datetime
In [67]: import calendar
In [68]: datetime.datetime.fromtimestamp(calendar.timegm(d.timetuple()))
Out[68]: datetime.datetime(2010, 10, 30, 13, 21, 12)
</code></pre>
<hr/>
<p>顺便说一下,最好将日期时间存储为原始的UTC日期时间,而不是原始的本地日期时间。这样,您的数据是本地时间不可知的,您只在必要时转换为本地时间或任何其他时区。类似于尽可能多地使用unicode,并且只在必要时才进行编码。</p>
<p>因此,如果您同意以原始UTC格式存储日期时间是最好的方法,那么您只需要定义:</p>
<pre><code>local_d = d.replace(tzinfo=None)
</code></pre>