about summary refs log tree commit diff
path: root/src/rt/rust_builtin.cpp
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-11-07 21:41:07 -0800
committerbors <bors@rust-lang.org>2013-11-07 21:41:07 -0800
commit075347b4453e4b4bc4dd2d2370e408e92041d4ce (patch)
treee28cf414eab24bbea7ecb8caf385c20862b9b078 /src/rt/rust_builtin.cpp
parentd26776a7755abf2227ba7a75f657c561999ed497 (diff)
parentfe18fe0f884ab4edb2b0a986b68572b361285599 (diff)
downloadrust-075347b4453e4b4bc4dd2d2370e408e92041d4ce.tar.gz
rust-075347b4453e4b4bc4dd2d2370e408e92041d4ce.zip
auto merge of #10281 : klutzy/rust/rt-timezone, r=alexcrichton
Previously #9418 fixed utf-8 assertion issue by wcsftime,
but the function didn't work as expected: it follows the locale
set by setlocale(), not the system code page.
This patch fixes it by manual multibyte-to-unicode conversion.
Diffstat (limited to 'src/rt/rust_builtin.cpp')
-rw-r--r--src/rt/rust_builtin.cpp13
1 files changed, 9 insertions, 4 deletions
diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp
index e8141d37ff1..66c8407413b 100644
--- a/src/rt/rust_builtin.cpp
+++ b/src/rt/rust_builtin.cpp
@@ -330,11 +330,16 @@ rust_localtime(int64_t sec, int32_t nsec, rust_tm *timeptr) {
     const char* zone = NULL;
 #if defined(__WIN32__)
     int32_t gmtoff = -timezone;
-    wchar_t wbuffer[64];
-    char buffer[256];
+    wchar_t wbuffer[64] = {0};
+    char buffer[256] = {0};
     // strftime("%Z") can contain non-UTF-8 characters on non-English locale (issue #9418),
-    // so time zone should be converted from UTF-16 string set by wcsftime.
-    if (wcsftime(wbuffer, sizeof(wbuffer) / sizeof(wchar_t), L"%Z", &tm) > 0) {
+    // so time zone should be converted from UTF-16 string.
+    // Since wcsftime depends on setlocale() result,
+    // instead we convert it using MultiByteToWideChar.
+    if (strftime(buffer, sizeof(buffer) / sizeof(char), "%Z", &tm) > 0) {
+        // ANSI -> UTF-16
+        MultiByteToWideChar(CP_ACP, 0, buffer, -1, wbuffer, sizeof(wbuffer) / sizeof(wchar_t));
+        // UTF-16 -> UTF-8
         WideCharToMultiByte(CP_UTF8, 0, wbuffer, -1, buffer, sizeof(buffer), NULL, NULL);
         zone = buffer;
     }