Skip to content

Commit 6a433f5

Browse files
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 @zooba https://bugs.python.org/issue36779 (cherry picked from commit b4c7def) Co-authored-by: Paul Monson <[email protected]>
1 parent 6eb2878 commit 6a433f5

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Ensure ``time.tzname`` is correct on Windows when the active code page is
2+
set to CP_UTF7 or CP_UTF8.

Modules/timemodule.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,19 @@ init_timezone(PyObject *m)
15711571
PyModule_AddIntConstant(m, "altzone", _Py_timezone-3600);
15721572
#endif
15731573
PyModule_AddIntConstant(m, "daylight", _Py_daylight);
1574+
#ifdef MS_WINDOWS
1575+
TIME_ZONE_INFORMATION tzinfo = {0};
1576+
GetTimeZoneInformation(&tzinfo);
1577+
otz0 = PyUnicode_FromWideChar(tzinfo.StandardName, -1);
1578+
if (otz0 == NULL) {
1579+
return -1;
1580+
}
1581+
otz1 = PyUnicode_FromWideChar(tzinfo.DaylightName, -1);
1582+
if (otz1 == NULL) {
1583+
Py_DECREF(otz0);
1584+
return -1;
1585+
}
1586+
#else
15741587
otz0 = PyUnicode_DecodeLocale(_Py_tzname[0], "surrogateescape");
15751588
if (otz0 == NULL) {
15761589
return -1;
@@ -1580,6 +1593,7 @@ init_timezone(PyObject *m)
15801593
Py_DECREF(otz0);
15811594
return -1;
15821595
}
1596+
#endif // MS_WINDOWS
15831597
PyObject *tzname_obj = Py_BuildValue("(NN)", otz0, otz1);
15841598
if (tzname_obj == NULL) {
15851599
return -1;

0 commit comments

Comments
 (0)