chrono_humanize/lib.rs
1//! Representation for chrono objects in human languages
2//!
3//! # Quick Start
4//!
5//! `HumanTime` objects are created from chrono objects, such as `chrono::DateTime`
6//! and `chrono::Duration`
7//!
8//! # Examples
9//!
10//! Convert current time taken as `Local::now()` to `HumanTime`
11//!
12//! ```
13//! let dt = chrono::Local::now();
14//! let ht = chrono_humanize::HumanTime::from(dt);
15//!
16//! assert_eq!("now", format!("{}", ht));
17//! ```
18//!
19//!
20//! ```
21//! let dt = chrono::Local::now() - chrono::Duration::minutes(58);
22//! let ht = chrono_humanize::HumanTime::from(dt);
23//!
24//! assert_eq!("an hour ago", format!("{}", ht));
25//! ```
26//!
27//! For full control over the text representation use `HumanTime::to_text_en()`
28//!
29//! ```
30//! use chrono::Duration;
31//! use chrono_humanize::{Accuracy, HumanTime, Tense};
32//!
33//! # fn main() {
34//! let dt = Duration::days(45);
35//! let ht = HumanTime::from(dt);
36//!
37//! assert_eq!("a month", ht.to_text_en(Accuracy::Rough, Tense::Present));
38//! assert_eq!("1 month, 2 weeks and 1 day", ht.to_text_en(Accuracy::Precise, Tense::Present));
39//! # }
40//! ```
41
42#![cfg_attr(feature = "pedantic", warn(clippy::pedantic))]
43#![warn(clippy::use_self)]
44#![warn(deprecated_in_future)]
45#![warn(future_incompatible)]
46#![warn(unreachable_pub)]
47#![warn(missing_debug_implementations)]
48#![warn(rust_2018_compatibility)]
49#![warn(rust_2018_idioms)]
50#![warn(unused)]
51#![deny(warnings)]
52#![doc(html_root_url = "https://docs.rs/chrono-humanize/0.2.2")]
53
54pub use crate::humantime::{Accuracy, HumanTime, Tense};
55
56mod humantime;
57
58/// Present the object in human friendly text form
59pub trait Humanize {
60 /// Emits `String` that represents current object in human friendly form
61 fn humanize(&self) -> String;
62}