From fdf60e7255a97a43575b5d2ed788b86a4a8bf786 Mon Sep 17 00:00:00 2001
From: spikecodes <19519553+spikecodes@users.noreply.github.com>
Date: Sat, 16 Jan 2021 11:40:32 -0800
Subject: [PATCH] Separate datetime into relative and absolute
---
src/post.rs | 18 +++++++++---------
src/utils.rs | 22 ++++++++++++++--------
static/style.css | 8 ++++++--
templates/post.html | 4 ++--
templates/search.html | 4 ++--
templates/subreddit.html | 2 +-
templates/user.html | 4 ++--
7 files changed, 36 insertions(+), 26 deletions(-)
diff --git a/src/post.rs b/src/post.rs
index 5850fa8..7fd66ec 100644
--- a/src/post.rs
+++ b/src/post.rs
@@ -1,11 +1,10 @@
// CRATES
-use crate::utils::{cookie, error, format_num, format_url, media, param, parse_rich_flair, prefs, request, rewrite_url, val, Comment, Flags, Flair, Post, Preferences};
+use crate::utils::{Comment, Flags, Flair, Post, Preferences, cookie, error, format_num, format_url, media, param, parse_rich_flair, prefs, request, rewrite_url, time, val};
use actix_web::{HttpRequest, HttpResponse};
use async_recursion::async_recursion;
use askama::Template;
-use time::OffsetDateTime;
// STRUCTS
#[derive(Template)]
@@ -67,7 +66,7 @@ async fn parse_post(json: &serde_json::Value) -> Post {
let post: &serde_json::Value = &json["data"]["children"][0];
// Grab UTC time as unix timestamp
- let unix_time: i64 = post["data"]["created_utc"].as_f64().unwrap_or_default().round() as i64;
+ let (rel_time, created) = time(post["data"]["created_utc"].as_f64().unwrap_or_default());
// Parse post score and upvote ratio
let score = post["data"]["score"].as_i64().unwrap_or_default();
let ratio: f64 = post["data"]["upvote_ratio"].as_f64().unwrap_or(1.0) * 100.0;
@@ -115,7 +114,8 @@ async fn parse_post(json: &serde_json::Value) -> Post {
},
media,
domain: val(post, "domain"),
- time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"),
+ rel_time,
+ created
}
}
@@ -132,10 +132,9 @@ async fn parse_comments(json: &serde_json::Value) -> Vec {
// For each comment, retrieve the values to build a Comment object
for comment in comment_data {
- let unix_time: i64 = comment["data"]["created_utc"].as_f64().unwrap_or(0.0).round() as i64;
- if unix_time == 0 {
- continue;
- }
+ let unix_time = comment["data"]["created_utc"].as_f64().unwrap_or_default();
+ if unix_time == 0.0 { continue }
+ let (rel_time, created) = time(unix_time);
let score = comment["data"]["score"].as_i64().unwrap_or(0);
let body = rewrite_url(&val(&comment, "body_html"));
@@ -151,7 +150,8 @@ async fn parse_comments(json: &serde_json::Value) -> Vec {
body,
author: val(&comment, "author"),
score: format_num(score),
- time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"),
+ rel_time,
+ created,
replies,
flair: Flair {
flair_parts: parse_rich_flair(
diff --git a/src/utils.rs b/src/utils.rs
index 7154224..efffca5 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -49,7 +49,8 @@ pub struct Post {
pub thumbnail: String,
pub media: String,
pub domain: String,
- pub time: String,
+ pub rel_time: String,
+ pub created: String,
}
// Comment with content, post, score and data/time that it was posted
@@ -59,7 +60,8 @@ pub struct Comment {
pub author: String,
pub flair: Flair,
pub score: String,
- pub time: String,
+ pub rel_time: String,
+ pub created: String,
pub replies: Vec,
}
@@ -240,11 +242,12 @@ pub fn parse_rich_flair(flair_type: String, rich_flair: Option<&Vec>, tex
}
}
-pub fn time(unix_time: i64) -> String {
- let time = OffsetDateTime::from_unix_timestamp(unix_time);
+pub fn time(created: f64) -> (String, String) {
+ let time = OffsetDateTime::from_unix_timestamp(created.round() as i64);
let time_delta = OffsetDateTime::now_utc() - time;
+
// If the time difference is more than a month, show full date
- if time_delta > Duration::days(30) {
+ let rel_time = if time_delta > Duration::days(30) {
time.format("%b %d '%y")
// Otherwise, show relative date/time
} else if time_delta.whole_days() > 0 {
@@ -253,7 +256,9 @@ pub fn time(unix_time: i64) -> String {
format!("{}h ago", time_delta.whole_hours())
} else {
format!("{}m ago", time_delta.whole_minutes())
- }
+ };
+
+ (rel_time, time.format("%b %d %Y %H:%M UTC"))
}
//
@@ -290,7 +295,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec Result<(Vec 0 %}
{% call utils::render_flair(item.flair.flair_parts) %}
{% endif %}
- {{ item.time }}
+ {{ item.rel_time }}
{{ item.body }}
{%- endmacro %}
@@ -54,7 +54,7 @@
{% call utils::render_flair(post.author_flair.flair_parts) %}
{% endif %}
•
- {{ post.time }}
+ {{ post.rel_time }}
{{ post.title }}
diff --git a/templates/search.html b/templates/search.html
index 6c2b836..5355732 100644
--- a/templates/search.html
+++ b/templates/search.html
@@ -53,7 +53,7 @@
{% call utils::render_flair(post.author_flair.flair_parts) %}
{% endif %}
•
- {{ post.time }}
+ {{ post.rel_time }}
{% if post.flair.flair_parts.len() > 0 %}
@@ -89,7 +89,7 @@
diff --git a/templates/subreddit.html b/templates/subreddit.html
index 82c2d23..fbc1899 100644
--- a/templates/subreddit.html
+++ b/templates/subreddit.html
@@ -44,7 +44,7 @@
•
u/{{ post.author }}
•
- {{ post.time }}
+ {{ post.rel_time }}
{% if post.flair.flair_parts.len() > 0 %}
diff --git a/templates/user.html b/templates/user.html
index f12f8e4..a249df1 100644
--- a/templates/user.html
+++ b/templates/user.html
@@ -36,7 +36,7 @@
{% call utils::render_flair(post.author_flair.flair_parts) %}
{% endif %}
•
- {{ post.time }}
+ {{ post.rel_time }}
{% if post.flair.background_color == "Comment" %}
@@ -74,7 +74,7 @@