DurationHuman

Struct DurationHuman 

Source
pub struct DurationHuman { /* private fields */ }
Expand description

Define a Duration in human readable form

§Examples

let duration = DurationHuman::try_from("80h").unwrap();
assert_eq!(format!("{:#}", duration), "3 days 8h".to_string());
assert_eq!(format!("{}", duration), "80h".to_string());
let duration = DurationHuman::try_from("72h").unwrap();
assert_eq!(format!("{:#}", duration), "3 days".to_string());
assert_eq!(format!("{}", duration), "3 days".to_string());
let duration = DurationHuman::try_from("18446744073709551615ns").unwrap();
assert_eq!(format!("{:#}", duration), "5 centuries 84 years 6 months 2 weeks 1 day 8h 34min 33s 709ms 551μs 615ns".to_string());
// roundtrip
let duration = DurationHuman::try_from("5 centuries 84 years 6 months 2 weeks 1 day 8h 34min 33s 709ms 551μs 615ns").unwrap();
let pretty = format!("{:#}", duration);
let duration_from_pretty = DurationHuman::try_from(pretty.as_str())?;
assert_eq!(duration, duration_from_pretty);
// precision is nano second
let duration = DurationHuman::try_from("604800μs").unwrap();
assert_eq!(format!("{:#}", duration), "604ms 800μs".to_string());
assert_eq!(duration.to_string(), "604800μs".to_string());
let duration = DurationHuman::try_from("604800ms").unwrap();
assert_eq!(format!("{:#}", duration), "10min 4s 800ms".to_string());
assert_eq!(duration.to_string(), "604800ms".to_string());
let duration = DurationHuman::try_from("604800s").unwrap();
assert_eq!(format!("{:#}", duration), "1 week".to_string());
let duration = DurationHuman::try_from("604800s").unwrap();
assert_eq!(format!("{:#}", duration), "1 week".to_string());
assert_eq!(format!("{}", duration), "1 week".to_string());
let duration = DurationHuman::try_from("608430s").unwrap();
assert_eq!(format!("{:#}", duration), "1 week 1h 30s".to_string());
assert_eq!(format!("{}", duration), "608430s".to_string());

Implementations§

Source§

impl DurationHuman

Source

pub const MICRO_SEC: u64 = 1_000u64

Source

pub const MILLI_SEC: u64 = 1_000_000u64

Source

pub const SEC: u64 = 1_000_000_000u64

Source

pub const MINUTE: u64 = 60_000_000_000u64

Source

pub const HOUR: u64 = 3_600_000_000_000u64

Source

pub const DAY: u64 = 86_400_000_000_000u64

Source

pub const WEEK: u64 = 604_800_000_000_000u64

Source

pub const YEAR: u64 = 31_557_600_000_000_000u64

Source

pub const MONTH: u64 = 2_629_800_000_000_000u64

Source

pub const CENTURY: u64 = 3_155_760_000_000_000_000u64

Source

pub const ONE_SECOND: Self

Source

pub const ONE_MILLISECOND: Self

Source

pub const fn new(nanos: u64) -> Self

Source

pub fn parse(human_readable: &str) -> Result<Self, DurationError>

Create a new duration from a human redable string

§Errors

DurationError when the parsing fails

Source

pub fn is_in(&self, range: &DurationHumanValidator) -> bool

Trait Implementations§

Source§

impl Add<Instant> for DurationHuman

Source§

fn add(self, rhs: Instant) -> Self::Output

Create a new std::time::Instant by adding one to this duration

§Example
let instant = Instant::now();
let duration = DurationHuman::try_from("420s")?;
let after = duration + instant;
let diff = DurationHuman::from(after - instant);
assert_eq!(format!("{}", diff), format!("7min"));
Source§

type Output = Instant

The resulting type after applying the + operator.
Source§

impl Clone for DurationHuman

Source§

fn clone(&self) -> DurationHuman

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DurationHuman

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DurationHuman

Source§

fn default() -> Self

Defaults to a 1min duration

Source§

impl Display for DurationHuman

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&DurationHuman> for Duration

Source§

fn from(duration: &DurationHuman) -> Self

For non human interaction features, just unwrap the std::time::Duration

§Example
let duration = DurationHuman::try_from("5min")?;
let duration = std::time::Duration::from(&duration);
assert_eq!(duration.as_secs_f32(), 300_f32);
Source§

impl From<&DurationHuman> for u64

Source§

fn from(duration: &DurationHuman) -> Self

convert this duration into nano seconds

Source§

impl From<Duration> for DurationHuman

Source§

fn from(inner: Duration) -> Self

Converts to this type from the input type.
Source§

impl From<DurationHuman> for OsStr

Source§

fn from(duration: DurationHuman) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for DurationHuman

Source§

fn from(nanos: u64) -> Self

Create a duration in nano seconds

Source§

impl Parse for DurationHuman

Source§

fn parse(input: ParseStream<'_>) -> Result<Self>

Source§

impl PartialEq for DurationHuman

Source§

fn eq(&self, other: &DurationHuman) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for DurationHuman

Source§

fn partial_cmp(&self, other: &DurationHuman) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl TryFrom<&str> for DurationHuman

Source§

type Error = DurationError

The type returned in the event of a conversion error.
Source§

fn try_from(value: &str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for DurationHuman

Source§

impl Eq for DurationHuman

Source§

impl StructuralPartialEq for DurationHuman

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<I> IntoResettable<OsStr> for I
where I: Into<OsStr>,

Source§

fn into_resettable(self) -> Resettable<OsStr>

Convert to the intended resettable type
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.