From 84b113e8250d50ffe8417c4cee37cf5d2756109b Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Thu, 28 Aug 2025 14:32:04 -0700 Subject: [PATCH] Add `languages::english::EnglishAbbreviated` This adds a new `EnglishAbbreviated` `Language` which takes advantage of the additional `Language` methods introduced in https://github.com/vi/timeago/pull/36 in order to produce more compact output. I'm not sure this feature is appropriate for this library (none of the other `Language`s encode stylistic opinions like this), but I wanted to demonstrate the usage of the features. --- src/languages/english.rs | 82 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/src/languages/english.rs b/src/languages/english.rs index 794a2f5..4a09e31 100644 --- a/src/languages/english.rs +++ b/src/languages/english.rs @@ -47,3 +47,85 @@ impl Language for English { } } } + +#[derive(Default)] +pub struct EnglishAbbreviated; + +impl Language for EnglishAbbreviated { + fn too_low(&self) -> &'static str { + "now" + } + + fn too_high(&self) -> &'static str { + "old" + } + + fn ago(&self) -> &'static str { + "" + } + + fn get_word(&self, tu: TimeUnit, _: u64) -> &'static str { + use TimeUnit::*; + match tu { + Nanoseconds => "ns", + Microseconds => "μs", + Milliseconds => "ms", + Seconds => "s", + Minutes => "m", + Hours => "h", + Days => "d", + Weeks => "wk", + Months => "mo", + Years => "yr", + } + } + + fn clone_boxed(&self) -> crate::BoxedLanguage { + todo!() + } + + fn place_ago_before(&self) -> bool { + false + } + + fn override_space_near_ago(&self) -> &str { + "" + } + + fn place_unit_before(&self, _: u64) -> bool { + false + } + + fn between_chunks(&self) -> &str { + " " + } + + fn between_value_and_word(&self) -> &str { + "" + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_abbreviated() { + fn fmt(seconds: u64) -> String { + crate::Formatter::with_language(EnglishAbbreviated) + .convert(std::time::Duration::from_secs(seconds)) + } + + assert_eq!(fmt(0), "now"); + assert_eq!(fmt(1), "1s"); + assert_eq!(fmt(59), "59s"); + assert_eq!(fmt(60), "1m"); + assert_eq!(fmt(65), "1m"); + assert_eq!(fmt(119), "1m"); + assert_eq!(fmt(120), "2m"); + assert_eq!(fmt(3599), "59m"); + assert_eq!(fmt(3600), "1h"); + assert_eq!(fmt(1000_000), "1wk"); + assert_eq!(fmt(1000_000_000), "31yr"); + } +}