Skip to content

Commit bd831bc

Browse files
committed
[Bug #20929] Win32: Use wcsftime
`_strftime_l` is not available on msvcrt that is still used by 32bit mingw compiler.
1 parent c9bbf7e commit bd831bc

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

time.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,11 +1659,21 @@ localtime_with_gmtoff_zone(const time_t *t, struct tm *result, long *gmtoff, VAL
16591659
*zone = zone_str(tm.tm_zone);
16601660
#elif defined(_WIN32)
16611661
{
1662-
char buf[64];
1663-
_locale_t utf8_locale = _create_locale(LC_TIME, ".65001");
1664-
size_t n = _strftime_l(buf, numberof(buf), "%Z", &tm, utf8_locale);
1665-
_free_locale(utf8_locale);
1666-
*zone = zone_str((ssize_t)n < 0 ? NULL : buf);
1662+
enum {tz_name_max = 32}; /* numberof(TIME_ZONE_INFORMATION::StandardName) */
1663+
WCHAR wbuf[tz_name_max + 1];
1664+
char cbuf[tz_name_max * 4 + 1];
1665+
size_t wlen = wcsftime(wbuf, numberof(wbuf), L"%Z", &tm);
1666+
DWORD clen = 0;
1667+
if (wlen > 0 && wlen < numberof(wbuf)) {
1668+
clen = WideCharToMultiByte(CP_UTF8, 0, wbuf, wlen, cbuf, sizeof(cbuf), NULL, NULL);
1669+
}
1670+
if (clen > 0 && clen < sizeof(cbuf)) {
1671+
cbuf[clen] = '\0';
1672+
*zone = zone_str(cbuf);
1673+
}
1674+
else {
1675+
*zone = zone_str(NULL);
1676+
}
16671677
}
16681678
#elif defined(HAVE_TZNAME) && defined(HAVE_DAYLIGHT)
16691679
/* this needs tzset or localtime, instead of localtime_r */

0 commit comments

Comments
 (0)