From ef3d94727fe74edde7ccb6f55eea1fbb668b4eb9 Mon Sep 17 00:00:00 2001 From: darienreese Date: Thu, 19 Mar 2026 20:28:02 -0400 Subject: [PATCH 1/4] add Temporal + RelativeTimeFormat support --- render.js | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/render.js b/render.js index b91bdd7..d31d303 100644 --- a/render.js +++ b/render.js @@ -28,6 +28,10 @@ class Markup_Render_Dom { constructor() { RELATIVE: (href, thing)=> href.replace(/^[/]{0,2}/, "https://"), ERROR: (href, thing)=> "about:blank#"+href, } + + const RTF = new Intl.RelativeTimeFormat("en", {style: "long"}) + + const TEMPORAL_SUPPORT = typeof Temporal !== 'undefined' function filter_url(url, thing) { try { @@ -51,27 +55,32 @@ class Markup_Render_Dom { constructor() { F: {dateStyle: 'full', timeStyle: 'short'}, R: "relative", } - function time_ago_2(date) { - if (!date) return "When?" - let t = date.getTime() - if (t<0 || isNaN(t)) return "Never?" - let seconds_away = (t - Date.now()) / 1000 - let seconds = Math.abs(seconds_away) + + function time_ago_2_base(seconds) { + const seconds_abs = Math.abs(seconds) let desc = [ - [31536000, 1, "year", "years"], - [2592000, 1, "month", "months"], - [86400, 1, "day", "days"], - [3600, 0, "hour", "hours"], - [60, 0, "min", "min"], - ].find(desc => seconds > desc[0]*0.96) + [31536000, 1, "year"], + [2592000, 1, "month"], + [86400, 1, "day"], + [3600, 0, "hour"], + [60, 0, "min"], + ].find(desc => seconds_abs > desc[0]*0.96) if (!desc) return "Just now" let round = (seconds/desc[0]).toFixed(desc[1]).replace(/[.]0/, "") - let units = +round==1 ? desc[2] : desc[3] - return `${round} ${units} ${seconds_away<=0 ? "ago" : "from now"}` - /*if (seconds <= -0.5) - return " IN THE FUTURE?" - return Math.round(seconds) + " seconds ago"*/ + return RTF.format(round, desc[2]) + } + + function time_ago_2(date) { + if (!date) return "When?" + let t = date.getTime() + if (t<0 || isNaN(t)) return "Never?" + return time_ago_2_base((t - Date.now()) / 1000) + } + + function time_ago_2_temporal(date) { + if (!date) return "When?" + return time_ago_2_base(date.since(Temporal.Now.instant()).total({ unit: 'second' })) } let preview @@ -370,11 +379,12 @@ we should create our own fake bullet elements instead.*/ timestamp: function({time, style}) { let e = this() - let date = new Date(time===null ? NaN : time) + const timeVal = time===null ? NaN : time + const date = TEMPORAL_SUPPORT ? Temporal.Instant.fromEpochMilliseconds(timeVal) : new Date(timeVal) let str let options = TIME_STYLES[style] || TIME_STYLES.f if (options==='relative') { - str = time_ago_2(date) + str = TEMPORAL_SUPPORT ? time_ago_2_temporal(date) : time_ago_2(date) } else { str = date.toLocaleString([], options) } From 6bcdaf619702661b6fe33a59ca48965039460ef3 Mon Sep 17 00:00:00 2001 From: darienreese Date: Thu, 19 Mar 2026 20:33:06 -0400 Subject: [PATCH 2/4] refactored "When?" message --- render.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/render.js b/render.js index d31d303..5565a5a 100644 --- a/render.js +++ b/render.js @@ -72,14 +72,12 @@ class Markup_Render_Dom { constructor() { } function time_ago_2(date) { - if (!date) return "When?" let t = date.getTime() if (t<0 || isNaN(t)) return "Never?" return time_ago_2_base((t - Date.now()) / 1000) } function time_ago_2_temporal(date) { - if (!date) return "When?" return time_ago_2_base(date.since(Temporal.Now.instant()).total({ unit: 'second' })) } @@ -384,7 +382,8 @@ we should create our own fake bullet elements instead.*/ let str let options = TIME_STYLES[style] || TIME_STYLES.f if (options==='relative') { - str = TEMPORAL_SUPPORT ? time_ago_2_temporal(date) : time_ago_2(date) + if (!date) str = "When?" + else str = TEMPORAL_SUPPORT ? time_ago_2_temporal(date) : time_ago_2(date) } else { str = date.toLocaleString([], options) } From 0e5a88c250219f38bc27fc48f66882c01f4db8ff Mon Sep 17 00:00:00 2001 From: darienreese Date: Thu, 19 Mar 2026 20:35:03 -0400 Subject: [PATCH 3/4] set time instead of creating timeVal --- render.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/render.js b/render.js index 5565a5a..fafdea0 100644 --- a/render.js +++ b/render.js @@ -377,8 +377,8 @@ we should create our own fake bullet elements instead.*/ timestamp: function({time, style}) { let e = this() - const timeVal = time===null ? NaN : time - const date = TEMPORAL_SUPPORT ? Temporal.Instant.fromEpochMilliseconds(timeVal) : new Date(timeVal) + time = time===null ? NaN : time + const date = TEMPORAL_SUPPORT ? Temporal.Instant.fromEpochMilliseconds(time) : new Date(time) let str let options = TIME_STYLES[style] || TIME_STYLES.f if (options==='relative') { From 466218b61a48e2fbb4a252384ad22d5e62f2e8af Mon Sep 17 00:00:00 2001 From: darienreese Date: Thu, 19 Mar 2026 20:40:19 -0400 Subject: [PATCH 4/4] remove time_ago_2 / temporal methods bc redundant --- render.js | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/render.js b/render.js index fafdea0..9fa9ebc 100644 --- a/render.js +++ b/render.js @@ -71,16 +71,6 @@ class Markup_Render_Dom { constructor() { return RTF.format(round, desc[2]) } - function time_ago_2(date) { - let t = date.getTime() - if (t<0 || isNaN(t)) return "Never?" - return time_ago_2_base((t - Date.now()) / 1000) - } - - function time_ago_2_temporal(date) { - return time_ago_2_base(date.since(Temporal.Now.instant()).total({ unit: 'second' })) - } - let preview let CREATE = { @@ -379,11 +369,18 @@ we should create our own fake bullet elements instead.*/ let e = this() time = time===null ? NaN : time const date = TEMPORAL_SUPPORT ? Temporal.Instant.fromEpochMilliseconds(time) : new Date(time) - let str + let str = "When?" let options = TIME_STYLES[style] || TIME_STYLES.f - if (options==='relative') { - if (!date) str = "When?" - else str = TEMPORAL_SUPPORT ? time_ago_2_temporal(date) : time_ago_2(date) + if (options==='relative' && date) { + if (TEMPORAL_SUPPORT) { + str = time_ago_2_base(date.since(Temporal.Now.instant()).total({ unit: 'second' })) + } else { + let t = date.getTime() + if (t<0 || isNaN(t)) return "Never?" + str = time_ago_2_base((t - Date.now()) / 1000) + } + } else if (options==='relative') { + /* leave str as default "When?" since date isn't set */ } else { str = date.toLocaleString([], options) }