servo/components/util/time.rs
James Sanders 3659218c59 Use thread::sleep instead of deprecated sleep_ms
Similarly, change one instance of `thread::park_timeout_ms`.

Fixes #8694
2015-11-29 13:41:04 -07:00

32 lines
1.1 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::time::Duration;
use std::{u32, u64};
pub const NANOS_PER_SEC: u32 = 1_000_000_000;
pub fn duration_from_seconds(secs: f64) -> Duration {
// Get number of seconds and check that it fits in a u64.
let whole_secs = secs.trunc();
assert!(whole_secs >= 0.0 && whole_secs <= u64::MAX as f64);
// Get number of nanoseconds. This should always fit in a u32, but check anyway.
let nanos = (secs.fract() * (NANOS_PER_SEC as f64)).trunc();
assert!(nanos >= 0.0 && nanos <= u32::MAX as f64);
Duration::new(whole_secs as u64, nanos as u32)
}
pub fn duration_from_nanoseconds(nanos: u64) -> Duration {
// Get number of seconds.
let secs = nanos / NANOS_PER_SEC as u64;
// Get number of extra nanoseconds. This should always fit in a u32, but check anyway.
let subsec_nanos = nanos % NANOS_PER_SEC as u64;
assert!(subsec_nanos <= u32::MAX as u64);
Duration::new(secs, subsec_nanos as u32)
}