#duration #string #timeago

bin+lib timeago

Given a Duration, lossily format it like in ‘N days ago’. Parsing it back to Duration is not supported yet.

12 releases

Uses old Rust 2015

0.3.0 Mar 3, 2021
0.2.2 Mar 3, 2021
0.2.1 Apr 16, 2020
0.2.0 Apr 8, 2019
0.0.1 Aug 12, 2017

#8 in Date and time

Download history 64/week @ 2020-12-25 169/week @ 2021-01-01 207/week @ 2021-01-08 129/week @ 2021-01-15 237/week @ 2021-01-22 144/week @ 2021-01-29 134/week @ 2021-02-05 120/week @ 2021-02-12 177/week @ 2021-02-19 153/week @ 2021-02-26 60/week @ 2021-03-05 220/week @ 2021-03-12 218/week @ 2021-03-19 252/week @ 2021-03-26 136/week @ 2021-04-02 158/week @ 2021-04-09

691 downloads per month
Used in 5 crates

MIT/Apache

64KB
1.5K SLoC

timeago

In Rust, format Duration into string like "1 hour ago" or "01hou".

Currently it does not take the calendar into account and assumes each month is about 30.4 days long.

Parsing such string back to a Duration is out of scope for this crate. Maybe see the chrono-english crate instead.

API

Documentation link

Simplified API excerpt (pseudocode):

pub struct Formatter<L : Language = English>{...}

impl Formatter {
    pub fn new() -> Formatter<English>;
    pub fn with_language(l: Language) -> Self;
    pub fn num_items(&mut self, x: usize) -> &mut Self;
    pub fn max_unit(&mut self, x: TimeUnit) -> &mut Self;
    pub fn min_unit(&mut self, x: TimeUnit) -> &mut Self;
    pub fn too_low(&mut self, x: &'static str) -> &mut Self;
    pub fn too_high(&mut self, x: &'static str) -> &mut Self;
    pub fn max_duration(&mut self, x: Duration) -> &mut Self;
    pub fn ago(&mut self, x: &'static str) -> &mut Self;
    
    pub fn convert(&self, d: Duration) -> String;
    pub fn convert_chrono(&self, from: chrono::DateTime, to: chrono::DateTime) -> String;
}

pub fn from_isolang(x : isolang::Language) -> Option<Box<Language>>;

pub fn format_5chars(d: Duration) -> String;

A Language can be constructed from isolang::Language.

Translations

  • English
  • Russian
  • French
  • Portuguese (contributed)
  • German (unchecked)
  • Belarusian (unchecked)
  • Polish (unchecked)
  • Spanish (contributed)
  • Chinese (contributed)
  • Romanian (contributed)
  • Swedish (contributed)
  • Turkish (contributed)
  • Japanese (contributed)
  • Danish (contributed)

If you checked some language and certify that it's allright, submit a pull request that removes "(unchecked)" or "(contributed)" in the list above.

Tool

There is a helper command line tool that allows easier experimenting when adding a new translation:

$ cargo run --features isolang en
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/timeago en`
60
1 minute ago
7200
2 hours ago

See also

  • chrono-humanize
  • compound_duration - split Duration into weeks/days/minues/etc. parts

Dependencies

~1MB
~15K SLoC