/** * 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, type Data, type ToolContext } from "../../context.ts"; import { col, typeOf, type Cond } from "../../../store.ts"; import type { 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`. */ export function 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. */ export function 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. */ export function 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. */ export function 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 = new Date(`${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) return null; 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: new Date(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`); } export interface Window { 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. */ export function 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. */ export function 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; }