Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert TryFrom<SystemTime> to our error type #1524

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::naive::{Days, IsoWeek, NaiveDate, NaiveDateTime, NaiveTime};
#[cfg(feature = "clock")]
use crate::offset::Local;
use crate::offset::{FixedOffset, MappedLocalTime, Offset, TimeZone, Utc};
#[cfg(any(feature = "clock", feature = "std"))]
#[cfg(feature = "clock")]
use crate::OutOfRange;
use crate::{try_err, try_ok_or, Datelike, Error, Months, TimeDelta, Timelike, Weekday};

Expand Down Expand Up @@ -1712,21 +1712,21 @@ impl str::FromStr for DateTime<Local> {

#[cfg(feature = "std")]
impl TryFrom<SystemTime> for DateTime<Utc> {
type Error = OutOfRange;
type Error = Error;

fn try_from(t: SystemTime) -> Result<DateTime<Utc>, OutOfRange> {
fn try_from(t: SystemTime) -> Result<DateTime<Utc>, Error> {
let (sec, nsec) = match t.duration_since(UNIX_EPOCH) {
Ok(dur) => {
// `t` is at or after the Unix epoch.
let sec = i64::try_from(dur.as_secs()).map_err(|_| OutOfRange::new())?;
let sec = i64::try_from(dur.as_secs()).map_err(|_| Error::OutOfRange)?;
let nsec = dur.subsec_nanos();
(sec, nsec)
}
Err(e) => {
// `t` is before the Unix epoch. `e.duration()` is how long before the epoch it
// is.
let dur = e.duration();
let sec = i64::try_from(dur.as_secs()).map_err(|_| OutOfRange::new())?;
let sec = i64::try_from(dur.as_secs()).map_err(|_| Error::OutOfRange)?;
let nsec = dur.subsec_nanos();
if nsec == 0 {
// Overflow safety: `sec` was returned by `dur.as_secs()`, and is guaranteed to
Expand All @@ -1744,7 +1744,7 @@ impl TryFrom<SystemTime> for DateTime<Utc> {
}
}
};
Utc.timestamp(sec, nsec).single().ok_or(OutOfRange::new())
DateTime::from_timestamp(sec, nsec)
}
}

Expand All @@ -1753,22 +1753,22 @@ impl TryFrom<SystemTime> for DateTime<Local> {
type Error = OutOfRange;

fn try_from(t: SystemTime) -> Result<DateTime<Local>, OutOfRange> {
DateTime::<Utc>::try_from(t).map(|t| t.with_timezone(&Local))
DateTime::<Utc>::try_from(t).map(|t| t.with_timezone(&Local)).map_err(|_| OutOfRange::new())
}
}

#[cfg(feature = "std")]
impl<Tz: TimeZone> TryFrom<DateTime<Tz>> for SystemTime {
type Error = OutOfRange;
type Error = Error;

fn try_from(dt: DateTime<Tz>) -> Result<SystemTime, OutOfRange> {
fn try_from(dt: DateTime<Tz>) -> Result<SystemTime, Error> {
let sec = dt.timestamp();
let sec_abs = sec.unsigned_abs();
let nsec = dt.timestamp_subsec_nanos();
if sec < 0 {
// `dt` is before the Unix epoch.
let mut t =
UNIX_EPOCH.checked_sub(Duration::new(sec_abs, 0)).ok_or_else(OutOfRange::new)?;
UNIX_EPOCH.checked_sub(Duration::new(sec_abs, 0)).ok_or(Error::OutOfRange)?;

// Overflow safety: `t` is before the Unix epoch. Adding nanoseconds therefore cannot
// overflow.
Expand All @@ -1777,7 +1777,7 @@ impl<Tz: TimeZone> TryFrom<DateTime<Tz>> for SystemTime {
Ok(t)
} else {
// `dt` is after the Unix epoch.
UNIX_EPOCH.checked_add(Duration::new(sec_abs, nsec)).ok_or_else(OutOfRange::new)
UNIX_EPOCH.checked_add(Duration::new(sec_abs, nsec)).ok_or(Error::OutOfRange)
}
}
}
Expand Down
Loading