Skip to content

Convert timezone to utf-8 on Windows #10281

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Nov 8, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 59 additions & 40 deletions src/libextra/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,8 +965,35 @@ mod tests {
use super::*;

use std::f64;
use std::os;
use std::result::{Err, Ok};
use std::libc;

#[cfg(windows)]
#[fixed_stack_segment]
fn set_time_zone() {
// Windows crt doesn't see any environment variable set by
// `SetEnvironmentVariable`, which `os::setenv` internally uses.
// It is why we use `putenv` here.
extern {
fn _putenv(envstring: *libc::c_char) -> libc::c_int;
}

unsafe {
// Windows does not understand "America/Los_Angeles".
// PST+08 may look wrong, but not! "PST" indicates
// the name of timezone. "+08" means UTC = local + 08.
do "TZ=PST+08".with_c_str |env| {
_putenv(env);
}
}
tzset();
}
#[cfg(not(windows))]
fn set_time_zone() {
use std::os;
os::setenv("TZ", "America/Los_Angeles");
tzset();
}

fn test_get_time() {
static SOME_RECENT_DATE: i64 = 1325376000i64; // 2012-01-01T00:00:00Z
Expand Down Expand Up @@ -1007,57 +1034,54 @@ mod tests {
}

fn test_at_utc() {
os::setenv("TZ", "America/Los_Angeles");
tzset();
set_time_zone();

let time = Timespec::new(1234567890, 54321);
let utc = at_utc(time);

assert!(utc.tm_sec == 30_i32);
assert!(utc.tm_min == 31_i32);
assert!(utc.tm_hour == 23_i32);
assert!(utc.tm_mday == 13_i32);
assert!(utc.tm_mon == 1_i32);
assert!(utc.tm_year == 109_i32);
assert!(utc.tm_wday == 5_i32);
assert!(utc.tm_yday == 43_i32);
assert!(utc.tm_isdst == 0_i32);
assert!(utc.tm_gmtoff == 0_i32);
assert!(utc.tm_zone == ~"UTC");
assert!(utc.tm_nsec == 54321_i32);
assert_eq!(utc.tm_sec, 30_i32);
assert_eq!(utc.tm_min, 31_i32);
assert_eq!(utc.tm_hour, 23_i32);
assert_eq!(utc.tm_mday, 13_i32);
assert_eq!(utc.tm_mon, 1_i32);
assert_eq!(utc.tm_year, 109_i32);
assert_eq!(utc.tm_wday, 5_i32);
assert_eq!(utc.tm_yday, 43_i32);
assert_eq!(utc.tm_isdst, 0_i32);
assert_eq!(utc.tm_gmtoff, 0_i32);
assert_eq!(utc.tm_zone, ~"UTC");
assert_eq!(utc.tm_nsec, 54321_i32);
}

fn test_at() {
os::setenv("TZ", "America/Los_Angeles");
tzset();
set_time_zone();

let time = Timespec::new(1234567890, 54321);
let local = at(time);

error!("time_at: {:?}", local);

assert!(local.tm_sec == 30_i32);
assert!(local.tm_min == 31_i32);
assert!(local.tm_hour == 15_i32);
assert!(local.tm_mday == 13_i32);
assert!(local.tm_mon == 1_i32);
assert!(local.tm_year == 109_i32);
assert!(local.tm_wday == 5_i32);
assert!(local.tm_yday == 43_i32);
assert!(local.tm_isdst == 0_i32);
assert!(local.tm_gmtoff == -28800_i32);
assert_eq!(local.tm_sec, 30_i32);
assert_eq!(local.tm_min, 31_i32);
assert_eq!(local.tm_hour, 15_i32);
assert_eq!(local.tm_mday, 13_i32);
assert_eq!(local.tm_mon, 1_i32);
assert_eq!(local.tm_year, 109_i32);
assert_eq!(local.tm_wday, 5_i32);
assert_eq!(local.tm_yday, 43_i32);
assert_eq!(local.tm_isdst, 0_i32);
assert_eq!(local.tm_gmtoff, -28800_i32);

// FIXME (#2350): We should probably standardize on the timezone
// abbreviation.
let zone = &local.tm_zone;
assert!(*zone == ~"PST" || *zone == ~"Pacific Standard Time");

assert!(local.tm_nsec == 54321_i32);
assert_eq!(local.tm_nsec, 54321_i32);
}

fn test_to_timespec() {
os::setenv("TZ", "America/Los_Angeles");
tzset();
set_time_zone();

let time = Timespec::new(1234567890, 54321);
let utc = at_utc(time);
Expand All @@ -1067,8 +1091,7 @@ mod tests {
}

fn test_conversions() {
os::setenv("TZ", "America/Los_Angeles");
tzset();
set_time_zone();

let time = Timespec::new(1234567890, 54321);
let utc = at_utc(time);
Expand All @@ -1083,8 +1106,7 @@ mod tests {
}

fn test_strptime() {
os::setenv("TZ", "America/Los_Angeles");
tzset();
set_time_zone();

match strptime("", "") {
Ok(ref tm) => {
Expand Down Expand Up @@ -1248,8 +1270,7 @@ mod tests {
}

fn test_ctime() {
os::setenv("TZ", "America/Los_Angeles");
tzset();
set_time_zone();

let time = Timespec::new(1234567890, 54321);
let utc = at_utc(time);
Expand All @@ -1262,8 +1283,7 @@ mod tests {
}

fn test_strftime() {
os::setenv("TZ", "America/Los_Angeles");
tzset();
set_time_zone();

let time = Timespec::new(1234567890, 54321);
let utc = at_utc(time);
Expand Down Expand Up @@ -1323,8 +1343,7 @@ mod tests {
// abbreviation.
let rfc822 = local.rfc822();
let prefix = ~"Fri, 13 Feb 2009 15:31:30 ";
assert!(rfc822 == prefix + "PST" ||
rfc822 == prefix + "Pacific Standard Time");
assert!(rfc822 == prefix + "PST" || rfc822 == prefix + "Pacific Standard Time");

assert_eq!(local.ctime(), ~"Fri Feb 13 15:31:30 2009");
assert_eq!(local.rfc822z(), ~"Fri, 13 Feb 2009 15:31:30 -0800");
Expand Down
13 changes: 9 additions & 4 deletions src/rt/rust_builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,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;
}
Expand Down