Skip to content

Commit

Permalink
tests adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Apr 2, 2024
1 parent eb24ea1 commit ccad49a
Show file tree
Hide file tree
Showing 17 changed files with 428 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
# (refactors Duration::round/total to be more DRY with since/until)
#
built-ins/Temporal/Duration/prototype/round/dst-rounding-result.js
built-ins/Temporal/Duration/prototype/round/dst-rounding-result-fullcalendar.js
# ^see built-ins/Temporal/Duration/prototype/round/dst-rounding-result-fullcalendar.js
built-ins/Temporal/Duration/prototype/total/dst-rounding-result.js
# ^see built-ins/Temporal/Duration/prototype/total/dst-rounding-result-fullcalendar.js
intl402/Temporal/Duration/prototype/round/relativeto-string-datetime.js
built-ins/Temporal/Duration/prototype/round/timezone-getpossibleinstantsfor-iterable.js
built-ins/Temporal/Duration/prototype/total/precision-exact-mathematical-values-4.js
Expand Down
82 changes: 47 additions & 35 deletions packages/temporal-polyfill/src/funcApi/diffUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
bigNanoToExactDays,
bigNanoToNumber,
compareBigNanos,
diffBigNanos,
Expand Down Expand Up @@ -28,47 +29,34 @@ import {
import { isoToEpochNano } from '../internal/timeMath'
import { queryNativeTimeZone } from '../internal/timeZoneNative'
import { totalRelativeDuration } from '../internal/total'
import {
DayTimeUnit,
Unit,
nanoInUtcDay,
nanoInUtcWeek,
} from '../internal/units'
import { TimeUnit, Unit } from '../internal/units'
import { NumberSign, bindArgs } from '../internal/utils'

export const diffZonedYears = bindArgs(diffZonedLargeUnits, Unit.Year)
export const diffZonedMonths = bindArgs(diffZonedLargeUnits, Unit.Month)
export const diffZonedWeeks = bindArgs(
diffZonedDayLikeUnits,
Unit.Week,
nanoInUtcWeek,
)
export const diffZonedDays = bindArgs(
diffZonedDayLikeUnits,
Unit.Day,
nanoInUtcDay,
)
export const diffZonedWeeks = bindArgs(diffZonedDayLikeUnits, Unit.Week, 7)
export const diffZonedDays = bindArgs(diffZonedDayLikeUnits, Unit.Day, 1)
export const diffZonedTimeUnits = bindArgs(
diffDayTimeLikeUnit,
diffTimeUnit,
extractEpochNano as MarkerToEpochNano,
)

export const diffPlainYears = bindArgs(diffPlainLargeUnits, Unit.Year)
export const diffPlainMonths = bindArgs(diffPlainLargeUnits, Unit.Month)
export const diffPlainWeeks = bindArgs(
diffDayTimeLikeUnit,
diffPlainDayLikeUnit,
isoToEpochNano as MarkerToEpochNano,
Unit.Week,
nanoInUtcWeek,
7,
)
export const diffPlainDays = bindArgs(
diffDayTimeLikeUnit,
diffPlainDayLikeUnit,
isoToEpochNano as MarkerToEpochNano,
Unit.Day,
nanoInUtcDay,
1,
)
export const diffPlainTimeUnits = bindArgs(
diffDayTimeLikeUnit,
diffTimeUnit,
isoToEpochNano as MarkerToEpochNano,
)

Expand Down Expand Up @@ -177,15 +165,15 @@ function diffDateUnits(
return res
}

// Day/Time Units
// Day-Like Units (weeks, days)
// -----------------------------------------------------------------------------

function diffZonedDayLikeUnits(
unit: Unit,
nanoInUnit: number,
unit: Unit.Week | Unit.Day,
daysInUnit: number,
record0: ZonedDateTimeSlots<string, string>,
record1: ZonedDateTimeSlots<string, string>,
options: RoundingModeName | RoundingMathOptions | undefined,
options?: RoundingModeName | RoundingMathOptions | undefined,
): number {
const [roundingInc, roundingMode] = refineUnitDiffOptions(unit, options)

Expand All @@ -202,25 +190,49 @@ function diffZonedDayLikeUnits(
record1,
sign,
)
let nanoDiff = moveBigNano(
const nanoDiff = moveBigNano(
diffBigNanos(isoToEpochNano(isoFields0)!, isoToEpochNano(isoFields1)!),
remainderNano,
)

let res = bigNanoToExactDays(nanoDiff) / daysInUnit

if (roundingInc) {
nanoDiff = roundBigNanoByInc(
nanoDiff,
nanoInUnit * roundingInc,
roundingMode!,
)
res = roundByInc(res, roundingInc, roundingMode!)
}

return bigNanoToNumber(nanoDiff, nanoInUnit, !roundingInc)
return res
}

function diffDayTimeLikeUnit(
function diffPlainDayLikeUnit(
markerToEpochNano: MarkerToEpochNano,
unit: Unit.Week | Unit.Day,
daysInUnit: number,
record0: Marker,
record1: Marker,
options?: RoundingModeName | RoundingMathOptions,
): number {
const [roundingInc, roundingMode] = refineUnitDiffOptions(unit, options)
const nanoDiff = diffBigNanos(
markerToEpochNano(record0),
markerToEpochNano(record1),
)

let res = bigNanoToExactDays(nanoDiff) / daysInUnit

if (roundingInc) {
res = roundByInc(res, roundingInc, roundingMode!)
}

return res
}

// Time Units
// -----------------------------------------------------------------------------

function diffTimeUnit(
markerToEpochNano: MarkerToEpochNano,
unit: DayTimeUnit | Unit.Week,
unit: TimeUnit,
nanoInUnit: number,
record0: Marker,
record1: Marker,
Expand Down
106 changes: 75 additions & 31 deletions packages/temporal-polyfill/src/funcApi/moveUtils.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,113 @@
import { createNativeConvertOps } from '../internal/calendarNativeQuery'
import {
createNativeDayOfYearOps,
createNativeDayOps,
createNativeDaysInMonthOps,
createNativeDaysInYearOps,
createNativeMoveOps,
createNativeWeekOps,
} from '../internal/calendarNativeQuery'
import { toInteger, toStrictInteger } from '../internal/cast'
import * as errorMessages from '../internal/errorMessages'
import { IsoDateFields } from '../internal/isoFields'
import { IsoDateFields, isoDateFieldNamesAlpha } from '../internal/isoFields'
import { computeIsoDayOfWeek } from '../internal/isoMath'
import { moveByDays, moveToDayOfMonthUnsafe } from '../internal/move'
import {
moveByDays,
moveToDayOfMonthUnsafe,
nativeYearMonthAdd,
} from '../internal/move'
import {
OverflowOptions,
refineOverflowOptions,
} from '../internal/optionsRefine'
import { DateSlots } from '../internal/slots'
import { epochMilliToIso } from '../internal/timeMath'
import { clampEntity } from '../internal/utils'
import { clampEntity, pluckProps } from '../internal/utils'

export function reversedMove<S>(
f: (slots: S, units: number) => S,
): (slots: S, units: number) => S {
return (slots, units) => {
return f(slots, -units)
f: (slots: S, units: number, options?: OverflowOptions) => S,
): (slots: S, units: number, options?: OverflowOptions) => S {
return (slots, units, options?: OverflowOptions) => {
return f(slots, -units, options)
}
}

// Move-by-Unit
// -----------------------------------------------------------------------------
// These functions validate input

export function moveByYears<S extends DateSlots<string>>(
slots: S,
years: number,
options?: OverflowOptions,
): S {
const calendarOps = createNativeConvertOps(slots.calendar)
const [year0] = calendarOps.dateParts(slots)
const year1 = year0 + years
return {
...slots,
...epochMilliToIso(calendarOps.epochMilli(year1)),
const overflow = refineOverflowOptions(options)
if (!years) {
return slots
}
const calendarOps = createNativeMoveOps(slots.calendar)
const isoFields = epochMilliToIso(
nativeYearMonthAdd(calendarOps, slots, toStrictInteger(years), 0, overflow),
)
const isoDateFields = pluckProps(isoDateFieldNamesAlpha, isoFields)
return { ...slots, ...isoDateFields }
}

export function moveByMonths<S extends DateSlots<string>>(
slots: S,
months: number,
options?: OverflowOptions,
): S {
const calendarOps = createNativeConvertOps(slots.calendar)
const [year0, month0] = calendarOps.dateParts(slots)
const [year1, month1] = calendarOps.monthAdd(year0, month0, months)
return {
...slots,
...epochMilliToIso(calendarOps.epochMilli(year1, month1)),
const overflow = refineOverflowOptions(options)
if (!months) {
return slots
}
const calendarOps = createNativeMoveOps(slots.calendar)
const isoFields = epochMilliToIso(
nativeYearMonthAdd(
calendarOps,
slots,
0,
toStrictInteger(months),
overflow,
),
)
const isoDateFields = pluckProps(isoDateFieldNamesAlpha, isoFields)
return { ...slots, ...isoDateFields }
}

export function moveByIsoWeeks<F extends IsoDateFields>(
slots: F,
weeks: number,
): F {
return moveByDays(slots, weeks * 7)
return moveByDays(slots, toStrictInteger(weeks) * 7)
}

export function moveByDaysStrict<F extends IsoDateFields>(
slots: F,
weeks: number,
): F {
return moveByDays(slots, toStrictInteger(weeks))
}

// Day-of-Unit / Week
// -----------------------------------------------------------------------------

const dayOfMonthLabel = 'dayOfMonth'
const dayLabel = 'day'
const dayOfWeekLabel = 'dayOfWeek'
const weekOfYearLabel = 'weekOfYear'

export function moveToDayOfYear<S extends DateSlots<string>>(
slots: S,
dayOfYear: number,
options: OverflowOptions | undefined,
options?: OverflowOptions,
): S {
const overflow = refineOverflowOptions(options)
const { calendar } = slots
const daysInYear = createNativeDaysInYearOps(calendar).daysInYear(slots)
const normDayOfYear = clampEntity(
'dayOfMonth',
dayOfYear,
dayOfMonthLabel,
toInteger(dayOfYear, dayOfMonthLabel),
1,
daysInYear,
overflow,
Expand All @@ -88,12 +120,18 @@ export function moveToDayOfYear<S extends DateSlots<string>>(
export function moveToDayOfMonth<S extends DateSlots<string>>(
slots: S,
day: number,
options: OverflowOptions | undefined,
options?: OverflowOptions,
): S {
const overflow = refineOverflowOptions(options)
const { calendar } = slots
const daysInMonth = createNativeDaysInMonthOps(calendar).daysInMonth(slots)
const normDayOfMonth = clampEntity('day', day, 1, daysInMonth, overflow)
const normDayOfMonth = clampEntity(
dayLabel,
toInteger(day, dayLabel),
1,
daysInMonth,
overflow,
)

return moveToDayOfMonthUnsafe(
createNativeDayOps(calendar),
Expand All @@ -105,17 +143,23 @@ export function moveToDayOfMonth<S extends DateSlots<string>>(
export function moveToDayOfWeek<S extends IsoDateFields>(
slots: S,
dayOfWeek: number,
options: OverflowOptions | undefined,
options?: OverflowOptions,
): S {
const overflow = refineOverflowOptions(options)
const normDayOfWeek = clampEntity('dayOfWeek', dayOfWeek, 1, 7, overflow)
const normDayOfWeek = clampEntity(
dayOfWeekLabel,
toInteger(dayOfWeek, dayOfWeekLabel),
1,
7,
overflow,
)
return moveByDays(slots, normDayOfWeek - computeIsoDayOfWeek(slots))
}

export function slotsWithWeekOfYear<S extends DateSlots<string>>(
slots: S,
weekOfYear: number,
options: OverflowOptions | undefined,
options?: OverflowOptions,
): S {
const overflow = refineOverflowOptions(options)
const calendarOps = createNativeWeekOps(slots.calendar)
Expand All @@ -126,8 +170,8 @@ export function slotsWithWeekOfYear<S extends DateSlots<string>>(
}

const normWeekOfYear = clampEntity(
'weekOfYear',
weekOfYear,
weekOfYearLabel,
toInteger(weekOfYear, weekOfYearLabel),
1,
weeksInYear!,
overflow,
Expand Down
Loading

0 comments on commit ccad49a

Please sign in to comment.