title: How to make a timezone aware datetime object
body: <p><strong>What I need to do</strong></p>
  <p>I have a timezone-unaware datetime object, to which I need to add a time zone in order to be able to compare it with other timezone-aware datetime objects. I do not want to convert my entire application to timezone unaware for this one legacy case.</p>
  <p><strong>What I've Tried</strong></p>
  <p>First, to demonstrate the problem:</p>
  <pre><code>Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
  [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
  Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
  &gt;&gt;&gt; import datetime
  &gt;&gt;&gt; import pytz
  &gt;&gt;&gt; unaware = datetime.datetime(2011,8,15,8,15,12,0)
  &gt;&gt;&gt; unaware
  datetime.datetime(2011, 8, 15, 8, 15, 12)
  &gt;&gt;&gt; aware = datetime.datetime(2011,8,15,8,15,12,0,pytz.UTC)
  &gt;&gt;&gt; aware
  datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=&lt;UTC&gt;)
  &gt;&gt;&gt; aware == unaware
  Traceback (most recent call last):
    File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  TypeError: can't compare offset-naive and offset-aware datetimes
  </code></pre>
  <p>First, I tried astimezone:</p>
  <pre><code>&gt;&gt;&gt; unaware.astimezone(pytz.UTC)
  Traceback (most recent call last):
    File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  ValueError: astimezone() cannot be applied to a naive datetime
  &gt;&gt;&gt;
  </code></pre>
  <p>It's not terribly surprising this failed, since it's actually trying to do a conversion.  Replace seemed like a better choice (as per <a href="https://stackoverflow.com/questions/4530069/python-how-to-get-a-value-of-datetime-today-that-is-timezone-aware">How do I get a value of datetime.today() in Python that is &quot;timezone aware&quot;?</a>):</p>
  <pre><code>&gt;&gt;&gt; unaware.replace(tzinfo=pytz.UTC)
  datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=&lt;UTC&gt;)
  &gt;&gt;&gt; unaware == aware
  Traceback (most recent call last):
    File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  TypeError: can't compare offset-naive and offset-aware datetimes
  &gt;&gt;&gt; 
  </code></pre>
  <p>But as you can see, replace seems to set the tzinfo, but not make the object aware.  I'm getting ready to fall back to doctoring the input string to have a timezone before parsing it (I'm using dateutil for parsing, if that matters), but that seems incredibly kludgy.</p>
  <p>Also, I've tried this in both Python 2.6 and Python 2.7, with the same results.</p>
  <p><strong>Context</strong></p>
  <p>I am writing a parser for some data files. There is an old format I need to support where the date string does not have a timezone indicator. I've already fixed the data source, but I still need to support the legacy data format. A one time conversion of the legacy data is not an option for various business BS reasons. While in general, I do not like the idea of hard-coding a default timezone, in this case it seems like the best option. I know with reasonable confidence that all the legacy data in question is in UTC, so I'm prepared to accept the risk of defaulting to that in this case.</p>