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.

96 lines (94 loc) • 4.48 kB
import { Temporal } from "../../internal/temporal.js"; import { plainDateTimeOf } from "../../internal/inputChecks.js"; import { getUnitSpan } from "../../internal/intervalCountHelpers.js"; import { isDateTimeUnitName, isDateUnitName } from "../../internal/unitNames.js"; import { resolveDateTimeUnit } from "../../internal/resolveDateTimeUnit.js"; import { getStartOfDateTimeUnit, getStartOfNextDateTimeUnit } from "../../internal/dateTimeUnitHelpers.js"; // src/plain/interval/intervalCountDateTime.ts /** One of each time unit, for stepping to the next time-unit bucket. */ const ONE_TIME_UNIT = { hour: { hours: 1 }, minute: { minutes: 1 }, second: { seconds: 1 }, millisecond: { milliseconds: 1 }, microsecond: { microseconds: 1 }, nanosecond: { nanoseconds: 1 } }; /** * Return the start of `unit` for a `Temporal.PlainDateTime`, for every DateTimeUnit, or null when * it lies before the representable range (the first PlainDateTime is * `-271821-04-19T00:00:00.000000001`, so even that date's midnight does not exist). * * Date units delegate to the shared helper; time units truncate via `round`. */ function startOfUnitIfRepresentable(source, unit) { try { if (isDateUnitName(unit)) return getStartOfDateTimeUnit(source, unit); return source.round({ smallestUnit: unit, roundingMode: "trunc" }); } catch { return null; } } /** * Return the start of the `unit` after the one holding `source`: step first, truncate after, so * `source`'s own start is never materialised. */ function startOfNextUnit(source, unit) { if (isDateUnitName(unit)) return getStartOfNextDateTimeUnit(source, unit); return source.add(ONE_TIME_UNIT[unit] ?? { nanoseconds: 1 }).round({ smallestUnit: unit, roundingMode: "trunc" }); } /** * Count how many `unit` boundaries a date-time interval crosses. * * - Counts calendar boundaries touched by the half-open interval `[start, end)` — distinct * from `diffDateTime`, which measures exact elapsed duration. An interval from 23:59 to * 00:01 is two minutes long but touches 2 day boundaries. * - The end boundary is excluded: `"2024-01-01T00:00:00"` to `"2024-01-03T00:00:00"` counts 2 days. * - 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 PlainDateTime * (`-271821-04-19T00:00:00.000000001`) 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). * * @param start ISO PlainDateTime string for the interval start * @param end ISO PlainDateTime string for the interval end * @param unit unit string — any `DateTimeUnit` * @returns number of unit boundaries touched, or null on invalid input * * @example intervalCountDateTime("2024-01-01T23:59:00", "2024-01-02T00:01:00", "day") // 2 * @example intervalCountDateTime("2024-01-01T00:00:00", "2024-01-03T00:00:00", "day") // 2 * @example intervalCountDateTime("2024-01-01T10:30:00", "2024-01-01T12:00:00", "hour") // 2 * @example intervalCountDateTime("2024-01-01T05:00:00", "2024-01-01T05:00:00", "day") // 0 (zero-length: holds no instant) * @example intervalCountDateTime("invalid", "2024-01-02T00:00:00", "day") // null */ function intervalCountDateTime(start, end, unit) { if (typeof start !== "string" || typeof end !== "string") return null; const startVal = plainDateTimeOf(start); const endVal = plainDateTimeOf(end); if (startVal === null || endVal === null) return null; if (typeof unit !== "string") return null; const resolvedUnit = resolveDateTimeUnit(unit); if (!isDateTimeUnitName(resolvedUnit)) return null; try { const order = Temporal.PlainDateTime.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.PlainDateTime.compare(startOfEnd, startVal) <= 0) return endBucket; return 1 + getUnitSpan(startOfNextUnit(startVal, resolvedUnit).until(startOfEnd, { largestUnit: resolvedUnit }), resolvedUnit) + endBucket; } catch { return null; } } export { intervalCountDateTime };