Skip to content

Commit

Permalink
Merge pull request #17 from asynchronics/feature/try_from
Browse files Browse the repository at this point in the history
Add `try_from_*` methods, rename some `from_*`
  • Loading branch information
sbarral authored Apr 7, 2024
2 parents 23857af + db8e121 commit 8b4b14f
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 165 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ let dt = clock.now().duration_since(t1);
println!("Elapsed: {}s, {}ns", dt.as_secs(), dt.subsec_nanos());

// Print out `t1` as a GPS timestamp.
let gps_t1: GpsTime = t1.to_tai_time();
let gps_t1: GpsTime = t1.to_tai_time().unwrap();
println!("GPS timestamp: {}s, {}ns", gps_t1.as_secs(), gps_t1.subsec_nanos());
```

Expand All @@ -98,7 +98,7 @@ use tai_time::{MonotonicTime, Tai1958Time};
// [±][Y]...[Y]YYYY-MM-DD hh:mm:ss[.d[d]...[d]]
// or:
// [±][Y]...[Y]YYYY-MM-DD'T'hh:mm:ss[.d[d]...[d]]
let t0 = MonotonicTime::from_date_time(2222, 11, 11, 12, 34, 56, 789000000).unwrap();
let t0 = MonotonicTime::try_from_date_time(2222, 11, 11, 12, 34, 56, 789000000).unwrap();
assert_eq!("2222-11-11 12:34:56.789".parse(), Ok(t0));

assert_eq!(
Expand Down
5 changes: 3 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use core::fmt;

/// The error type returned when the result of a conversion to or from a
/// [`TaiTime`](crate::TaiTime) is outside the representable range.
/// [`TaiTime`](crate::TaiTime) is outside the representable range, or the
/// conversion would cause the result to overflow.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OutOfRangeError(pub(crate) ());
Expand Down Expand Up @@ -53,7 +54,7 @@ impl fmt::Display for DateTimeError {
Self::InvalidNanosecond(nanosec) => {
write!(fmt, "nanosecond value '{}' is not valid", nanosec)
}
Self::OutOfRange => "timestamp out of representable range".fmt(fmt),
Self::OutOfRange => "timestamp outside representable range".fmt(fmt),
}
}
}
Expand Down
Loading

0 comments on commit 8b4b14f

Please sign in to comment.