@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
132 lines (130 loc) • 5.87 kB
JavaScript
import { Temporal } from "./temporal.js";
import { isNamedTimeZone } from "./zonedWallClock.js";
import { zonedNextTransition } from "./zonedWallClockOperations.js";
import { NANOSECONDS_PER_DAY } from "./foreignEpochs.js";
// src/internal/offsetTimeline.ts
/**
* The longest span one transition search covers: three 366-day years, the horizon the
* polyfill-compatible search keeps with or without a limit (TC39 `GetNamedTimeZoneNextTransition`
* leaves the horizon to the implementation). A search that passes `until` inside it is exact.
*/
const MAX_SEARCH_SPAN = 3n * 366n * NANOSECONDS_PER_DAY;
function offsetTimeline(frame, fromNs, toNs, maxChanges) {
const first = new Temporal.ZonedDateTime(fromNs, frame.timeZone);
const timeline = {
frame,
fromNs,
toNs: fromNs,
changes: [],
offsets: [BigInt(first.offsetNanoseconds) + frame.shiftNanoseconds]
};
return extendTimeline(timeline, toNs, maxChanges) ? timeline : null;
}
/**
* Grow `timeline` so that it is complete up to `toNs`, one search of at most three years at a
* time, or one search per change up to `toNs` under `maxChanges`. Each search passes `until`,
* so it never stops at a clock-based horizon short of it. Returns `false` once the timeline holds
* more than `maxChanges` changes.
*/
function extendTimeline(timeline, toNs, maxChanges = Infinity) {
const { frame } = timeline;
if (toNs <= timeline.toNs) return true;
if (maxChanges !== Infinity) {
let at = new Temporal.ZonedDateTime(timeline.toNs, frame.timeZone);
for (;;) {
const next = zonedNextTransition(at, toNs);
if (next === null) break;
timeline.changes.push(next.epochNanoseconds);
timeline.offsets.push(BigInt(next.offsetNanoseconds) + frame.shiftNanoseconds);
at = next;
if (timeline.changes.length > maxChanges) return false;
}
} else if (isNamedTimeZone(frame.timeZone)) {
let chunkStart = timeline.toNs;
while (chunkStart < toNs) {
const chunkEnd = toNs - chunkStart > MAX_SEARCH_SPAN ? chunkStart + MAX_SEARCH_SPAN : toNs;
let at = new Temporal.ZonedDateTime(chunkStart, frame.timeZone);
for (;;) {
const next = zonedNextTransition(at, chunkEnd);
if (next === null) break;
timeline.changes.push(next.epochNanoseconds);
timeline.offsets.push(BigInt(next.offsetNanoseconds) + frame.shiftNanoseconds);
at = next;
}
chunkStart = chunkEnd;
}
}
timeline.toNs = toNs;
return true;
}
/**
* Make `timeline` complete up to `ns`, reading ahead so a walk that advances a day at a time
* searches in chunks: it grows by at least its own length, up to three years.
*/
function ensureTimeline(timeline, ns) {
if (ns <= timeline.toNs) return;
const length = timeline.toNs - timeline.fromNs;
const step = length > MAX_SEARCH_SPAN ? MAX_SEARCH_SPAN : length;
extendTimeline(timeline, ns > timeline.toNs + step ? ns : timeline.toNs + step);
}
/** Index of the stretch holding `ns`: the number of changes at or before it. */
function stretchAt(timeline, ns) {
const { changes } = timeline;
let low = 0;
let high = changes.length;
while (low < high) {
const middle = low + high >>> 1;
if (changes[middle] <= ns) low = middle + 1;
else high = middle;
}
return low;
}
/** The offset in force at `ns`, in nanoseconds east of UTC. */
function offsetOf(timeline, ns) {
return timeline.offsets[stretchAt(timeline, ns)];
}
/**
* TC39 `GetPossibleEpochNanoseconds`, read from the timeline: the instants whose wall clock is
* `wallNs` (the wall clock read as UTC), ascending. A stretch contributes `wallNs - offset` when
* that instant lies in the stretch; offsets differ by well under two days, so only the stretches
* within two days either side can.
*/
function possibleAt(timeline, wallNs) {
const { changes, offsets } = timeline;
const last = stretchAt(timeline, wallNs + 2n * NANOSECONDS_PER_DAY);
const possible = [];
for (let index = stretchAt(timeline, wallNs - 2n * NANOSECONDS_PER_DAY); index <= last; index++) {
const instant = wallNs - offsets[index];
if ((index === 0 || instant >= changes[index - 1]) && (index === changes.length || instant < changes[index])) possible.push(instant);
}
return possible;
}
/**
* TC39 `GetEpochNanosecondsFor`: the instant a wall clock names, by
* `DisambiguatePossibleEpochNanoseconds`. One candidate is the answer; two give the first
* (`"compatible"`, `"earlier"`) or last (`"later"`); none moves the wall clock by the offset
* change, back for `"earlier"` and forward otherwise. `undefined` where `"reject"` refuses.
* Throws `RangeError` where a moved wall clock names nothing, which no zone's data produces.
*/
function resolveWallNs(timeline, wallNs, disambiguation) {
const possible = possibleAt(timeline, wallNs);
if (possible.length === 1) return possible[0];
if (disambiguation === "reject") return;
if (possible.length > 1) return disambiguation === "later" ? possible[possible.length - 1] : possible[0];
const change = offsetOf(timeline, wallNs + NANOSECONDS_PER_DAY) - offsetOf(timeline, wallNs - NANOSECONDS_PER_DAY);
const shifted = possibleAt(timeline, disambiguation === "earlier" ? wallNs - change : wallNs + change);
if (shifted.length === 0) throw new RangeError("the wall clock cannot be resolved");
return disambiguation === "earlier" ? shifted[0] : shifted[shifted.length - 1];
}
/**
* TC39 `GetStartOfDay`: the first instant of the local date `epochDay` days from 1970-01-01 -
* its first candidate for midnight, else (midnight skipped) the change that ends the gap.
*/
function startOfDayNs(timeline, epochDay) {
const midnight = BigInt(epochDay) * NANOSECONDS_PER_DAY;
const possible = possibleAt(timeline, midnight);
if (possible.length > 0) return possible[0];
const { changes } = timeline;
return changes[stretchAt(timeline, midnight - NANOSECONDS_PER_DAY)];
}
export { ensureTimeline, offsetOf, offsetTimeline, possibleAt, resolveWallNs, startOfDayNs };