/** * A record's time and the `since`/`until` window over it, shared by * `list_records` and `search_records`: a record's time is its table's date * column where the definition names one, else when the record was made. Both * read as instants in SQL (`julianday`), so a day, a UTC stamp and a stamp at * an offset compare as the moments they are. */import { fail, optionalString, typeData, typeToolContext } from"../../context.ts";import { col, typeOf, typeCond } from"../../../store.ts";importtype { Table } from"./table.ts";import { isDate } from"./values.ts";import { isKnownZone, joinMoment } from"./zone.ts";/** The column a table's records are timed by: its date, else `created_at`. */exportfunction timeColumn(t: Table): string {return t.date ?? "created_at";}/** A record's time as a julian day in SQL, NULL where the date is empty or not a time. */exportfunction timeSql(t: Table): string {const d = t.date;return d ? `(CASE WHEN ${typeOf(d)} = 'text' THEN julianday(${col(d)}) END)` : "julianday(created_at)";}/** Newest first by the record's time, the undated last, then the latest written. */exportfunction newestSql(t: Table): string {const at = timeSql(t);return`${at} IS NULL, ${at} DESC, updated_at DESC, id`;}/** A record's time as epoch milliseconds, or null: what a merge across tables orders by. */exportfunction timeOf(t: Table, row: { data: Data; created_at: string }): { at: string | null; ms: number | null } {const v = t.date ? row.data[t.date] : row.created_at;if (typeof v !== "string" || !v) return { at: null, ms: null };const ms = Date.parse(isDate(v) ? `${v}T00:00:00Z` : v);return { at: v, ms: Number.isNaN(ms) ? null : ms };}const INSTANT = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:\d{2})$/;function addDay(day: string): string {const d = newDate(`${day}T00:00:00Z`); d.setUTCDate(d.getUTCDate() + 1);return d.toISOString().slice(0, 10);}/** One end of the window as an instant: a day starts (or, for `until`, ends) in the reader's zone; a stamp is itself. */function bound(ctx: ToolContext, args: Data, key: "since" | "until"): { at: string; inclusive: boolean } | null {const v = args[key];if (v === undefined || v === null) returnnull;const text = typeof v === "string" ? v.trim() : "";const zone = optionalString(args, "timezone") ?? ctx.timezone;if (!isKnownZone(zone)) fail(`"${zone}" is not a known time zone`);if (isDate(text)) return key === "since" ? { at: joinMoment(text, null, zone), inclusive: true } : { at: joinMoment(addDay(text), null, zone), inclusive: false };if (INSTANT.test(text) && !Number.isNaN(Date.parse(text))) return { at: newDate(Date.parse(text)).toISOString(), inclusive: true }; fail(`${key} is a day (YYYY-MM-DD, in the reader's zone) or an instant with its offset (2026-10-25T09:00:00Z); ${JSON.stringify(v)} is not one`);}exportinterfaceWindow { since: { at: string; inclusive: boolean } | null; until: { at: string; inclusive: boolean } | null;}/** The ask's `since` and `until`, read once; a window that holds no time is refused. */exportfunction readWindow(ctx: ToolContext, args: Data): Window {const since = bound(ctx, args, "since");const until = bound(ctx, args, "until");if (since && until && Date.parse(since.at) > Date.parse(until.at) - (until.inclusive ? 0 : 1)) fail("since is after until: the window holds no time");return { since, until };}/** * The window as conditions on the record's time: since from that moment on * (a day from its start), until up to that moment (a day through its end). A * record with no time falls outside any window. */exportfunction windowSql(t: Table, w: Window): Cond[] {const at = timeSql(t);const out: Cond[] = [];if (w.since) out.push({ sql: `${at} >= julianday(?)`, args: [w.since.at] });if (w.until) out.push({ sql: `${at} ${w.until.inclusive ? "<=" : "<"} julianday(?)`, args: [w.until.at] });return out;}