Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normative: Audit of user-visible operations in Temporal #2519

Merged
merged 4 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 86 additions & 19 deletions polyfill/lib/duration.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import bigInt from 'big-integer';

import * as ES from './ecmascript.mjs';
import { MakeIntrinsicClass } from './intrinsicclass.mjs';
import { CalendarMethodRecord } from './methodrecord.mjs';
import {
YEARS,
MONTHS,
Expand All @@ -17,7 +18,6 @@ import {
NANOSECONDS,
CALENDAR,
INSTANT,
TIME_ZONE,
CreateSlots,
GetSlot,
SetSlot
Expand Down Expand Up @@ -250,7 +250,7 @@ export class Duration {
}

let largestUnit = ES.GetTemporalUnit(roundTo, 'largestUnit', 'datetime', undefined, ['auto']);
let { plainRelativeTo, zonedRelativeTo } = ES.ToRelativeTemporalObject(roundTo);
let { plainRelativeTo, zonedRelativeTo, timeZoneRec } = ES.ToRelativeTemporalObject(roundTo);
const roundingIncrement = ES.ToTemporalRoundingIncrement(roundTo);
const roundingMode = ES.ToTemporalRoundingMode(roundTo, 'halfExpand');
let smallestUnit = ES.GetTemporalUnit(roundTo, 'smallestUnit', 'datetime', undefined);
Expand Down Expand Up @@ -319,20 +319,55 @@ export class Duration {
// if either is needed in one of the operations below, because the
// conversion is user visible
precalculatedPlainDateTime = ES.GetPlainDateTimeFor(
GetSlot(zonedRelativeTo, TIME_ZONE),
timeZoneRec,
GetSlot(zonedRelativeTo, INSTANT),
GetSlot(zonedRelativeTo, CALENDAR)
);
plainRelativeTo = ES.TemporalDateTimeToDate(precalculatedPlainDateTime);
}

let calendarRec;
if (zonedRelativeTo || plainRelativeTo) {
const calendar = GetSlot(zonedRelativeTo ?? plainRelativeTo, CALENDAR);
calendarRec = new CalendarMethodRecord(calendar);
if (
years !== 0 ||
months !== 0 ||
weeks !== 0 ||
largestUnit === 'year' ||
largestUnit === 'month' ||
largestUnit === 'week' ||
smallestUnit === 'year' ||
smallestUnit === 'month' ||
smallestUnit === 'week'
) {
calendarRec.lookup('dateAdd');
}
if (
largestUnit === 'year' ||
(largestUnit === 'month' && years !== 0) ||
smallestUnit === 'year' ||
// Edge condition in AdjustRoundedDurationDays:
(zonedRelativeTo &&
!roundingGranularityIsNoop &&
smallestUnit !== 'year' &&
smallestUnit !== 'month' &&
smallestUnit !== 'week' &&
smallestUnit !== 'day' &&
(largestUnit === 'year' || largestUnit === 'month' || largestUnit === 'week'))
) {
calendarRec.lookup('dateUntil');
}
}

({ years, months, weeks, days } = ES.UnbalanceDateDurationRelative(
years,
months,
weeks,
days,
largestUnit,
plainRelativeTo
plainRelativeTo,
calendarRec
));
({ years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
ES.RoundDuration(
Expand All @@ -350,7 +385,9 @@ export class Duration {
smallestUnit,
roundingMode,
plainRelativeTo,
calendarRec,
zonedRelativeTo,
timeZoneRec,
precalculatedPlainDateTime
));
if (zonedRelativeTo) {
Expand All @@ -370,6 +407,8 @@ export class Duration {
smallestUnit,
roundingMode,
zonedRelativeTo,
calendarRec,
timeZoneRec,
precalculatedPlainDateTime
));
({ days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = ES.BalanceTimeDurationRelative(
Expand All @@ -382,6 +421,7 @@ export class Duration {
nanoseconds,
largestUnit,
zonedRelativeTo,
timeZoneRec,
precalculatedPlainDateTime
));
} else {
Expand All @@ -402,7 +442,8 @@ export class Duration {
weeks,
days,
largestUnit,
plainRelativeTo
plainRelativeTo,
calendarRec
));

return new Duration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
Expand All @@ -428,7 +469,7 @@ export class Duration {
} else {
totalOf = ES.GetOptionsObject(totalOf);
}
let { plainRelativeTo, zonedRelativeTo } = ES.ToRelativeTemporalObject(totalOf);
let { plainRelativeTo, zonedRelativeTo, timeZoneRec } = ES.ToRelativeTemporalObject(totalOf);
const unit = ES.GetTemporalUnit(totalOf, 'unit', 'datetime', ES.REQUIRED);

let precalculatedPlainDateTime;
Expand All @@ -445,27 +486,46 @@ export class Duration {
// Convert a ZonedDateTime relativeTo to PlainDate only if needed in one
// of the operations below, because the conversion is user visible
precalculatedPlainDateTime = ES.GetPlainDateTimeFor(
GetSlot(zonedRelativeTo, TIME_ZONE),
timeZoneRec,
GetSlot(zonedRelativeTo, INSTANT),
GetSlot(zonedRelativeTo, CALENDAR)
);
plainRelativeTo = ES.TemporalDateTimeToDate(precalculatedPlainDateTime);
}

let calendar, calendarRec;
if (zonedRelativeTo) {
calendar = GetSlot(zonedRelativeTo, CALENDAR);
} else if (plainRelativeTo) {
calendar = GetSlot(plainRelativeTo, CALENDAR);
}
if (calendar) {
calendarRec = new CalendarMethodRecord(calendar);
if (years !== 0 || months !== 0 || weeks !== 0 || unit === 'year' || unit === 'month' || unit === 'week') {
calendarRec.lookup('dateAdd');
}
if (unit === 'year' || (unit === 'month' && years !== 0)) {
calendarRec.lookup('dateUntil');
}
}

// Convert larger units down to days
({ years, months, weeks, days } = ES.UnbalanceDateDurationRelative(
years,
months,
weeks,
days,
unit,
plainRelativeTo
plainRelativeTo,
calendarRec
));
// If the unit we're totalling is smaller than `days`, convert days down to that unit.
let balanceResult;
if (zonedRelativeTo) {
const intermediate = ES.MoveRelativeZonedDateTime(
zonedRelativeTo,
calendarRec,
timeZoneRec,
years,
months,
weeks,
Expand All @@ -481,7 +541,8 @@ export class Duration {
microseconds,
nanoseconds,
unit,
intermediate
intermediate,
timeZoneRec
);
} else {
balanceResult = ES.BalancePossiblyInfiniteTimeDuration(
Expand Down Expand Up @@ -517,7 +578,9 @@ export class Duration {
unit,
'trunc',
plainRelativeTo,
calendarRec,
zonedRelativeTo,
timeZoneRec,
precalculatedPlainDateTime
);
return total;
Expand Down Expand Up @@ -655,20 +718,24 @@ export class Duration {
) {
return 0;
}
const { plainRelativeTo, zonedRelativeTo } = ES.ToRelativeTemporalObject(options);
const { plainRelativeTo, zonedRelativeTo, timeZoneRec } = ES.ToRelativeTemporalObject(options);

const calendarUnitsPresent = y1 !== 0 || y2 !== 0 || mon1 !== 0 || mon2 !== 0 || w1 !== 0 || w2 !== 0;

let calendarRec;
if (zonedRelativeTo || plainRelativeTo) {
calendarRec = new CalendarMethodRecord(GetSlot(zonedRelativeTo ?? plainRelativeTo, CALENDAR));
if (calendarUnitsPresent) calendarRec.lookup('dateAdd');
}

if (zonedRelativeTo && (calendarUnitsPresent || d1 != 0 || d2 !== 0)) {
const instant = GetSlot(zonedRelativeTo, INSTANT);
const timeZone = GetSlot(zonedRelativeTo, TIME_ZONE);
const calendar = GetSlot(zonedRelativeTo, CALENDAR);
const precalculatedPlainDateTime = ES.GetPlainDateTimeFor(timeZone, instant, calendar);
const precalculatedPlainDateTime = ES.GetPlainDateTimeFor(timeZoneRec, instant, calendarRec.receiver);

const after1 = ES.AddZonedDateTime(
instant,
timeZone,
calendar,
timeZoneRec,
calendarRec,
y1,
mon1,
w1,
Expand All @@ -683,8 +750,8 @@ export class Duration {
);
const after2 = ES.AddZonedDateTime(
instant,
timeZone,
calendar,
timeZoneRec,
calendarRec,
y2,
mon2,
w2,
Expand All @@ -702,8 +769,8 @@ export class Duration {

if (calendarUnitsPresent) {
// plainRelativeTo may be undefined, and if so Unbalance will throw
({ days: d1 } = ES.UnbalanceDateDurationRelative(y1, mon1, w1, d1, 'day', plainRelativeTo));
({ days: d2 } = ES.UnbalanceDateDurationRelative(y2, mon2, w2, d2, 'day', plainRelativeTo));
({ days: d1 } = ES.UnbalanceDateDurationRelative(y1, mon1, w1, d1, 'day', plainRelativeTo, calendarRec));
({ days: d2 } = ES.UnbalanceDateDurationRelative(y2, mon2, w2, d2, 'day', plainRelativeTo, calendarRec));
}
h1 = bigInt(h1).add(bigInt(d1).multiply(24));
h2 = bigInt(h2).add(bigInt(d2).multiply(24));
Expand Down
Loading