Skip to content

Commit

Permalink
Added an implementation of From<Time> for Duration.
Browse files Browse the repository at this point in the history
  • Loading branch information
M@ Dunlap committed May 1, 2019
1 parent 8c5eb43 commit 5bb09d5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ pub mod num {
#[cfg(not(feature = "std"))]
pub use num_traits::float::FloatCore as Float;

pub use num_traits::{pow, FromPrimitive, Num, One, Saturating, Signed, Zero};
pub use num_traits::{pow, AsPrimitive, FromPrimitive, Num, One, Saturating, Signed, Zero};

#[cfg(feature = "bigint-support")]
pub use num_bigint::{BigInt, BigUint};
Expand Down
14 changes: 14 additions & 0 deletions src/si/time.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Time (base unit second, s<sup>1</sup>).
use ::lib::time::Duration;

quantity! {
/// Time (base unit second, s<sup>1</sup>).
quantity: Time; "time";
Expand Down Expand Up @@ -44,3 +46,15 @@ quantity! {
@year: 3.1536_E7; "a", "year", "years";
}
}

impl<U, V> From<Time<U, V>> for Duration
where
U: ::si::Units<V>,
V: ::num::Num + ::num::AsPrimitive<f64> + ::Conversion<V>,
{
fn from(t: Time<U, V>) -> Duration {
let secs: f64 = t.value.as_();
let nanos = (secs * 1e9) as u64 % 1e9 as u64;
Duration::new(secs as u64, nanos as u32)
}
}

0 comments on commit 5bb09d5

Please sign in to comment.