From 9a62ff1adcb957da7b3cde6ddc4113a9f5582318 Mon Sep 17 00:00:00 2001 From: Paul Monson Date: Thu, 2 May 2019 17:59:00 -0700 Subject: [PATCH 1/4] bpo-36799: time.tzname returns empty string on Windows if default codepage is a Unicode codepage --- Modules/timemodule.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 3df17ac4fb68f5..2caba3b37bcdcd 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -1619,9 +1619,28 @@ init_timezone(PyObject *m) And I'm lazy and hate C so nyer. */ #ifdef HAVE_DECL_TZNAME +#ifdef MS_WINDOWS +# include + // https://bugs.python.org/issue36778 + // workaround bug in UCRT + // strptime fails if the current locale is a Unicode locale + int cp = GetACP(); + char* save_locale = NULL; + if (cp == CP_UTF8 || cp == CP_UTF7) + { + save_locale = setlocale(LC_CTYPE, NULL); + setlocale(LC_CTYPE, "C"); + } +#endif PyObject *otz0, *otz1; tzset(); PyModule_AddIntConstant(m, "timezone", _Py_timezone); +#ifdef MS_WINDOWS + if (cp == CP_UTF8 || cp == CP_UTF7) + { + setlocale(LC_CTYPE, save_locale); + } +#endif #ifdef HAVE_ALTZONE PyModule_AddIntConstant(m, "altzone", altzone); #else From 634776e56acce0ab6d10dce268a6a1f4eff51bff Mon Sep 17 00:00:00 2001 From: Paul Monson Date: Thu, 23 May 2019 13:03:54 -0700 Subject: [PATCH 2/4] fix tznames without changing locale --- Modules/timemodule.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 2caba3b37bcdcd..8abc62f653286f 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -1619,34 +1619,28 @@ init_timezone(PyObject *m) And I'm lazy and hate C so nyer. */ #ifdef HAVE_DECL_TZNAME -#ifdef MS_WINDOWS -# include - // https://bugs.python.org/issue36778 - // workaround bug in UCRT - // strptime fails if the current locale is a Unicode locale - int cp = GetACP(); - char* save_locale = NULL; - if (cp == CP_UTF8 || cp == CP_UTF7) - { - save_locale = setlocale(LC_CTYPE, NULL); - setlocale(LC_CTYPE, "C"); - } -#endif PyObject *otz0, *otz1; tzset(); PyModule_AddIntConstant(m, "timezone", _Py_timezone); -#ifdef MS_WINDOWS - if (cp == CP_UTF8 || cp == CP_UTF7) - { - setlocale(LC_CTYPE, save_locale); - } -#endif #ifdef HAVE_ALTZONE PyModule_AddIntConstant(m, "altzone", altzone); #else PyModule_AddIntConstant(m, "altzone", _Py_timezone-3600); #endif 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; + } +#else otz0 = PyUnicode_DecodeLocale(_Py_tzname[0], "surrogateescape"); if (otz0 == NULL) { return -1; @@ -1656,6 +1650,7 @@ init_timezone(PyObject *m) Py_DECREF(otz0); return -1; } +#endif // MS_WINDOWS PyObject *tzname_obj = Py_BuildValue("(NN)", otz0, otz1); if (tzname_obj == NULL) { return -1; From 1c78c19f6f1a35647fceeb271692111b3e291f55 Mon Sep 17 00:00:00 2001 From: Paul Monson Date: Tue, 11 Jun 2019 15:43:20 -0700 Subject: [PATCH 3/4] add news item --- .../next/Windows/2019-06-11-15-41-34.bpo-36779.0TMw6f.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Misc/NEWS.d/next/Windows/2019-06-11-15-41-34.bpo-36779.0TMw6f.rst 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 00000000000000..52663157478f3a --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2019-06-11-15-41-34.bpo-36779.0TMw6f.rst @@ -0,0 +1,4 @@ +If the default ANSI code page on Windows is CP_UTF7, or CP_UTF8 tzset() +initializes tzname[0] and tzname[1] to empty strings. By getting the time +zone name information directly from GetTimeZoneInformation Python can set +the value of time.tzname correctly. From ce08e582dc13e669aa5a32d0857cc46c0fcfbdef Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Wed, 12 Jun 2019 15:54:49 -0700 Subject: [PATCH 4/4] Simplify NEWS --- .../next/Windows/2019-06-11-15-41-34.bpo-36779.0TMw6f.rst | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) 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 index 52663157478f3a..618cfcae7b8993 100644 --- 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 @@ -1,4 +1,2 @@ -If the default ANSI code page on Windows is CP_UTF7, or CP_UTF8 tzset() -initializes tzname[0] and tzname[1] to empty strings. By getting the time -zone name information directly from GetTimeZoneInformation Python can set -the value of time.tzname correctly. +Ensure ``time.tzname`` is correct on Windows when the active code page is +set to CP_UTF7 or CP_UTF8.