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.

77 lines (75 loc) • 5.01 kB
import { Temporal } from "../temporal.js"; import { isCalendarArithmeticCompatNeeded } from "../temporalCompat/capabilities.js"; import { durationFrom } from "../durationFrom.js"; import { utcEpochNanoseconds } from "../zonedWallClock.js"; import { NANOSECONDS_PER_DAY, PLAIN_DATE_PROBE, bigintSign, calendarDateUntil, compareAtLimit, dateDurationDays, defaultLargestUnit, isCalendarUnit, largerUnit, plainContext, roundAtLimit, roundRelativeDuration, roundWithPlainRelativeTo, roundingSettings, timeZoneAnnotation, toInternalDuration, totalAtLimit, totalWithPlainRelativeTo } from "./operations.js"; // src/internal/zonedDifference/calendarArithmetic.ts /** * `PlainDate#until(other, options)` with rounding options for two dates in one calendar, as TC39 * `DifferenceTemporalPlainDate` computes it: `CalendarDateUntil` through the Temporal compat layer, * then — unless the granularity is one day — `RoundRelativeDuration` in a plain context. * * @param one the start, in the calendar to measure in * @param two the end, in the same calendar * @param options Temporal `largestUnit` / `smallestUnit` / `roundingIncrement` / `roundingMode` * @returns the rounded date duration; throws where TC39 Temporal throws */ function plainDateUntilWithRounding(one, two, options) { PLAIN_DATE_PROBE.until(PLAIN_DATE_PROBE, options); const settings = roundingSettings(options.largestUnit, options.smallestUnit ?? "day", (smallestUnit) => largerUnit("day", smallestUnit), options.roundingIncrement, options.roundingMode ?? "trunc"); if (Temporal.PlainDate.compare(one, two) === 0) return new Temporal.Duration(); const calendar = one.calendarId; const start = one.withCalendar("iso8601").toPlainDateTime(); const end = two.withCalendar("iso8601").toPlainDateTime(); let duration = { date: calendarDateUntil(calendar, start.toPlainDate(), end.toPlainDate(), settings.largestUnit), time: 0n }; if (settings.smallestUnit !== "day" || settings.increment !== 1) duration = roundRelativeDuration(duration, utcEpochNanoseconds(start), utcEpochNanoseconds(end), plainContext(calendar, start), settings); const { years, months, weeks, days } = duration.date; return new Temporal.Duration(years, months, weeks, days); } /** * The plain `relativeTo` Temporal would resolve (TC39 `ToRelativeTemporalObject` keeps only the * date) when its calendar needs the Temporal compat layer in this runtime, or null. A string with * no calendar annotation is ISO, so it is never parsed here. */ function plainRelativeToNeedingCompat(relativeTo) { let plain; try { if (relativeTo instanceof Temporal.PlainDateTime) plain = relativeTo.toPlainDate(); else if (typeof relativeTo === "string" && relativeTo.includes("u-ca=") && !timeZoneAnnotation.test(relativeTo)) plain = Temporal.PlainDate.from(relativeTo); else return null; } catch { return null; } return isCalendarArithmeticCompatNeeded(plain.calendarId) ? plain : null; } /** `Temporal.Duration.compare` with a plain `relativeTo`, when either operand has calendar units. */ function compareWithPlainRelativeTo(one, two, plain) { if (!isCalendarUnit(defaultLargestUnit(one)) && !isCalendarUnit(defaultLargestUnit(two))) return null; const first = toInternalDuration(one); const second = toInternalDuration(two); const time1 = first.time + BigInt(dateDurationDays(first.date, plain)) * NANOSECONDS_PER_DAY; const time2 = second.time + BigInt(dateDurationDays(second.date, plain)) * NANOSECONDS_PER_DAY; return bigintSign(time1 - time2); } /** Defect 3: `Duration#total` by the spec when the `relativeTo` calendar needs the compat layer. */ function calendarTotalBySpec(duration, unitOption, unit, relativeTo, zoned) { if (zoned !== null) return isCalendarArithmeticCompatNeeded(zoned.calendarId) ? totalAtLimit(duration, zoned, unitOption, unit) : null; const plain = plainRelativeToNeedingCompat(relativeTo); return plain === null ? null : totalWithPlainRelativeTo(duration, plain, unitOption, unit); } /** Defect 3: `Duration#round` by the spec when the `relativeTo` calendar needs the compat layer. */ function calendarRoundBySpec(duration, roundTo, settings, zoned) { if (zoned !== null) return isCalendarArithmeticCompatNeeded(zoned.calendarId) ? roundAtLimit(duration, zoned, roundTo, settings) : null; const plain = plainRelativeToNeedingCompat(roundTo.relativeTo); return plain === null ? null : roundWithPlainRelativeTo(duration, plain, roundTo, settings); } /** Defect 3: `Duration.compare` by the spec when the `relativeTo` calendar needs the compat layer. */ function calendarCompareBySpec(one, two, relativeTo, zoned) { if (zoned !== null) return isCalendarArithmeticCompatNeeded(zoned.calendarId) ? compareAtLimit(durationFrom(one), durationFrom(two), zoned) : null; const plain = plainRelativeToNeedingCompat(relativeTo); return plain === null ? null : compareWithPlainRelativeTo(durationFrom(one), durationFrom(two), plain); } export { calendarCompareBySpec, calendarRoundBySpec, calendarTotalBySpec, plainDateUntilWithRounding };