Separate datetime into relative and absolute

This commit is contained in:
spikecodes 2021-01-16 11:40:32 -08:00
parent ab102ca32c
commit fdf60e7255
7 changed files with 36 additions and 26 deletions

View File

@ -1,11 +1,10 @@
// CRATES // 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 actix_web::{HttpRequest, HttpResponse};
use async_recursion::async_recursion; use async_recursion::async_recursion;
use askama::Template; use askama::Template;
use time::OffsetDateTime;
// STRUCTS // STRUCTS
#[derive(Template)] #[derive(Template)]
@ -67,7 +66,7 @@ async fn parse_post(json: &serde_json::Value) -> Post {
let post: &serde_json::Value = &json["data"]["children"][0]; let post: &serde_json::Value = &json["data"]["children"][0];
// Grab UTC time as unix timestamp // 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 // Parse post score and upvote ratio
let score = post["data"]["score"].as_i64().unwrap_or_default(); 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; 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, media,
domain: val(post, "domain"), 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<Comment> {
// For each comment, retrieve the values to build a Comment object // For each comment, retrieve the values to build a Comment object
for comment in comment_data { for comment in comment_data {
let unix_time: i64 = comment["data"]["created_utc"].as_f64().unwrap_or(0.0).round() as i64; let unix_time = comment["data"]["created_utc"].as_f64().unwrap_or_default();
if unix_time == 0 { if unix_time == 0.0 { continue }
continue; let (rel_time, created) = time(unix_time);
}
let score = comment["data"]["score"].as_i64().unwrap_or(0); let score = comment["data"]["score"].as_i64().unwrap_or(0);
let body = rewrite_url(&val(&comment, "body_html")); let body = rewrite_url(&val(&comment, "body_html"));
@ -151,7 +150,8 @@ async fn parse_comments(json: &serde_json::Value) -> Vec<Comment> {
body, body,
author: val(&comment, "author"), author: val(&comment, "author"),
score: format_num(score), score: format_num(score),
time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"), rel_time,
created,
replies, replies,
flair: Flair { flair: Flair {
flair_parts: parse_rich_flair( flair_parts: parse_rich_flair(

View File

@ -49,7 +49,8 @@ pub struct Post {
pub thumbnail: String, pub thumbnail: String,
pub media: String, pub media: String,
pub domain: 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 // Comment with content, post, score and data/time that it was posted
@ -59,7 +60,8 @@ pub struct Comment {
pub author: String, pub author: String,
pub flair: Flair, pub flair: Flair,
pub score: String, pub score: String,
pub time: String, pub rel_time: String,
pub created: String,
pub replies: Vec<Comment>, pub replies: Vec<Comment>,
} }
@ -240,11 +242,12 @@ pub fn parse_rich_flair(flair_type: String, rich_flair: Option<&Vec<Value>>, tex
} }
} }
pub fn time(unix_time: i64) -> String { pub fn time(created: f64) -> (String, String) {
let time = OffsetDateTime::from_unix_timestamp(unix_time); let time = OffsetDateTime::from_unix_timestamp(created.round() as i64);
let time_delta = OffsetDateTime::now_utc() - time; let time_delta = OffsetDateTime::now_utc() - time;
// If the time difference is more than a month, show full date // 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") time.format("%b %d '%y")
// Otherwise, show relative date/time // Otherwise, show relative date/time
} else if time_delta.whole_days() > 0 { } 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()) format!("{}h ago", time_delta.whole_hours())
} else { } else {
format!("{}m ago", time_delta.whole_minutes()) 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<Post
// For each post from posts list // For each post from posts list
for post in post_list { for post in post_list {
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());
let score = post["data"]["score"].as_i64().unwrap_or_default(); 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; let ratio: f64 = post["data"]["upvote_ratio"].as_f64().unwrap_or(1.0) * 100.0;
let title = val(post, "title"); let title = val(post, "title");
@ -337,7 +342,8 @@ 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: time(unix_time), rel_time,
created
}); });
} }

View File

@ -676,7 +676,7 @@ a.search_subreddit:hover {
padding: 5px; padding: 5px;
} }
.datetime { .created {
opacity: 0.5; opacity: 0.5;
} }
@ -901,7 +901,11 @@ td, th {
padding: 5px 0; padding: 5px 0;
} }
.datetime { /* .comment_data::marker {
font-size: 18px;
} */
.created {
width: 100%; width: 100%;
} }
} }

View File

@ -25,7 +25,7 @@
{% if item.flair.flair_parts.len() > 0 %} {% if item.flair.flair_parts.len() > 0 %}
<small class="author_flair">{% call utils::render_flair(item.flair.flair_parts) %}</small> <small class="author_flair">{% call utils::render_flair(item.flair.flair_parts) %}</small>
{% endif %} {% endif %}
<span class="datetime">{{ item.time }}</span> <span class="created" title="{{ post.created }}">{{ item.rel_time }}</span>
</summary> </summary>
<div class="comment_body">{{ item.body }}</div> <div class="comment_body">{{ item.body }}</div>
{%- endmacro %} {%- endmacro %}
@ -54,7 +54,7 @@
<small class="author_flair">{% call utils::render_flair(post.author_flair.flair_parts) %}</small> <small class="author_flair">{% call utils::render_flair(post.author_flair.flair_parts) %}</small>
{% endif %} {% endif %}
<span class="dot">&bull;</span> <span class="dot">&bull;</span>
<span class="datetime">{{ post.time }}</span> <span class="created" title="{{ post.created }}">{{ post.rel_time }}</span>
</p> </p>
<a href="{{ post.permalink }}" class="post_title"> <a href="{{ post.permalink }}" class="post_title">
{{ post.title }} {{ post.title }}

View File

@ -53,7 +53,7 @@
<small class="author_flair">{% call utils::render_flair(post.author_flair.flair_parts) %}</small> <small class="author_flair">{% call utils::render_flair(post.author_flair.flair_parts) %}</small>
{% endif %} {% endif %}
<span class="dot">&bull;</span> <span class="dot">&bull;</span>
<span class="datetime">{{ post.time }}</span> <span class="created" title="{{ post.created }}">{{ post.rel_time }}</span>
</p> </p>
<p class="post_title"> <p class="post_title">
{% if post.flair.flair_parts.len() > 0 %} {% if post.flair.flair_parts.len() > 0 %}
@ -89,7 +89,7 @@
<details class="comment_right" open> <details class="comment_right" open>
<summary class="comment_data"> <summary class="comment_data">
<a class="comment_link" href="{{ post.permalink }}">COMMENT</a> <a class="comment_link" href="{{ post.permalink }}">COMMENT</a>
<span class="datetime">{{ post.time }}</span> <span class="created" title="{{ post.created }}">{{ post.rel_time }}</span>
</summary> </summary>
<p class="comment_body">{{ post.body }}</p> <p class="comment_body">{{ post.body }}</p>
</details> </details>

View File

@ -44,7 +44,7 @@
<span class="dot">&bull;</span> <span class="dot">&bull;</span>
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a> <a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
<span class="dot">&bull;</span> <span class="dot">&bull;</span>
<span class="datetime">{{ post.time }}</span> <span class="created" title="{{ post.created }}">{{ post.rel_time }}</span>
</p> </p>
<p class="post_title"> <p class="post_title">
{% if post.flair.flair_parts.len() > 0 %} {% if post.flair.flair_parts.len() > 0 %}

View File

@ -36,7 +36,7 @@
<small class="author_flair">{% call utils::render_flair(post.author_flair.flair_parts) %}</small> <small class="author_flair">{% call utils::render_flair(post.author_flair.flair_parts) %}</small>
{% endif %} {% endif %}
<span class="dot">&bull;</span> <span class="dot">&bull;</span>
<span class="datetime">{{ post.time }}</span> <span class="created" title="{{ post.created }}">{{ post.rel_time }}</span>
</p> </p>
<p class="post_title"> <p class="post_title">
{% if post.flair.background_color == "Comment" %} {% if post.flair.background_color == "Comment" %}
@ -74,7 +74,7 @@
<details class="comment_right" open> <details class="comment_right" open>
<summary class="comment_data"> <summary class="comment_data">
<a class="comment_link" href="{{ post.permalink }}">COMMENT</a> <a class="comment_link" href="{{ post.permalink }}">COMMENT</a>
<span class="datetime">{{ post.time }}</span> <span class="created" title="{{ post.created }}">{{ post.rel_time }}</span>
</summary> </summary>
<p class="comment_body">{{ post.body }}</p> <p class="comment_body">{{ post.body }}</p>
</details> </details>