Python datetime difference between .localize and tzinfo
<p>Why do these two lines produce different results? </p>

<pre><code>&gt;&gt;&gt; import pytz
&gt;&gt;&gt; from datetime ipmort datetime

&gt;&gt;&gt; local_tz = pytz.timezone("America/Los_Angeles")

&gt;&gt;&gt; d1 = local_tz.localize(datetime(2015, 8, 1, 0, 0, 0, 0)) # line 1
&gt;&gt;&gt; d2 = datetime(2015, 8, 1, 0, 0, 0, 0, local_tz) # line 2
&gt;&gt;&gt; d1 == d2
False
</code></pre>

<p>What's the reason for the difference, and which should I use to localize a datetime?</p>

<p>When you create <code>d2=datetime(2015, 8, 1, 0, 0, 0, 0, local_tz)</code> in this way. It does not handle daylight savings time correctly. But, <code>local_tz.localize()</code> does.</p>

<p>d1 is </p>

<pre><code>datetime.datetime(2015, 8, 1, 0, 0, 
                  tzinfo=&lt;DstTzInfo 'America/Los_Angeles' PDT-1 day, 17:00:00 DST&gt;)
</code></pre>

<p>d2 is</p>

<pre><code>datetime.datetime(2015, 8, 1, 0, 0, 
                  tzinfo=&lt;DstTzInfo 'America/Los_Angeles' PST-1 day, 16:00:00 STD&gt;)
</code></pre>

<p>You can see that they are not representing the same time.</p>

<p><code>d2</code> way it's fine if you are gonna work with UTC. Because UTC does not have daylight savings time transitions to deal with. </p>

<p>So, the correct way to handle timezone, it's using <code>local_tz.localize()</code></p>

python datetime timezone pytz