[repo] Workspace: Scaffold Phase 0 with schema, engine, content, ui and the PWA shell
pnpm + turbo workspace with the commands from CLAUDE.md §3. The contracts from §6 as JSON Schemas with generated types; localDay as the one place a day is computed; the forbidden-word list shared by lint:copy and the library validator, with 13 negative fixtures; a Svelte 5 PWA shell built under /Rosea/ with a meta CSP. E2E and offline suites run against the built bundle; the offline one closes the server rather than emulating. Analysis: .agents/features/phase0-workspace.md
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* The ONLY place in the repository where a day is computed (CLAUDE.md §7).
|
||||
*
|
||||
* A day is always local to a named IANA time zone, and the zone is always a
|
||||
* parameter. The engine never asks the device which zone it is in: the app
|
||||
* passes the zone of the city the player chose, or the device's own zone as the
|
||||
* fallback (CLAUDE.md §4 `profile`).
|
||||
*
|
||||
* Never `toISOString().slice(0, 10)`: that is the UTC day, and in Prague it
|
||||
* records Monday-evening care as Tuesday's.
|
||||
*/
|
||||
|
||||
/** A calendar day, `YYYY-MM-DD`, in some player's local time. */
|
||||
export type LocalDay = string;
|
||||
|
||||
const LOCAL_DAY = /^\d{4}-\d{2}-\d{2}$/;
|
||||
|
||||
// Memoised per zone. Constructing a DateTimeFormat costs far more than using one.
|
||||
// This is a cache of a pure function's machinery, not state: the output depends
|
||||
// only on the arguments.
|
||||
const formatters = new Map<string, Intl.DateTimeFormat>();
|
||||
|
||||
function formatterFor(timeZone: string): Intl.DateTimeFormat {
|
||||
let formatter = formatters.get(timeZone);
|
||||
if (formatter === undefined) {
|
||||
// Throws RangeError for an unknown zone, which is the right failure: a
|
||||
// mistyped zone silently falling back to UTC is the bug this module exists
|
||||
// to prevent.
|
||||
formatter = new Intl.DateTimeFormat('en-CA', {
|
||||
timeZone,
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
calendar: 'iso8601',
|
||||
numberingSystem: 'latn'
|
||||
});
|
||||
formatters.set(timeZone, formatter);
|
||||
}
|
||||
return formatter;
|
||||
}
|
||||
|
||||
/** The local calendar day of `epochMs` in `timeZone`. */
|
||||
export function localDay(epochMs: number, timeZone: string): LocalDay {
|
||||
if (!Number.isFinite(epochMs)) {
|
||||
throw new RangeError(`localDay: epochMs must be a finite number, got ${String(epochMs)}`);
|
||||
}
|
||||
|
||||
const parts = formatterFor(timeZone).formatToParts(epochMs);
|
||||
const part = (type: Intl.DateTimeFormatPartTypes): string => {
|
||||
const value = parts.find((p) => p.type === type)?.value;
|
||||
if (value === undefined) throw new RangeError(`localDay: no ${type} for ${epochMs} in ${timeZone}`);
|
||||
return value;
|
||||
};
|
||||
|
||||
const day = `${part('year')}-${part('month')}-${part('day')}`;
|
||||
if (!LOCAL_DAY.test(day)) {
|
||||
// Years before 1000 or after 9999 would not be four digits. No player tends
|
||||
// a rose then; refusing is better than writing an ambiguous day to the log.
|
||||
throw new RangeError(`localDay: ${epochMs} gives an unrepresentable day '${day}'`);
|
||||
}
|
||||
return day;
|
||||
}
|
||||
|
||||
/** Whether `value` has the shape of a LocalDay. Does not check that the date exists. */
|
||||
export function isLocalDay(value: string): value is LocalDay {
|
||||
return LOCAL_DAY.test(value);
|
||||
}
|
||||
Reference in New Issue
Block a user