@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.
75 lines (73 loc) • 4.43 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { calendarDateUntil } from "../../internal/temporalCompat/calendarDateArithmetic.js";
import { parseCalendarDatePairForArithmetic } from "../../internal/calendarDatePairPolicy.js";
import { getUnitSpan } from "../../internal/intervalCountHelpers.js";
import { resolveDateTimeUnit } from "../../internal/resolveDateTimeUnit.js";
import { isValidCalendarDate } from "../validate/isValidCalendarDate.js";
import { isValidDateUnit } from "../validate/isValidDateUnit.js";
import { getStartOfDateUnit, getStartOfNextDateUnit } from "../../internal/dateUnitHelpers.js";
// src/plain/interval/intervalCountDate.ts
/** The start of the date `unit` holding `source`, or null when it lies before the representable range. */
function startOfUnitIfRepresentable(source, unit) {
try {
return getStartOfDateUnit(source, unit);
} catch {
return null;
}
}
/**
* Count how many `unit` boundaries a date interval crosses.
*
* - Counts calendar boundaries touched by the half-open interval `[start, end)` — distinct
* from `diffDate`, which measures exact elapsed duration.
* - `"2024-01-01"` to `"2024-01-03"` counted in days is 2: the end boundary is excluded.
* - A zero-length interval (`start === end`) returns `0`: the empty `[start, start)` holds no instant,
* so it touches no unit (before 1.16.0 it counted 1 when mid-unit).
* - Weeks start on Monday (ISO 8601).
* - A unit that began before the first representable date (`-271821-04-19`) is still counted:
* whole units are measured from the unit after `start`'s, never from `start`'s own start.
* - Accepts singular or plural units (`"day"` and `"days"` behave identically).
* - Returns `null` on invalid input (unparseable start/end, `start > end`, unsupported unit,
* or a unit that has no effect on `PlainDate`, e.g. `"hours"`).
* - Accepts RFC 9557 calendar-annotated PlainDate strings — E5 (issue #78). When `start` and `end`
* carry the *same* calendar tag, boundaries are counted in that calendar (a Hebrew leap year
* crosses 13 month boundaries, not 12). When they name different calendars (a bare ISO string
* names `iso8601`) the result is `null`, as Temporal's `until` throws when `CalendarEquals` is
* false (before 1.16.0 it counted in ISO).
* - Compatibility: since 1.16.0 calendar strings are RFC 9557 (ISO digits, `[u-ca=<id>]`, canonical
* calendar ids); see `isValidCalendarDate`.
*
* @param start ISO PlainDate string for the interval start, optionally calendar-annotated
* @param end ISO PlainDate string for the interval end, optionally calendar-annotated
* @param unit unit string — `"year" | "month" | "week" | "day"` (time units return null)
* @returns number of unit boundaries touched, or null on invalid input
*
* @example intervalCountDate("2024-01-01", "2024-01-03", "day") // 2
* @example intervalCountDate("2024-01-15", "2024-03-10", "month") // 3
* @example intervalCountDate("2024-01-15", "2024-01-15", "month") // 0 (zero-length: holds no instant)
* @example intervalCountDate("2024-01-01", "2024-01-10", "hour") // null
* @example intervalCountDate("invalid", "2024-01-10", "day") // null
* @example intervalCountDate("2023-09-16[u-ca=hebrew]", "2024-10-03[u-ca=hebrew]", "month") // 13 (Hebrew leap year, measured in Hebrew)
* @example intervalCountDate("2024-10-03[u-ca=hebrew]", "2024-11-03", "month") // null (different calendars)
*/
function intervalCountDate(start, end, unit) {
if (typeof start !== "string" || typeof end !== "string") return null;
if (!isValidCalendarDate(start) || !isValidCalendarDate(end)) return null;
if (typeof unit !== "string") return null;
const resolvedUnit = resolveDateTimeUnit(unit);
if (!isValidDateUnit(resolvedUnit)) return null;
try {
const { a: startVal, b: endVal } = parseCalendarDatePairForArithmetic(start, end);
const order = Temporal.PlainDate.compare(startVal, endVal);
if (order > 0) return null;
if (order === 0) return 0;
const startOfEnd = startOfUnitIfRepresentable(endVal, resolvedUnit);
if (startOfEnd === null) return 1;
const endBucket = startOfEnd.equals(endVal) ? 0 : 1;
if (Temporal.PlainDate.compare(startOfEnd, startVal) <= 0) return endBucket;
return 1 + getUnitSpan(Temporal.Duration.from(calendarDateUntil(getStartOfNextDateUnit(startVal, resolvedUnit), startOfEnd, resolvedUnit)), resolvedUnit) + endBucket;
} catch {
return null;
}
}
export { intervalCountDate };