diff options
author | Daniil Bondarev <xonatius@gmail.com> | 2020-08-01 11:28:24 -0700 |
---|---|---|
committer | Daniil Bondarev <xonatius@gmail.com> | 2020-09-28 00:11:27 -0700 |
commit | 91effece134079bebd07869089ec36b1efbdc06a (patch) | |
tree | 999e08d6469a0f9bcdce421eeb4c02c617df858e /src/sys/time.rs | |
parent | 2c2440521acb5942a5f937b8d3126577cf91106a (diff) | |
download | nix-91effece134079bebd07869089ec36b1efbdc06a.zip |
Added clock_gettime, clock_getres, clock_settime, clock_getcpuclockid
Diffstat (limited to 'src/sys/time.rs')
-rw-r--r-- | src/sys/time.rs | 26 |
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) -> ×pec { &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); |