[engine] Rose: Replay the care log into growth from days of care, drift, moods, needs and numbers
This commit is contained in:
@@ -61,6 +61,31 @@ export function localDay(epochMs: number, timeZone: string): LocalDay {
|
||||
return day;
|
||||
}
|
||||
|
||||
const clockFormatters = new Map<string, Intl.DateTimeFormat>();
|
||||
|
||||
/** Minutes after local midnight on the clock of `timeZone` (0–1439): what a clock there shows. */
|
||||
export function localMinutes(epochMs: number, timeZone: string): number {
|
||||
if (!Number.isFinite(epochMs)) throw new RangeError(`localMinutes: epochMs must be a finite number, got ${String(epochMs)}`);
|
||||
let f = clockFormatters.get(timeZone);
|
||||
if (f === undefined) {
|
||||
f = new Intl.DateTimeFormat('en-GB', { timeZone, hour: '2-digit', minute: '2-digit', hourCycle: 'h23', numberingSystem: 'latn' });
|
||||
clockFormatters.set(timeZone, f);
|
||||
}
|
||||
const parts = f.formatToParts(epochMs);
|
||||
const n = (type: Intl.DateTimeFormatPartTypes): number => Number(parts.find((p) => p.type === type)?.value);
|
||||
return n('hour') * 60 + n('minute');
|
||||
}
|
||||
|
||||
/** Whole calendar days from `from` to `to`: positive when `to` is later. */
|
||||
export function daysBetween(from: LocalDay, to: LocalDay): number {
|
||||
const utc = (day: LocalDay): number => {
|
||||
if (!isLocalDay(day)) throw new RangeError(`daysBetween: '${day}' is not a LocalDay`);
|
||||
const [y, m, d] = day.split('-').map(Number) as [number, number, number];
|
||||
return Date.UTC(y, m - 1, d);
|
||||
};
|
||||
return Math.round((utc(to) - utc(from)) / 86_400_000);
|
||||
}
|
||||
|
||||
/** The calendar day `days` after `day` (before, if negative). A calendar step: no zone, no DST. */
|
||||
export function addDays(day: LocalDay, days: number): LocalDay {
|
||||
if (!isLocalDay(day)) throw new RangeError(`addDays: '${day}' is not a LocalDay`);
|
||||
|
||||
Reference in New Issue
Block a user