@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.
58 lines (56 loc) • 3.07 kB
JavaScript
import { zonedNextTransition } from "./zonedWallClockOperations.js";
// src/internal/zoneBucketCap.ts
/**
* A bounded walk over a zone's local buckets (hours, days, weeks, months) can refuse a span that is
* certainly too long without stepping through it. `bucketRange` refuses a range past 10,000
* buckets this way, and the free-time walk a dwell past 10,000 local days. `false` always means
* "not certain": the caller walks, exactly as it would without this check, so no answer changes.
*/
const NANOSECONDS_PER_HOUR = 3600000000000n;
/**
* The longest wall-clock stretch of each unit. Any wall-clock interval this long holds a unit
* start: an hour holds an hour start, 24 hours a midnight, 7 days a Monday, 31 days a first of
* the month.
*/
const LONGEST_UNIT_NANOSECONDS = {
hour: NANOSECONDS_PER_HOUR,
day: 24n * NANOSECONDS_PER_HOUR,
week: 168n * NANOSECONDS_PER_HOUR,
month: 744n * NANOSECONDS_PER_HOUR
};
/** Offset transitions read before the pre-count gives up and leaves the answer to the walk. */
const MAX_PRECOUNT_TRANSITIONS = 4096;
/**
* True when `[start, end)` certainly touches more than `max` buckets of `unit`, decided from the span
* instead of stepping bucket by bucket (which costs milliseconds a step in some zones).
*
* Over a prefix of the range, the local wall clock runs continuously between offset transitions.
* Those stretches cover at least the prefix's length less every backward offset change, in at most
* one stretch more than there are transitions, and each stretch of wall clock holds at least
* `floor(length / LONGEST_UNIT_NANOSECONDS)` distinct unit starts, each the start of its own
* bucket before `end`. The count subtracts two per stretch for the partial units at its ends.
* The bound also holds for a count of distinct local dates, which the free-time walk keeps by
* folding a day that re-enters an earlier date: a date can re-enter only at an offset transition,
* where the two units dropped per stretch already sit, and the starts counted inside one stretch
* fall on strictly increasing dates.
* `false` means "not certain": the walk decides, exactly as before.
*/
function certainlyTouchesMoreBuckets(startZoned, endZoned, unit, max) {
const unitNs = LONGEST_UNIT_NANOSECONDS[unit];
const startNs = startZoned.epochNanoseconds;
const prefixEndNs = [endZoned.epochNanoseconds, startNs + 2n * BigInt(max + 1) * unitNs].reduce((a, b) => a < b ? a : b);
if (prefixEndNs - startNs <= BigInt(max) * unitNs) return false;
let backwardNs = 0n;
let transitions = 0;
try {
for (let current = startZoned, next = zonedNextTransition(current, prefixEndNs - 1n); next !== null; current = next, next = zonedNextTransition(current, prefixEndNs - 1n)) {
if (++transitions > MAX_PRECOUNT_TRANSITIONS) return false;
const shift = BigInt(current.offsetNanoseconds - next.offsetNanoseconds);
backwardNs += shift > 0n ? shift : 0n;
}
} catch {
return false;
}
return (prefixEndNs - startNs - backwardNs) / unitNs - 2n * BigInt(transitions + 1) > BigInt(max);
}
export { certainlyTouchesMoreBuckets };