diff --git a/src/utils.rs b/src/utils.rs index 443e6d0..0003cf0 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 // @@ -263,7 +275,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec