回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我对pytz的.localize()函数有一些奇怪的问题。有时它不会调整本地化的日期时间:</p>
<p>。行为本地化:</p>
<pre><code>>>> tz
<DstTzInfo 'Africa/Abidjan' LMT-1 day, 23:44:00 STD>
>>> d
datetime.datetime(2009, 9, 2, 14, 45, 42, 91421)
>>> tz.localize(d)
datetime.datetime(2009, 9, 2, 14, 45, 42, 91421,
tzinfo=<DstTzInfo 'Africa/Abidjan' GMT0:00:00 STD>)
>>> tz.normalize(tz.localize(d))
datetime.datetime(2009, 9, 2, 14, 45, 42, 91421,
tzinfo=<DstTzInfo 'Africa/Abidjan' GMT0:00:00 STD>)
</code></pre>
<p>如您所见,时间并没有因为本地化/规范化操作而改变。
但是,如果使用.replace:</p>
<pre><code>>>> d.replace(tzinfo=tz)
datetime.datetime(2009, 9, 2, 14, 45, 42, 91421,
tzinfo=<DstTzInfo 'Africa/Abidjan' LMT-1 day, 23:44:00 STD>)
>>> tz.normalize(d.replace(tzinfo=tz))
datetime.datetime(2009, 9, 2, 15, 1, 42, 91421,
tzinfo=<DstTzInfo 'Africa/Abidjan' GMT0:00:00 STD>)
</code></pre>
<p>它似乎在调整日期时间。</p>
<p>问题是-哪个是正确的,为什么其他人错了?</p>