Intervals and the grid
The grid
Every cache has an interval, which is required and has no default. It is one of two things:
- a fixed interval in milliseconds, with an
alignmentOffset(default 0); - a calendar interval: a day, week, month, quarter or year in a time zone (below).
A fixed interval and its offset define the grid as the timestamps t with
(t - alignmentOffset) % interval === 0Each grid point is a slot. A cache stores at most one point per slot, and every timestamp it stores lies on the grid. The interval is the smallest distance between two points: a series at one minute and the same series at one hour are two caches (there is no resampling).
Timestamps are milliseconds since the epoch, as Float64 numbers, and must be safe integers (|t| ≤ 2^53 − 1). Internally all range arithmetic runs on integer slot indices, and one module converts between milliseconds and slots, so off-by-one errors have one place to live.
alignmentOffset
The offset moves a fixed grid. Daily bars stamped at 14:30 UTC are on a grid of one day offset by 14.5 hours:
const daily = await client.cache({
id: "candles/ACME/1d",
interval: 86_400_000,
alignmentOffset: 52_200_000, // 14:30 UTC, the market's open
fields: { open: "f64", high: "f64", low: "f64", close: "f64" },
});Any integer offset is accepted and normalised into [0, interval): an offset of interval + 5 names the same grid as 5, and both configs resolve identically. A fixed grid does not move: a session that follows daylight saving time does not fit one offset all year, and needs a calendar interval.
Calendar intervals
A calendar interval makes each slot a calendar unit in a time zone. The slot starts when the zone's clock reads at on the unit's first day:
interface CalendarInterval {
unit: "day" | "week" | "month" | "quarter" | "year";
zone?: string; // an IANA time zone; default "UTC"
at?: string; // "HH:MM", 24-hour; default "00:00"
weekStart?: 1 | 2 | 3 | 4 | 5 | 6 | 7; // weeks only: 1 = Monday (default) … 7 = Sunday
}A New York session opens at 09:30 local time, which is 14:30 UTC in winter and 13:30 UTC in summer. With a calendar interval both are slot starts, so daily bars are stamped at the open all year:
// One slot per New York session day, starting at the open: 14:30 UTC in
// winter and 13:30 UTC in summer.
const sessions = await client.cache({
id: "candles/ACME/1d-ny",
interval: { unit: "day", zone: "America/New_York", at: "09:30" },
fields: { close: "f64" },
});
// New York moved to daylight saving time on 8 March 2026. Both opens are
// slot starts, so both bars are on the grid.
await sessions.put({
timestamps: [
Date.UTC(2026, 2, 6, 14, 30), // Friday, 09:30 EST
Date.UTC(2026, 2, 9, 13, 30), // Monday, 09:30 EDT
],
fields: { close: [101.5, 102.25] },
});Everything else works as on a fixed grid, in milliseconds: the ranges you pass, the timestamps a read returns, coverage, misses, watermarks and events. What changes is where the grid points lie.
- Every calendar day is a slot. A weekend or a holiday is an empty slot: the fetcher's response covers it and holds no bar for it, so the cache records a confirmed gap and does not ask again. The cache holds no holiday list.
alignmentOffsetis not used.zoneandatplace the slots; an offset other than 0 is rejected withConfigError.- Zones are compared as written.
US/EasternandAmerica/New_Yorkare two grids, and a second tab that names the other one is refused. Use the canonical IANA name. A zone the runtime does not know is rejected withConfigError. - Transitions. A local time that a clock change skips starts after the change (an
atof 02:30 on the day clocks go forward starts at 03:30), and one that occurs twice starts at the first. - Domain. The years 1 to 9999. A range or timestamp outside them is rejected as one beyond the safe integers is on a fixed grid.
- Reading the config back.
intervalisnumber | CalendarIntervalin a cache's config and in a fetcher's request, so TypeScript code narrows it before doing arithmetic (typeof interval === "number"). - Sizes count slots.
gapSplitKandsegmentSlotCapcount days, weeks or months: with the defaults, a closure of more than four days splits a segment, and one page of a daily cache spans about 89 years. - Units below a day are not calendar units. A fixed interval covers those that divide an hour.
Zone rules come from the runtime's Intl, so the library bundles no time-zone data. A zone other than "UTC" needs a runtime whose Intl.DateTimeFormat knows the longOffset time-zone name (Chrome 95, Firefox 91, Safari 15.4, Node 18, or newer); in an older one such a zone is rejected with ConfigError, as an unknown zone is. Two runtimes can hold different rules, for example a server and the browser that hydrates its snapshot; see Server rendering for what the cache checks then.
Grid arithmetic in the page
A view often needs the grid itself: the range a read returns for what is on screen, the bar forming now, or the start of the next one. On a calendar grid a fixed step is wrong at every edge, so @anishbenji/tscache/grid exports the arithmetic the cache uses, for both kinds of interval:
// A view's range as the cache reads it, and the bar forming now, for any
// interval: the arithmetic the engine itself uses.
const nySession = {
unit: "day",
zone: "America/New_York",
at: "09:30",
} as const;
const shown = snapRange(
{ start: Date.UTC(2026, 2, 6, 20), end: Date.UTC(2026, 2, 9, 20) },
nySession,
); // 6 March 14:30 UTC to 10 March 13:30 UTC
const forming = slotStart(Date.UTC(2026, 2, 9, 20), nySession); // 9 March 13:30 UTC
const next = nextSlotStart(forming, nySession); // 10 March 13:30 UTC| Function | Returns |
|---|---|
snapRange(range, interval, alignmentOffset?) | range snapped outward to the grid: what get reads for it |
slotStart(t, interval, alignmentOffset?) | the start of the slot that contains t |
nextSlotStart(t, interval, alignmentOffset?) | the start of the slot after it; the difference is the slot's length |
An application that does not import the entry does not load it.
Data that does not fit either grid
A put with a timestamp off the grid is rejected whole ('misaligned'), so data on another schedule is mapped onto a grid in the fetcher, before the cache sees it. These recipes cover the common cases.
Daily bars from an exchange that observes daylight saving time
Use a calendar interval with the exchange's zone and its opening time, as above. If the backend stamps a daily bar at midnight of its trading date instead, use { unit: "day" } (UTC) or { unit: "day", zone } with the default at, whichever the backend's midnight is.
Weekly bars
A week is a fixed 604,800,000 ms, so weekly bars fit the grid when every bar is stamped on the same weekday at the same UTC time. The offset puts slot 0 on that weekday. The epoch, 1 January 1970, was a Thursday, so Monday at 00:00 UTC is an offset of 4 days, 345,600,000 ms (Sunday is 3 days, 259,200,000 ms):
const DAY = 86_400_000;
const WEEK = 7 * DAY; // 604_800_000
// 1 January 1970, the epoch, was a Thursday. The first Monday, 5 January
// 1970, is 4 days later, so an offset of 4 days puts every slot on a
// Monday at 00:00 UTC (for Sunday weeks, 3 days).
const MONDAY = 4 * DAY; // 345_600_000
const weekly = await client.cache({
id: "candles/ACME/1w",
interval: WEEK,
alignmentOffset: MONDAY,
fields: { close: "f64" },
});
await weekly.put({
timestamps: [Date.UTC(2026, 10, 2), Date.UTC(2026, 10, 9)], // two Mondays
fields: { close: [99, 103] },
});A week that starts at local midnight in a zone, and so moves by an hour twice a year, is a calendar week: { unit: "week", zone: "Europe/London" }, with weekStart for a week that does not start on Monday. A fixed week and a calendar week number their slots differently, so they are two caches.
Monthly, quarterly and yearly bars
Months differ in length, so no millisecond interval holds them. A calendar interval does:
// One slot per calendar month, starting at 00:00 UTC of its first day.
const monthly = await client.cache({
id: "candles/ACME/1mo",
interval: { unit: "month" },
fields: { close: "f64" },
});
await monthly.put({
timestamps: [Date.UTC(2026, 8, 1), Date.UTC(2026, 9, 1)], // September, October
fields: { close: [97, 104] },
});
// Ranges are instants too, snapped outward as on any grid: mid-January
// reads from January's start.
const months = await monthly.get(
{ start: Date.UTC(2026, 0, 15), end: Date.UTC(2026, 11, 1) },
{ cacheOnly: true },
);
console.log([...months.timestamps].map((t) => new Date(t).toISOString()));Quarters and years work the same way (unit: "quarter", unit: "year"), and each takes a zone and an at like a day does.
A calendar the units do not cover
For a schedule none of the units describes (fiscal periods of four or five weeks, say), map each period to an ordinal in the fetcher: store period n at timestamp n on a fixed grid of interval 1, and convert back when drawing. Every range, watermark and timestamp of such a cache is then an ordinal, so the fetcher converts the requested ordinals to the periods it asks the backend for.
Jittered timestamps
Samples that arrive near, but not on, a fixed step are moved to the slot they belong to in the fetcher. For a metric scraped every minute by a job that only ever runs late (12:00:02, 12:01:03, 12:01:58), flooring, Math.floor(t / interval) * interval with offset 0, assigns each sample to the slot that starts at or before it. Jitter on both sides of the step (12:00:58 meant for 12:01) needs rounding to the nearest slot instead, Math.round(t / interval) * interval, since flooring would move such a sample into the slot before. Either way two samples can land in one slot. A batch's timestamps must be unique, so decide which one the slot keeps (the last, a mean, a sum) before the put:
const MINUTE = 60_000;
// In the fetcher: move each sample to the slot it belongs to, then keep one
// sample per slot (here the last; a mean or a sum are other choices), since
// a batch's timestamps must be unique and ascending.
function toGrid(
samples: { t: number; value: number }[], // ascending by t
interval: number,
): { timestamps: number[]; values: number[] } {
const timestamps: number[] = [];
const values: number[] = [];
for (const { t, value } of samples) {
const slot = Math.floor(t / interval) * interval; // alignmentOffset 0
if (timestamps.at(-1) === slot) values[values.length - 1] = value;
else {
timestamps.push(slot);
values.push(value);
}
}
return { timestamps, values };
}
const gauge = await client.cache({
id: "metrics/cpu/1m",
interval: MINUTE,
fields: { load: "f32" },
});
// A scraper that only ever runs late: 12:00:02, 12:01:03 and 12:01:58 (a
// repeated scrape). The last two share the 12:01 slot, which keeps 0.25.
const { timestamps, values } = toGrid(
[
{ t: Date.UTC(2026, 10, 10, 12, 0, 2), value: 0.5 },
{ t: Date.UTC(2026, 10, 10, 12, 1, 3), value: 0.75 },
{ t: Date.UTC(2026, 10, 10, 12, 1, 58), value: 0.25 },
],
MINUTE,
);
await gauge.put({ timestamps, fields: { load: values } });Ranges
A Range is { start, end } in milliseconds, inclusive at both ends. That is how chart libraries report a visible range (Lightweight Charts' setVisibleRange, uPlot's scale min/max), so a chart's range feeds get directly.
| Call | Range on the grid? | What happens |
|---|---|---|
get, invalidate | need not be | snapped outward: start down to the slot at or before it, end up to the slot at or after it; reading or forgetting a little more is safe |
put's options.range | need not be | snapped inward: it claims the grid points inside it and no others, since claiming a slot nobody named would record a confirmed gap |
put's timestamps | must be | a timestamp off the grid rejects the whole batch with PutError 'misaligned' |
start === end is legal: one slot when it is aligned, two when it is not (outward snap). A range whose endpoints are not finite numbers within ±(2^53 − 1), or with start > end, rejects with InvalidRangeError; so does a range outside a calendar interval's years 1 to 9999. The coverage and misses of a result are on the grid, in milliseconds.
// Not on the grid: read outward, from the open at or before the start to
// the open at or after the end.
const week = await daily.get({
start: Date.UTC(2026, 9, 26, 9, 0),
end: Date.UTC(2026, 9, 30, 9, 0),
});
console.log(week.misses[0]?.range); // 25 Oct 14:30 UTC to 30 Oct 14:30 UTCReading up to now
An end of Date.now() snaps outward to the next grid point, whose bar does not exist yet, so the read asks the backend for it. A fetcher that reports meta.finalizedUntil keeps that slot provisional; without a watermark, the empty slot would be recorded as a confirmed gap. Floor the end to the grid when you read up to the present: Math.floor(now / interval) * interval with a fixed interval and offset 0, or slotStart(now, interval) from @anishbenji/tscache/grid for any interval.
Batches
A put batch (and a fetcher's response) is validated as a whole before anything changes:
fieldsnames exactly the cache's fields, each anumber[]or a typed array ('field-mismatch');- every field has as many values as there are timestamps (
'length-mismatch'); - timestamps are aligned, strictly ascending and unique; the first offender is reported with its index and value (
'misaligned','unsorted','duplicate'); - with
options.range, every timestamp lies inside it ('range-mismatch'); - every value is one its field can hold (
'field-mismatch', naming the field, index, timestamp and value; the error'soffenderFieldis the field).
What each field accepts:
- Every field rejects a value that is not a number:
null,undefined, a hole in an array, a string ("1"included), a boolean, a bigint or an object. - Float fields (
f64,f32) accept every number,NaN,Infinityand-Infinityincluded.f32stores the nearest float32. - Integer fields (
i32,u32,i16,u16,i8,u8) accept only an integer inside the dtype's range (-0and an integral float such as2.0included), stored as given. A fraction (1.5for ani32field),NaN,±Infinityand a value outside the range (300or-1foru8) are rejected: round in the fetcher if the backend sends fractions for an integer field.
Nothing is coerced: a null from a JSON backend is rejected, not stored as 0. A typed array of the field's own dtype is accepted as it is, without a scan. An empty batch is legal: with a range it states that the range holds no points.
A JSON backend has no NaN, so it usually sends null for a missing value. Map null to NaN for float fields in the fetcher (Fetchers); a response that still holds one is rejected, and its range is reported 'fetch-failed'.
try {
await cache.put({
timestamps: [t0 + 30_000],
fields: { temperature: [20], errors: [0] },
});
} catch (error) {
if (error instanceof PutError) {
console.log(error.code, error.offenderIndex, error.expected);
// "misaligned" 0 "t ≡ 0 (mod 60000)"
}
}