Files
HealthLog/src/lib/insights/measurement-freshness.ts
T
Marc-André Bombeck 7c5ff89def feat(lint): refuse UTC day keys outside the time zone module
`instant.toISOString().slice(0, 10)` and parsing a key back as UTC
midnight are the idioms behind every day-boundary fix this year: the
evening west of UTC and the small hours east of it land on the
neighbouring day. A new rule, healthlog/no-utc-day-key, refuses them in
src/ outside src/lib/tz and tests, and refuses a disable directive for
itself that is not next-line or gives no reason.

The time zone module now names every legitimate form: userDayKey for a
person's day, dateOnlyKey / dateOnlyAtNoonUtc / dayKeyAsUtcMidnight for
date-only values and day labels, daysBetweenDateKeys / weekdayOfDateKey /
isCalendarDateKey / statedDateKey for key arithmetic and parsing.

Of the 133 sites the rule found, 84 are gone. Most moved onto a named
helper with no change in behaviour; these changed behaviour:

- cycle calendar, insights, Coach snapshot and prediction refresh start
  their lookbacks at the first instant of the user's day
- a model-transcribed date like "June 10, 2026" no longer lands a day
  early in a process east of UTC (documents and lab scans)
- a custom report range covers exactly the picked days in the profile
  zone instead of starting the evening before west of UTC
- date-of-birth pickers cap at the user's today, not the UTC one
- an allergy onset date is stored at noon UTC like other date-only values
- the report's cycle summary, the Coach's usual BP range and the intake
  projector's rollup recompute use the user's day
- vaccination dates render as dates instead of the previous evening

49 remain, each with its reason on the line above: 39 UTC by design (15
file name stamps, rollup and object keys, the UTC-day score engines) and
10 marked baseline. A guard test requires a classified reason on every
directive and holds the baseline to a ceiling that only goes down.
2026-10-01 09:59:57 +02:00

57 lines
2.4 KiB
TypeScript

import { daysBetweenDateKeys } from "@/lib/tz/format";
/**
* How old a reading may be before "today" stops being true about it.
*
* Several surfaces make present-tense statements out of the freshest stored
* reading: the day-signals card ("one of your vitals is outside its usual
* range today"), the baseline-drift card ("your pulse is above your usual
* range"), and the narrated hero line on the dashboard. Every one of them was
* handed "the latest value" with no notion of when it was taken, so a reading
* from last week was narrated as this morning's — the reader is told something
* about a day on which nothing was measured at all.
*
* The answer belongs beside the data, not beside each sentence. A reading is
* material for a claim about today when it comes from today or from yesterday;
* anything older is history, and history is stated with its date.
*
* The window is deliberately one day rather than zero. A vital taken at 23:40
* and read at 00:10 is the same reading it was thirty minutes earlier, and a
* morning weigh-in narrated that evening is still current. Two days is not:
* by then a day has passed with no reading in it, and the sentence would be
* describing a gap.
*/
/** Whole days of age a reading may carry and still support a present claim. */
export const TODAY_CLAIM_MAX_AGE_DAYS = 1;
/**
* Whether a reading of this age may back a present-tense statement.
*
* An absent, negative or non-finite age answers `false`. A missing age is not
* a fresh one, and it is exactly the case that produced the wrong sentence:
* nothing knew how old the value was, so everything assumed it was new.
*/
export function isCurrentForTodayClaim(
daysAgo: number | null | undefined,
): boolean {
return (
typeof daysAgo === "number" &&
Number.isFinite(daysAgo) &&
daysAgo >= 0 &&
daysAgo <= TODAY_CLAIM_MAX_AGE_DAYS
);
}
/**
* Whole days between two `YYYY-MM-DD` local-day keys (`then` before `today`).
*
* Both keys are already resolved in the reader's own timezone by the caller,
* so the arithmetic is plain calendar subtraction — parsing them as UTC
* midnights keeps it free of any second zone conversion. A malformed key
* answers `null` rather than a fabricated distance.
*/
export function dayKeyAgeInDays(then: string, today: string): number | null {
const days = daysBetweenDateKeys(then, today);
return Number.isFinite(days) ? days : null;
}