Skip to content

Commit

Permalink
refactor: Use checked substitution & convert u32 conversiont to None …
Browse files Browse the repository at this point in the history
…on err
  • Loading branch information
Pscheidl authored and djc committed Sep 19, 2022
1 parent 49c4bad commit 40800ce
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/naive/datetime/mod.rs
Expand Up @@ -158,10 +158,13 @@ impl NaiveDateTime {
let secs = millis / 1000;

if millis < 0 {
let nsecs = (millis % 1000).abs() as u32 * NANOS_IN_MILLISECOND;
NaiveDateTime::from_timestamp_opt(secs - 1, NANOS_IN_SECOND - nsecs)
let nsecs = u32::try_from((millis % 1000).abs()).ok()? * NANOS_IN_MILLISECOND;
NaiveDateTime::from_timestamp_opt(
secs.checked_sub(1)?,
NANOS_IN_SECOND.checked_sub(nsecs)?,
)
} else {
let nsecs = (millis % 1000) as u32 * NANOS_IN_MILLISECOND;
let nsecs = u32::try_from(millis % 1000).ok()? * NANOS_IN_MILLISECOND;
NaiveDateTime::from_timestamp_opt(secs, nsecs)
}
}
Expand Down

0 comments on commit 40800ce

Please sign in to comment.