Python fromtimestamp() method inconsistent
<p>I am getting data from a WSO2 DAS, which contains a Unix time stamp. I have been developing this website using PyCharm. I primarily develop on a Windows 10 machine, occasionally on a MAC, and deploy on a Linux box. </p>

<p>I have a fairly simple use case, where I want to take the date from WSO2, convert it to a human readable local time, and display it to the user. <strong>The problem</strong> that I am seeing is different results using Python to convert the time stamp on a Windows and MAC machine. </p>

<pre><code># Convert the millisecond time stamp to seconds
ts = int(hit['timestamp']) / 1000 

# Convert the timestamp to a human readable format
ts = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
</code></pre>

<p>The python documentation for the fromtimestamp() method says the following:</p>

<blockquote>
  <p>date.fromtimestamp(timestamp)
  Return the local date corresponding to the POSIX timestamp, such as is returned by time.time(). This may raise ValueError, if the timestamp is out of the range of values supported by the platform C localtime() function. Itâs common for this to be restricted to years from 1970 through 2038. Note that on non-POSIX systems that include leap seconds in their notion of a timestamp, leap seconds are ignored by fromtimestamp().</p>
</blockquote>

<p>That documentation can be found here:<a href="https://docs.python.org/2/library/datetime.html" rel="nofollow">https://docs.python.org/2/library/datetime.html</a> </p>

<p>Now this works perfectly on my windows machine. </p>

<p><strong>Line 1 Results on Windows</strong>: 1474560434.73</p>

<p>Plugging this into a timestamp converter results in:</p>

<p>GMT: Thu, 22 Sep 2016 16:07:14 GMT</p>

<p>Your time zone: 9/22/2016, 11:07:14 AM GMT-5:00 DST</p>

<p>Perfect! So I expect the result of line two to be 2016-09-22 11:07:14. This can be seen copied directly from my code:</p>

<p><strong>Line 2 Results on Windows</strong>: '2016-09-22 11:07:14'</p>

<p><strong>Now is where the fun begins.</strong> I have the exact same code on my MAC, also being run from PyCharm. Running the same code, line one results in:</p>

<p><strong>Line 1 Results on MAC</strong>: 1474560434.73</p>

<p>Same as above, we expect thsi to be 11:07:14 AM on 9/22. </p>

<p><strong>Line 2 Results on MAC</strong>: '2016-09-22 16:07:14'</p>

<p>No good. This is the result, still in UTC.</p>

<p>I have confirmed that both computers are set to my local time zone. What is <strong>even more weird</strong> is that while trying to debug this issue I found the following results:</p>

<pre><code>t = time.localtime()
u = time.gmtime(time.mktime(t))
offset = timegm(t) - timegm(u)
</code></pre>

<p>Running the above code in a terminal on my MAC gives the correct local time (UTC-5), the correct UTC time, and the offset of -18000 (which is correct). But! Running that same code in PyCharm on my MAC gives an offset of zero, and shows the local time and UTC time as the same. </p>

<p>I was originally confused by why the results are different on my Windows machine vs. MAC, but could assume that it was an OS difference. But now I am seeing differences on the MAC terminal vs. MAC PyCharm. All I can assume is that my MAC machine is referencing the correct time zone (which is why it works in the terminal) but PyCharm is referencing some other location for timezone and thinks that it is UTC. </p>

<p>Python probably uses the TZ from OS/shell environment.</p>

<p>PyCharm seems to use TIME_ZONE found in settings.py file. <a href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones" rel="nofollow">Here</a> is the complete list of timezones. Try it and see.</p>

python osx datetime pycharm