diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c
index f78a80f..c37ddd5 100644
--- a/src/backend/utils/adt/pg_locale.c
+++ b/src/backend/utils/adt/pg_locale.c
@@ -455,10 +455,13 @@ PGLC_localeconv(void)
 
 #ifdef WIN32
 /*
- * result is obtained by locale setup of LC_TIME in the environment 
- * of windows at present CP_ACP. Therefore, conversion is needed
- * for SERVER_ENCODING. SJIS which is not especially made to server 
- * encoding in Japan returns. 
+ * On win32, strftime() returns the encoding in CP_ACP, which is likely
+ * different from SERVER_ENCODING. This is especially important in Japanese
+ * versions of Windows which will use SJIS encoding, which we don't support
+ * as a server encoding.
+ *
+ * Replace strftime() with a version that gets the string in UTF16 and then
+ * converts it to the appropriate encoding as necessary.
  */
 static size_t
 strftime_win32(char *dst, size_t dstlen, const char *format, const struct tm *tm)
@@ -467,19 +470,19 @@ strftime_win32(char *dst, size_t dstlen, const char *format, const struct tm *tm
 	wchar_t	wbuf[MAX_L10N_DATA];
 	int		encoding;
 
-	len = strftime(dst, dstlen, format, tm);
 	encoding = GetDatabaseEncoding();
 	if (encoding == PG_SQL_ASCII)
 		return len;
 
-	len = MultiByteToWideChar(CP_ACP, 0, dst, len, wbuf, MAX_L10N_DATA);
+	len = wcsftime(wbuf, sizeof(wbuf), format, tm);
 	if (len == 0)
-		ereport(ERROR,
-			(errmsg("could not convert string to wide character:error %lu", GetLastError())));
+		/* strftime called failed - return 0 with the contents of dst unspecified */
+		return 0;
+
 	len = WideCharToMultiByte(CP_UTF8, 0, wbuf, len, dst, dstlen, NULL, NULL);
 	if (len == 0)
 		ereport(ERROR,
-			(errmsg("could not convert wide character to UTF-8:error %lu", GetLastError())));
+			(errmsg("could not convert string to UTF-8:error %lu", GetLastError())));
 
 	dst[len] = '\0';
 	if (encoding != PG_UTF8)
