Merge pull request #48 from robrobinbin/relative_timestamps

Relative timestamps for posts younger than 24h
This commit is contained in:
Spike 2021-01-12 15:15:37 -08:00 committed by GitHub
commit 22589c8296
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<Post
stickied: post["data"]["stickied"].as_bool().unwrap_or_default(),
},
permalink: val(post, "permalink"),
time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d '%y"), // %b %e '%y
time: time(unix_time),
});
}