Date with a time zone specified as a string is parsed as naive
<p>I'm curious why the timezone in this example, <code>GMT</code>, is not parsed as a valid one:</p>

<pre><code>&gt;&gt;&gt; from datetime import datetime
&gt;&gt;&gt; import pytz
&gt;&gt;&gt; b = 'Mon, 3 Oct 2016 21:24:17 GMT'
&gt;&gt;&gt; fmt = '%a, %d %b %Y %H:%M:%S %Z'
&gt;&gt;&gt; datetime.strptime(b, fmt).astimezone(pytz.utc)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
ValueError: astimezone() cannot be applied to a naive datetime
</code></pre>

<p>Doing the same with a <code>-0700</code> instead of <code>GMT</code> and <code>%z</code> instead of <code>%Z</code> in the format works just fine.</p>

<p>What's the proper way to parse dates ending in string time zones if not this?</p>

<p>Use <code>.replace()</code> method with <code>datetime</code> object to update the time zone info.</p>

<pre><code>&gt;&gt;&gt; datetime.strptime(b, fmt).replace(tzinfo=pytz.utc)
datetime.datetime(2016, 10, 3, 21, 24, 17, tzinfo=&lt;UTC&gt;)
</code></pre>

<p>Since you mentioned, <code>.astimezone()</code> is working with <code>%Z</code> instead of <code>%s</code> in the format string. Even though there is <code>z</code> in both the formatting (difference in just case), but they are totally different in terms of what they represent. </p>

<p>As per the <a href="http://strftime.org/" rel="nofollow"><code>strftime</code></a>'s directive document:</p>

<blockquote>
  <p><strong>%z</strong> :  UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).</p>
  
  <p><strong>%Z</strong> :  Time zone name (empty string if the object is naive).</p>
</blockquote>

python datetime