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
impl HumanDuration
Sourcepub fn from(system_time: Option<SystemTime>) -> Self
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. PassNoneto 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);Sourcepub fn concise(&self) -> String
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(), "-");