Night Light was all-or-nothing. Add an optional schedule with From/To time inputs so the warm overlay only shows during set hours and toggles itself on/off automatically (the overlay re-checks every minute; no reload). Overnight windows that wrap midnight (e.g. 21:00 -> 07:00) are handled. Window logic is the pure, unit-tested isWithinTimeWindow/parseHHMM in utils/timeWindow.ts. New settings: nightLightSchedule/Start/End (default 21:00-07:00). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
23 lines
1005 B
TypeScript
23 lines
1005 B
TypeScript
// Parse an "HH:mm" (24h) string into minutes-since-midnight, or null if malformed.
|
|
export function parseHHMM(value: string): number | null {
|
|
const m = /^(\d{2}):(\d{2})$/.exec(value);
|
|
if (!m) return null;
|
|
const hours = Number(m[1]);
|
|
const minutes = Number(m[2]);
|
|
if (hours > 23 || minutes > 59) return null;
|
|
return hours * 60 + minutes;
|
|
}
|
|
|
|
// Is `now`'s wall-clock time inside the [start, end) window? The window may wrap
|
|
// past midnight (start > end, e.g. 21:00 → 07:00). A zero-length window
|
|
// (start === end) is treated as never active. Malformed inputs → false.
|
|
export function isWithinTimeWindow(start: string, end: string, now: Date = new Date()): boolean {
|
|
const s = parseHHMM(start);
|
|
const e = parseHHMM(end);
|
|
if (s === null || e === null || s === e) return false;
|
|
const cur = now.getHours() * 60 + now.getMinutes();
|
|
if (s < e) return cur >= s && cur < e;
|
|
// Overnight wrap: active from start until midnight, then midnight until end.
|
|
return cur >= s || cur < e;
|
|
}
|