3

I stumbled across an issue running Python 2.7.8 on Windows 8.1. I was trying to pull the UTC offset for the local timezone of the computer using time.strftime('%z'), I was expecting to get back a UTC offset (-0700) instead I got the full timezone name (Pacific Daylight Time). I re-ran strftime() on another Python interpreter (on a RaspberryPi running Python 2.7.3) and got the expected UTC offset.

The code ran on the windows machine with output is

>>> import time
>>> time.strftime('%z')
'Pacific Daylight Time'

The code ran on the RaspberryPi was identical, except for the output (the RaspberryPi's local time is set to UTC)

>>> import time
>>> time.strftime('%z')
'+0000'

I know most of Python's time methods are wrappers to system calls, is this a case where the underlying system call isn't working as described for did the implementation of strftime's formatting characters change from 2.7.3 and 2.7.8 (although the docs for 2.7.8 say lowercase z should return a UTC offset)?

Some more information about the timezone and locale information of the Windows computer:

>>> import locale; locale.getlocale(); locale.getdefaultlocale()
(None, None)
('en_US', 'cp1252')
>>> time.tzname
('Pacific Standard Time', 'Pacific Daylight Time')
rcopley
  • 31
  • 1
  • 3
  • 1
    Could you please add the output of `import locale; locale.getlocale(); locale.getdefaultlocale()` and `time.tzname` to your question? (Particularly for the Windows system). – Lukas Graf Oct 14 '14 at 20:26
  • 1
    The docs are based on the output of a library which is *not* the Windows implementation, and this is noted in the documentation somewhere. There have also been mailing list discussions on how prominent this warning should be. – Mark Ransom Oct 14 '14 at 20:30
  • 1
    In 2.7, [`time.strftime`](https://docs.python.org/2/library/time.html#time.strftime) isn't even documented to support `'%z'` _at all_, much less consistently across platforms. – abarnert Oct 14 '14 at 20:30
  • 1
    Also see [footnote 1](https://docs.python.org/2/library/time.html#id2), which explains that "the `%z` escape that expands to the preferred hour/minute offset is not supported by all ANSI C libraries". – abarnert Oct 14 '14 at 20:31
  • @Cameron: You're right, it's a dup. The answers there don't really explain things very well, but I'll add a new one over there, and we can close this as a dup… – abarnert Oct 14 '14 at 20:34
  • @Cameron thanks for the link, that's exactly the same issue, I tried searching for it and couldn't find anything. The bug report had a workaround that appears to work `datetime.now(tz.tzlocal()).strftime('%z')` – rcopley Oct 14 '14 at 20:36
  • 1
    related: [Getting computer's utc offset in Python](http://stackoverflow.com/q/3168096/4279) – jfs Oct 16 '14 at 08:35

0 Answers0