From d267956cf786c01ca799b6318334cfef78c565b0 Mon Sep 17 00:00:00 2001 From: Ferit Date: Sun, 18 Oct 2026 22:50:44 +0300 Subject: [PATCH] fix: schedule automatic updates for the Unix epoch --- src/relative-time-element.ts | 2 +- test/relative-time.js | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/relative-time-element.ts b/src/relative-time-element.ts index 2daf2d1..65cb6e0 100644 --- a/src/relative-time-element.ts +++ b/src/relative-time-element.ts @@ -107,7 +107,7 @@ const dateObserver = new (class { this.elements.add(element) if (this.updating) return const date = element.date - if (date && date.getTime()) { + if (date) { const ms = getUnitFactor(element) const time = Date.now() + ms if (time < this.time || this.time <= Date.now()) { diff --git a/test/relative-time.js b/test/relative-time.js index 563a134..8b24e74 100644 --- a/test/relative-time.js +++ b/test/relative-time.js @@ -40,6 +40,30 @@ suite('relative-time', function () { } }) + test('keeps duration updates scheduled for the Unix epoch', async () => { + freezeTime(new Date(5000)) + const originalSetTimeout = window.setTimeout + const time = document.createElement('relative-time') + let tick + globalThis.setTimeout = window.setTimeout = function (callback) { + tick = callback + return 0 + } + try { + time.setAttribute('format', 'duration') + time.setAttribute('datetime', '1970-01-01T00:00:00.000Z') + await Promise.resolve() + assert.equal(time.shadowRoot.textContent, '5 seconds') + assert.isFunction(tick) + Date.now = () => 6000 + tick() + assert.equal(time.shadowRoot.textContent, '6 seconds') + } finally { + globalThis.setTimeout = window.setTimeout = originalSetTimeout + time.disconnectedCallback() + } + }) + test('reschedules future micro datetime updates at the explicit threshold boundary', async () => { const originalSetTimeout = window.setTimeout const delays = []