use chrono::{DateTime, Local};
#[inline]
#[must_use]
pub fn humanize(datetime: DateTime<Local>) -> String {
humanize_impl(datetime, Local::now())
}
fn humanize_impl(datetime: DateTime<Local>, now: DateTime<Local>) -> String {
const MINUTE: i64 = 60;
const HOUR: i64 = 3_600;
const DAY: i64 = 86_400;
const MONTH: i64 = 30 * DAY;
const YEAR: i64 = 365 * DAY;
let secs = now.signed_duration_since(datetime).num_seconds();
let is_past = secs >= 0;
let abs = secs.saturating_abs();
if abs < 30 {
"just now".to_string()
} else if abs < 90 {
if is_past {
"a minute ago".to_string()
} else {
"in a minute".to_string()
}
} else if abs < 45 * MINUTE {
let n = abs / MINUTE;
let unit = if n == 1 { "minute" } else { "minutes" };
if is_past {
format!("{n} {unit} ago")
} else {
format!("in {n} {unit}")
}
} else if abs < 90 * MINUTE {
if is_past {
"an hour ago".to_string()
} else {
"in an hour".to_string()
}
} else if abs < 22 * HOUR {
let n = abs / HOUR;
let unit = if n == 1 { "hour" } else { "hours" };
if is_past {
format!("{n} {unit} ago")
} else {
format!("in {n} {unit}")
}
} else if abs < 36 * HOUR {
if is_past {
"yesterday".to_string()
} else {
"tomorrow".to_string()
}
} else if abs < 25 * DAY {
let n = abs / DAY;
let unit = if n == 1 { "day" } else { "days" };
if is_past {
format!("{n} {unit} ago")
} else {
format!("in {n} {unit}")
}
} else if abs < 45 * DAY {
if is_past {
"a month ago".to_string()
} else {
"in a month".to_string()
}
} else if abs < 10 * MONTH {
let n = abs / MONTH;
let unit = if n == 1 { "month" } else { "months" };
if is_past {
format!("{n} {unit} ago")
} else {
format!("in {n} {unit}")
}
} else if abs < 18 * MONTH {
if is_past {
"a year ago".to_string()
} else {
"in a year".to_string()
}
} else {
let n = abs / YEAR;
let unit = if n == 1 { "year" } else { "years" };
if is_past {
format!("{n} {unit} ago")
} else {
format!("in {n} {unit}")
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::relative::functions::{days_ago, hours_from_now};
use chrono::Duration;
fn h_past(secs: i64) -> String {
let now = Local::now();
humanize_impl(now - Duration::seconds(secs), now)
}
fn h_future(secs: i64) -> String {
let now = Local::now();
humanize_impl(now + Duration::seconds(secs), now)
}
#[test]
fn test_humanize_just_now_past() {
assert_eq!(h_past(10), "just now");
}
#[test]
fn test_humanize_just_now_future() {
assert_eq!(h_future(10), "just now");
}
#[test]
fn test_humanize_a_minute_ago() {
assert_eq!(h_past(60), "a minute ago");
}
#[test]
fn test_humanize_in_a_minute() {
assert_eq!(h_future(60), "in a minute");
}
#[test]
fn test_humanize_minutes_ago() {
assert_eq!(h_past(5 * 60), "5 minutes ago");
}
#[test]
fn test_humanize_in_minutes() {
assert_eq!(h_future(5 * 60), "in 5 minutes");
}
#[test]
fn test_humanize_1_minute_singular_ago() {
assert_eq!(h_past(95), "1 minute ago");
}
#[test]
fn test_humanize_an_hour_ago() {
assert_eq!(h_past(60 * 60), "an hour ago");
}
#[test]
fn test_humanize_in_an_hour() {
assert_eq!(h_future(60 * 60), "in an hour");
}
#[test]
fn test_humanize_hours_ago() {
assert_eq!(h_past(5 * 3600), "5 hours ago");
}
#[test]
fn test_humanize_in_hours() {
assert_eq!(h_future(5 * 3600), "in 5 hours");
}
#[test]
fn test_humanize_yesterday() {
assert_eq!(h_past(24 * 3600), "yesterday");
}
#[test]
fn test_humanize_tomorrow() {
assert_eq!(h_future(24 * 3600), "tomorrow");
}
#[test]
fn test_humanize_days_ago() {
assert_eq!(h_past(5 * 86_400), "5 days ago");
}
#[test]
fn test_humanize_in_days() {
assert_eq!(h_future(5 * 86_400), "in 5 days");
}
#[test]
fn test_humanize_a_month_ago() {
assert_eq!(h_past(30 * 86_400), "a month ago");
}
#[test]
fn test_humanize_in_a_month() {
assert_eq!(h_future(30 * 86_400), "in a month");
}
#[test]
fn test_humanize_months_ago() {
assert_eq!(h_past(3 * 30 * 86_400), "3 months ago");
}
#[test]
fn test_humanize_in_months() {
assert_eq!(h_future(3 * 30 * 86_400), "in 3 months");
}
#[test]
fn test_humanize_a_year_ago() {
assert_eq!(h_past(13 * 30 * 86_400), "a year ago");
}
#[test]
fn test_humanize_in_a_year() {
assert_eq!(h_future(13 * 30 * 86_400), "in a year");
}
#[test]
fn test_humanize_years_ago() {
assert_eq!(h_past(3 * 365 * 86_400), "3 years ago");
}
#[test]
fn test_humanize_in_years() {
assert_eq!(h_future(3 * 365 * 86_400), "in 3 years");
}
#[test]
fn test_humanize_in_1_minute_singular() {
assert_eq!(h_future(95), "in 1 minute");
}
#[test]
fn test_humanize_1_hour_singular_ago() {
assert_eq!(h_past(5401), "1 hour ago");
}
#[test]
fn test_humanize_in_1_hour_singular() {
assert_eq!(h_future(5401), "in 1 hour");
}
#[test]
fn test_humanize_1_day_singular_ago() {
assert_eq!(h_past(37 * 3_600), "1 day ago");
}
#[test]
fn test_humanize_in_1_day_singular() {
assert_eq!(h_future(37 * 3_600), "in 1 day");
}
#[test]
fn test_humanize_1_month_singular_ago() {
assert_eq!(h_past(46 * 86_400), "1 month ago");
}
#[test]
fn test_humanize_in_1_month_singular() {
assert_eq!(h_future(46 * 86_400), "in 1 month");
}
#[test]
fn test_humanize_1_year_singular_ago() {
assert_eq!(h_past(19 * 30 * 86_400), "1 year ago");
}
#[test]
fn test_humanize_in_1_year_singular() {
assert_eq!(h_future(19 * 30 * 86_400), "in 1 year");
}
#[test]
fn test_humanize_exactly_30s_past_is_a_minute_ago() {
assert_eq!(h_past(30), "a minute ago");
}
#[test]
fn test_humanize_exactly_30s_future_is_in_a_minute() {
assert_eq!(h_future(30), "in a minute");
}
#[test]
fn test_humanize_89s_past_is_a_minute_ago() {
assert_eq!(h_past(89), "a minute ago");
}
#[test]
fn test_humanize_44_minutes_past() {
assert_eq!(h_past(44 * 60), "44 minutes ago");
}
#[test]
fn test_humanize_exactly_45_minutes_past_is_an_hour_ago() {
assert_eq!(h_past(45 * 60), "an hour ago");
}
#[test]
fn test_humanize_exactly_45_minutes_future_is_in_an_hour() {
assert_eq!(h_future(45 * 60), "in an hour");
}
#[test]
fn test_humanize_89_minutes_past_is_an_hour_ago() {
assert_eq!(h_past(89 * 60), "an hour ago");
}
#[test]
fn test_humanize_21_hours_past() {
assert_eq!(h_past(21 * 3_600), "21 hours ago");
}
#[test]
fn test_humanize_exactly_22_hours_past_is_yesterday() {
assert_eq!(h_past(22 * 3_600), "yesterday");
}
#[test]
fn test_humanize_exactly_22_hours_future_is_tomorrow() {
assert_eq!(h_future(22 * 3_600), "tomorrow");
}
#[test]
fn test_humanize_35_hours_past_is_yesterday() {
assert_eq!(h_past(35 * 3_600), "yesterday");
}
#[test]
fn test_humanize_exactly_36_hours_past_is_1_day_ago() {
assert_eq!(h_past(36 * 3_600), "1 day ago");
}
#[test]
fn test_humanize_24_days_past() {
assert_eq!(h_past(24 * 86_400), "24 days ago");
}
#[test]
fn test_humanize_exactly_25_days_past_is_a_month_ago() {
assert_eq!(h_past(25 * 86_400), "a month ago");
}
#[test]
fn test_humanize_exactly_25_days_future_is_in_a_month() {
assert_eq!(h_future(25 * 86_400), "in a month");
}
#[test]
fn test_humanize_exactly_45_days_past_is_months_ago() {
assert_eq!(h_past(45 * 86_400), "1 month ago");
}
#[test]
fn test_humanize_9_months_past() {
assert_eq!(h_past(9 * 30 * 86_400), "9 months ago");
}
#[test]
fn test_humanize_exactly_10_months_past_is_a_year_ago() {
assert_eq!(h_past(10 * 30 * 86_400), "a year ago");
}
#[test]
fn test_humanize_exactly_10_months_future_is_in_a_year() {
assert_eq!(h_future(10 * 30 * 86_400), "in a year");
}
#[test]
fn test_humanize_exactly_18_months_past_is_years_ago() {
assert_eq!(h_past(18 * 30 * 86_400), "1 year ago");
}
#[test]
fn test_humanize_exactly_18_months_future_is_in_years() {
assert_eq!(h_future(18 * 30 * 86_400), "in 1 year");
}
#[test]
fn test_humanize_now_is_just_now() {
let now = Local::now();
assert_eq!(humanize_impl(now, now), "just now");
}
#[test]
fn test_humanize_with_days_ago_relative() {
let dt: DateTime<Local> = days_ago(3).unwrap().into();
assert_eq!(humanize(dt), "3 days ago");
}
#[test]
fn test_humanize_with_hours_from_now_relative() {
let r = hours_from_now(5).unwrap();
let result = humanize(r.into());
assert!(result.starts_with("in "), "expected 'in …', got: {result}");
}
#[test]
fn test_humanize_extreme_past_is_n_years_ago() {
let secs: i64 = 1_000 * 365 * 86_400;
let result = h_past(secs);
assert!(
result.ends_with("years ago"),
"expected 'N years ago', got: {result}"
);
}
#[test]
fn test_humanize_extreme_future_is_in_n_years() {
let secs: i64 = 1_000 * 365 * 86_400;
let result = h_future(secs);
assert!(
result.starts_with("in ") && result.ends_with("years"),
"expected 'in N years', got: {result}"
);
}
}