@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.
88 lines (86 loc) • 5.49 kB
JavaScript
import { isOptionsArgument } from "../../internal/isObject.js";
import { frameZoned, normalizeZoneFrame } from "../../internal/zoneFrame.js";
import { halfOpenIntersection } from "../../internal/halfOpenIntervals.js";
import { parseUnixEpochInterval, resolveUnixEpochUnit, unixEpochToInstant } from "../../internal/unixEpochValue.js";
import { countZonedLocalDates } from "../../internal/zonedLocalDates.js";
// src/unix/interval/intervalOverlappingDaysUnix.ts
/**
* Return how many distinct calendar dates two Unix epoch intervals share, in a given
* time zone.
*
* - Counts the local dates that hold at least one instant of the half-open intersection
* `[max(aStart, bStart), min(aEnd, bEnd))`. The end is excluded, so an intersection ending at
* local midnight does not touch the date that midnight starts.
* - Each instant in the intersection contributes its own local date in `timeZone`, and each date
* counts once. A date the zone deleted is not counted (`Pacific/Apia` skipped 2011-12-30), and a
* fall-back into the previous date does not lose that date (`America/Goose_Bay`, 2010-11-07).
* - Returns `null` when the intersection crosses more than 10,000 zone transitions (the same cap
* as `intervalCountZoned`).
* - Compatibility: before 1.16.0 the count was the calendar difference of the two endpoint dates
* plus one. To get that number for overlapping intervals, with `start`/`end` the intersection:
* `diffDate(convertUnixToPlainDate(start, { timeZone }), convertUnixToPlainDate(end, { timeZone }), "days") + 1`.
* - Returns `0` when the intersection is empty (a well-defined answer, not invalid input): when the
* intervals are disjoint, when they touch (e.g. `aEnd === bStart`), or when either is empty.
* - Returns `null` if either interval is invalid (`start > end`).
* - Returns `null` on invalid input (epoch values that are not safe integers or numeric strings of
* one — fractions, empty strings and values beyond ±(2^53 − 1) are invalid — or an invalid
* timeZone).
* - Diverges from date-fns's `getOverlappingDaysInIntervals`, which rounds up elapsed
* 24-hour periods instead of counting calendar dates. To reproduce date-fns's number,
* compose `intervalIntersectionUnix` with `intervalCountUnix`:
* `const span = intervalIntersectionUnix(aStart, aEnd, bStart, bEnd); span ? intervalCountUnix(span.start, span.end, "day") : 0;`
* - **Limit at an offset with seconds.** The instant is placed in the offset's whole-minute zone,
* moved by its seconds, and the moved instant must be inside Temporal's range. So within the
* offset's seconds (under a minute) of the last instant Temporal supports
* (`+275760-09-13T00:00:00Z`) for an offset east of UTC, or of the first
* (`-271821-04-20T00:00:00Z`) for one west, this returns `null`. An offset to the minute has no
* such limit.
*
* @param aStart Unix epoch value (seconds or milliseconds) — first interval start
* @param aEnd Unix epoch value (seconds or milliseconds) — first interval end
* @param bStart Unix epoch value (seconds or milliseconds) — second interval start
* @param bEnd Unix epoch value (seconds or milliseconds) — second interval end
* @param options optional: how the epochs are read and the zone the dates are counted in
* @returns number of shared calendar dates, `0` when disjoint, or null on invalid input
*
* @example intervalOverlappingDaysUnix(0, 172800000, 86400000, 259200000, { timeZone: "UTC" }) // 1 (the intersection [86400000, 172800000) is 1970-01-02)
* @example intervalOverlappingDaysUnix(0, 172800001, 86400000, 259200000, { timeZone: "UTC" }) // 2
* @example intervalOverlappingDaysUnix(0, 86400000, 86400000, 172800000, { timeZone: "UTC" }) // 0 (touching: no shared instant)
* @example intervalOverlappingDaysUnix(0, 86400000, 172800000, 259200000, { timeZone: "UTC" }) // 0 (disjoint)
* @example intervalOverlappingDaysUnix(1289098830000, 1289100600000, 1289098830000, 1289100600000, { timeZone: "America/Goose_Bay" }) // 2 (the clock fell back into 2010-11-06)
* @example diffDate(convertUnixToPlainDate(1289098830000, { timeZone: "America/Goose_Bay" }), convertUnixToPlainDate(1289100600000, { timeZone: "America/Goose_Bay" }), "days") + 1 // 0 (pre-1.16.0 count)
* @example intervalOverlappingDaysUnix(NaN, 172800000, 86400000, 259200000, { timeZone: "UTC" }) // null
*/
function intervalOverlappingDaysUnix(aStart, aEnd, bStart, bEnd, options) {
try {
if (!isOptionsArgument(options)) return null;
const a = parseUnixEpochInterval(aStart, aEnd);
const b = parseUnixEpochInterval(bStart, bEnd);
if (a === null || b === null) return null;
const { start: a1, end: a2 } = a;
const { start: b1, end: b2 } = b;
const epochUnit = resolveUnixEpochUnit(options?.epochUnit);
const frame = normalizeZoneFrame(options?.timeZone);
if (frame === null || epochUnit === null) return null;
const overlap = halfOpenIntersection({
start: a1,
end: a2
}, {
start: b1,
end: b2
}, (left, right) => left - right);
if (overlap === null) return 0;
if (overlap.start === overlap.end) return 0;
const first = unixEpochToInstant(overlap.start, epochUnit);
const afterLast = unixEpochToInstant(overlap.end, epochUnit);
if (first === null || afterLast === null) return null;
try {
return countZonedLocalDates(frameZoned(first, frame), frameZoned(afterLast, frame).subtract({ nanoseconds: 1 }));
} catch {
return null;
}
} catch {
return null;
}
}
export { intervalOverlappingDaysUnix };