Struct duration_human::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
impl DurationHuman
pub const MICRO_SEC: u64 = 1_000u64
pub const MILLI_SEC: u64 = 1_000_000u64
pub const SEC: u64 = 1_000_000_000u64
pub const MINUTE: u64 = 60_000_000_000u64
pub const HOUR: u64 = 3_600_000_000_000u64
pub const DAY: u64 = 86_400_000_000_000u64
pub const WEEK: u64 = 604_800_000_000_000u64
pub const YEAR: u64 = 31_557_600_000_000_000u64
pub const MONTH: u64 = 2_629_800_000_000_000u64
pub const CENTURY: u64 = 3_155_760_000_000_000_000u64
pub const ONE_SECOND: Self = _
pub const ONE_MILLISECOND: Self = _
pub const fn new(nanos: u64) -> Self
sourcepub fn parse(human_readable: &str) -> Result<Self, DurationError>
pub fn parse(human_readable: &str) -> Result<Self, DurationError>
pub fn is_in(&self, range: &DurationHumanValidator) -> bool
Trait Implementations§
source§impl Add<Instant> for DurationHuman
impl Add<Instant> for DurationHuman
source§fn add(self, rhs: Instant) -> Self::Output
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§impl Clone for DurationHuman
impl Clone for DurationHuman
source§fn clone(&self) -> DurationHuman
fn clone(&self) -> DurationHuman
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for DurationHuman
impl Debug for DurationHuman
source§impl Display for DurationHuman
impl Display for DurationHuman
source§impl From<&DurationHuman> for Duration
impl From<&DurationHuman> for Duration
source§fn from(duration: &DurationHuman) -> Self
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
impl From<&DurationHuman> for u64
source§fn from(duration: &DurationHuman) -> Self
fn from(duration: &DurationHuman) -> Self
convert this duration into nano seconds
source§impl From<Duration> for DurationHuman
impl From<Duration> for DurationHuman
source§impl From<DurationHuman> for OsStr
impl From<DurationHuman> for OsStr
source§fn from(duration: DurationHuman) -> Self
fn from(duration: DurationHuman) -> Self
Converts to this type from the input type.
source§impl From<u64> for DurationHuman
impl From<u64> for DurationHuman
source§impl Parse for DurationHuman
impl Parse for DurationHuman
fn parse(input: ParseStream<'_>) -> Result<Self>
source§impl PartialEq<DurationHuman> for DurationHuman
impl PartialEq<DurationHuman> for DurationHuman
source§fn eq(&self, other: &DurationHuman) -> bool
fn eq(&self, other: &DurationHuman) -> bool
source§impl PartialOrd<DurationHuman> for DurationHuman
impl PartialOrd<DurationHuman> for DurationHuman
source§fn partial_cmp(&self, other: &DurationHuman) -> Option<Ordering>
fn partial_cmp(&self, other: &DurationHuman) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moresource§impl TryFrom<&str> for DurationHuman
impl TryFrom<&str> for DurationHuman
impl Copy for DurationHuman
impl Eq for DurationHuman
impl StructuralEq for DurationHuman
impl StructuralPartialEq for DurationHuman
Auto Trait Implementations§
impl RefUnwindSafe for DurationHuman
impl Send for DurationHuman
impl Sync for DurationHuman
impl Unpin for DurationHuman
impl UnwindSafe for DurationHuman
Blanket Implementations§
source§impl<I> IntoResettable<OsStr> for Iwhere
I: Into<OsStr>,
impl<I> IntoResettable<OsStr> for Iwhere
I: Into<OsStr>,
source§fn into_resettable(self) -> Resettable<OsStr>
fn into_resettable(self) -> Resettable<OsStr>
Convert to the intended resettable type