Skip to content

Commit

Permalink
Bump MSRV to 1.48.0 and fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
hotate29 authored and djc committed Nov 28, 2022
1 parent 2aa2221 commit 95532db
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 21 additions & 12 deletions src/offset/local/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use std::io;
use std::mem;
use std::ptr;
use std::time::{SystemTime, UNIX_EPOCH};

use winapi::shared::minwindef::*;
Expand All @@ -20,7 +21,8 @@ use super::{FixedOffset, Local};
use crate::{DateTime, Datelike, LocalResult, NaiveDate, NaiveDateTime, NaiveTime, Timelike};

pub(super) fn now() -> DateTime<Local> {
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`.
Expand All @@ -36,7 +38,7 @@ pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult<Date
tm_yday: 0, // and this
tm_isdst: -1,
// This seems pretty fake?
tm_utcoff: if local { 1 } else { 0 },
tm_utcoff: i32::from(local),
// do not set this, OS APIs are heavily inconsistent in terms of leap second handling
tm_nsec: 0,
};
Expand All @@ -54,28 +56,35 @@ pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult<Date
assert_eq!(tm.tm_nsec, 0);
tm.tm_nsec = d.nanosecond() as i32;

// #TODO - there should be ambiguous cases, investigate?
LocalResult::Single(tm_to_datetime(tm))
tm_to_datetime(tm)
}

/// Converts a `time::Tm` struct into the timezone-aware `DateTime`.
fn tm_to_datetime(mut tm: Tm) -> DateTime<Local> {
fn tm_to_datetime(mut tm: Tm) -> LocalResult<DateTime<Local>> {
if tm.tm_sec >= 60 {
tm.tm_nsec += (tm.tm_sec - 59) * 1_000_000_000;
tm.tm_sec = 59;
}

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
Expand Down Expand Up @@ -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
}
Expand All @@ -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);
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 95532db

Please sign in to comment.