From c44afa17c940c4c75611bdb50f61eb54edce9dcb Mon Sep 17 00:00:00 2001 From: Zoey Date: Fri, 16 Dec 2022 04:23:01 -0800 Subject: [PATCH] Make image timestamps even more readable --- site/templates/image.hbs | 2 +- src/images.rs | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/site/templates/image.hbs b/site/templates/image.hbs index 13567e1..22476d3 100644 --- a/site/templates/image.hbs +++ b/site/templates/image.hbs @@ -1,6 +1,6 @@

{{title}}

- {{timestamp}} + Published {{timestamp}} {{alt}} {{#if desc}}

{{desc}}

diff --git a/src/images.rs b/src/images.rs index 7c7751c..288d59b 100644 --- a/src/images.rs +++ b/src/images.rs @@ -6,7 +6,7 @@ use std::{ use anyhow::Context; use itertools::Itertools; use rss::{validation::Validate, ChannelBuilder, ItemBuilder}; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize, Serializer}; use time::{format_description::well_known::Rfc2822, OffsetDateTime}; use url::Url; @@ -275,10 +275,25 @@ struct ImageTemplateData<'i> { /// The image's ID. id: &'i str, /// The image's timestamp. (Duplicated to change the serialization method.) - #[serde(serialize_with = "time::serde::rfc2822::serialize")] + #[serde(serialize_with = "ImageTemplateData::timestamp_formatter")] timestamp: OffsetDateTime, } +impl<'i> ImageTemplateData<'i> { + fn timestamp_formatter(timestamp: &OffsetDateTime, serializer: S) -> Result + where + S: Serializer, + { + let out = timestamp + .format( + &time::format_description::parse("[weekday], [month repr:long] [day], [year]") + .expect("Should never fail"), + ) + .expect("Should never fail"); + serializer.serialize_str(&out) + } +} + /// Template data for image lists. #[derive(Debug, Serialize)] struct ImageListTemplateData<'i> {