Skip to content

Commit

Permalink
Fast-path conversion of Temporal.PlainDate to relativeTo object
Browse files Browse the repository at this point in the history
The value of a relativeTo property in a property bag is already allowed to
be either a Temporal.PlainDateTime or Temporal.ZonedDateTime. If passing a
Temporal.PlainDate, then create a Temporal.PlainDateTime from its slots as
we do in ToTemporalDateTime.

See: #1428
  • Loading branch information
ptomato committed Apr 16, 2021
1 parent 3119ab8 commit 32b1588
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,21 @@ export const ES = ObjectAssign({}, ES2020, {
let year, month, day, hour, minute, second, millisecond, microsecond, nanosecond, calendar, timeZone, offset;
if (ES.Type(relativeTo) === 'Object') {
if (ES.IsTemporalZonedDateTime(relativeTo) || ES.IsTemporalDateTime(relativeTo)) return relativeTo;
if (ES.IsTemporalDate(relativeTo)) {
const TemporalDateTime = GetIntrinsic('%Temporal.PlainDateTime%');
return new TemporalDateTime(
GetSlot(relativeTo, ISO_YEAR),
GetSlot(relativeTo, ISO_MONTH),
GetSlot(relativeTo, ISO_DAY),
0,
0,
0,
0,
0,
0,
GetSlot(relativeTo, CALENDAR)
);
}
calendar = relativeTo.calendar;
if (calendar === undefined) calendar = ES.GetISO8601Calendar();
calendar = ES.ToTemporalCalendar(calendar);
Expand Down
2 changes: 2 additions & 0 deletions spec/abstractops.html
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ <h1>ToRelativeTemporalObject ( _options_ )</h1>
1. If Type(_value_) is Object, then
1. If _value_ has either an [[InitializedTemporalDateTime]] or [[InitializedTemporalZonedDateTime]] internal slot, then
1. Return _value_.
1. If _value_ has an [[InitializedTemporalDate]] internal slot, then
1. Return ? CreateTemporalDateTime(_value_.[[ISOYear]], _value_.[[ISOMonth]], _value_.[[ISODay]], 0, 0, 0, 0, 0, 0, _value_.[[Calendar]]).
1. Let _calendar_ be ? GetOptionalTemporalCalendar(_value_).
1. Let _fieldNames_ be ? CalendarFields(_calendar_, « *"day"*, *"month"*, *"monthCode"*, *"year"* »).
1. Let _fields_ be ? PrepareTemporalFields(_value_, _fieldNames_, «»).
Expand Down

0 comments on commit 32b1588

Please sign in to comment.