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.

89 lines (87 loc) • 5.02 kB
import { Temporal } from "../temporal.js"; import { isNudgeWindowCompatNeeded } from "../temporalCompat/capabilities.js"; import { isObject } from "../isObject.js"; import { differencePlainDateTimeWithRounding, isDateUnit, largerUnit, optionsAreValid, roundAtLimit, roundWithPlainRelativeTo, roundingSettings, temporalDurationFromInternal, timeZoneAnnotation, totalAtLimit, totalWithPlainRelativeTo } from "./operations.js"; // src/internal/zonedDifference/nudgeWindow.ts const PLAIN_DATE_TIME_PROBE = new Temporal.PlainDateTime(2e3, 1, 1); /** * Units whose nudge window can miss its target: only a month, or a year that carries months with it. * A week or a day is a fixed count of days, so `relativeTo + r1 units` never has to constrain. */ function isNudgeWindowUnit(unit) { return unit === "month" || unit === "year"; } /** * The day of month a `relativeTo` resolves to, or 0 when there is none to read. * * Only the 29th, 30th and 31st can reach defect 4: below that, adding a month never constrains * the day, so the two denominators coincide and the polyfill's answer is already the spec's. */ function relativeToDayOfMonth(relativeTo, zoned) { if (zoned !== null) return zoned.day; try { if (relativeTo instanceof Temporal.PlainDateTime) return relativeTo.day; if (relativeTo instanceof Temporal.PlainDate) return relativeTo.day; if (typeof relativeTo === "string") return Temporal.PlainDate.from(relativeTo).day; if (isObject(relativeTo)) return Temporal.PlainDate.from(relativeTo).day; } catch { return 0; } return 0; } /** The plain `relativeTo` Temporal would resolve, whatever its calendar, or null. */ function plainRelativeTo(relativeTo) { try { if (relativeTo instanceof Temporal.PlainDate) return relativeTo; if (relativeTo instanceof Temporal.PlainDateTime) return relativeTo.toPlainDate(); if (typeof relativeTo === "string" && !timeZoneAnnotation.test(relativeTo)) return Temporal.PlainDate.from(relativeTo); if (isObject(relativeTo) && !(relativeTo instanceof Temporal.ZonedDateTime)) return Temporal.PlainDate.from(relativeTo); } catch { return null; } return null; } /** * Defect 4: `Duration#total` by the spec when the polyfill would divide the month fraction by the * wrong month. Returns null whenever the defect cannot apply, leaving the polyfill's own answer. */ function monthTotalBySpec(duration, unitOption, unit, relativeTo, zoned) { if (!isNudgeWindowUnit(unit) || relativeToDayOfMonth(relativeTo, zoned) < 29 || !isNudgeWindowCompatNeeded()) return null; if (zoned !== null) return totalAtLimit(duration, zoned, unitOption, unit); const plain = plainRelativeTo(relativeTo); return plain === null ? null : totalWithPlainRelativeTo(duration, plain, unitOption, unit); } /** * Defect 4: `Duration#round` by the spec when the polyfill would round over a nudge window that * does not contain the target. Returns null whenever the defect cannot apply, leaving the * polyfill's own answer. */ function monthRoundBySpec(duration, roundTo, settings, zoned) { if (!isNudgeWindowUnit(settings.smallestUnit) || relativeToDayOfMonth(roundTo.relativeTo, zoned) < 29 || !isNudgeWindowCompatNeeded()) return null; if (zoned !== null) return roundAtLimit(duration, zoned, roundTo, settings); const plain = plainRelativeTo(roundTo.relativeTo); return plain === null ? null : roundWithPlainRelativeTo(duration, plain, roundTo, settings); } /** * `PlainDateTime#until` / `#since`, correct when the polyfill would round over a nudge window that * does not contain the target (defect 4). * * The plain difference functions round through the polyfill directly, because neither the range * limits nor the non-ISO calendars reach them the way they reach the zoned ones. A calendar * `smallestUnit` measured from a day that a month add would constrain is the one case that does, so * it takes the spec path and everything else stays the polyfill's own call. * * @param one the earlier plain datetime * @param two the later plain datetime * @param options Temporal difference options, passed through unchanged when the defect cannot apply * @returns the difference; throws where TC39 Temporal throws */ function plainUntilWithRounding(one, two, options = {}) { const settings = roundingSettings(options.largestUnit, options.smallestUnit, (smallestUnit) => largerUnit("hour", smallestUnit), options.roundingIncrement, options.roundingMode ?? "trunc"); if (settings !== null && isNudgeWindowUnit(settings.smallestUnit) && one.day >= 29 && one.calendarId === two.calendarId && Temporal.PlainDateTime.compare(one, two) <= 0 && isNudgeWindowCompatNeeded() && optionsAreValid(() => PLAIN_DATE_TIME_PROBE.until(PLAIN_DATE_TIME_PROBE, options))) { const rounded = differencePlainDateTimeWithRounding(one, two, one.calendarId, settings); return temporalDurationFromInternal(rounded, isDateUnit(settings.largestUnit) ? "hour" : settings.largestUnit); } return one.until(two, options); } export { isNudgeWindowUnit, monthRoundBySpec, monthTotalBySpec, plainUntilWithRounding };