简体   繁体   中英

Python fromtimestamp() method inconsistent

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.

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. The problem that I am seeing is different results using Python to convert the time stamp on a Windows and MAC machine.

# 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')

The python documentation for the fromtimestamp() method says the following:

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().

That documentation can be found here: https://docs.python.org/2/library/datetime.html

Now this works perfectly on my windows machine.

Line 1 Results on Windows : 1474560434.73

Plugging this into a timestamp converter results in:

GMT: Thu, 22 Sep 2016 16:07:14 GMT

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

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:

Line 2 Results on Windows : '2016-09-22 11:07:14'

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

Line 1 Results on MAC : 1474560434.73

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

Line 2 Results on MAC : '2016-09-22 16:07:14'

No good. This is the result, still in UTC.

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

t = time.localtime()
u = time.gmtime(time.mktime(t))
offset = timegm(t) - timegm(u)

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.

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.

Python probably uses the TZ from OS/shell environment.

PyCharm seems to use TIME_ZONE found in settings.py file. Here is the complete list of timezones. Try it and see.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM