From 0fcd327795aed0195b1061dc4c50ee3c18b6cfd6 Mon Sep 17 00:00:00 2001 From: Pybsama <294532706+Pybsama@users.noreply.github.com> Date: Wed, 29 Jul 2026 13:02:08 +0800 Subject: [PATCH 1/2] fix: honor year precision for elapsed durations --- src/duration.ts | 3 ++- test/duration.ts | 12 ++++++++++++ test/relative-time.js | 12 ++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/duration.ts b/src/duration.ts index eb41a53c..2329918c 100644 --- a/src/duration.ts +++ b/src/duration.ts @@ -156,7 +156,8 @@ export function elapsedTime(date: Date, precision: Unit = 'second', now = Date.n const day = Math.floor(hr / 24) const month = Math.floor(day / 30) const year = Math.floor(month / 12) - const i = unitNames.indexOf(precision) || unitNames.length + const precisionIndex = unitNames.indexOf(precision) + const i = precisionIndex === -1 ? unitNames.length : precisionIndex return new Duration( i >= 0 ? year * sign : 0, i >= 1 ? (month - year * 12) * sign : 0, diff --git a/test/duration.ts b/test/duration.ts index 95968531..8fab295f 100644 --- a/test/duration.ts +++ b/test/duration.ts @@ -224,6 +224,18 @@ suite('duration', function () { input: '2023-03-21T16:03:00.000Z', expected: '-P1DT20H', }, + { + now: '2022-10-24T14:46:00.000Z', + input: '2024-10-24T14:46:00.000Z', + precision: 'year', + expected: 'P2Y', + }, + { + now: '2022-10-24T14:46:00.000Z', + input: '2020-10-24T14:46:00.000Z', + precision: 'year', + expected: '-P2Y', + }, ]) for (const {input, now, precision = 'millisecond', expected} of elapsed) { test(`${input} is ${expected} elapsed from ${now} (precision ${precision})`, () => { diff --git a/test/relative-time.js b/test/relative-time.js index 307ba594..59b658b4 100644 --- a/test/relative-time.js +++ b/test/relative-time.js @@ -1561,6 +1561,12 @@ suite('relative-time', function () { format: 'duration', expected: '2 years, 11 days', }, + { + datetime: '2024-10-24T14:46:00.000Z', + format: 'duration', + precision: 'year', + expected: '2 years', + }, { datetime: '2024-10-24T14:46:00.000Z', format: 'duration', @@ -2680,6 +2686,12 @@ suite('relative-time', function () { format: 'elapsed', expected: '1y', }, + { + datetime: '2020-10-24T14:46:00.000Z', + format: 'elapsed', + precision: 'year', + expected: '2y', + }, // Dates in the past { From 0a350cb171a688932186d84be3a1741666899ad2 Mon Sep 17 00:00:00 2001 From: Pybsama <294532706+Pybsama@users.noreply.github.com> Date: Wed, 29 Jul 2026 13:07:47 +0800 Subject: [PATCH 2/2] fix: preserve unknown precision behavior --- src/duration.ts | 3 +-- test/duration.ts | 7 +++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/duration.ts b/src/duration.ts index 2329918c..0195fc20 100644 --- a/src/duration.ts +++ b/src/duration.ts @@ -156,8 +156,7 @@ export function elapsedTime(date: Date, precision: Unit = 'second', now = Date.n const day = Math.floor(hr / 24) const month = Math.floor(day / 30) const year = Math.floor(month / 12) - const precisionIndex = unitNames.indexOf(precision) - const i = precisionIndex === -1 ? unitNames.length : precisionIndex + const i = unitNames.indexOf(precision) return new Duration( i >= 0 ? year * sign : 0, i >= 1 ? (month - year * 12) * sign : 0, diff --git a/test/duration.ts b/test/duration.ts index 8fab295f..87707de1 100644 --- a/test/duration.ts +++ b/test/duration.ts @@ -1,5 +1,6 @@ import {assert} from '@open-wc/testing' import {applyDuration, Duration, elapsedTime, getRelativeTimeUnit, roundToSingleUnit} from '../src/duration.ts' +import type {Unit} from '../src/duration.ts' import type {DurationFormatOptions} from '../src/duration-format-ponyfill.ts' import {Temporal} from '@js-temporal/polyfill' @@ -236,6 +237,12 @@ suite('duration', function () { precision: 'year', expected: '-P2Y', }, + { + now: '2022-10-24T14:46:00.000Z', + input: '2024-10-24T14:46:00.000Z', + precision: 'unknown' as Unit, + expected: 'PT0S', + }, ]) for (const {input, now, precision = 'millisecond', expected} of elapsed) { test(`${input} is ${expected} elapsed from ${now} (precision ${precision})`, () => {