mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 23:12:04 +00:00
fix(dates): show the year whenever a date is not the current one
Sweeps every surface that renders a date from before this year without a year suffix — illness episodes/flares/day-logs, lab and custom-metric charts, the mood chart family, medication titration/side-effects/dose- history/next-dose captions (incl. the Dec→Jan rollover), cycle predictions and history, the BBT chart, the intraday-pulse day-nav, the date-range filter chip, and the nutrient/compliance charts. Each now routes through dateShortSmart / dateWithWeekdaySmart (format-locale.ts) or the matching date-format.ts helper, which add the year only when it differs from the current one — mirroring the workout list's existing model instead of a bare "19.02." that reads as this year's. Also fixes two sites that bypassed the app's locale entirely: the trusted-devices expiry (bare toLocaleDateString, host locale) and the admin token-name timestamp (hard-coded de-DE + Europe/Berlin) now both render through the user's own locale, hour-cycle, and timezone preference.
This commit is contained in:
1 parent
7f44e2fa37
commit
1ff179178f
28 files changed
+158
-104
No files matched your search
@@ -5,11 +5,12 @@
|
||||
* field order the `<DateField>` primitive and `useFormatters().date()` render.
|
||||
* AUTO defers to the active locale; DMY / MDY / YMD pin the order regardless.
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
formatDate,
|
||||
formatDateWithWeekday,
|
||||
formatDateWithWeekdaySmart,
|
||||
parseIsoDate,
|
||||
resolveDateLocale,
|
||||
isDateFormatPreference,
|
||||
@@ -112,6 +113,41 @@ describe("formatDateWithWeekday (#490)", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// Issue #66 (date-format sweep) — the dose-history ledger scrolls day
|
||||
// groups across year boundaries; a bare "Di., 16.12." from last December
|
||||
// must not read as this year's. "Now" is faked so the boundary is
|
||||
// deterministic regardless of the day this suite runs.
|
||||
describe("formatDateWithWeekdaySmart (#66)", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date("2026-07-10T12:00:00Z"));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("omits the year for a day key in the current UTC year", () => {
|
||||
expect(formatDateWithWeekdaySmart("2026-07-14", "AUTO", "en")).toBe(
|
||||
"Tue, 07/14",
|
||||
);
|
||||
});
|
||||
|
||||
it("adds the year for a day key from a prior UTC year", () => {
|
||||
expect(formatDateWithWeekdaySmart("2025-12-16", "AUTO", "en")).toBe(
|
||||
"Tue, 12/16/2025",
|
||||
);
|
||||
expect(formatDateWithWeekdaySmart("2025-12-16", "AUTO", "de")).toBe(
|
||||
"Di., 16.12.2025",
|
||||
);
|
||||
});
|
||||
|
||||
it("returns an empty string for empty / unparseable input", () => {
|
||||
expect(formatDateWithWeekdaySmart("", "AUTO", "en")).toBe("");
|
||||
expect(formatDateWithWeekdaySmart(null, "AUTO", "en")).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseIsoDate", () => {
|
||||
it("parses a valid ISO calendar date to a UTC midnight Date", () => {
|
||||
const d = parseIsoDate("2026-02-19");
|
||||
|
||||
@@ -265,17 +265,6 @@ describe("makeFormatters", () => {
|
||||
// that prints as 2025 in New York must carry the year.
|
||||
expect(nyFmt.dateShortSmart(newYearUtcEve)).toContain("2025");
|
||||
});
|
||||
|
||||
it("monthYear always carries the year (chart axis / next-dose labels)", () => {
|
||||
const de = makeFormatters("de");
|
||||
const en = makeFormatters("en");
|
||||
const dec = new Date("2025-12-30T10:00:00Z");
|
||||
const jan = new Date("2026-01-15T10:00:00Z");
|
||||
expect(de.monthYear(dec)).toContain("2025");
|
||||
expect(de.monthYear(jan)).toContain("2026");
|
||||
expect(en.monthYear(dec)).toBe("Dec 2025");
|
||||
expect(en.monthYear(jan)).toBe("Jan 2026");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -167,6 +167,31 @@ export function formatDateWithWeekday(
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Same field set as {@link formatDateWithWeekday}, but adds the year
|
||||
* whenever `value`'s UTC calendar year differs from the current UTC year
|
||||
* — issue #66 (date-format sweep). A day group from a previous year (e.g.
|
||||
* the dose-history ledger scrolled past a year boundary) must not read as
|
||||
* this year's "Di., 16.12.".
|
||||
*/
|
||||
export function formatDateWithWeekdaySmart(
|
||||
value: Date | string | null | undefined,
|
||||
pref: DateFormatPreference,
|
||||
locale: Locale,
|
||||
): string {
|
||||
if (value === null || value === undefined || value === "") return "";
|
||||
const date = value instanceof Date ? value : parseIsoDate(value);
|
||||
if (date === null || Number.isNaN(date.getTime())) return "";
|
||||
const showYear = date.getUTCFullYear() !== new Date().getUTCFullYear();
|
||||
return date.toLocaleDateString(resolveDateLocale(pref, locale), {
|
||||
timeZone: "UTC",
|
||||
weekday: "short",
|
||||
day: "2-digit",
|
||||
month: "2-digit",
|
||||
...(showYear ? { year: "numeric" as const } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a `yyyy-MM-dd` string to a UTC `Date` at midnight. Returns null on
|
||||
* a value that is not exactly the ISO calendar-date shape so callers can
|
||||
|
||||
@@ -171,11 +171,6 @@ export interface Formatters {
|
||||
dateWithWeekdaySmart: (value: DateInput) => string;
|
||||
/** For axis labels: 3-letter month abbreviation. */
|
||||
monthShort: (value: DateInput) => string;
|
||||
/**
|
||||
* Month + year, e.g. "Feb. 2026" / "Feb 2026" — for axis labels and
|
||||
* next-dose captions that need to disambiguate a Dec → Jan rollover.
|
||||
*/
|
||||
monthYear: (value: DateInput) => string;
|
||||
}
|
||||
|
||||
export function makeFormatters(
|
||||
@@ -299,13 +294,6 @@ export function makeFormatters(
|
||||
timeZone: tz,
|
||||
month: "short",
|
||||
}).format(asDate(value)),
|
||||
|
||||
monthYear: (value) =>
|
||||
getDateTimeFormat(intlLocale, {
|
||||
timeZone: tz,
|
||||
month: "short",
|
||||
year: "numeric",
|
||||
}).format(asDate(value)),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user