From 235b938dab1f15884751a15588dfb804457aec9c Mon Sep 17 00:00:00 2001 From: Adam Shaw Date: Sat, 2 Mar 2024 00:30:38 -0500 Subject: [PATCH] partially go back to old Marker system, better for code splitting for PlainDate --- .../src/classApi/duration.ts | 2 +- .../src/internal/bagRefine.ts | 2 +- .../temporal-polyfill/src/internal/compare.ts | 23 ++-- .../temporal-polyfill/src/internal/diff.ts | 62 ++++----- .../src/internal/durationMath.ts | 64 ++++------ .../src/internal/isoParse.ts | 2 +- .../src/internal/markerSystem.ts | 119 ++++++++++++++++++ .../temporal-polyfill/src/internal/move.ts | 47 +++---- .../src/internal/relativeSystem.ts | 50 -------- .../temporal-polyfill/src/internal/round.ts | 80 ++++++------ .../temporal-polyfill/src/internal/slots.ts | 8 +- .../temporal-polyfill/src/internal/total.ts | 60 ++++----- 12 files changed, 268 insertions(+), 251 deletions(-) create mode 100644 packages/temporal-polyfill/src/internal/markerSystem.ts delete mode 100644 packages/temporal-polyfill/src/internal/relativeSystem.ts diff --git a/packages/temporal-polyfill/src/classApi/duration.ts b/packages/temporal-polyfill/src/classApi/duration.ts index a0057bdc..cd34afc9 100644 --- a/packages/temporal-polyfill/src/classApi/duration.ts +++ b/packages/temporal-polyfill/src/classApi/duration.ts @@ -18,12 +18,12 @@ import { DurationBag } from '../internal/fields' import { LocalesArg } from '../internal/intlFormatUtils' import { formatDurationIso } from '../internal/isoFormat' import { parseDuration, parseRelativeToSlots } from '../internal/isoParse' +import { RelativeToSlots } from '../internal/markerSystem' import { DurationRoundOptions, RelativeToOptions, TotalUnitOptionsWithRel, } from '../internal/optionsRefine' -import { RelativeToSlots } from '../internal/relativeSystem' import { BrandingSlots, DurationBranding, diff --git a/packages/temporal-polyfill/src/internal/bagRefine.ts b/packages/temporal-polyfill/src/internal/bagRefine.ts index d2fdd124..47762267 100644 --- a/packages/temporal-polyfill/src/internal/bagRefine.ts +++ b/packages/temporal-polyfill/src/internal/bagRefine.ts @@ -80,6 +80,7 @@ import { isoMonthsInYear, } from './isoMath' import { parseOffsetNano } from './isoParse' +import { RelativeToSlotsNoCalendar } from './markerSystem' import { OffsetDisambig, Overflow } from './options' import { OverflowOptions, @@ -89,7 +90,6 @@ import { refineOverflowOptions, refineZonedFieldOptions, } from './optionsRefine' -import { RelativeToSlotsNoCalendar } from './relativeSystem' import { DurationSlots, PlainDateSlots, diff --git a/packages/temporal-polyfill/src/internal/compare.ts b/packages/temporal-polyfill/src/internal/compare.ts index a2331bb8..b3525c73 100644 --- a/packages/temporal-polyfill/src/internal/compare.ts +++ b/packages/temporal-polyfill/src/internal/compare.ts @@ -4,13 +4,13 @@ import { durationFieldNamesAsc } from './durationFields' import { getLargestDurationUnit } from './durationMath' import * as errorMessages from './errorMessages' import { IsoDateFields, IsoDateTimeFields, IsoTimeFields } from './isoFields' -import { moveRelativeMarker } from './move' -import { RelativeToOptions, normalizeOptions } from './optionsRefine' import { RelativeToSlots, - createRelativeSystem, - relativeMarkerToEpochNano, -} from './relativeSystem' + anyMarkerToEpochNano, + createMarkerSystem, + createMoveMarker, +} from './markerSystem' +import { RelativeToOptions, normalizeOptions } from './optionsRefine' import { DurationSlots, IdLike, @@ -89,21 +89,16 @@ export function compareDurations( throw new RangeError(errorMessages.missingRelativeTo) } - const [marker, calendarOps, timeZoneOps] = createRelativeSystem( + const [marker, calendarOps, timeZoneOps] = createMarkerSystem( getCalendarOps, getTimeZoneOps, relativeToSlots, ) + const moveMarker = createMoveMarker(calendarOps, timeZoneOps) return compareBigNanos( - relativeMarkerToEpochNano( - moveRelativeMarker(durationSlots0, marker, calendarOps, timeZoneOps), - timeZoneOps, - ), - relativeMarkerToEpochNano( - moveRelativeMarker(durationSlots1, marker, calendarOps, timeZoneOps), - timeZoneOps, - ), + anyMarkerToEpochNano(moveMarker(marker, durationSlots0)), + anyMarkerToEpochNano(moveMarker(marker, durationSlots1)), ) } diff --git a/packages/temporal-polyfill/src/internal/diff.ts b/packages/temporal-polyfill/src/internal/diff.ts index 6cf94646..95463f3d 100644 --- a/packages/temporal-polyfill/src/internal/diff.ts +++ b/packages/temporal-polyfill/src/internal/diff.ts @@ -23,10 +23,16 @@ import { isoTimeFieldNamesAsc, } from './isoFields' import { isoMonthsInYear } from './isoMath' -import { moveByIsoDays, moveToMonthStart } from './move' +import { MarkerToEpochNano, MoveMarker } from './markerSystem' +import { + moveByIsoDays, + moveDateEfficient, + moveDateTime, + moveToMonthStart, + moveZonedEpochSlots, +} from './move' import { RoundingMode } from './options' import { DiffOptions, copyOptions, refineDiffOptions } from './optionsRefine' -import { RelativeMarkerSlots } from './relativeSystem' import { computeNanoInc, roundBigNano, @@ -43,6 +49,7 @@ import { ZonedDateTimeSlots, ZonedEpochSlots, createDurationSlots, + extractEpochNano, isIdLikeEqual, } from './slots' import { @@ -144,10 +151,10 @@ export function diffZonedDateTimes( smallestUnit, roundingInc, roundingMode, - // RelativeSystem... + // MarkerMoveSystem... zonedDateTimeSlots0, - calendarOps, - timeZoneOps, + extractEpochNano as MarkerToEpochNano, + bindArgs(moveZonedEpochSlots, calendarOps, timeZoneOps) as MoveMarker, ) } } @@ -208,9 +215,10 @@ export function diffPlainDateTimes( smallestUnit, roundingInc, roundingMode, - // RelativeSystem... + // MarkerMoveSystem... plainDateTimeSlots0, - calendarOps, + isoToEpochNano as MarkerToEpochNano, + bindArgs(moveDateTime, calendarOps) as MoveMarker, ) } } @@ -323,9 +331,10 @@ function diffDateLike( smallestUnit, roundingInc, roundingMode, - // RelativeSystem... - { ...startIsoFields, ...isoTimeFieldDefaults }, - calendarOps, + // MarkerMoveSystem... + startIsoFields, + isoToEpochNano as MarkerToEpochNano, + bindArgs(moveDateEfficient, calendarOps) as MoveMarker, ) } } @@ -364,39 +373,14 @@ export function diffPlainTimes( ) } -export function diffRelativeMarkers( - largestUnit: Unit, - // RelativeSystem (w/ two points)... - slots0: RelativeMarkerSlots, - slots1: RelativeMarkerSlots, - calendarOps: DiffOps, - timeZoneOps?: TimeZoneOps, -): DurationFields { - if (timeZoneOps) { - return diffZonedEpochSlotsExact( - calendarOps, - timeZoneOps, - slots0 as ZonedEpochSlots, - slots1 as ZonedEpochSlots, - largestUnit, - ) - } - return diffDateTimesExact( - calendarOps, - slots0 as IsoDateTimeFields, - slots1 as IsoDateTimeFields, - largestUnit, - ) -} - // Exact Diffing // ----------------------------------------------------------------------------- export function diffZonedEpochSlotsExact( calendarOps: DiffOps, timeZoneOps: TimeZoneOps, - slots0: ZonedEpochSlots, - slots1: ZonedEpochSlots, + slots0: ZonedEpochSlots, + slots1: ZonedEpochSlots, largestUnit: Unit, origOptions?: DiffOptions, ): DurationFields { @@ -463,8 +447,8 @@ function diffZonedEpochNanoViaCalendar( calendarOps: DiffOps, timeZoneOps: TimeZoneOps, sign: NumberSign, - slots0: ZonedEpochSlots, - slots1: ZonedEpochSlots, + slots0: ZonedEpochSlots, + slots1: ZonedEpochSlots, largestUnit: Unit, origOptions?: DiffOptions, ): DurationFields { diff --git a/packages/temporal-polyfill/src/internal/durationMath.ts b/packages/temporal-polyfill/src/internal/durationMath.ts index 3ad1610e..a6915bbb 100644 --- a/packages/temporal-polyfill/src/internal/durationMath.ts +++ b/packages/temporal-polyfill/src/internal/durationMath.ts @@ -1,6 +1,5 @@ import { BigNano, addBigNanos, bigNanoToNumber } from './bigNano' import { DiffOps } from './calendarOps' -import { diffRelativeMarkers } from './diff' import { DurationFields, DurationTimeFields, @@ -10,7 +9,14 @@ import { durationFieldNamesAsc, } from './durationFields' import * as errorMessages from './errorMessages' -import { moveRelativeMarker } from './move' +import { + DiffMarkers, + Marker, + MarkerToEpochNano, + MoveMarker, + RelativeToSlots, + createMarkerDiffSystem, +} from './markerSystem' import { Overflow } from './options' import { DurationRoundOptions, @@ -18,12 +24,6 @@ import { normalizeOptions, refineDurationRoundOptions, } from './optionsRefine' -import { - RelativeMarkerSlots, - RelativeToSlots, - createRelativeSystem, - relativeMarkerToEpochNano, -} from './relativeSystem' import { roundDayTimeDuration, roundRelativeDuration } from './round' import { DurationSlots, createDurationSlots } from './slots' import { TimeZoneOps } from './timeZoneOps' @@ -48,36 +48,20 @@ export function spanDuration( durationFields0: DurationFields, durationFields1: DurationFields | undefined, // HACKy largestUnit: Unit, // TODO: more descrimination? - // RelativeSystem... - marker: RelativeMarkerSlots, - calendarOps: DiffOps, - timeZoneOps?: TimeZoneOps, + // MarkerDiffSystem... + marker: Marker, + markerToEpochNano: MarkerToEpochNano, + moveMarker: MoveMarker, + diffMarkers: DiffMarkers, ): [DurationFields, BigNano] { - let endMarker = moveRelativeMarker( - durationFields0, - marker, - calendarOps, - timeZoneOps, - ) + let endMarker = moveMarker(marker, durationFields0) if (durationFields1) { - endMarker = moveRelativeMarker( - durationFields1, - endMarker, - calendarOps, - timeZoneOps, - ) + endMarker = moveMarker(endMarker, durationFields1) } - const balancedDuration = diffRelativeMarkers( - largestUnit, - marker, - endMarker, - calendarOps, - timeZoneOps, - ) - - return [balancedDuration, relativeMarkerToEpochNano(endMarker, timeZoneOps)] + const balancedDuration = diffMarkers(marker, endMarker, largestUnit) + return [balancedDuration, markerToEpochNano(endMarker)] } // Adding @@ -130,7 +114,11 @@ export function addDurations( slots, otherSlots, largestUnit, - ...createRelativeSystem(getCalendarOps, getTimeZoneOps, relativeToSlots), + ...createMarkerDiffSystem( + getCalendarOps, + getTimeZoneOps, + relativeToSlots, + ), )[0], ) } @@ -155,7 +143,7 @@ function addDayTimeDurations( } } -// Rounding (with RelativeSystem) +// Rounding // ----------------------------------------------------------------------------- export function roundDuration( @@ -199,7 +187,7 @@ export function roundDuration( throw new RangeError(errorMessages.missingRelativeTo) } - const relativeSystem = createRelativeSystem( + const diffSystem = createMarkerDiffSystem( getCalendarOps, getTimeZoneOps, relativeToSlots, @@ -215,7 +203,7 @@ export function roundDuration( slots, undefined, largestUnit, - ...relativeSystem, + ...diffSystem, ) const origSign = slots.sign @@ -235,7 +223,7 @@ export function roundDuration( smallestUnit, roundingInc, roundingMode, - ...relativeSystem, + ...diffSystem, ) } diff --git a/packages/temporal-polyfill/src/internal/isoParse.ts b/packages/temporal-polyfill/src/internal/isoParse.ts index da0ea87c..c1444871 100644 --- a/packages/temporal-polyfill/src/internal/isoParse.ts +++ b/packages/temporal-polyfill/src/internal/isoParse.ts @@ -16,10 +16,10 @@ import { isIsoDateFieldsValid, isoEpochFirstLeapYear, } from './isoMath' +import { RelativeToSlots } from './markerSystem' import { moveToMonthStart } from './move' import { EpochDisambig, OffsetDisambig, Overflow } from './options' import { ZonedFieldOptions, refineZonedFieldOptions } from './optionsRefine' -import { RelativeToSlots } from './relativeSystem' import { DateSlots, DurationSlots, diff --git a/packages/temporal-polyfill/src/internal/markerSystem.ts b/packages/temporal-polyfill/src/internal/markerSystem.ts new file mode 100644 index 00000000..24a4c6d9 --- /dev/null +++ b/packages/temporal-polyfill/src/internal/markerSystem.ts @@ -0,0 +1,119 @@ +import { BigNano } from './bigNano' +import { DiffOps, MoveOps } from './calendarOps' +import { diffDateTimesExact, diffZonedEpochSlotsExact } from './diff' +import { DurationFields } from './durationFields' +import { + IsoDateFields, + IsoDateTimeFields, + isoTimeFieldDefaults, +} from './isoFields' +import { moveDateTime, moveZonedEpochSlots } from './move' +import { DateSlots, EpochAndZoneSlots, ZonedEpochSlots } from './slots' +import { isoToEpochNano } from './timeMath' +import { TimeZoneOps } from './timeZoneOps' +import { Unit } from './units' +import { Callable, bindArgs } from './utils' + +// the "origin" +export type RelativeToSlots = DateSlots | ZonedEpochSlots + +// the "origin", returned from bag refining +export type RelativeToSlotsNoCalendar = IsoDateFields | EpochAndZoneSlots + +// a date marker that's moved away from the "origin" +export type Marker = IsoDateFields | IsoDateTimeFields | ZonedEpochSlots + +// Generic System +// ----------------------------------------------------------------------------- + +export type MarkerSystem = [Marker, CO, TimeZoneOps?] + +export function createMarkerSystem( + getCalendarOps: (calendarSlot: C) => CO, + getTimeZoneOps: (timeZoneSlot: T) => TimeZoneOps, + relativeToSlots: RelativeToSlots, +): MarkerSystem { + const calendarOps = getCalendarOps(relativeToSlots.calendar) + + if ((relativeToSlots as ZonedEpochSlots).epochNanoseconds) { + return [ + relativeToSlots as ZonedEpochSlots, + calendarOps, + getTimeZoneOps((relativeToSlots as ZonedEpochSlots).timeZone), + ] + } + + return [{ ...relativeToSlots, ...isoTimeFieldDefaults }, calendarOps] +} + +// Atomic Operations +// ----------------------------------------------------------------------------- + +export type MoveMarker = ( + marker: Marker, + durationFields: DurationFields, +) => Marker + +export type DiffMarkers = ( + marker0: Marker, + marker1: Marker, + largestUnit: Unit, +) => DurationFields + +export type MarkerToEpochNano = (marker: Marker) => BigNano + +export function createMoveMarker( + calendarOps: MoveOps, + timeZoneOps: TimeZoneOps | undefined, +): MoveMarker { + if (timeZoneOps) { + return bindArgs(moveZonedEpochSlots, calendarOps, timeZoneOps) as Callable + } + return bindArgs(moveDateTime as Callable, calendarOps) +} + +export function createDiffMarkers( + calendarOps: DiffOps, + timeZoneOps: TimeZoneOps | undefined, +): DiffMarkers { + if (timeZoneOps) { + return bindArgs( + diffZonedEpochSlotsExact, + calendarOps, + timeZoneOps, + ) as Callable + } + return bindArgs(diffDateTimesExact, calendarOps) as Callable +} + +export function anyMarkerToEpochNano(marker: Marker): BigNano { + return ( + (marker as ZonedEpochSlots).epochNanoseconds || + isoToEpochNano(marker as IsoDateTimeFields)! + ) +} + +// System w/ Diff Operations +// ----------------------------------------------------------------------------- + +export type MarkerMoveSystem = [Marker, MarkerToEpochNano, MoveMarker] + +export type MarkerDiffSystem = [...MarkerMoveSystem, DiffMarkers] + +export function createMarkerDiffSystem( + getCalendarOps: (calendarSlot: C) => DiffOps, + getTimeZoneOps: (timeZoneSlot: T) => TimeZoneOps, + relativeToSlots: RelativeToSlots, +): MarkerDiffSystem { + const [marker, calendarOps, timeZoneOps] = createMarkerSystem( + getCalendarOps, + getTimeZoneOps, + relativeToSlots, + ) + return [ + marker, + anyMarkerToEpochNano, + createMoveMarker(calendarOps, timeZoneOps), + createDiffMarkers(calendarOps, timeZoneOps), + ] +} diff --git a/packages/temporal-polyfill/src/internal/move.ts b/packages/temporal-polyfill/src/internal/move.ts index fbf9b878..5bc7b6c1 100644 --- a/packages/temporal-polyfill/src/internal/move.ts +++ b/packages/temporal-polyfill/src/internal/move.ts @@ -29,9 +29,9 @@ import { } from './isoFields' import { isoMonthsInYear } from './isoMath' import { OverflowOptions, refineOverflowOptions } from './optionsRefine' -import { RelativeMarkerSlots } from './relativeSystem' import { DurationSlots, + EpochSlots, InstantSlots, PlainDateSlots, PlainDateTimeSlots, @@ -89,17 +89,15 @@ export function moveZonedDateTime( const timeZoneOps = getTimeZoneOps(zonedDateTimeSlots.timeZone) const calendarOps = getCalendarOps(zonedDateTimeSlots.calendar) - const movedEpochNanoseconds = moveZonedEpochSlots( - calendarOps, - timeZoneOps, - zonedDateTimeSlots, - doSubtract ? negateDurationFields(durationSlots) : durationSlots, - options, - ) - return { ...zonedDateTimeSlots, // retain timeZone/calendar, order - epochNanoseconds: movedEpochNanoseconds, + ...moveZonedEpochSlots( + calendarOps, + timeZoneOps, + zonedDateTimeSlots, + doSubtract ? negateDurationFields(durationSlots) : durationSlots, + options, + ), } } @@ -190,25 +188,6 @@ export function movePlainTime( ) } -export function moveRelativeMarker( - durationFields: DurationFields, - // RelativeSystem... - slots: RelativeMarkerSlots, - calendarOps: MoveOps, - timeZoneOps?: TimeZoneOps, -): RelativeMarkerSlots { - if (timeZoneOps) { - const epochNanoseconds = moveZonedEpochSlots( - calendarOps, - timeZoneOps, - slots as ZonedEpochSlots, - durationFields, - ) - return { epochNanoseconds } as ZonedEpochSlots - } - return moveDateTime(calendarOps, slots as IsoDateTimeFields, durationFields) -} - // Low-Level // ----------------------------------------------------------------------------- @@ -227,10 +206,10 @@ timeZoneOps must be derived from zonedEpochSlots.timeZone export function moveZonedEpochSlots( calendarOps: MoveOps, timeZoneOps: TimeZoneOps, - slots: ZonedEpochSlots, + slots: ZonedEpochSlots, durationFields: DurationFields, options?: OverflowOptions, -): BigNano { +): EpochSlots { const timeOnlyNano = durationFieldsToBigNano(durationFields, Unit.Hour) let epochNano = slots.epochNanoseconds @@ -259,7 +238,9 @@ export function moveZonedEpochSlots( ) } - return checkEpochNanoInBounds(epochNano) + return { + epochNanoseconds: checkEpochNanoInBounds(epochNano), + } } export function moveDateTime( @@ -294,7 +275,7 @@ export function moveDateTime( /* Skips calendar if moving days only */ -function moveDateEfficient( +export function moveDateEfficient( calendarOps: MoveOps, isoDateFields: IsoDateFields, durationFields: DurationFields, diff --git a/packages/temporal-polyfill/src/internal/relativeSystem.ts b/packages/temporal-polyfill/src/internal/relativeSystem.ts deleted file mode 100644 index 46218f57..00000000 --- a/packages/temporal-polyfill/src/internal/relativeSystem.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { BigNano } from './bigNano' -import { - IsoDateFields, - IsoDateTimeFields, - isoTimeFieldDefaults, -} from './isoFields' -import { DateSlots, EpochAndZoneSlots, ZonedEpochSlots } from './slots' -import { isoToEpochNano } from './timeMath' -import { TimeZoneOps } from './timeZoneOps' - -// the "origin" -export type RelativeToSlots = DateSlots | ZonedEpochSlots - -// the "origin", returned from bag refining -export type RelativeToSlotsNoCalendar = IsoDateFields | EpochAndZoneSlots - -// a date marker that's moved away from the "origin" -export type RelativeMarkerSlots = - | IsoDateTimeFields - | ZonedEpochSlots - -export type RelativeSystem = [RelativeMarkerSlots, CO, TimeZoneOps?] - -export function createRelativeSystem( - getCalendarOps: (calendarSlot: C) => CO, - getTimeZoneOps: (timeZoneSlot: T) => TimeZoneOps, - slots: RelativeToSlots, -): RelativeSystem { - const calendarOps = getCalendarOps(slots.calendar) - - if ((slots as ZonedEpochSlots).epochNanoseconds) { - return [ - slots as ZonedEpochSlots, - calendarOps, - getTimeZoneOps((slots as ZonedEpochSlots).timeZone), - ] - } - - return [{ ...slots, ...isoTimeFieldDefaults }, calendarOps] -} - -export function relativeMarkerToEpochNano( - marker: RelativeMarkerSlots, - timeZoneOps?: TimeZoneOps, -): BigNano { - if (timeZoneOps) { - return (marker as ZonedEpochSlots).epochNanoseconds - } - return isoToEpochNano(marker as IsoDateTimeFields)! -} diff --git a/packages/temporal-polyfill/src/internal/round.ts b/packages/temporal-polyfill/src/internal/round.ts index a005e088..21ef43d7 100644 --- a/packages/temporal-polyfill/src/internal/round.ts +++ b/packages/temporal-polyfill/src/internal/round.ts @@ -6,7 +6,6 @@ import { createBigNano, diffBigNanos, } from './bigNano' -import { DiffOps } from './calendarOps' import { DurationFieldName, DurationFields, @@ -26,7 +25,13 @@ import { IsoTimeFields, isoTimeFieldDefaults, } from './isoFields' -import { moveByIsoDays, moveRelativeMarker } from './move' +import { + DiffMarkers, + Marker, + MarkerToEpochNano, + MoveMarker, +} from './markerSystem' +import { moveByIsoDays } from './move' import { EpochDisambig, OffsetDisambig, @@ -35,10 +40,7 @@ import { } from './options' import { RoundingOptions, refineRoundOptions } from './optionsRefine' import { - RelativeMarkerSlots, - relativeMarkerToEpochNano, -} from './relativeSystem' -import { + EpochSlots, InstantSlots, PlainDateTimeSlots, PlainTimeSlots, @@ -254,7 +256,7 @@ export function roundTimeToNano( ) } -// Duration (w/o RelativeSystem) +// Duration // ----------------------------------------------------------------------------- export function roundDayTimeDuration( @@ -316,15 +318,16 @@ export function roundRelativeDuration( smallestUnit: Unit, roundingInc: number, roundingMode: RoundingMode, - // RelativeSystem... - marker: RelativeMarkerSlots, - calendarOps: DiffOps, - timeZoneOps?: TimeZoneOps, + // MarkerDiffSystem... + marker: Marker, + markerToEpochNano: MarkerToEpochNano, + moveMarker: MoveMarker, + _diffMarkers?: DiffMarkers, ): DurationFields { const nudgeFunc = ( smallestUnit > Unit.Day ? nudgeRelativeDuration - : timeZoneOps && smallestUnit < Unit.Day + : (marker as EpochSlots).epochNanoseconds && smallestUnit < Unit.Day ? nudgeRelativeDurationTime : nudgeDurationDayTime ) as typeof nudgeRelativeDuration // most general @@ -336,10 +339,10 @@ export function roundRelativeDuration( smallestUnit, roundingInc, roundingMode, - // RelativeSystem for nudgeRelativeDuration... + // MarkerMoveSystem... marker, - calendarOps, - timeZoneOps, + markerToEpochNano, + moveMarker, ) // grew a day/week/month/year? @@ -349,10 +352,10 @@ export function roundRelativeDuration( roundedEpochNano, largestUnit, Math.max(Unit.Day, smallestUnit), - // RelativeSystem... + // MarkerMoveSystem... marker, - calendarOps, - timeZoneOps, + markerToEpochNano, + moveMarker, ) } @@ -487,10 +490,10 @@ function nudgeRelativeDurationTime( smallestUnit: TimeUnit, // always Day roundingInc: number, roundingMode: RoundingMode, - // RelativeSystem... - marker: RelativeMarkerSlots, - calendarOps: DiffOps, - timeZoneOps?: TimeZoneOps, + // MarkerMoveSystem... + marker: Marker, + markerToEpochNano: MarkerToEpochNano, + moveMarker: MoveMarker, ): [ durationFields: DurationFields, movedEpochNano: BigNano, @@ -572,10 +575,10 @@ function nudgeRelativeDuration( baseDurationFields, smallestUnit, roundingInc * sign, - // RelativeSystem... + // MarkerMoveSystem... marker, - calendarOps, - timeZoneOps, + markerToEpochNano, + moveMarker, ) // usually between 0-1, however can be higher when weeks aren't bounded by months @@ -603,10 +606,10 @@ function bubbleRelativeDuration( endEpochNano: BigNano, largestUnit: Unit, smallestUnit: Unit, // guaranteed Day/Week/Month/Year - // RelativeSystem... - marker: RelativeMarkerSlots, - calendarOps: DiffOps, - timeZoneOps?: TimeZoneOps, + // MarkerMoveSystem... + marker: Marker, + markerToEpochNano: MarkerToEpochNano, + moveMarker: MoveMarker, ): DurationFields { const sign = computeDurationSign(durationFields) @@ -626,9 +629,8 @@ function bubbleRelativeDuration( ) baseDurationFields[durationFieldNamesAsc[currentUnit]] += sign - const thresholdEpochNano = relativeMarkerToEpochNano( - moveRelativeMarker(baseDurationFields, marker, calendarOps, timeZoneOps), - timeZoneOps, + const thresholdEpochNano = markerToEpochNano( + moveMarker(marker, baseDurationFields), ) const beyondThreshold = bigNanoToNumber( diffBigNanos(thresholdEpochNano, endEpochNano), diff --git a/packages/temporal-polyfill/src/internal/slots.ts b/packages/temporal-polyfill/src/internal/slots.ts index fdb284d8..6b2858ff 100644 --- a/packages/temporal-polyfill/src/internal/slots.ts +++ b/packages/temporal-polyfill/src/internal/slots.ts @@ -140,10 +140,12 @@ export type BrandingSlots = { branding: string } export type EpochSlots = { epochNanoseconds: BigNano } export type EpochAndZoneSlots = EpochSlots & { timeZone: T } +export type ZonedEpochSlots = EpochAndZoneSlots & { + calendar: C +} export type DateSlots = IsoDateFields & { calendar: C } export type DateTimeSlots = IsoDateTimeFields & { calendar: C } -export type ZonedEpochSlots = EpochAndZoneSlots & { calendar: C } export type PlainDateSlots = IsoDateFields & { branding: typeof PlainDateBranding @@ -202,6 +204,10 @@ export function getEpochNano(slots: EpochSlots): bigint { return bigNanoToBigInt(slots.epochNanoseconds) } +export function extractEpochNano(slots: EpochSlots): BigNano { + return slots.epochNanoseconds +} + // ID-like // ----------------------------------------------------------------------------- diff --git a/packages/temporal-polyfill/src/internal/total.ts b/packages/temporal-polyfill/src/internal/total.ts index d5ffa7f9..b46c2658 100644 --- a/packages/temporal-polyfill/src/internal/total.ts +++ b/packages/temporal-polyfill/src/internal/total.ts @@ -13,14 +13,15 @@ import { spanDuration, } from './durationMath' import * as errorMessages from './errorMessages' -import { moveRelativeMarker } from './move' -import { TotalUnitOptionsWithRel, refineTotalOptions } from './optionsRefine' import { - RelativeMarkerSlots, + DiffMarkers, + Marker, + MarkerToEpochNano, + MoveMarker, RelativeToSlots, - createRelativeSystem, - relativeMarkerToEpochNano, -} from './relativeSystem' + createMarkerDiffSystem, +} from './markerSystem' +import { TotalUnitOptionsWithRel, refineTotalOptions } from './optionsRefine' import { DurationSlots } from './slots' import { TimeZoneOps } from './timeZoneOps' import { DayTimeUnit, Unit, UnitName, unitNanoMap } from './units' @@ -51,16 +52,16 @@ export function totalDuration( throw new RangeError(errorMessages.missingRelativeTo) } - const relativeSystem = createRelativeSystem( + const diffSystem = createMarkerDiffSystem( getCalendarOps, getTimeZoneOps, relativeToSlots, ) return totalRelativeDuration( - ...spanDuration(slots, undefined, totalUnit, ...relativeSystem), + ...spanDuration(slots, undefined, totalUnit, ...diffSystem), totalUnit, - ...relativeSystem, + ...diffSystem, ) } @@ -68,10 +69,11 @@ function totalRelativeDuration( durationFields: DurationFields, endEpochNano: BigNano, totalUnit: Unit, - // RelativeSystem... - marker: RelativeMarkerSlots, - calendarOps: DiffOps, - timeZoneOps?: TimeZoneOps, + // MarkerDiffSystem... + marker: Marker, + markerToEpochNano: MarkerToEpochNano, + moveMarker: MoveMarker, + _diffMarkers?: DiffMarkers, ): number { const sign = computeDurationSign(durationFields) @@ -79,10 +81,10 @@ function totalRelativeDuration( clearDurationFields(durationFields, totalUnit - 1), totalUnit, sign, - // RelativeSystem... + // MarkerSystem... marker, - calendarOps, - timeZoneOps, + markerToEpochNano, + moveMarker, ) const frac = computeEpochNanoFrac(epochNano0, epochNano1, endEpochNano) @@ -107,29 +109,19 @@ export function clampRelativeDuration( durationFields: DurationFields, clampUnit: Unit, clampDistance: number, - // RelativeSystem... - marker: RelativeMarkerSlots, - calendarOps: DiffOps, - timeZoneOps?: TimeZoneOps, + // MarkerMoveSystem... + marker: Marker, + markerToEpochNano: MarkerToEpochNano, + moveMarker: MoveMarker, ) { const clampDurationFields = { ...durationFieldDefaults, [durationFieldNamesAsc[clampUnit]]: clampDistance, } - const marker0 = moveRelativeMarker( - durationFields, - marker, - calendarOps, - timeZoneOps, - ) - const marker1 = moveRelativeMarker( - clampDurationFields, - marker0, - calendarOps, - timeZoneOps, - ) - const epochNano0 = relativeMarkerToEpochNano(marker0, timeZoneOps) - const epochNano1 = relativeMarkerToEpochNano(marker1, timeZoneOps) + const marker0 = moveMarker(marker, durationFields) + const marker1 = moveMarker(marker0, clampDurationFields) + const epochNano0 = markerToEpochNano(marker0) + const epochNano1 = markerToEpochNano(marker1) return [epochNano0, epochNano1] }