UNPKG

@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.

66 lines • 4.74 kB
import { UnixUnit } from "../validate/isValidUnixUnit.js"; /** * 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 */ export declare function intervalOverlappingDaysUnix(aStart: number | string, aEnd: number | string, bStart: number | string, bEnd: number | string, options?: { /** * The unit the epoch values are counted in: `"seconds"` or `"milliseconds"`, singular or * plural. Any other value returns `null`. * * @defaultValue `"milliseconds"` */ epochUnit?: UnixUnit; /** * The time zone the calendar dates are counted in: an IANA name, a UTC offset (a time zone * identifier such as `+05:30`, `+0530` or `-08`, or a stored offset `±HH:MM[:SS]`, what * `getTimeZoneOffset` returns), or `"local"` for the system time zone. An unknown zone returns * `null`. * * @defaultValue `"UTC"` */ timeZone?: string; }): number | null;