18 releases
0.2.3 | Jul 22, 2023 |
---|---|
0.2.2 | Aug 17, 2022 |
0.2.1 | Apr 27, 2021 |
0.1.2 | Feb 2, 2021 |
0.0.6 | Jul 21, 2016 |
#18 in Date and time
129,706 downloads per month
Used in 476 crates
(65 directly)
18KB
332 lines
Chrono Humanize - Make your text time representation human appealing
Build status
branch | status |
---|---|
master | |
develop |
Quick Start
use chrono::{Local, Duration};
use chrono_humanize::HumanTime;
let dt = Local::now() + Duration::days(35);
let ht = HumanTime::from(dt);
let english = format!("{}", ht);
assert_eq!("in a month", english);
lib.rs
:
Representation for chrono objects in human languages
Quick Start
HumanTime
objects are created from chrono objects, such as chrono::DateTime
and chrono::Duration
Examples
Convert current time taken as Local::now()
to HumanTime
let dt = chrono::Local::now();
let ht = chrono_humanize::HumanTime::from(dt);
assert_eq!("now", format!("{}", ht));
let dt = chrono::Local::now() - chrono::Duration::minutes(58);
let ht = chrono_humanize::HumanTime::from(dt);
assert_eq!("an hour ago", format!("{}", ht));
For full control over the text representation use HumanTime::to_text_en()
use chrono::Duration;
use chrono_humanize::{Accuracy, HumanTime, Tense};
let dt = Duration::days(45);
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
~1–6MB
~26K SLoC