[Python-checkins] bpo-36779: time.tzname returns empty string on Windows if default cod… (GH-13073)

Miss Islington (bot) webhook-mailer at python.org
Thu Jun 13 09:42:56 EDT 2019

commit: 6a433f5ae63de72a85d20b05ff826c6f72d529b7
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-06-13T06:42:46-07:00

bpo-36779: time.tzname returns empty string on Windows if default cod… (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


(cherry picked from commit b4c7defe58695a6670a8fdeaef67a638bbb47e42)

Co-authored-by: Paul Monson <paulmon at users.noreply.github.com>

A Misc/NEWS.d/next/Windows/2019-06-11-15-41-34.bpo-36779.0TMw6f.rst
M Modules/timemodule.c

diff --git a/Misc/NEWS.d/next/Windows/2019-06-11-15-41-34.bpo-36779.0TMw6f.rst b/Misc/NEWS.d/next/Windows/2019-06-11-15-41-34.bpo-36779.0TMw6f.rst
new file mode 100644
index 000000000000..618cfcae7b89
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2019-06-11-15-41-34.bpo-36779.0TMw6f.rst
@@ -0,0 +1,2 @@
+Ensure ``time.tzname`` is correct on Windows when the active code page is
+set to CP_UTF7 or CP_UTF8.
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index ae7de5b2c766..4c8e2cb2344b 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -1571,6 +1571,19 @@ init_timezone(PyObject *m)
     PyModule_AddIntConstant(m, "altzone", _Py_timezone-3600);
     PyModule_AddIntConstant(m, "daylight", _Py_daylight);
+#ifdef MS_WINDOWS
+    TIME_ZONE_INFORMATION tzinfo = {0};
+    GetTimeZoneInformation(&tzinfo);
+    otz0 = PyUnicode_FromWideChar(tzinfo.StandardName, -1);
+    if (otz0 == NULL) {
+        return -1;
+    }
+    otz1 = PyUnicode_FromWideChar(tzinfo.DaylightName, -1);
+    if (otz1 == NULL) {
+        Py_DECREF(otz0);
+        return -1;
+    }
     otz0 = PyUnicode_DecodeLocale(_Py_tzname[0], "surrogateescape");
     if (otz0 == NULL) {
         return -1;
@@ -1580,6 +1593,7 @@ init_timezone(PyObject *m)
         return -1;
+#endif // MS_WINDOWS
     PyObject *tzname_obj = Py_BuildValue("(NN)", otz0, otz1);
     if (tzname_obj == NULL) {
         return -1;

More information about the Python-checkins mailing list