Fix: ensure correct relative dates formatter uses calendar days (#7174)
Docker CI / Docker Build & Push (push) Has been cancelled
Lint / Linting Checks (push) Has been cancelled
Release Drafter / Update Release Draft (push) Has been cancelled
Release Drafter / Auto Label PR (push) Has been cancelled
Tests / vitest (1) (push) Has been cancelled
Tests / vitest (2) (push) Has been cancelled
Tests / vitest (3) (push) Has been cancelled
Tests / vitest (4) (push) Has been cancelled

This commit is contained in:
shamoon
2026-09-23 11:41:55 -07:00
committed by GitHub
parent d1c1ee7a1e
commit a7a4378a40
2 changed files with 52 additions and 0 deletions
+6
View File
@@ -106,6 +106,12 @@ function relativeDate(date, formatter) {
const unitIndex = cutoffs.findIndex((cutoff) => cutoff > Math.abs(delta));
const divisor = unitIndex ? cutoffs[unitIndex - 1] : 1;
if (units[unitIndex] === "day") {
// compare calendar days, not elapsed 24h blocks
const startOfDay = (d) => new Date(d.getFullYear(), d.getMonth(), d.getDate());
return formatter.format(Math.round((startOfDay(date) - startOfDay(new Date())) / 86400000), "day");
}
return formatter.format(Math.floor(delta / divisor), units[unitIndex]);
}
+46
View File
@@ -0,0 +1,46 @@
import { createRequire } from "node:module";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const require = createRequire(import.meta.url);
const i18nextConfig = require("../../next-i18next.config.js");
function getRelativeDateFormatter() {
let relativeDateFormatter;
i18nextConfig.use[0].init({
services: {
formatter: {
add(name, formatter) {
if (name === "relativeDate") relativeDateFormatter = formatter;
},
},
},
});
return relativeDateFormatter;
}
describe("relativeDate formatter", () => {
const formatRelativeDate = getRelativeDateFormatter();
const options = { numeric: "auto" };
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 8, 23, 16, 0));
});
afterEach(() => {
vi.useRealTimers();
});
it.each([
["hours away on the same day", new Date(2026, 8, 23, 20, 0), "in 4 hours"],
["next calendar day", new Date(2026, 8, 24, 20, 0), "tomorrow"],
["two calendar days away but under 48h", new Date(2026, 8, 25, 10, 40), "in 2 days"],
["previous calendar day", new Date(2026, 8, 22, 10, 0), "yesterday"],
["two calendar days ago but under 48h", new Date(2026, 8, 21, 20, 0), "2 days ago"],
])("formats %s", (_description, value, expected) => {
expect(formatRelativeDate(value.toISOString(), "en", options)).toBe(expected);
});
});