Files
HealthLog/src/lib/labs/reading-day.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

38 lines
1.4 KiB
TypeScript

/**
* The calendar day a lab reading belongs to.
*
* Two kinds of reading share `LabResult.takenAt`. A scanned or document
* reading states a date only, stored at noon UTC (rows from before the noon
* anchor sit at UTC midnight); its day is that date. A reading entered by
* hand carries the real instant of the draw; its day is the user's day of
* that instant. Cutting both on the UTC day, as the duplicate checks did,
* put a morning draw east of UTC on the previous day, so a scan of the same
* report wrote the reading a second time.
*/
import { dateOnlyKey, dayKeyAsUtcMidnight } from "@/lib/tz/date-only";
import { shiftDateKey, userDayKey } from "@/lib/tz/format";
const DAY_MS = 86_400_000;
const NOON_MS = 43_200_000;
export function labReadingDay(takenAt: Date, tz: string): string {
const msOfDay = ((takenAt.getTime() % DAY_MS) + DAY_MS) % DAY_MS;
if (msOfDay === 0 || msOfDay === NOON_MS) return dateOnlyKey(takenAt);
return userDayKey(takenAt, tz);
}
/**
* A `takenAt` range wide enough to hold every reading whose
* {@link labReadingDay} is `day`, in any zone from UTC-12 to UTC+14. It
* over-selects by design; filter the rows with `labReadingDay` afterwards.
*/
export function labReadingDaySearchRange(day: string): {
gte: Date;
lt: Date;
} {
return {
gte: dayKeyAsUtcMidnight(shiftDateKey(day, -1)),
lt: dayKeyAsUtcMidnight(shiftDateKey(day, 2)),
};
}