diff --git a/src/time/src/lib.rs b/src/time/src/lib.rs index 1bb87a76bc251e520061494e126305a762bda562..10538eba2e1500519de7d64ede543aa6500a6a65 100644 --- a/src/time/src/lib.rs +++ b/src/time/src/lib.rs @@ -1,6 +1,7 @@ //! time implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html #![no_std] +#![feature(const_fn)] extern crate platform; @@ -34,23 +35,22 @@ pub struct tm { pub tm_zone: *const c_char, } -impl Default for tm { - fn default() -> tm { - tm { - tm_sec: 0, - tm_min: 0, - tm_hour: 0, - tm_mday: 0, - tm_mon: 0, - tm_year: 0, - tm_wday: 0, - tm_yday: 0, - tm_isdst: 0, - tm_gmtoff: 0, - tm_zone: UTC, - } - } -} +unsafe impl Sync for tm {} + +// The C Standard says that localtime and gmtime return the same pointer. +static mut TM: tm = tm { + tm_sec: 0, + tm_min: 0, + tm_hour: 0, + tm_mday: 0, + tm_mon: 0, + tm_year: 0, + tm_wday: 0, + tm_yday: 0, + tm_isdst: 0, + tm_gmtoff: 0, + tm_zone: UTC, +}; #[repr(C)] pub struct itimerspec { @@ -112,8 +112,7 @@ pub extern "C" fn getdate(string: *const c_char) -> tm { #[no_mangle] pub extern "C" fn gmtime(timer: *const time_t) -> *mut tm { - let mut result: tm = Default::default(); - return gmtime_r(timer, &mut result); + unsafe { gmtime_r(timer, &mut TM) } } #[no_mangle] diff --git a/tests/gmtime.c b/tests/gmtime.c index 33aabcd5249ea44d36c29c861da814e99f2743b4..59a71a79cd1d341ed1b8644074fbe2f7238e4128 100644 --- a/tests/gmtime.c +++ b/tests/gmtime.c @@ -9,6 +9,14 @@ int main(int argc, char** argv) { .tm_wday = 4, .tm_yday = 0, .tm_isdst = 0, .tm_gmtoff = 0, .tm_zone = "UTC" }; tm *info = gmtime(&a); + if (info->tm_sec != expected.tm_sec || info->tm_min != expected.tm_min || + info->tm_hour != expected.tm_hour || info->tm_mday != expected.tm_mday || + info->tm_year != expected.tm_year || info->tm_wday != expected.tm_wday || + info->tm_yday != expected.tm_yday || info->tm_isdst != expected.tm_isdst || + info->tm_gmtoff != expected.tm_gmtoff || strcmp(info->tm_zone, expected.tm_zone) != 0) { + exit(1); + } + if (info->tm_sec != expected.tm_sec || info->tm_min != expected.tm_min || info->tm_hour != expected.tm_hour || info->tm_mday != expected.tm_mday || info->tm_year != expected.tm_year || info->tm_wday != expected.tm_wday ||