Skip to content

Instantly share code, notes, and snippets.

@macintux
Last active September 7, 2019 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macintux/78516cba33cf8d58868917b7043d2d50 to your computer and use it in GitHub Desktop.
Save macintux/78516cba33cf8d58868917b7043d2d50 to your computer and use it in GitHub Desktop.
I've often found time frustrating to deal with in Python. There's a popular library that completely ignores time zones (although regrettably at the moment I can't remember what it is). This, though, is particularly egregious: time.strftime (which relies on local libraries) is giving completely wrong values for time zones.

Python 3.7.3, MacOS 10.14.5

>>> epoch = int(datetime.datetime.now().timestamp())
>>> epoch
1567872682
>>> gmt = time.gmtime(epoch)
>>> gmt.tm_zone
'UTC'
>>> time.strftime('%Z', gmt)
'UTC'
>>> time.strftime('%z', gmt)
'-0500'

I'm currently at -0400 (EDT) and my best guess is that because the tm_isdst flag is set to false, it's interpreting my Eastern time zone as it would be when we're no longer on DST.

time.struct_time(tm_year=2019, tm_mon=9, tm_mday=7, tm_hour=16, tm_min=11, tm_sec=22, tm_wday=5, tm_yday=250, tm_isdst=0)

In my Anaconda install on Windows 7, it's worse (or maybe better: more consistently broken).

Python 3.7.1, Windows 7

>>> gmt.tm_zone
'UTC'
>>> time.strftime('%Z', gmt)
'Eastern Standard Time'
>>> time.strftime('%z', gmt)
'-0500'

On Amazon Linux, Python 3.6, the system works properly. In fact it addresses another problem with the above: technically UTC isn't a time zone, while GMT is.

Python 3.6.4, Amazon Linux

>>> gmt = time.gmtime(epoch)
>>> gmt.tm_zone
'GMT'
>>> time.strftime('%Z', gmt)
'GMT'
>>> time.strftime('%z', gmt)
'+0000'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment