summaryrefslogtreecommitdiff
path: root/src/sys/time.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/sys/time.rs')
-rw-r--r--src/sys/time.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/sys/time.rs b/src/sys/time.rs
index 973a3526..269b4253 100644
--- a/src/sys/time.rs
+++ b/src/sys/time.rs
@@ -1,4 +1,5 @@
use std::{cmp, fmt, ops};
+use std::time::Duration;
use std::convert::From;
use libc::{c_long, timespec, timeval};
pub use libc::{time_t, suseconds_t};
@@ -66,6 +67,21 @@ impl From<timespec> for TimeSpec {
}
}
+impl From<Duration> for TimeSpec {
+ fn from(duration: Duration) -> Self {
+ TimeSpec(timespec {
+ tv_sec: duration.as_secs() as time_t,
+ tv_nsec: duration.subsec_nanos() as c_long
+ })
+ }
+}
+
+impl From<TimeSpec> for Duration {
+ fn from(timespec: TimeSpec) -> Self {
+ Duration::new(timespec.0.tv_sec as u64, timespec.0.tv_nsec as u32)
+ }
+}
+
impl AsRef<timespec> for TimeSpec {
fn as_ref(&self) -> &timespec {
&self.0
@@ -484,6 +500,7 @@ fn div_rem_64(this: i64, other: i64) -> (i64, i64) {
#[cfg(test)]
mod test {
use super::{TimeSpec, TimeVal, TimeValLike};
+ use std::time::Duration;
#[test]
pub fn test_timespec() {
@@ -495,6 +512,15 @@ mod test {
}
#[test]
+ pub fn test_timespec_from() {
+ let duration = Duration::new(123, 123_456_789);
+ let timespec = TimeSpec::nanoseconds(123_123_456_789);
+
+ assert_eq!(TimeSpec::from(duration), timespec);
+ assert_eq!(Duration::from(timespec), duration);
+ }
+
+ #[test]
pub fn test_timespec_neg() {
let a = TimeSpec::seconds(1) + TimeSpec::nanoseconds(123);
let b = TimeSpec::seconds(-1) + TimeSpec::nanoseconds(-123);