Giter VIP home page Giter VIP logo

timeago's Introduction

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.

With isolang feature off, it supports Rust from version 1.24.

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)
  • Italian (contributed)
  • Ukrainian (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

timeago's People

Contributors

anykao avatar asynclegs avatar casperstorm avatar decathorpe avatar dragostis avatar ivanceras avatar joseluis avatar krk avatar lovasoa avatar mariacruceat avatar paolobarbolini avatar speckyyt avatar vi avatar xeniagda avatar zhanghandong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

timeago's Issues

missing license file for Apache-2.0

Hi! I'm working on packaging this crate for Fedora Linux since it is a new dependency of eza (a maintained exa fork). During technical review, we noticed that this crate's metadata states that the license is "MIT/Apache-2.0", but the license text only contains the MIT license.

Please also include a copy of the Apache-2.0 license text (the idiomatic way to handle this in Rust crates seems to be to have both LICENSE-MIT and LICENSE-APACHE files). Both the MIT and the Apache-2.0 license require redistributed sources to contain a copy of the license text, so the missing license text is currently preventing us from packaging eza.

error[E0599]: no method named `convert_chrono` found for type `timeago::Formatter` in the current scope

I am trying to run the example code for using convert_chrono in my own project, and I am being told by rustc that the method cannot be found on the Formatter type. I've checked all of my imports and such and I can't tell why I am still getting this error. I am using timeago 0.1 on rustc 1.25 (stable-x86_64-apple-darwin). I can work around it by converting my DateTimes to a Duration first and using the regular convert function but would appreciate it if this one worked too as it would simplify my code. Any help figuring out why this isn't working is appreciated, thank you for creating such a useful library.

edit: here's the code in my project

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");

the method convert_chrono is not found on the type that F is (Formatter)

what about future times?

It would be nice to have the equivalent of .ago("ago") but for future times like .in("in") so when you have a time in the future it says in 8 hours and ago() and in() are mutually exclusive.

Add Human format

Humans can't really comprehend time at the microseconds level. We need a format to put less than 1 second to 'just now'.

Helper command doesn't place `ago` properly

from: src/languages/french.rs

assert_eq!(f.convert(Duration::from_secs(60)), "il y a 1 minute");

But running the command:

$ cargo run fr
    Finished dev [unoptimized + debuginfo] target(s) in 0.07s
     Running `(..)/timeago/target/debug/timeago fr`

60
1 minute il y a

Greater precision formats

Currently the format only displays the highest unit of time and chops off the rest. It would be great for it to include hours, minutes, seconds etc if you want and be customizable.

Suffix "ago" optional

Can you make the ago part of the string optional? It's useful in more contexts that way

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.