1 unstable release
0.1.1 | Aug 7, 2024 |
---|
#150 in Value formatting
18KB
338 lines
Jiff Humanize
Quick Start
use jiff::{Local, Duration};
use jiffy::HumanTime;
let dt = jiff::Zoned::now() + Duration::days(35);
let ht = HumanTime::from(dt);
let english = format!("{}", ht);
assert_eq!("in a month", english);
lib.rs
:
Representation for jiff objects in human languages
Quick Start
HumanTime
objects are created from jiff objects, such as jiff::DateTime
and jiff::Span
Examples
Convert current time taken as now
to HumanTime
let dt = jiff::Zoned::now();
let ht = jiffy::HumanTime::from(dt);
assert_eq!("now", format!("{}", ht));
let dt = jiff::Zoned::now().checked_sub(58.minutes()).unwrap()
let ht = jiffy::HumanTime::from(dt);
assert_eq!("an hour ago", format!("{}", ht));
For full control over the text representation use HumanTime::to_text_en()
use jiff::ToSpan;;
use jiffy::{Accuracy, HumanTime, Tense};
let dt = 45.days();
let ht = HumanTime::from(dt);
assert_eq!("a month", ht.to_text_en(Accuracy::Rough, Tense::Present));
assert_eq!("1 month, 2 weeks and 1 day", ht.to_text_en(Accuracy::Precise, Tense::Present));
Dependencies
~2.5MB
~34K SLoC