Workaround for rust-lang-deprecated/time#137.

In Windows libtime populate `time::Tm` from `SYSTEMTIME`, which
unfortunately does not contain `tm_yday`. It tries to calculate it
from other fields, but... as one can say it is completely wrong.
Since other fields are copied in verbatim we work around this
problem by using a less efficient method.

Fixes #85.
This commit is contained in:
Kang Seonghoon 2016-08-03 00:38:08 +09:00
parent 00858c6363
commit 5f8c6e611a
1 changed files with 13 additions and 2 deletions

View File

@ -26,8 +26,19 @@ fn tm_to_datetime(mut tm: stdtime::Tm) -> DateTime<Local> {
tm.tm_sec = 59; tm.tm_sec = 59;
} }
// from_yo is more efficient than from_ymd (since it's the internal representation). #[cfg(not(windows))]
let date = NaiveDate::from_yo(tm.tm_year + 1900, tm.tm_yday as u32 + 1); fn tm_to_naive_date(tm: &stdtime::Tm) -> NaiveDate {
// from_yo is more efficient than from_ymd (since it's the internal representation).
NaiveDate::from_yo(tm.tm_year + 1900, tm.tm_yday as u32 + 1)
}
#[cfg(windows)]
fn tm_to_naive_date(tm: &stdtime::Tm) -> NaiveDate {
// ...but tm_yday is broken in Windows (issue #85)
NaiveDate::from_ymd(tm.tm_year + 1900, tm.tm_mon as u32 + 1, tm.tm_mday as u32)
}
let date = tm_to_naive_date(&tm);
let time = NaiveTime::from_hms_nano(tm.tm_hour as u32, tm.tm_min as u32, let time = NaiveTime::from_hms_nano(tm.tm_hour as u32, tm.tm_min as u32,
tm.tm_sec as u32, tm.tm_nsec as u32); tm.tm_sec as u32, tm.tm_nsec as u32);
let offset = FixedOffset::east(tm.tm_utcoff); let offset = FixedOffset::east(tm.tm_utcoff);