diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-10-03 01:58:58 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-03 01:58:58 +0000 |
commit | fe0aa236cf96969b5d80c5c6504601c6e746edcf (patch) | |
tree | 999e08d6469a0f9bcdce421eeb4c02c617df858e /src/sys | |
parent | 2c2440521acb5942a5f937b8d3126577cf91106a (diff) | |
parent | 91effece134079bebd07869089ec36b1efbdc06a (diff) | |
download | nix-fe0aa236cf96969b5d80c5c6504601c6e746edcf.zip |
Merge #1281
1281: Added clock_gettime, clock_getres, clock_settime, clock_getcpuclockid r=asomers a=xonatius
Picked up #1100 and added `clock_getcpuclockid` call as well. Credits to @kevinwern for the initial version.
https://www.man7.org/linux/man-pages/man2/clock_gettime.2.html
https://www.man7.org/linux/man-pages/man3/clock_getcpuclockid.3.html
Closes #1275
Co-authored-by: Daniil Bondarev <xonatius@gmail.com>
Diffstat (limited to 'src/sys')
-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); |