From 5f8c6e611aa8d2aff4f62e362e705b3ccbda517b Mon Sep 17 00:00:00 2001 From: Kang Seonghoon Date: Wed, 3 Aug 2016 00:38:08 +0900 Subject: [PATCH] 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. --- src/offset/local.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/offset/local.rs b/src/offset/local.rs index 10fff19..e31e23d 100644 --- a/src/offset/local.rs +++ b/src/offset/local.rs @@ -26,8 +26,19 @@ fn tm_to_datetime(mut tm: stdtime::Tm) -> DateTime { tm.tm_sec = 59; } - // from_yo is more efficient than from_ymd (since it's the internal representation). - let date = NaiveDate::from_yo(tm.tm_year + 1900, tm.tm_yday as u32 + 1); + #[cfg(not(windows))] + 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, tm.tm_sec as u32, tm.tm_nsec as u32); let offset = FixedOffset::east(tm.tm_utcoff);