-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-36779: time.tzname returns empty string on Windows if default cod… #13073
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
Conversation
…epage is a Unicode codepage
This workaround enables time.tzname to work on Windows ARM32. |
Hi @paulmon I have edited the PR because that was the wrong BPO issue. Also, I have added this PR to the right bpo issue and removed it from the other one. |
Thank you! |
Definitely needs the tabs converted to spaces, and I wonder if there's a better (Windows-specific) way to get this value without changing the locale? If we're going to have an ifdef, we may as well use the proper APIs, provided the values come back the same (I have no idea what values to expect here). @pganssle may also have an opinion. Or perhaps not :) |
I'll try to get the timezone name without changing the locale. |
Regardless of the resolution, I think this needs a test. What is this supposed to return on Windows? What does it return with the workaround? I think you can definitely get the time zone and alt zone names without changing the locale. One way to get it is from the registry, which is how we do it in There may be a more convenient way as well. |
This is the fix for a failing test. It's supposed to return 'UTC-08:00' and it returns an empty string. FAIL: test_strptime (test.datetimetester.TestSubclassDateTime_Fast) Traceback (most recent call last): |
This is very confusing to me. This is the relevant test, and it seems like what it's doing is creating three strings: You can see what it's testing by running this script: import time
from datetime import datetime
for tzseconds, tzname in ((0, 'UTC'), (0, 'GMT'),
(-time.timezone, time.tzname[0])):
print("-----")
print(f"tzseconds: {tzseconds}; tzname: {tzname}")
if tzseconds < 0:
sign = '-'
seconds = -tzseconds
else:
sign ='+'
seconds = tzseconds
hours, minutes = divmod(seconds//60, 60)
dtstr = "{}{:02d}{:02d} {}".format(sign, hours, minutes, tzname)
print(dtstr) On my system, this returns:
I am not sure where |
On Windows IoT Core
on my dev machine (Windows 10 Enterprise)
|
UTC-0800 is my local time zone
This returns I tracked this this down to a bug in tzset() that sets _tzname[0] and _tzname[1] to empty strings when the default ANSI code page is set to CP_UTF8, or CP_UTF7. For more on the default ANSI code page see , see GetACP( ) The default ANSI code page on most Windows installs is not CP_UTF7, or CP_UTF8. On my dev machine for example it is 1252 (Code Page Identifiers). On Windows IoT Core the default ANSI code page is 65001 or CP_UTF8. So in line 2511 of datetimetester.py _time.tzname[0] is empty causing the test to fail.
Another way to check what the default ANSI codepage on your system is to run this command
|
I think it doesn't raise an exception because it's valid to use "" as parameter to a format string. |
This is what I expected, but I don't understand why the test was working in the first place, on Linux: >>> datetime.strptime("-0800 ", '%z %Z')
...
ValueError: time data '-0800 ' does not match format '%z %Z'
>>> datetime.strptime("-0800 Pacific Standard Time", "%z %Z")
...
ValueError: time data '-0800 Pacific Standard Time' does not match format '%z %Z' Is allowing empty format specifiers a Windows-specific thing? @paulmon If the two commands above don't fail for you, what do they return? What is |
Closing and re-opening to force azure pipelines to run again. |
There seems to be a connection between the value of time.tzname and the results of those two commands. Without the current GetTimeZoneInformation fix
With the GetTimeZoneInformation fix
|
@paulmon I think this one deserves a NEWS item - could you add it? |
@paulmon: Status check is done, and it's a success ✅ . |
Thanks @paulmon for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8. |
GH-14032 is a backport of this pull request to the 3.8 branch. |
GH-13073) (GH-14032) Calling setlocale(LC_CTYPE, "") on a system where GetACP() returns CP_UTF8 results in empty strings in _tzname[]. This causes time.tzname to be an empty string. I have reported the bug to the UCRT team and will follow up, but it will take some time get a fix into production. In the meantime one possible workaround is to temporarily change the locale by calling setlocale(LC_CTYPE, "C") before calling _tzset and restore the current locale after if the GetACP() == CP_UTF8 or CP_UTF7 @zooba https://bugs.python.org/issue36779 (cherry picked from commit b4c7def) Co-authored-by: Paul Monson <[email protected]>
Thanks @paulmon for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7. |
pythonGH-13073) Calling setlocale(LC_CTYPE, "") on a system where GetACP() returns CP_UTF8 results in empty strings in _tzname[]. This causes time.tzname to be an empty string. I have reported the bug to the UCRT team and will follow up, but it will take some time get a fix into production. In the meantime one possible workaround is to temporarily change the locale by calling setlocale(LC_CTYPE, "C") before calling _tzset and restore the current locale after if the GetACP() == CP_UTF8 or CP_UTF7 @zooba https://bugs.python.org/issue36779 (cherry picked from commit b4c7def) Co-authored-by: Paul Monson <[email protected]>
GH-14057 is a backport of this pull request to the 3.7 branch. |
GH-13073) Calling setlocale(LC_CTYPE, "") on a system where GetACP() returns CP_UTF8 results in empty strings in _tzname[]. This causes time.tzname to be an empty string. I have reported the bug to the UCRT team and will follow up, but it will take some time get a fix into production. In the meantime one possible workaround is to temporarily change the locale by calling setlocale(LC_CTYPE, "C") before calling _tzset and restore the current locale after if the GetACP() == CP_UTF8 or CP_UTF7 @zooba https://bugs.python.org/issue36779 (cherry picked from commit b4c7def) Co-authored-by: Paul Monson <[email protected]>
@paulmon Hey sorry I never followed up on this - this is belated, but for the record I agree with this change. Thank you for for doing this - particularly because I learned something new about how |
|
I reported this bug as: https://bugs.python.org/issue37314 |
It looks like a different commit caused this buildbot failure. The only file changed in this commit is timemodule.c, and the build failures are all in _iomodule.c |
Welcome to the exciting universe of buildbots! |
pythonGH-13073) Calling setlocale(LC_CTYPE, "") on a system where GetACP() returns CP_UTF8 results in empty strings in _tzname[]. This causes time.tzname to be an empty string. I have reported the bug to the UCRT team and will follow up, but it will take some time get a fix into production. In the meantime one possible workaround is to temporarily change the locale by calling setlocale(LC_CTYPE, "C") before calling _tzset and restore the current locale after if the GetACP() == CP_UTF8 or CP_UTF7 @zooba https://bugs.python.org/issue36779
pythonGH-13073) Calling setlocale(LC_CTYPE, "") on a system where GetACP() returns CP_UTF8 results in empty strings in _tzname[]. This causes time.tzname to be an empty string. I have reported the bug to the UCRT team and will follow up, but it will take some time get a fix into production. In the meantime one possible workaround is to temporarily change the locale by calling setlocale(LC_CTYPE, "C") before calling _tzset and restore the current locale after if the GetACP() == CP_UTF8 or CP_UTF7 @zooba https://bugs.python.org/issue36779
Calling setlocale(LC_CTYPE, "") on a system where GetACP() returns CP_UTF8 results in empty strings in _tzname[].
This causes time.tzname to be an empty string.
I have reported the bug to the UCRT team and will follow up, but it will take some time get a fix into production.
In the meantime one possible workaround is to temporarily change the locale by calling setlocale(LC_CTYPE, "C") before calling _tzset and restore the current locale after if the GetACP() == CP_UTF8 or CP_UTF7
@zooba
https://bugs.python.org/issue36779