@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.
64 lines (62 loc) • 2.84 kB
JavaScript
import { Temporal } from "../temporal.js";
import { calendarFieldsOf } from "./calendarFields.js";
import { MAX_ISO_EPOCH_DAYS } from "../epochNanoseconds.js";
// src/internal/temporalCompat/fieldSearch.ts
/** TC39 `ISODateWithinLimits`: -271821-04-19 is one day before -10^8 epoch days. */
const MIN_EPOCH_DAY = -(MAX_ISO_EPOCH_DAYS + 1);
const MAX_EPOCH_DAY = MAX_ISO_EPOCH_DAYS;
/** 2^64 days covers any window; the full range needs 28 halvings. */
const MAX_BISECTION_STEPS = 64;
const UNIX_EPOCH_DATE = new Temporal.PlainDate(1970, 1, 1);
function dateAt(epochDay) {
return UNIX_EPOCH_DATE.add({ days: epochDay });
}
function compareKeys(a, b) {
return a.year - b.year || a.month - b.month || a.day - b.day;
}
function keyAt(calendarId, epochDay) {
return calendarFieldsOf(dateAt(epochDay), calendarId);
}
function epochDayOf(date) {
return UNIX_EPOCH_DATE.until(date.withCalendar("iso8601"), { largestUnit: "days" }).days;
}
/** True when `date` is the last representable date, +275760-09-13. */
function isLastRepresentableDate(date) {
return epochDayOf(date) === MAX_EPOCH_DAY;
}
/**
* The latest in-range ISO date whose `calendarId` key is at or before `target`, or `null` when
* `target` precedes the first representable date. Throws RangeError if the search fails to
* converge (callers return their sentinel).
*/
function floorDateForFields(calendarId, target) {
return floorBetween(calendarId, target, MIN_EPOCH_DAY, MAX_EPOCH_DAY);
}
/**
* `floorDateForFields` within `[from, from + days]` (clamped to the range): `null` when `target`
* precedes `from`, the window's last date when `target` is at or past it.
*/
function floorDateForFieldsBetween(calendarId, target, from, days) {
const low = Math.max(MIN_EPOCH_DAY, epochDayOf(from));
return floorBetween(calendarId, target, low, Math.min(MAX_EPOCH_DAY, low + days));
}
function floorBetween(calendarId, target, lowEpochDay, highEpochDay) {
let low = lowEpochDay;
let high = highEpochDay;
if (compareKeys(keyAt(calendarId, low), target) > 0) return null;
if (compareKeys(keyAt(calendarId, high), target) <= 0) return dateAt(high);
for (let step = 0; step < MAX_BISECTION_STEPS; step++) {
if (high - low <= 1) return dateAt(low);
const middle = Math.floor((low + high) / 2);
if (compareKeys(keyAt(calendarId, middle), target) <= 0) low = middle;
else high = middle;
}
throw new RangeError(`Field search for ${calendarId} did not converge`);
}
/** The ISO date (iso8601) whose `calendarId` fields are exactly `target`, or `null`. */
function isoForFields(calendarId, target) {
const floor = floorDateForFields(calendarId, target);
if (floor === null) return null;
return compareKeys(calendarFieldsOf(floor, calendarId), target) === 0 ? floor : null;
}
export { floorDateForFields, floorDateForFieldsBetween, isLastRepresentableDate, isoForFields };