/** * Calendar kernel — pure arithmetic, formatting, and parsing over the world's * integer time line. * * The contract is the calendar design of 2026-07-14 (its §5 and §5b). The * binding rules, restated where they bite: * * - The number line is the only truth. A calendar names it; it never owns it. * - Absolute year index is ZERO-based: step 0 opens absolute year 0. All * division is Euclidean floor (negatives depend on it). * - format(step) is total and O(config) — it never walks history. * - Intercalary days belong to no month and render via `format_intercalary`. * - A KNOWN token with no applicable value renders empty (with separator * collapse); an UNKNOWN token renders literally. * - parse() matches calendar words case-insensitively; bare numbers are ALWAYS * the raw stored step. Guaranteed property: format(parse(format(n))) === * format(n). Exact integer recovery is NOT promised under lossy formats. * * Everything here is pure. No atlas imports beyond nothing — this module is * consumed by the charts room, popovers, and (via freeze) the publish path. */ interface CalendarEra { name: string; abbrev?: string; /** Up-era: domain start (inclusive). Down-era: the PIVOT (exclusive upper bound). */ start: number; /** Up-era: domain end (exclusive); defaults to the next era's start. Down-era: lower bound (inclusive); defaults to -infinity. */ end?: number; /** true (default): years number from `start`. false: pure label, {year} shows the absolute year index. */ restart?: boolean; /** First year number of a restart era: 0 or 1 (default 1). */ year_base?: 0 | 1; /** 'up' (default) or 'down' (BC / Dune B.G. — counts toward the pivot). */ direction?: 'up' | 'down'; /** Closed eras format old dates but are never offered at date entry. */ closed?: boolean; } interface CalendarMonths { names: string[]; /** Same length as names. */ lengths: number[]; } interface CalendarWeek { length: number; names: string[]; /** 'free' (default): runs across month boundaries, anchor step 0 = names[0]. * 'nested': weekday = (day_of_month - 1) mod length; ragged final week legal. */ mode?: 'free' | 'nested'; } interface CalendarIntercalary { name: string; /** 0 = before month 1; k = after month k. Requires months. */ after_month: number; /** Present only in years where floorMod(absYear + offset, N) === 0 (the leap rule). */ every_years?: number; /** Version 2: divisors that flip the rule in order, each a multiple of the one before * (`every_years: 4, except: [100, 400]`: every 4, not every 100, yet every 400). */ except?: number[]; /** Version 2: the rule reads year absYear + offset (default 0; 1 for a calendar with no year 0). */ offset?: number; /** Counts in the free-running week (default true). The Shire lock = opting * exactly Mid-year's Day + Overlithe out (364 = 52 × 7). */ in_week?: boolean; } interface CalendarConfig { version: number; /** Immutable sentinel: the world's time_basic_unit at creation. Machinery only. */ grain: string; /** The culture's display word for one step ("Sol"). Defaults to grain. */ step_name?: string; eras?: CalendarEra[]; bundles?: { /** Coarse-calendar year length. Legal ONLY when months are absent. */ year?: { steps: number; }; months?: CalendarMonths; week?: CalendarWeek; }; intercalary?: CalendarIntercalary[]; /** Regular-day template. Tokens: {day} {month} {weekday} {year} {era} {step}. */ format?: string; /** Intercalary-day template; may also use {intercalary}. */ format_intercalary?: string; } /** A step, taken apart. Fields are null when the structure doesn't exist. */ interface StepBreakdown { step: number; /** Absolute year index (zero-based from step 0). */ absYear: number; /** The era-local display year (restart arithmetic applied), or absYear when no era / restart:false. */ year: number; era: CalendarEra | null; /** 1-based month index, or null (intercalary day / no months). */ month: number | null; monthName: string | null; /** 1-based day within the month, or null. */ day: number | null; /** Intercalary day name when the step IS one, else null. */ intercalary: string | null; weekday: string | null; } interface ParseResult { step: number; } interface ParseError { error: string; } declare function floorDiv(a: number, b: number): number; declare function floorMod(a: number, b: number): number; declare function validateCalendarConfig(cfg: CalendarConfig): string[]; /** * Defensive reader for wild configs (hand-edited files, other tools). Returns * null when the value is not a structurally usable calendar — callers fall * back to bare numbers, never a broken axis. */ declare function readCalendarConfig(value: unknown): CalendarConfig | null; /** True when absolute year y contains a leap entry with period N (the version-1 rule). */ declare function isLeapYear(y: number, N: number): boolean; /** True when the intercalary day falls in absolute year y: always without `every_years`, else by its rule. */ declare function intercalaryInYear(cfg: CalendarConfig, ic: CalendarIntercalary, y: number): boolean; /** First step of absolute year y. yearStart(0) === 0 by construction. */ declare function yearStartStep(cfg: CalendarConfig, y: number): number; /** Length in steps of absolute year y. */ declare function yearLength(cfg: CalendarConfig, y: number): number; /** Absolute year index of a step. Zero-based; closed-form seed + bounded correction. */ declare function absoluteYear(cfg: CalendarConfig, step: number): number; interface EraDomain { era: CalendarEra; lo: number; hi: number; } declare function resolveEraDomains(eras: CalendarEra[]): EraDomain[]; /** * The era owning a step: domain containment; overlaps resolve to the latest start, then an * up era over a down one, then the later era in the list (the profile, calendar.md). The * comparator was `up ? -1 : 1` before 4.42.2: inconsistent for two eras of one direction, * so their order was the sort's, not the list's. */ declare function eraForStep(cfg: CalendarConfig, step: number): CalendarEra | null; /** The display year for a step under an era (or the absolute year without one). */ declare function displayYear(cfg: CalendarConfig, step: number, era: CalendarEra | null): number; interface DaySlot { kind: 'month' | 'intercalary'; month?: number; day?: number; intercalary?: CalendarIntercalary; } /** One year's slots, in yearSlots' order, asked the four questions its callers ask. */ declare class YearLayout { private readonly runs; private readonly list; readonly total: number; constructor(cfg: CalendarConfig, y: number); /** slots[i]: undefined unless i is a whole index inside the year. */ at(i: number): DaySlot | undefined; /** The intercalary days out of the week at the indexes 0, 1, ... below offset; reading past the year's last * slot throws, as the loop it replaces did. */ skippedBefore(offset: number): number; indexOfIntercalary(intercalary: CalendarIntercalary): number; indexOfDay(month: number, day: number): number; } /** Weekday name for a step, or null (no week / nested outside months / skipped intercalary day). */ declare function weekdayForStep(cfg: CalendarConfig, step: number, pre?: StepPosition): string | null; interface StepPosition { absYear: number; offset: number; slot: DaySlot | null; slots: YearLayout | null; } declare function breakdownStep(cfg: CalendarConfig, step: number): StepBreakdown; /** Format a step. Total: every integer formats to a string under every readable config. */ declare function formatStep(cfg: CalendarConfig, step: number): string; /** * Compact year-level label: "{year} {era-abbrev}" — the axis-tick wording when * zoomed wide (full formatStep at tick density would be a wall of month names). * Outside every era: the absolute year index alone. */ declare function formatYearLabel(cfg: CalendarConfig, step: number): string; /** * The base year length in steps (leap days excluded), or null when the * calendar has no year structure above the step. Consumers use it as a * zoom threshold (ticks tighter than a year → full dates, else year labels). */ declare function approxYearLength(cfg: CalendarConfig): number | null; /** * A duration in steps, spoken: "19 years" when the calendar has year * structure and the span reaches it, else "12 days" in the step word. Uses * the BASE year length (leap days are noise at duration granularity); one * decimal below 10 years, whole above. Pure. */ declare function formatDuration(cfg: CalendarConfig, steps: number): string; /** * The first step of each month in absolute year y, in order. O(config) — walks * month lengths + intercalary placement, never the year's days. Empty when the * calendar has no months. */ declare function monthStartSteps(cfg: CalendarConfig, y: number): { month: number; name: string; step: number; }[]; /** * Mid-rung label for dense axis ticks: "11 Greening 454" — day, month, display * year, NO era word (the era bands carry the era; a label wall repeating "FT" * is noise). Intercalary steps read "Hearthmoot 454". Monthless calendars fall * back to the year label. */ declare function formatMidLabel(cfg: CalendarConfig, step: number): string; interface ParseOptions { /** The world's now (used to default a missing year, per contract rule 9). */ now?: number | null; } /** Absolute year for an era-local display year (inverse of displayYear). */ declare function absYearForEraYear(cfg: CalendarConfig, era: CalendarEra, Y: number): number; /** * Parse a written date to a step. * * Accepted shapes (calendar words case-insensitive): * "-454" / "454" -> the raw stored step, always * "454 AE" / "Year 12 FT" -> year (+ era) * "Frostwane 454" / "12 Frostwane 454" (+ era) * "Shieldmeet 1372" (+ era) -> intercalary day * "Frostwane 12" (no year) -> resolved against the now's year * Partial dates snap to the period's first step. Missing year with no now = * error (never guess). p(f(n)) == n is NOT promised; f(p(f(n))) == f(n) is. */ declare function parseDate(cfg: CalendarConfig, input: string, opts?: ParseOptions): ParseResult | ParseError; declare function isParseError(r: ParseResult | ParseError): r is ParseError; /** * World calendars: the calendar kernel, and which calendars a world holds. * * `calendar.ts` beside this file came from Atlas's `src/core/calendar.ts` in 4.20.0, and is * the one copy since Atlas 245aad3 deleted its own and imports this one: * a kernel change is an SDK release, and Atlas's 48 kernel tests run against it there too. * Its contract is still Atlas's calendar design, §5 and §5b. This file adapts Atlas's * `src/core/calendar-lens.ts` (readCalendarValue, worldCalendars, and its resolveCalendarLens as resolveCalendar) to * any element shape, since the SDK has no Atlas `Element`. * * The field: a calendar is an element (a Construct by convention) carrying `atlas_calendar`. * Atlas owns the field, its meaning and its writes (its calendar design, §5 and §5b); every * other tool reads it and never writes it (a prefix names the owner, not * who may read). Detection keys on the field's presence, never on the supertype. * Everything degrades to bare numbers: a missing, dangling or malformed calendar is null, * never an error. */ /** The supertype Atlas gives a calendar Construct: convention and display only. */ declare const CALENDAR_SUPERTYPE = "calendar"; /** Atlas's field for a calendar's config (owned by Atlas: read it, never write it). */ declare const CALENDAR_FIELD = "atlas_calendar"; /** Any element: an id, a name, and the rest of its fields. */ interface OwCalendarElement { id: string; name?: string | null; [field: string]: unknown; } interface WorldCalendar { /** The calendar element's id. */ id: string; /** The element's name: the calendar's display name. */ name: string; config: CalendarConfig; } /** * The tolerant `atlas_calendar` read: an object or a JSON string (another tool may * serialize it; Atlas writes objects). Null for anything unusable. Read the field only * through this: a reader that took objects alone would blank a string config. */ declare function readCalendarValue(raw: unknown): CalendarConfig | null; /** * Every usable calendar in the world: the elements with a valid `atlas_calendar`. * A malformed config is skipped: a broken calendar is invisible, not an error. */ declare function worldCalendars(elements: Iterable): WorldCalendar[]; /** * Resolve a chosen calendar id against the elements. A null id, a dangling id or an * invalid config resolve to null: bare numbers. * * `fallbackToOnly`: when no calendar was ever chosen (`activeId` is `undefined`, not * `null`) and the world holds exactly one, use it. `null` is an explicit "no calendar" and * stays null. With several calendars and no choice, nothing is guessed. An unknown option is a * TypeError (the caller's bug, not the world's data). */ declare function resolveCalendar(elements: Iterable, activeId: string | null | undefined, opts?: { fallbackToOnly?: boolean; }): WorldCalendar | null; /** The World's key that names its calendar: the id of the element that carries it. */ declare const WORLD_CALENDAR_KEY = "x_calendar"; /** * The calendar the world's dates speak in, by the World's own choice (the calendar profile, * "Which calendar speaks"). `world` is the World's keys (the body `GET /world` returns, * `world.json` in a folder); `elements` are the world's elements. * * - The World names, in `x_calendar`, the id of an element among `elements` that carries a * calendar: that calendar speaks. The id is matched as written, letter case included. * - The World names anything else (an id no element has, an element with no calendar or one whose * value is not a calendar, an empty string, a value that is no string): none speaks, and the * dates stay bare numbers. Another calendar never speaks in its place. * - The World names none (the key absent or `null`): the only calendar among `elements` speaks; * with several, none. * * Pass the whole world's elements: a reader that holds part of a world can miss the named * element or a second calendar. A tool with a person in front of it may keep that person's own * pick over the World's ({@link resolveCalendar}). A `world` that is not an object is a TypeError. */ declare function speakingCalendar(world: Readonly>, elements: Iterable): WorldCalendar | null; /** * The old name of {@link resolveCalendar}, with the same answers: "lens" now names a cut of a * world, not a chosen calendar. * * @deprecated Since 4.85.0. Use `resolveCalendar`; this name leaves in 5.0. */ declare function resolveCalendarLens(elements: Iterable, activeId: string | null | undefined, opts?: { fallbackToOnly?: boolean; }): WorldCalendar | null; export { CALENDAR_FIELD, CALENDAR_SUPERTYPE, WORLD_CALENDAR_KEY, absYearForEraYear, absoluteYear, approxYearLength, breakdownStep, displayYear, eraForStep, floorDiv, floorMod, formatDuration, formatMidLabel, formatStep, formatYearLabel, intercalaryInYear, isLeapYear, isParseError, monthStartSteps, parseDate, readCalendarConfig, readCalendarValue, resolveCalendar, resolveCalendarLens, resolveEraDomains, speakingCalendar, validateCalendarConfig, weekdayForStep, worldCalendars, yearLength, yearStartStep }; export type { CalendarConfig, CalendarEra, CalendarIntercalary, CalendarMonths, CalendarWeek, EraDomain, OwCalendarElement, ParseError, ParseOptions, ParseResult, StepBreakdown, WorldCalendar };