Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/utilities/src/dateTimeFormat/relative-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright IBM Corp. 2024
* Copyright IBM Corp. 2024, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -47,6 +47,18 @@ describe(locale, () => {
expect(actualOutput).toBe(rtf.format(1, 'weeks'));
});

test(`-364 days → ${rtf.format(-12, 'months')}`, () => {
const date = Date.now() - 1000 * 60 * 60 * 24 * 364;
const actualOutput = relative.format(date, { locale, style });
expect(actualOutput).toBe(rtf.format(-12, 'months'));
});

test(`+364 days → ${rtf.format(12, 'months')}`, () => {
const date = Date.now() + 1000 * 60 * 60 * 24 * 364;
const actualOutput = relative.format(date, { locale, style });
expect(actualOutput).toBe(rtf.format(12, 'months'));
});

tests.forEach(([unit, unitsPassed, secondsInUnit]) => {
const datePast = Date.now() - 1000 * unitsPassed * secondsInUnit;
const expectedPast = rtf.format(unitsPassed * -1, unit);
Expand Down
6 changes: 4 additions & 2 deletions packages/utilities/src/dateTimeFormat/relative.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright IBM Corp. 2024
* Copyright IBM Corp. 2024, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -24,7 +24,9 @@ export function format(
const hours = Math.trunc(minutes / 60);
const days = Math.trunc(hours / 24);
const weeks = Math.trunc(days / 7);
const months = Math.trunc(weeks / 4);
// A month is counted as 4 weeks, which reaches 13 just before a full year,
// so cap it at 12
const months = Math.max(-12, Math.min(12, Math.trunc(weeks / 4)));
const years = Math.trunc(days / 365);

if (Math.abs(seconds) < 60) {
Expand Down
Loading