Skip to content

Commit b4c7def

Browse files
paulmonmiss-islington
authored andcommitted
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
1 parent 95f61c8 commit b4c7def

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
@@ -1581,6 +1581,19 @@ init_timezone(PyObject *m)
15811581
PyModule_AddIntConstant(m, "altzone", _Py_timezone-3600);
15821582
#endif
15831583
PyModule_AddIntConstant(m, "daylight", _Py_daylight);
1584+
#ifdef MS_WINDOWS
1585+
TIME_ZONE_INFORMATION tzinfo = {0};
1586+
GetTimeZoneInformation(&tzinfo);
1587+
otz0 = PyUnicode_FromWideChar(tzinfo.StandardName, -1);
1588+
if (otz0 == NULL) {
1589+
return -1;
1590+
}
1591+
otz1 = PyUnicode_FromWideChar(tzinfo.DaylightName, -1);
1592+
if (otz1 == NULL) {
1593+
Py_DECREF(otz0);
1594+
return -1;
1595+
}
1596+
#else
15841597
otz0 = PyUnicode_DecodeLocale(_Py_tzname[0], "surrogateescape");
15851598
if (otz0 == NULL) {
15861599
return -1;
@@ -1590,6 +1603,7 @@ init_timezone(PyObject *m)
15901603
Py_DECREF(otz0);
15911604
return -1;
15921605
}
1606+
#endif // MS_WINDOWS
15931607
PyObject *tzname_obj = Py_BuildValue("(NN)", otz0, otz1);
15941608
if (tzname_obj == NULL) {
15951609
return -1;

0 commit comments

Comments
 (0)