-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
time.strftime handling %z/%Z badly #82232
Comments
My apologies if this is a duplicate; there are more than a few bug reports on time and strftime, but I wasn't able to locate any reporting this specifically. I'd be happy to do more in-depth tests with some guidance. I see that this is largely the responsibility of system libraries, but it seems likely that Python is doing something incorrectly when invoking them since the behavior is broken in both Mac and Windows. On both my personal Mac laptop and work Windows laptop, time.strftime('%z') gives the wrong output. On Windows, '%Z' also fails. 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. Technically displaying "UTC" is also a bug, since the actual time zone is "GMT". Python 3.6 on my Amazon Linux instance handles everything correctly. Mac, 3.7.3: >>> 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' Python 3.7.1, Windows 7 >>> gmt.tm_zone
'UTC'
>>> time.strftime('%Z', gmt)
'Eastern Standard Time'
>>> time.strftime('%z', gmt)
'-0500' Python 3.6.8, Amazon Linux >>> epoch = int(datetime.datetime.now().timestamp())
>>> gmt = time.gmtime(epoch)
>>> gmt.tm_zone
'GMT'
>>> time.strftime('%Z', gmt)
'GMT'
>>> time.strftime('%z', gmt)
'+0000' |
This issue still exists in Python 3.10.4. On Windows, import time
print(time.strftime("%Y-%m-%d %H:%M:%S %z", time.gmtime()))
print(time.strftime("%Y-%m-%d %H:%M:%S %z", time.localtime()))
# outputs
# 2022-06-01 11:22:12 +0800
# 2022-06-01 19:22:12 +0800 You see strftime's %z on that gmttime() is not right. |
Reproduced on:
Note that datetime works as expected. |
This is a bug but it seems to be os-windows and os-mac since I cannot reproduce on Linux and neither could the user. |
Possibly related: #121782 |
In case this can help, in cmd:
Could it be related to tzset ? |
I suspect the problem here is probably that Then when you ask the naive time to display its timezone, it's probably picking your local one (hence I'm not a datetime expert, and I'm not sure who's most active to have a think about this and how easily it can be changed (or even what the change is). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: