mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 23:12:04 +00:00
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.
This commit is contained in:
1 parent
5820078008
commit
7c5ff89def
105 files changed
+807
-200
No files matched your search
+41
-5
@@ -21,6 +21,38 @@ export function isDateOnlyKey(value: string): boolean {
|
||||
return DATE_ONLY_RE.test(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* True for a `YYYY-MM-DD` string that names a real calendar date.
|
||||
* `2026-02-30` passes the shape check but overflows to 2 March when parsed,
|
||||
* so a reader would return a different day than the one asked for.
|
||||
*/
|
||||
export function isCalendarDateKey(value: string): boolean {
|
||||
if (!DATE_ONLY_RE.test(value)) return false;
|
||||
const parsed = new Date(`${value}T00:00:00.000Z`);
|
||||
return (
|
||||
!Number.isNaN(parsed.getTime()) && parsed.toISOString().startsWith(value)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The calendar date a free-form date string states, or `null`. An ISO string
|
||||
* (`2026-06-10`, `2026-06-10T08:00:00+09:00`) states the date it starts with.
|
||||
* Anything else (`June 10, 2026`) is parsed the way `Date` parses it, as local
|
||||
* time in the process zone, so the date is read back in that same zone;
|
||||
* reading it as UTC put it on the previous day for a process east of UTC.
|
||||
*/
|
||||
export function statedDateKey(raw: string): string | null {
|
||||
const trimmed = raw.trim();
|
||||
const isoPrefix = /^(\d{4}-\d{2}-\d{2})(?:$|T|\s)/u.exec(trimmed);
|
||||
if (isoPrefix) {
|
||||
return isCalendarDateKey(isoPrefix[1]) ? isoPrefix[1] : null;
|
||||
}
|
||||
const parsed = new Date(trimmed);
|
||||
if (Number.isNaN(parsed.getTime())) return null;
|
||||
const pad = (n: number) => String(n).padStart(2, "0");
|
||||
return `${String(parsed.getFullYear()).padStart(4, "0")}-${pad(parsed.getMonth() + 1)}-${pad(parsed.getDate())}`;
|
||||
}
|
||||
|
||||
/** The instant a date-only value is stored as: 12:00 UTC on that date. */
|
||||
export function dateOnlyAtNoonUtc(key: string): Date {
|
||||
return new Date(`${key}T12:00:00.000Z`);
|
||||
@@ -36,11 +68,15 @@ export function dateOnlyKey(value: Date): string {
|
||||
}
|
||||
|
||||
/**
|
||||
* The value a `@db.Date` column holds for calendar date `key` (Prisma reads
|
||||
* and writes those columns as UTC midnight). Use it to compare such a column
|
||||
* with a day: `endsOn >= dbDate(todayKey)` keeps a course through its last
|
||||
* day, where comparing against the instant `now` dropped that day.
|
||||
* Calendar date `key` as UTC midnight. A LABEL, never the instant the day
|
||||
* began for anyone: it is how a `@db.Date` column holds a date (Prisma reads
|
||||
* and writes those as UTC midnight), how the day rollups key `bucketStart`,
|
||||
* and how a day-keyed series places a point on a time axis. Use it to compare
|
||||
* such a column with a day (`endsOn >= dayKeyAsUtcMidnight(todayKey)` keeps a
|
||||
* course through its last day, where comparing with the instant `now` dropped
|
||||
* it) or to build such a label. The first instant of a person's day is
|
||||
* `startOfLocalDayKey(key, tz)`.
|
||||
*/
|
||||
export function dbDate(key: string): Date {
|
||||
export function dayKeyAsUtcMidnight(key: string): Date {
|
||||
return new Date(`${key}T00:00:00.000Z`);
|
||||
}
|
||||
@@ -233,6 +233,27 @@ export function shiftDateKey(dateKey: string, deltaDays: number): string {
|
||||
return dt.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whole calendar days from `fromKey` to `toKey` (negative when `toKey` is
|
||||
* earlier). Pure key arithmetic like {@link shiftDateKey}: both keys are
|
||||
* already resolved calendar dates, so no zone takes part. `NaN` when either
|
||||
* key is malformed, so a caller decides what a bad key means.
|
||||
*/
|
||||
export function daysBetweenDateKeys(fromKey: string, toKey: string): number {
|
||||
const a = Date.parse(`${fromKey}T00:00:00.000Z`);
|
||||
const b = Date.parse(`${toKey}T00:00:00.000Z`);
|
||||
return Math.round((b - a) / 86_400_000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Weekday of a calendar date key, 0 = Sunday … 6 = Saturday (the
|
||||
* `getDay()` / schedule `daysOfWeek` convention). A calendar date has the
|
||||
* same weekday in every zone, so this needs none.
|
||||
*/
|
||||
export function weekdayOfDateKey(dateKey: string): number {
|
||||
return new Date(`${dateKey}T00:00:00.000Z`).getUTCDay();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wall-clock hour (0–23) an observer in `tz` reads off the clock at
|
||||
* `date`. Used for the time-of-day greeting so a traveller whose device
|
||||
|
||||
Reference in new issue
Block a user