Struct humantime::Duration
[−]
[src]
pub struct Duration(_);
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.
Example
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))
Methods from Deref<Target = StdDuration>
fn as_secs(&self) -> u64
1.3.0[src]
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
.
Examples
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);
fn subsec_nanos(&self) -> u32
1.3.0[src]
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).
Examples
use std::time::Duration; let duration = Duration::from_millis(5010); assert_eq!(duration.as_secs(), 5); assert_eq!(duration.subsec_nanos(), 10_000_000);
fn checked_add(self, rhs: Duration) -> Option<Duration>
1.16.0[src]
Checked Duration
addition. Computes self + other
, returning None
if overflow occurred.
Examples
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);
fn checked_sub(self, rhs: Duration) -> Option<Duration>
1.16.0[src]
Checked Duration
subtraction. Computes self - other
, returning None
if the result would be negative or if underflow occurred.
Examples
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);
fn checked_mul(self, rhs: u32) -> Option<Duration>
1.16.0[src]
Checked Duration
multiplication. Computes self * other
, returning
None
if overflow occurred.
Examples
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);
fn checked_div(self, rhs: u32) -> Option<Duration>
1.16.0[src]
Checked Duration
division. Computes self / other
, returning None
if other == 0
.
Examples
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);
Trait Implementations
impl Debug for Duration
[src]
impl PartialEq for Duration
[src]
fn eq(&self, __arg_0: &Duration) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Duration) -> bool
[src]
This method tests for !=
.
impl Eq for Duration
[src]
impl Hash for Duration
[src]
fn hash<__H: Hasher>(&self, __arg_0: &mut __H)
[src]
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl Clone for Duration
[src]
fn clone(&self) -> Duration
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl Copy for Duration
[src]
impl AsRef<StdDuration> for Duration
[src]
fn as_ref(&self) -> &StdDuration
[src]
Performs the conversion.
impl Deref for Duration
[src]
type Target = StdDuration
The resulting type after dereferencing.
fn deref(&self) -> &StdDuration
[src]
Dereferences the value.
impl Into<StdDuration> for Duration
[src]
fn into(self) -> StdDuration
[src]
Performs the conversion.
impl From<StdDuration> for Duration
[src]
fn from(dur: StdDuration) -> Duration
[src]
Performs the conversion.
impl FromStr for Duration
[src]
type Err = Error
The associated error which can be returned from parsing.
fn from_str(s: &str) -> Result<Duration, Self::Err>
[src]
Parses a string s
to return a value of this type. Read more