title: Why does the Django time zone setting effect epoch time?
body: <p>I have a small Django project that imports data dumps from MongoDB into MySQL. Inside these Mongo dumps are dates stored in epoch time. I would expect epoch time to be the same regardless of time zone but what I am seeing is that the Django <a href="https://docs.djangoproject.com/en/dev/ref/settings/#std%3asetting-TIME_ZONE" rel="nofollow">TIME_ZONE</a> setting has an effect on the data created in MySQL.</p>
  
  <p>I have been testing my database output with the MySQL <a href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_unix-timestamp" rel="nofollow">UNIX_TIMESTAMP</a> function. If I insert a date with the epoch of <code>1371131402880</code> (this includes milliseconds) I have my timezone set to <code>'America/New_York'</code>, UNIX_TIMESTAMP gives me <code>1371131402</code>, which is the same epoch time excluding milliseconds. However if I set my timezone to <code>'America/Chicago'</code> I get <code>1371127802</code>.</p>
  
  <p>This is my code to convert the epoch times into Python <code>datetime</code> objects,</p>
  
  <pre><code>from datetime import datetime
  from django.utils.timezone import utc
  
  secs = float(epochtime) / 1000.0
  dt = datetime.fromtimestamp(secs)
  </code></pre>
  
  <p>I tried to fix the issue by putting an explict timezone on the <code>datetime</code> object,</p>
  
  <pre><code># epoch time is in UTC by default
  dt = dt.replace(tzinfo=utc)
  </code></pre>
  
  <p><a href="http://pythonfiddle.com/tests-for-epoch-issue" rel="nofollow">PythonFiddle for the code</a></p>
  
  <p>I've tested this Python code in isolation and it gives me the expected results. However it does not give the correct results after inserting these object into MySQL through a Django model <a href="https://docs.djangoproject.com/en/dev/ref/models/fields/#datetimefield" rel="nofollow">DateTimeField</a> field.</p>
  
  <p>Here is my MySQL query,</p>
  
  <pre><code>SELECT id, `date`, UNIX_TIMESTAMP(`date`) FROM table
  </code></pre>
  
  <p>I test this by comparing the unix timestamp column in the result of this query against the MongoDB JSON dumps to see if the epoch matches. </p>
  
  <p>What exactly is going on here? Why should timezone have any effect on epoch times?</p>
  
  <p>Just for reference, I am using Django 1.5.1 and MySQL-python 1.2.4. I also have the Django <a href="https://docs.djangoproject.com/en/dev/topics/i18n/timezones/" rel="nofollow">USE_TZ</a> flag set to <code>true</code>.</p>