From 5dc3279ac30d020f7f2bce1846d4e2a7241be877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Pe=C5=A1ek?= Date: Thu, 23 Mar 2023 13:18:48 +0100 Subject: [PATCH] fix: make time work with future dates --- src/utils.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index e6cb2f7..d07efad 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -815,20 +815,28 @@ pub fn format_num(num: i64) -> (String, String) { // Parse a relative and absolute time from a UNIX timestamp pub fn time(created: f64) -> (String, String) { let time = OffsetDateTime::from_unix_timestamp(created.round() as i64).unwrap_or(OffsetDateTime::UNIX_EPOCH); - let time_delta = OffsetDateTime::now_utc() - time; + let min = time.min(OffsetDateTime::now_utc()); + let max = time.max(OffsetDateTime::now_utc()); + let time_delta = max - min; // If the time difference is more than a month, show full date - let rel_time = if time_delta > Duration::days(30) { + let mut rel_time = if time_delta > Duration::days(30) { time.format(format_description!("[month repr:short] [day] '[year repr:last_two]")).unwrap_or_default() // Otherwise, show relative date/time } else if time_delta.whole_days() > 0 { - format!("{}d ago", time_delta.whole_days()) + format!("{}d", time_delta.whole_days()) } else if time_delta.whole_hours() > 0 { - format!("{}h ago", time_delta.whole_hours()) + format!("{}h", time_delta.whole_hours()) } else { - format!("{}m ago", time_delta.whole_minutes()) + format!("{}m", time_delta.whole_minutes()) }; + if OffsetDateTime::now_utc() < time { + rel_time += " left"; + } else { + rel_time += " ago"; + } + ( rel_time, time