From 374f53eb320844be14d13bf637afc33f2077bb22 Mon Sep 17 00:00:00 2001 From: robrobinbin <> Date: Tue, 12 Jan 2021 19:59:32 +0100 Subject: [PATCH] Relative timestamps for recent posts --- src/utils.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 443e6d0..88c1224 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -7,7 +7,7 @@ use base64::encode; use regex::Regex; use serde_json::from_str; use std::collections::HashMap; -use time::OffsetDateTime; +use time::{OffsetDateTime, Duration}; use url::Url; // @@ -185,6 +185,18 @@ pub async fn media(data: &serde_json::Value) -> (String, String) { (post_type.to_string(), url) } +pub fn time(unix_time: i64) -> String { + let time = OffsetDateTime::from_unix_timestamp(unix_time); + let time_delta = OffsetDateTime::now_utc() - time; + if time_delta > Duration::days(1) { + time.format("%b %d '%y") // %b %e '%y + } else if time_delta.whole_hours() > 0 { + format!("{}h ago", time_delta.whole_hours()) + } else { + format!("{}m ago", time_delta.whole_minutes()) + } +} + // // JSON PARSING // @@ -227,8 +239,8 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec Result<(Vec