Python2 vs Python3: Different result when converting to datetime from timestamp Python2 vs Python3: Different result when converting to datetime from timestamp python-3.x python-3.x

Python2 vs Python3: Different result when converting to datetime from timestamp


This is a little bit tricky. According to my knowledge, this only happen with python 3.6. For short, you need to call time.tzset after set TZ environment. I've encountered it sometime ago (I don't remember exactly), and I don't have pre-3.6 python to test, so please bear with me. I've just checked this issue on my colleague's python3.5, it works as expected (without putting time.tzset())

The time.tzset docs say:

Reset the time conversion rules used by the library routines. The environment variable TZ specifies how this is done. It will also set the variables tzname (from the TZ environment variable), timezone (non-DST seconds West of UTC), altzone (DST seconds west of UTC) and daylight (to 0 if this timezone does not have any daylight saving time rules, or to nonzero if there is a time, past, present or future when daylight saving time applies).

Just put the time.tzset():

See the results

EDITED: I've just made some search, this behavior had been (mistakenly) reported as a bug: datetime in Python 3.6+ no longer respects 'TZ' environment variable