Skip to content

implement TimeValLike for std::time::Duration #511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion src/sys/time.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cmp, fmt, ops};
use std::{cmp, fmt, ops, time};
use libc::{c_long, time_t, suseconds_t, timespec, timeval};

pub trait TimeValLike: Sized {
Expand Down Expand Up @@ -160,6 +160,53 @@ impl TimeValLike for TimeSpec {
}
}

impl TimeValLike for time::Duration {
fn seconds(seconds: i64) -> Self {
time::Duration::from_secs(seconds as u64)
}

fn milliseconds(milliseconds: i64) -> Self {
time::Duration::from_millis(milliseconds as u64)
}

fn microseconds(microseconds: i64) -> Self {
let secs = microseconds / 1_000_000;
let nanos = (microseconds - (secs * 1_000_000)) / 1_000;
time::Duration::new(secs as u64, nanos as u32)
}

fn nanoseconds(nanoseconds: i64) -> Self {
let secs = nanoseconds / 1_000_000_000;
let nanos = nanoseconds - (secs * 1_000_000_000);
time::Duration::new(secs as u64, nanos as u32)
}

fn num_seconds(&self) -> i64 {
self.as_secs() as i64
}

fn num_milliseconds(&self) -> i64 {
let seconds_part: u64 = self.as_secs().checked_mul(1_000)
.expect("Duration::num_milliseconds out of bounds");
let subsecond_part: u32 = self.subsec_nanos() / 1_000_000;
(seconds_part + subsecond_part as u64) as i64
}

fn num_microseconds(&self) -> i64 {
let seconds_part: u64 = self.as_secs().checked_mul(1_000_000)
.expect("Duration::num_microseconds out of bounds");
let subsecond_part: u32 = self.subsec_nanos() / 1_000;
(seconds_part + subsecond_part as u64) as i64
}

fn num_nanoseconds(&self) -> i64 {
let seconds_part: u64 = self.as_secs().checked_mul(1_000_000_000)
.expect("Duration::num_nanoseconds out of bounds");
let subsecond_part: u32 = self.subsec_nanos();
(seconds_part + subsecond_part as u64) as i64
}
}

impl TimeSpec {
fn nanos_mod_sec(&self) -> c_long {
if self.tv_sec() < 0 && self.tv_nsec() > 0 {
Expand Down Expand Up @@ -496,6 +543,7 @@ fn div_rem_64(this: i64, other: i64) -> (i64, i64) {

#[cfg(test)]
mod test {
use std::time::Duration;
use super::{TimeSpec, TimeVal, TimeValLike};

#[test]
Expand Down Expand Up @@ -569,4 +617,18 @@ mod test {
assert_eq!(TimeVal::nanoseconds(1402).to_string(), "0.000001 seconds");
assert_eq!(TimeVal::seconds(-86401).to_string(), "-86401 seconds");
}

#[test]
fn test_timeval_like_duration() {
let large = Duration::from_secs(1_000_000_000);
assert_eq!(large.num_seconds(), 1_000_000_000);
assert_eq!(large.num_milliseconds(), 1_000_000_000_000);
assert_eq!(large.num_microseconds(), 1_000_000_000_000_000);
assert_eq!(large.num_nanoseconds(), 1_000_000_000_000_000_000);

assert_eq!(Duration::from_secs(10), Duration::seconds(10));

assert_eq!(Duration::from_secs(3600).num_minutes(), 60);
assert_eq!(Duration::from_secs(3600).num_hours(), 1);
}
}