diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 20db931e1a..fa1ebcf258 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -49,7 +49,7 @@ jobs: - uses: actions/checkout@v2 - uses: dtolnay/rust-toolchain@master with: - toolchain: 1.38.0 + toolchain: 1.48.0 - uses: Swatinem/rust-cache@v1 # run --lib and --doc to avoid the long running integration tests which are run elsewhere - run: cargo test --lib --features unstable-locales,wasmbind,clock,serde,winapi --color=always -- --color=always diff --git a/src/offset/local/windows.rs b/src/offset/local/windows.rs index e6415015c1..ff77c25231 100644 --- a/src/offset/local/windows.rs +++ b/src/offset/local/windows.rs @@ -10,6 +10,7 @@ use std::io; use std::mem; +use std::ptr; use std::time::{SystemTime, UNIX_EPOCH}; use winapi::shared::minwindef::*; @@ -20,7 +21,8 @@ use super::{FixedOffset, Local}; use crate::{DateTime, Datelike, LocalResult, NaiveDate, NaiveDateTime, NaiveTime, Timelike}; pub(super) fn now() -> DateTime { - tm_to_datetime(Timespec::now().local()) + let datetime = tm_to_datetime(Timespec::now().local()); + datetime.single().expect("invalid time") } /// Converts a local `NaiveDateTime` to the `time::Timespec`. @@ -36,7 +38,7 @@ pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult LocalResult DateTime { +fn tm_to_datetime(mut tm: Tm) -> LocalResult> { if tm.tm_sec >= 60 { tm.tm_nsec += (tm.tm_sec - 59) * 1_000_000_000; tm.tm_sec = 59; @@ -67,15 +68,23 @@ fn tm_to_datetime(mut tm: Tm) -> DateTime { let date = NaiveDate::from_ymd_opt(tm.tm_year + 1900, tm.tm_mon as u32 + 1, tm.tm_mday as u32) .unwrap(); - let time = NaiveTime::from_hms_nano( + + let time = NaiveTime::from_hms_nano_opt( tm.tm_hour as u32, tm.tm_min as u32, tm.tm_sec as u32, tm.tm_nsec as u32, ); - let offset = FixedOffset::east_opt(tm.tm_utcoff).unwrap(); - DateTime::from_utc(date.and_time(time) - offset, offset) + match time { + Some(time) => { + let offset = FixedOffset::east_opt(tm.tm_utcoff).unwrap(); + let datetime = DateTime::from_utc(date.and_time(time) - offset, offset); + // #TODO - there should be ambiguous cases, investigate? + LocalResult::Single(datetime) + } + None => LocalResult::None, + } } /// A record specifying a time value in seconds and nanoseconds, where @@ -220,7 +229,7 @@ fn system_time_to_tm(sys: &SYSTEMTIME, tm: &mut Tm) { } else { 0 }; - let july = if month > 7 { 1 } else { 0 }; + let july = i32::from(month > 7); (month - 1) * 30 + month / 2 + (day - 1) - leap + july } @@ -241,7 +250,7 @@ fn time_to_local_tm(sec: i64, tm: &mut Tm) { let mut utc = mem::zeroed(); let mut local = mem::zeroed(); call!(FileTimeToSystemTime(&ft, &mut utc)); - call!(SystemTimeToTzSpecificLocalTime(0 as *const _, &mut utc, &mut local)); + call!(SystemTimeToTzSpecificLocalTime(ptr::null(), &utc, &mut local)); system_time_to_tm(&local, tm); let local = system_time_to_file_time(&local); @@ -270,8 +279,8 @@ fn local_tm_to_time(tm: &Tm) -> i64 { unsafe { let mut ft = mem::zeroed(); let mut utc = mem::zeroed(); - let mut sys_time = tm_to_system_time(tm); - call!(TzSpecificLocalTimeToSystemTime(0 as *mut _, &mut sys_time, &mut utc)); + let sys_time = tm_to_system_time(tm); + call!(TzSpecificLocalTimeToSystemTime(ptr::null(), &sys_time, &mut utc)); call!(SystemTimeToFileTime(&utc, &mut ft)); file_time_to_unix_seconds(&ft) }