A Duration
type to represent a span of time, typically used for system
timeouts.
Each Duration
is composed of a whole number of seconds and a fractional part
represented in nanoseconds. If the underlying system does not support
nanosecond-level precision, APIs binding a system timeout will typically round up
the number of nanoseconds.
Duration
s implement many common traits, including Add
, Sub
, and other
ops
traits.
use std::time::Duration;
let five_seconds = Duration::new(5, 0);
let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5);
assert_eq!(five_seconds_and_five_nanos.as_secs(), 5);
assert_eq!(five_seconds_and_five_nanos.subsec_nanos(), 5);
let ten_millis = Duration::from_millis(10);Run
Creates a new Duration
from the specified number of whole seconds and
additional nanoseconds.
If the number of nanoseconds is greater than 1 billion (the number of
nanoseconds in a second), then it will carry over into the seconds provided.
This constructor will panic if the carry from the nanoseconds overflows
the seconds counter.
use std::time::Duration;
let five_seconds = Duration::new(5, 0);Run
Creates a new Duration
from the specified number of whole seconds.
use std::time::Duration;
let duration = Duration::from_secs(5);
assert_eq!(5, duration.as_secs());
assert_eq!(0, duration.subsec_nanos());Run
Creates a new Duration
from the specified number of milliseconds.
use std::time::Duration;
let duration = Duration::from_millis(2569);
assert_eq!(2, duration.as_secs());
assert_eq!(569_000_000, duration.subsec_nanos());Run
Creates a new Duration
from the specified number of microseconds.
use std::time::Duration;
let duration = Duration::from_micros(1_000_002);
assert_eq!(1, duration.as_secs());
assert_eq!(2000, duration.subsec_nanos());Run
Creates a new Duration
from the specified number of nanoseconds.
use std::time::Duration;
let duration = Duration::from_nanos(1_000_000_123);
assert_eq!(1, duration.as_secs());
assert_eq!(123, duration.subsec_nanos());Run
Returns the number of whole seconds contained by this Duration
.
The returned value does not include the fractional (nanosecond) part of the
duration, which can be obtained using subsec_nanos
.
use std::time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(duration.as_secs(), 5);Run
To determine the total number of seconds represented by the Duration
,
use as_secs
in combination with subsec_nanos
:
use std::time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(5.730023852,
duration.as_secs() as f64
+ duration.subsec_nanos() as f64 * 1e-9);Run
Returns the fractional part of this Duration
, in whole milliseconds.
This method does not return the length of the duration when
represented by milliseconds. The returned number always represents a
fractional portion of a second (i.e. it is less than one thousand).
use std::time::Duration;
let duration = Duration::from_millis(5432);
assert_eq!(duration.as_secs(), 5);
assert_eq!(duration.subsec_millis(), 432);Run
Returns the fractional part of this Duration
, in whole microseconds.
This method does not return the length of the duration when
represented by microseconds. The returned number always represents a
fractional portion of a second (i.e. it is less than one million).
use std::time::Duration;
let duration = Duration::from_micros(1_234_567);
assert_eq!(duration.as_secs(), 1);
assert_eq!(duration.subsec_micros(), 234_567);Run
Returns the fractional part of this Duration
, in nanoseconds.
This method does not return the length of the duration when
represented by nanoseconds. The returned number always represents a
fractional portion of a second (i.e. it is less than one billion).
use std::time::Duration;
let duration = Duration::from_millis(5010);
assert_eq!(duration.as_secs(), 5);
assert_eq!(duration.subsec_nanos(), 10_000_000);Run
🔬 This is a nightly-only experimental API. (
duration_as_u128
#50202)
Returns the total number of whole milliseconds contained by this Duration
.
use std::time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(duration.as_millis(), 5730);Run
🔬 This is a nightly-only experimental API. (
duration_as_u128
#50202)
Returns the total number of whole microseconds contained by this Duration
.
use std::time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(duration.as_micros(), 5730023);Run
🔬 This is a nightly-only experimental API. (
duration_as_u128
#50202)
Returns the total number of nanoseconds contained by this Duration
.
use std::time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(duration.as_nanos(), 5730023852);Run
Checked Duration
addition. Computes self + other
, returning None
if overflow occurred.
Basic usage:
use std::time::Duration;
assert_eq!(Duration::new(0, 0).checked_add(Duration::new(0, 1)), Some(Duration::new(0, 1)));
assert_eq!(Duration::new(1, 0).checked_add(Duration::new(std::u64::MAX, 0)), None);Run
Checked Duration
subtraction. Computes self - other
, returning None
if the result would be negative or if overflow occurred.
Basic usage:
use std::time::Duration;
assert_eq!(Duration::new(0, 1).checked_sub(Duration::new(0, 0)), Some(Duration::new(0, 1)));
assert_eq!(Duration::new(0, 0).checked_sub(Duration::new(0, 1)), None);Run
Checked Duration
multiplication. Computes self * other
, returning
None
if overflow occurred.
Basic usage:
use std::time::Duration;
assert_eq!(Duration::new(0, 500_000_001).checked_mul(2), Some(Duration::new(1, 2)));
assert_eq!(Duration::new(std::u64::MAX - 1, 0).checked_mul(2), None);Run
Checked Duration
division. Computes self / other
, returning None
if other == 0
.
Basic usage:
use std::time::Duration;
assert_eq!(Duration::new(2, 0).checked_div(2), Some(Duration::new(1, 0)));
assert_eq!(Duration::new(1, 0).checked_div(2), Some(Duration::new(0, 500_000_000)));
assert_eq!(Duration::new(2, 0).checked_div(0), None);Run
🔬 This is a nightly-only experimental API. (
duration_float
#54361)
Returns the number of seconds contained by this Duration
as f64
.
The returned value does include the fractional (nanosecond) part of the duration.
#![feature(duration_float)]
use std::time::Duration;
let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.as_float_secs(), 2.7);Run
🔬 This is a nightly-only experimental API. (
duration_float
#54361)
Creates a new Duration
from the specified number of seconds.
This constructor will panic if secs
is not finite, negative or overflows Duration
.
#![feature(duration_float)]
use std::time::Duration;
let dur = Duration::from_float_secs(2.7);
assert_eq!(dur, Duration::new(2, 700_000_000));Run
🔬 This is a nightly-only experimental API. (
duration_float
#54361)
Multiply Duration
by f64
.
This method will panic if result is not finite, negative or overflows Duration
.
#![feature(duration_float)]
use std::time::Duration;
let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000));
assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0));Run
🔬 This is a nightly-only experimental API. (
duration_float
#54361)
Divide Duration
by f64
.
This method will panic if result is not finite, negative or overflows Duration
.
#![feature(duration_float)]
use std::time::Duration;
let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611));
assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_598));Run
🔬 This is a nightly-only experimental API. (
duration_float
#54361)
Divide Duration
by Duration
and return f64
.
#![feature(duration_float)]
use std::time::Duration;
let dur1 = Duration::new(2, 700_000_000);
let dur2 = Duration::new(5, 400_000_000);
assert_eq!(dur1.div_duration(dur2), 0.5);Run