Uh oh!
There was an error while loading. Please reload this page.
bpo-36779: time.tzname returns empty string on Windows if default cod… - #13073
Conversation
…epage is a Unicode codepage
This workaround enables time.tzname to work on Windows ARM32. |
matrixise
commented
May 22, 2019
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. |
paulmon
commented
May 22, 2019
Thank you! |
zooba
commented
May 22, 2019
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 :) |
paulmon
commented
May 22, 2019
I'll try to get the timezone name without changing the locale. |
pganssle
commented
May 22, 2019
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): |
pganssle
commented
May 23, 2019
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: importtimefromdatetimeimportdatetimefortzseconds, tznamein ((0, 'UTC'), (0, 'GMT'),
(-time.timezone, time.tzname[0])):
print("-----")
print(f"tzseconds: {tzseconds}; tzname: {tzname}")
iftzseconds<0:
sign='-'seconds=-tzsecondselse:
sign='+'seconds=tzsecondshours, 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 |
paulmon
commented
May 23, 2019
I think it doesn't raise an exception because it's valid to use "" as parameter to a format string. |
pganssle
commented
May 23, 2019
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: timedata'-0800 'doesnotmatchformat'%z %Z'>>>datetime.strptime("-0800 Pacific Standard Time", "%z %Z")
...
ValueError: timedata'-0800 Pacific Standard Time'doesnotmatchformat'%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 |
paulmon
commented
May 23, 2019
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
commented
May 29, 2019
zooba
commented
Jun 11, 2019
@paulmon I think this one deserves a NEWS item - could you add it? |
miss-islington
commented
Jun 12, 2019
@paulmon: Status check is done, and it's a success ✅ . |
miss-islington
commented
Jun 12, 2019
Thanks @paulmon for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8. |
bedevere-bot
commented
Jun 12, 2019
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 @zoobahttps://bugs.python.org/issue36779 (cherry picked from commit b4c7def) Co-authored-by: Paul Monson <paulmon@users.noreply.github.com>
miss-islington
commented
Jun 13, 2019
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 @zoobahttps://bugs.python.org/issue36779 (cherry picked from commit b4c7def) Co-authored-by: Paul Monson <paulmon@users.noreply.github.com>
bedevere-bot
commented
Jun 13, 2019
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 @zoobahttps://bugs.python.org/issue36779 (cherry picked from commit b4c7def) Co-authored-by: Paul Monson <paulmon@users.noreply.github.com>
@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 |
bedevere-bot
commented
Jun 17, 2019
|
vstinner
commented
Jun 17, 2019
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 |
vstinner
commented
Jun 17, 2019
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 @zoobahttps://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 @zoobahttps://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