A wrapper for duration that has FromStr
implementation
This is useful if you want to use it somewhere where FromStr
is
expected.
See parse_duration
for the description of the format.
use std::time::Duration;
let x: Duration;
x = "12h 5min 2ns".parse::<humantime::Duration>().unwrap().into();
assert_eq!(x, Duration::new(12*3600 + 5*60, 2))
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);
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);
Returns the fractional part of this Duration
, in 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);
Returns the fractional part of this Duration
, in 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);
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);
Formats the value using the given formatter. Read more
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
Feeds this value into the given [Hasher
]. Read more
Feeds a slice of this type into the given [Hasher
]. Read more
Performs copy-assignment from source
. Read more
The resulting type after dereferencing.
The associated error which can be returned from parsing.
Parses a string s
to return a value of this type. Read more
Formats the value using the given formatter. Read more