Skip to main content

HumanDuration

Struct HumanDuration 

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

Humanises a SystemTime timestamp into a relative time string.

Computes the elapsed time between the given timestamp and the current time, then formats it as a human-readable duration (e.g. "45s ago", "2 hours ago", "yesterday", "3m from now"). Handles both past and future timestamps.

If the timestamp is None, all output methods return "-".

The Display implementation outputs the full format.

§Examples

use humanly::HumanDuration;
use std::time::{Duration, SystemTime};

let past = Some(SystemTime::now() - Duration::from_secs(120));
assert_eq!(HumanDuration::from(past).concise(), "2m ago");

assert_eq!(HumanDuration::from(None).concise(), "-");

Implementations§

Source§

impl HumanDuration

Source

pub fn from(system_time: Option<SystemTime>) -> Self

Creates a new HumanDuration from an optional SystemTime.

§Parameters
  • system_time - The timestamp to compute relative duration from. Pass None to represent an absent or unknown time.
§Returns

A new HumanDuration instance.

§Examples
use humanly::HumanDuration;
use std::time::{Duration, SystemTime};

let duration = HumanDuration::from(Some(SystemTime::now() - Duration::from_secs(60)));
let unknown = HumanDuration::from(None);
Source

pub fn concise(&self) -> String

Returns the relative duration in concise format.

Uses short suffixes: s (seconds), m (minutes), h (hours), d (days), w/wk (weeks), mo (months), y/yr (years), followed by ago or from now.

§Returns

A String with the concise relative time (e.g. "45s ago", "2h from now"), "just now" if less than one second, or "-" if the timestamp is None.

§Examples
use humanly::HumanDuration;
use std::time::{Duration, SystemTime};

let past = Some(SystemTime::now() - Duration::from_secs(90));
assert_eq!(HumanDuration::from(past).concise(), "1m ago");

assert_eq!(HumanDuration::from(None).concise(), "-");

Trait Implementations§

Source§

impl Display for HumanDuration

Source§

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

Formats the value using the given formatter. Read more

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> 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<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.