Struct timeago::Formatter

source ·
pub struct Formatter<L: Language = English> { /* private fields */ }
Expand description

Main formatter struct. Build it with new() and maybe modify some options, then use convert.

let f = timeago::Formatter::new();
let d = std::time::Duration::from_secs(3600);
assert_eq!(f.convert(d), "1 hour ago");

Implementations§

source§

impl Formatter

source

pub fn new() -> Formatter

Constructor for some default formatting in English

It emits one chunk, limits to seconds and has no maximum duration.

source§

impl<L: Language> Formatter<L>

source

pub fn with_language(l: L) -> Self

Constructor for some default formatting with specified language instance

It emits one item (chunk), limits to seconds and has no maximum duration.

source

pub fn num_items(&mut self, x: usize) -> &mut Self

Set number of time unit items to emit (for example, 1 item is for “1 year”; 3 items is for “1 year 3 months 17 days”). Zero chunks like “0 minutes” are not emitted, expect of at the end if too_low is "0". Default is 1.

let mut f = timeago::Formatter::new();
f.num_items(1);
let d = std::time::Duration::from_secs(3600+60+3);
assert_eq!(f.convert(d), "1 hour ago");
f.num_items(2);
assert_eq!(f.convert(d), "1 hour 1 minute ago");
f.num_items(3);
assert_eq!(f.convert(d), "1 hour 1 minute 3 seconds ago");
f.num_items(4);
assert_eq!(f.convert(d), "1 hour 1 minute 3 seconds ago");
source

pub fn max_unit(&mut self, x: TimeUnit) -> &mut Self

Set maximum used unit. Not to be confused with max_duration. Should not affect appearance of “old” or other too_high values.

let mut f = timeago::Formatter::new();
f.max_unit(timeago::TimeUnit::Hours);
let d = std::time::Duration::from_secs(60);
assert_eq!(f.convert(d), "1 minute ago");
let d = std::time::Duration::from_secs(3600);
assert_eq!(f.convert(d), "1 hour ago");
let d = std::time::Duration::from_secs(24*3600);
assert_eq!(f.convert(d), "24 hours ago");
let d = std::time::Duration::from_secs(30*24*3600);
assert_eq!(f.convert(d), "720 hours ago");
source

pub fn min_unit(&mut self, x: TimeUnit) -> &mut Self

Set minimum used unit. Durations below minimally representable by that unit emit too_low value like “now”, or like “0 days” instead of normal output. When num_items > 1, it also acts as precision limiter.

let mut f = timeago::Formatter::new();
f.min_unit(timeago::TimeUnit::Minutes);
let d = std::time::Duration::from_secs(30);
assert_eq!(f.convert(d), "now");
let d = std::time::Duration::from_secs(90);
assert_eq!(f.convert(d), "1 minute ago");
let mut f = timeago::Formatter::new();
f.num_items(99);
let d = std::time::Duration::new(1*3600*24 + 2*3600 + 3*60 + 4, 500_000_000);
assert_eq!(f.convert(d), "1 day 2 hours 3 minutes 4 seconds ago");
f.min_unit(timeago::TimeUnit::Hours);
assert_eq!(f.convert(d), "1 day 2 hours ago");
f.min_unit(timeago::TimeUnit::Microseconds);
assert_eq!(f.convert(d), "1 day 2 hours 3 minutes 4 seconds 500 milliseconds ago");
f.min_unit(timeago::TimeUnit::Months);
assert_eq!(f.convert(d), "now");
source

pub fn too_low(&mut self, x: &'static str) -> &mut Self

Override what is used instead of “now” for too short durations (not representable with the time unit configures as min_unit). Setting this to special value "0" causes emitting output like “0 days”, depending on min_unit property. Note that Language’s too_low is not used in this case, except of for "0".

let mut f = timeago::Formatter::new();
f.min_unit(timeago::TimeUnit::Months)
 .too_low("this month");
let d = std::time::Duration::from_secs(24*3600);
assert_eq!(f.convert(d), "this month");
let mut f = timeago::Formatter::new();
f.min_unit(timeago::TimeUnit::Minutes);
let d = std::time::Duration::from_secs(30);
assert_eq!(f.convert(d), "now");
f.too_low("-");
assert_eq!(f.convert(d), "-");
f.too_low("");
assert_eq!(f.convert(d), "");
f.too_low("0");
assert_eq!(f.convert(d), "0 minutes ago");
source

pub fn too_high(&mut self, x: &'static str) -> &mut Self

Override what is used instead of “old” for too high units. Note that Language’s too_high is not used in this case.

let mut f = timeago::Formatter::new();
f.max_duration(std::time::Duration::from_secs(3600*24*30));
f.too_high("ancient");
let d = std::time::Duration::from_secs(1000_000_000_000);
assert_eq!(f.convert(d), "ancient");
source

pub fn max_duration(&mut self, x: Duration) -> &mut Self

Maximum duration before it start giving “old” (or other too_high value)

let mut f = timeago::Formatter::new();
f.max_duration(std::time::Duration::new(3600*24*30, 0));
let d = std::time::Duration::from_secs(1000_000_000);
assert_eq!(f.convert(d), "old");
source

pub fn ago(&mut self, x: &'static str) -> &mut Self

Override what is used instead of “ago”. Empty string literal "" is a bit special in the space handling.

let mut f = timeago::Formatter::new();
let d = std::time::Duration::from_secs(60);
assert_eq!(f.convert(d), "1 minute ago");
f.ago("later");
assert_eq!(f.convert(d), "1 minute later");
f.ago("");
assert_eq!(f.convert(d), "1 minute");
source

pub fn convert_chrono<Tz1, Tz2>( &self, from: DateTime<Tz1>, to: DateTime<Tz2> ) -> Stringwhere Tz1: TimeZone, Tz2: TimeZone,

Format the timespan between from and to as a string like “15 days ago”.

Requires chrono Cargo feature.

from should come before to, otherwise "???" will be returned.

Currently it doesn’t actually take the calendar into account and just converts datetimes into a plain old std::time::Duration, but in future here may be a proper implementation.

extern crate chrono;
extern crate timeago;
let mut f = timeago::Formatter::new();
f.num_items(2);
let from = chrono::DateTime::parse_from_rfc3339("2013-12-19T15:00:00+03:00").unwrap();
let to   = chrono::DateTime::parse_from_rfc3339("2013-12-23T17:00:00+03:00").unwrap();
assert_eq!(f.convert_chrono(from, to), "4 days 2 hours ago");
source

pub fn convert(&self, d: Duration) -> String

Convert specified Duration to a String representing approximation of specified timespan as a string like “5 days ago”, with specified by other methods settings. See module-level doc for more info.

let f = timeago::Formatter::new();
let d = std::time::Duration::from_secs(3600*24);
assert_eq!(f.convert(d), "1 day ago");

Trait Implementations§

source§

impl Clone for Formatter<BoxedLanguage>

source§

fn clone(&self) -> Formatter<BoxedLanguage>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Default for Formatter

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<L> RefUnwindSafe for Formatter<L>where L: RefUnwindSafe,

§

impl<L> Send for Formatter<L>where L: Send,

§

impl<L> Sync for Formatter<L>where L: Sync,

§

impl<L> Unpin for Formatter<L>where L: Unpin,

§

impl<L> UnwindSafe for Formatter<L>where L: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.