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.

53 lines (51 loc) • 3.1 kB
import { isNudgeOriginCompatNeeded } from "../temporalCompat/capabilities.js"; import { isNamedTimeZone, possibleEpochNanoseconds } from "../zonedWallClock.js"; import { atTimeOf, isDateUnit, isoDateTimeFor, roundAtLimit, totalAtLimit } from "./operations.js"; // src/internal/zonedDifference/nudgeOrigin.ts /** * True when a length from `zoned` must take the spec path (defect 5): the runtime would mismeasure * it, or the specification has no value for it. `length` is the target's exact time, or a duration * to add to `zoned`. At most four wall-clock resolutions, and no loop. * * - `zoned` is either pass of a repeated wall clock, which the runtime mismeasures. * - `target` is. A forward window's end bound can only fall short of the target when its nominal wall * clock is past the target's and its exact time is before it, so a backward transition lies between * them and the target's wall clock repeats. A date difference that disagrees in sign needs the same. * - `target` is earlier, and the window's end bound is in a gap. Its nominal wall clock is at or * before the target's, and `"compatible"` moves it forward past the target. The bound falls at * the origin's time of day, on the target's date or the one before it, as a gap is at most 24 hours. * * An addition that throws, or a wall clock that cannot be resolved near a range limit, is left to the * range-limit fallbacks. */ function originNeedsSpecPath(zoned, length) { const timeZone = zoned.timeZoneId; if (!isNamedTimeZone(timeZone) || !isNudgeOriginCompatNeeded()) return false; try { const target = typeof length === "bigint" ? length : zoned.add(length).epochNanoseconds; const start = isoDateTimeFor(timeZone, zoned.epochNanoseconds); const end = isoDateTimeFor(timeZone, target); if (possibleEpochNanoseconds(timeZone, start).length > 1 || possibleEpochNanoseconds(timeZone, end).length > 1) return true; if (target >= zoned.epochNanoseconds) return false; const endDate = end.toPlainDate(); return [endDate, endDate.subtract({ days: 1 })].some((date) => possibleEpochNanoseconds(timeZone, atTimeOf(date, start)).length === 0); } catch { return false; } } /** * Defect 5: `Duration#total` by the spec when `originNeedsSpecPath` holds. Time units subtract exact * times, so only a date unit can reach it. Where the specification has no value this throws. */ function originTotalBySpec(duration, unitOption, unit, zoned) { return zoned !== null && isDateUnit(unit) && originNeedsSpecPath(zoned, duration) ? totalAtLimit(duration, zoned, unitOption, unit) : null; } /** * Defect 5: `Duration#round` by the spec when `originNeedsSpecPath` holds. The `largestUnit` decides: * a date one builds a date difference even when `smallestUnit` is an hour. Where the specification * has no value this throws. */ function originRoundBySpec(duration, roundTo, settings, zoned) { return zoned !== null && isDateUnit(settings.largestUnit) && originNeedsSpecPath(zoned, duration) ? roundAtLimit(duration, zoned, roundTo, settings) : null; } export { originNeedsSpecPath, originRoundBySpec, originTotalBySpec };