From 662fe8de2f2915cde393743acaf193443018e7af Mon Sep 17 00:00:00 2001 From: Simon Lecoq <22963968+lowlighter@users.noreply.github.com> Date: Wed, 13 Oct 2021 22:08:31 -0400 Subject: [PATCH] feat(app/utils, plugins): normalize dates with improved format (#573) --- source/app/metrics/utils.mjs | 8 ++++++++ source/app/web/statics/app.placeholder.js | 8 ++++++++ source/plugins/achievements/index.mjs | 2 +- source/plugins/core/index.mjs | 2 +- source/plugins/music/index.mjs | 2 +- source/plugins/stackoverflow/index.mjs | 4 ++-- source/plugins/tweets/index.mjs | 2 +- source/templates/classic/partials/activity.ejs | 2 +- source/templates/classic/partials/posts.ejs | 2 +- source/templates/classic/partials/rss.ejs | 2 +- source/templates/markdown/example.md | 2 +- source/templates/markdown/partials/activity.ejs | 2 +- source/templates/markdown/partials/posts.ejs | 4 ++-- source/templates/markdown/partials/rss.ejs | 2 +- source/templates/repository/partials/activity.ejs | 2 +- source/templates/repository/partials/rss.ejs | 2 +- 16 files changed, 32 insertions(+), 16 deletions(-) diff --git a/source/app/metrics/utils.mjs b/source/app/metrics/utils.mjs index 363c242a..c5c7f873 100644 --- a/source/app/metrics/utils.mjs +++ b/source/app/metrics/utils.mjs @@ -101,6 +101,14 @@ export function formatters({timeZone} = {}) { /**Date formatter */ format.date = function(string, options) { + if (options.date) { + delete options.date + Object.assign(options, {day:"numeric", month:"short", year:"numeric"}) + } + if (options.time) { + delete options.time + Object.assign(options, {hour:"2-digit", minute:"2-digit", second:"2-digit"}) + } return new Intl.DateTimeFormat("en-GB", {timeZone, ...options}).format(new Date(string)) } diff --git a/source/app/web/statics/app.placeholder.js b/source/app/web/statics/app.placeholder.js index 2afa7419..8d3798dd 100644 --- a/source/app/web/statics/app.placeholder.js +++ b/source/app/web/statics/app.placeholder.js @@ -1131,6 +1131,14 @@ return `${text.substring(0, length)}…` } data.f.date = function(string, options) { + if (options.date) { + delete options.date + Object.assign(options, {day:"numeric", month:"short", year:"numeric"}) + } + if (options.time) { + delete options.time + Object.assign(options, {hour:"2-digit", minute:"2-digit", second:"2-digit"}) + } return new Intl.DateTimeFormat("en-GB", options).format(new Date(string)) } data.f.license = function(text) { diff --git a/source/plugins/achievements/index.mjs b/source/plugins/achievements/index.mjs index 1f36c40c..35a35f32 100644 --- a/source/plugins/achievements/index.mjs +++ b/source/plugins/achievements/index.mjs @@ -28,7 +28,7 @@ export default async function({login, q, imports, data, computed, graphql, queri .map(({title, unlock, ...achievement}) => ({ prefix:({S:"Master", A:"Super", B:"Great"}[achievement.rank] ?? ""), title, - unlock:!/invalid date/i.test(unlock) ? `${imports.format.date(unlock, {timeStyle:"short"})} on ${imports.format.date(unlock, {dateStyle:"short"})}` : null, + unlock:!/invalid date/i.test(unlock) ? `${imports.format.date(unlock, {time:true})} on ${imports.format.date(unlock, {date:true})}` : null, ...achievement, })) .map(({icon, ...achievement}) => ({icon:icon.replace(/#primary/g, colors[achievement.rank][0]).replace(/#secondary/g, colors[achievement.rank][1]), ...achievement})) diff --git a/source/plugins/core/index.mjs b/source/plugins/core/index.mjs index bc825030..7985d545 100644 --- a/source/plugins/core/index.mjs +++ b/source/plugins/core/index.mjs @@ -120,7 +120,7 @@ export default async function({login, q}, {conf, data, rest, graphql, plugins, q data.meta = { version:conf.package.version, author:conf.package.author, - generated:imports.format.date(new Date(), {dateStyle:"short", timeStyle:"short"}) + generated:imports.format.date(new Date(), {date:true, time:true}) } //Debug flags diff --git a/source/plugins/music/index.mjs b/source/plugins/music/index.mjs index a8521e34..cc051ed4 100644 --- a/source/plugins/music/index.mjs +++ b/source/plugins/music/index.mjs @@ -164,7 +164,7 @@ export default async function({login, imports, data, q, account}, {enabled = fal name:track.name, artist:track.artists[0].name, artwork:track.album.images[0].url, - played_at:played_at ? `${imports.format.date(played_at, {timeStyle:"short"})} on ${imports.format.date(played_at, {dateStyle:"short"})}` : null, + played_at:played_at ? `${imports.format.date(played_at, {time:true})} on ${imports.format.date(played_at, {date:true})}` : null, })) //Ensure no duplicate are added for (const track of loaded) { diff --git a/source/plugins/stackoverflow/index.mjs b/source/plugins/stackoverflow/index.mjs index c16723bc..ef3a8e3d 100644 --- a/source/plugins/stackoverflow/index.mjs +++ b/source/plugins/stackoverflow/index.mjs @@ -89,7 +89,7 @@ const format = { accepted, comments, author, - created:imports.format.date(creation_date * 1000, {dateStyle:"short"}), + created:imports.format.date(creation_date * 1000, {date:true}), link, id, question_id, @@ -136,7 +136,7 @@ const format = { comments, views, author, - created:imports.format.date(creation_date * 1000, {dateStyle:"short"}), + created:imports.format.date(creation_date * 1000, {date:true}), link, id, accepted_answer_id, diff --git a/source/plugins/tweets/index.mjs b/source/plugins/tweets/index.mjs index fdc74227..bfdbb142 100644 --- a/source/plugins/tweets/index.mjs +++ b/source/plugins/tweets/index.mjs @@ -69,7 +69,7 @@ export default async function({login, imports, data, q, account}, {enabled = fal //Format text console.debug(`metrics/compute/${login}/plugins > tweets > formatting tweet ${tweet.id}`) - tweet.createdAt = `${imports.format.date(tweet.created_at, {timeStyle:"short"})} on ${imports.format.date(tweet.created_at, {dateStyle:"short"})}` + tweet.createdAt = `${imports.format.date(tweet.created_at, {time:true})} on ${imports.format.date(tweet.created_at, {date:true})}` tweet.text = imports.htmlescape( //Escape tags imports.htmlescape(tweet.text, {"<":true, ">":true}) diff --git a/source/templates/classic/partials/activity.ejs b/source/templates/classic/partials/activity.ejs index 2ba4e083..5023450a 100644 --- a/source/templates/classic/partials/activity.ejs +++ b/source/templates/classic/partials/activity.ejs @@ -169,7 +169,7 @@ <% } %> <% if (plugins.activity.timestamps) { %>
<% } %> diff --git a/source/templates/classic/partials/posts.ejs b/source/templates/classic/partials/posts.ejs index b2965be8..f8e14237 100644 --- a/source/templates/classic/partials/posts.ejs +++ b/source/templates/classic/partials/posts.ejs @@ -23,7 +23,7 @@