Relative timestamps for recent posts

This commit is contained in:
robrobinbin 2021-01-12 19:59:32 +01:00
parent 065d82a5f5
commit 374f53eb32
1 changed files with 16 additions and 4 deletions

View File

@ -7,7 +7,7 @@ use base64::encode;
use regex::Regex; use regex::Regex;
use serde_json::from_str; use serde_json::from_str;
use std::collections::HashMap; use std::collections::HashMap;
use time::OffsetDateTime; use time::{OffsetDateTime, Duration};
use url::Url; use url::Url;
// //
@ -185,6 +185,18 @@ pub async fn media(data: &serde_json::Value) -> (String, String) {
(post_type.to_string(), url) (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 // JSON PARSING
// //
@ -263,7 +275,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
stickied: post["data"]["stickied"].as_bool().unwrap_or_default(), stickied: post["data"]["stickied"].as_bool().unwrap_or_default(),
}, },
permalink: val(post, "permalink"), permalink: val(post, "permalink"),
time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d '%y"), // %b %e '%y time: time(unix_time),
}); });
} }