Commitments stored as rules (start, length, once/daily/weekly, end, skips) in the church's timezone, daylight saving included; the day's coverage as counts only, cached until a booking changes, live over server-sent events (X-Accel-Buffering off, a comment every 25 s). Booking, skipping, changing, cancelling within the 8-hour rule, renewing (a signed single-use link), "I prayed" (self-reported). Campaigns and moderated testimonies; the admin's bookings with names, always access-logged; booking on a member's behalf. Reminders (every minute) and renewal notices (14 and 3 days before) queued in an outbox for the messaging slice. The heartbeat's prayer counts; export and delete for a member's prayer. Migration 20261202000000_house_of_prayer (checked on PostgreSQL 18: applies, no diff). 66 backend tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
120 lines
4.9 KiB
TypeScript
120 lines
4.9 KiB
TypeScript
/**
|
|
* Local dates and times in the church's timezone (APP-1.0-PLAN §1), with `Intl` only. A local day
|
|
* is a `YYYY-MM-DD` string and a time in it is minutes after midnight; instants are UTC `Date`s.
|
|
*
|
|
* Daylight saving: a local time that doesn't exist (the hour the clocks jump forward) has no
|
|
* instant (`zonedTime` answers null); a local time that happens twice (the hour the clocks go
|
|
* back) means its first instant. South Africa has neither, but a church elsewhere may.
|
|
*/
|
|
|
|
const formatters = new Map<string, Intl.DateTimeFormat>();
|
|
|
|
function formatter(tz: string): Intl.DateTimeFormat {
|
|
let f = formatters.get(tz);
|
|
if (!f) {
|
|
f = new Intl.DateTimeFormat('en-CA', { timeZone: tz, hourCycle: 'h23', year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' });
|
|
formatters.set(tz, f);
|
|
}
|
|
return f;
|
|
}
|
|
|
|
function parts(at: Date, tz: string) {
|
|
const out: Record<string, number> = {};
|
|
for (const p of formatter(tz).formatToParts(at)) if (p.type !== 'literal') out[p.type] = Number(p.value);
|
|
return out as { year: number; month: number; day: number; hour: number; minute: number; second: number };
|
|
}
|
|
|
|
const pad = (n: number, w = 2) => String(n).padStart(w, '0');
|
|
|
|
/** Whether `tz` is a timezone `Intl` knows. */
|
|
export function isTimezone(tz: string): boolean {
|
|
try {
|
|
formatter(tz);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/** The local date, minute of the day and weekday (0 = Sunday) of an instant in `tz`. */
|
|
export function localParts(at: Date, tz: string): { date: string; minute: number; weekday: number } {
|
|
const p = parts(at, tz);
|
|
const date = `${pad(p.year, 4)}-${pad(p.month)}-${pad(p.day)}`;
|
|
return { date, minute: p.hour * 60 + p.minute, weekday: weekday(date) };
|
|
}
|
|
|
|
/** The local date of an instant in `tz`. */
|
|
export const localDate = (at: Date, tz: string) => localParts(at, tz).date;
|
|
|
|
/** The offset of `tz` from UTC at an instant, in milliseconds (SAST: +2 hours). */
|
|
function offsetAt(t: number, tz: string): number {
|
|
const p = parts(new Date(t), tz);
|
|
return Date.UTC(p.year, p.month - 1, p.day, p.hour, p.minute, p.second) - Math.floor(t / 1000) * 1000;
|
|
}
|
|
|
|
const DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
|
|
/** Whether `s` is a real `YYYY-MM-DD` date. */
|
|
export function isDate(s: unknown): s is string {
|
|
if (typeof s !== 'string') return false;
|
|
const m = DATE.exec(s);
|
|
if (!m) return false;
|
|
const d = new Date(Date.UTC(Number(m[1]), Number(m[2]) - 1, Number(m[3])));
|
|
return d.getUTCFullYear() === Number(m[1]) && d.getUTCMonth() === Number(m[2]) - 1 && d.getUTCDate() === Number(m[3]);
|
|
}
|
|
|
|
const utcDay = (date: string) => {
|
|
const m = DATE.exec(date);
|
|
if (!m) throw new Error(`Not a date: ${date}`);
|
|
return Date.UTC(Number(m[1]), Number(m[2]) - 1, Number(m[3]));
|
|
};
|
|
const fromUtcDay = (t: number) => {
|
|
const d = new Date(t);
|
|
return `${pad(d.getUTCFullYear(), 4)}-${pad(d.getUTCMonth() + 1)}-${pad(d.getUTCDate())}`;
|
|
};
|
|
|
|
/** `date` plus `n` days. */
|
|
export const addDays = (date: string, n: number) => fromUtcDay(utcDay(date) + n * 86400e3);
|
|
|
|
/** `date` plus `n` months, kept inside the month (31 January + 1 month = 28 or 29 February). */
|
|
export function addMonths(date: string, n: number): string {
|
|
const d = new Date(utcDay(date));
|
|
const target = new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth() + n, 1));
|
|
const last = new Date(Date.UTC(target.getUTCFullYear(), target.getUTCMonth() + 1, 0)).getUTCDate();
|
|
target.setUTCDate(Math.min(d.getUTCDate(), last));
|
|
return fromUtcDay(target.getTime());
|
|
}
|
|
|
|
/** Days from `a` to `b` (negative when `b` is earlier). */
|
|
export const daysBetween = (a: string, b: string) => Math.round((utcDay(b) - utcDay(a)) / 86400e3);
|
|
|
|
/** 0 = Sunday … 6 = Saturday. */
|
|
export const weekday = (date: string) => new Date(utcDay(date)).getUTCDay();
|
|
|
|
/**
|
|
* The instant of `minute` minutes after local midnight on `date` in `tz` (minute may be 1440 or
|
|
* more: the next day). Null when that local time doesn't exist (clocks jumping forward).
|
|
*/
|
|
export function zonedTime(date: string, minute: number, tz: string): Date | null {
|
|
const day = addDays(date, Math.floor(minute / 1440));
|
|
const min = ((minute % 1440) + 1440) % 1440;
|
|
const guess = utcDay(day) + min * 60e3;
|
|
// Two passes find the offset in force at that local time; the earlier of two (clocks going back).
|
|
const first = guess - offsetAt(guess, tz);
|
|
const candidates = [first, guess - offsetAt(first, tz), guess - offsetAt(first - 3600e3, tz), guess - offsetAt(first + 3600e3, tz)];
|
|
const ok = [...new Set(candidates)].filter((t) => {
|
|
const p = localParts(new Date(t), tz);
|
|
return p.date === day && p.minute === min;
|
|
});
|
|
return ok.length ? new Date(Math.min(...ok)) : null;
|
|
}
|
|
|
|
/** The instant local midnight starts `date` (the first instant of the day, whatever DST does). */
|
|
export function startOfDay(date: string, tz: string): Date {
|
|
for (let m = 0; m < 180; m += 15) {
|
|
const t = zonedTime(date, m, tz);
|
|
if (t) return t;
|
|
}
|
|
throw new Error(`No start of ${date} in ${tz}`);
|
|
}
|