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

Misc fixes #1172

Merged
merged 7 commits into from
Nov 11, 2020
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
2 changes: 1 addition & 1 deletion docs/cookbook/meetingPlanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ timeZones.forEach(({ name, tz }) => {

// Show the date in midnight cells
let formatOptions;
if (columnTime.hour === 0) {
if (columnTime.hour === columnTime.startOfDay().hour) {
formatOptions = { month: 'short', day: 'numeric' };
} else {
formatOptions = { hour: 'numeric' };
Expand Down
4 changes: 2 additions & 2 deletions docs/duration.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ new Temporal.Duration(); // => PT0S

## Static methods

### Temporal.Duration.**from**(_thing_: any, _options_?: object) : Temporal.Duration
### Temporal.Duration.**from**(_thing_: any) : Temporal.Duration

**Parameters:**

Expand Down Expand Up @@ -172,7 +172,7 @@ d.blank; // => true

## Methods

### duration.**with**(_durationLike_: object, _options_?: object) : Temporal.Duration
### duration.**with**(_durationLike_: object) : Temporal.Duration

**Parameters:**

Expand Down
186 changes: 71 additions & 115 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,14 @@ export const ES = ObjectAssign({}, ES2020, {
year = GetSlot(date, ISO_YEAR);
month = GetSlot(date, ISO_MONTH);
day = GetSlot(date, ISO_DAY);
({ hour, minute, second, millisecond, microsecond, nanosecond } = fields);
const TemporalPlainTime = GetIntrinsic('%Temporal.PlainTime%');
const time = calendar.timeFromFields(fields, {}, TemporalPlainTime);
hour = GetSlot(time, ISO_HOUR);
minute = GetSlot(time, ISO_MINUTE);
second = GetSlot(time, ISO_SECOND);
millisecond = GetSlot(time, ISO_MILLISECOND);
microsecond = GetSlot(time, ISO_MICROSECOND);
nanosecond = GetSlot(time, ISO_NANOSECOND);
} else {
({
year,
Expand Down Expand Up @@ -1856,22 +1863,6 @@ export const ES = ObjectAssign({}, ES2020, {

return { deltaDays, hour, minute, second, millisecond, microsecond, nanosecond };
},
BalanceDurationDate: (years, months, startYear, startMonth, startDay) => {
if (months < 0) {
years -= 1;
months += 12;
}
let { year, month } = ES.BalanceYearMonth(startYear + years, startMonth + months);
while (startDay > ES.DaysInMonth(year, month)) {
months -= 1;
if (months < 0) {
years -= 1;
months += 12;
}
({ year, month } = ES.BalanceYearMonth(startYear + years, startMonth + months));
}
return { year, month, years, months };
},
BalanceDuration: (days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, largestUnit) => {
hours = bigInt(hours).add(bigInt(days).multiply(24));
minutes = bigInt(minutes).add(hours.multiply(60));
Expand Down Expand Up @@ -2413,97 +2404,6 @@ export const ES = ObjectAssign({}, ES2020, {
));
return { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds };
},
// TODO: remove AdjustDayRelativeTo after relativeTo lands for duration.add
AdjustDayRelativeTo: (years, months, weeks, days, direction, largestUnit, relativeTo) => {
const calendar = GetSlot(relativeTo, CALENDAR);
const dtRelative = ES.GetTemporalDateTimeFor(
GetSlot(relativeTo, TIME_ZONE),
GetSlot(relativeTo, INSTANT),
calendar
);
const relYear = GetSlot(dtRelative, ISO_YEAR);
const relMonth = GetSlot(dtRelative, ISO_MONTH);
const relDay = GetSlot(dtRelative, ISO_DAY);
const relHour = GetSlot(dtRelative, ISO_HOUR);
const relMinute = GetSlot(dtRelative, ISO_MINUTE);
const relSecond = GetSlot(dtRelative, ISO_SECOND);
const relMillisecond = GetSlot(dtRelative, ISO_MILLISECOND);
const relMicrosecond = GetSlot(dtRelative, ISO_MICROSECOND);
const relNanosecond = GetSlot(dtRelative, ISO_NANOSECOND);
const oneDayEarlier = ES.AddDateTime(
relYear,
relMonth,
relDay,
relHour,
relMinute,
relSecond,
relMillisecond,
relMicrosecond,
relNanosecond,
calendar,
years,
months,
weeks,
days + direction,
0,
0,
0,
0,
0,
0,
'constrain'
);
let hours, minutes, seconds, milliseconds, microseconds, nanoseconds;
({
years,
months,
weeks,
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds
} = ES.DifferenceDateTime(
relYear,
relMonth,
relDay,
relHour,
relMinute,
relSecond,
relMillisecond,
relMicrosecond,
relNanosecond,
oneDayEarlier.year,
oneDayEarlier.month,
oneDayEarlier.day,
oneDayEarlier.hour,
oneDayEarlier.minute,
oneDayEarlier.second,
oneDayEarlier.millisecond,
oneDayEarlier.microsecond,
oneDayEarlier.nanosecond,
calendar,
largestUnit
));
return ES.RoundDuration(
years,
months,
weeks,
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds,
1,
'days',
'ceil',
dtRelative
);
},
DifferenceZonedDateTime: (start, end, largestUnit, roundingIncrement, smallestUnit, roundingMode) => {
const ns1 = GetSlot(start, EPOCHNANOSECONDS);
const ns2 = GetSlot(end, EPOCHNANOSECONDS);
Expand Down Expand Up @@ -2568,9 +2468,29 @@ export const ES = ObjectAssign({}, ES2020, {
ES.DurationSign(years, months, weeks, days, 0, 0, 0, 0, 0, 0) === 1 &&
intermediateNs.greater(ns2)
) {
// TODO: after PlainDate.add rounding lands, uncomment use of relativeTo
// dateDuration = dateDuration.subtract({ days: -1, relativeTo: dtEarlier });
({ years, months, weeks, days } = ES.AdjustDayRelativeTo(years, months, weeks, days, -1, largestUnit, start));
({ years, months, weeks, days } = ES.AddDuration(
years,
months,
weeks,
days,
0,
0,
0,
0,
0,
0,
0,
0,
0,
-1,
0,
0,
0,
0,
0,
0,
dtStart
));
intermediateNs = ES.AddZonedDateTime(
GetSlot(start, INSTANT),
timeZone,
Expand All @@ -2594,7 +2514,29 @@ export const ES = ObjectAssign({}, ES2020, {
let timeRemainderNs = 0;
do {
// calculate length of the next day (day that contains the time remainder)
const oneDayFartherDuration = ES.AdjustDayRelativeTo(years, months, weeks, days, direction, largestUnit, start);
const oneDayFartherDuration = ES.AddDuration(
years,
months,
weeks,
days,
0,
0,
0,
0,
0,
0,
0,
0,
0,
direction,
0,
0,
0,
0,
0,
0,
dtStart
);
const oneDayFartherNs = ES.AddZonedDateTime(
GetSlot(start, INSTANT),
timeZone,
Expand Down Expand Up @@ -2662,14 +2604,28 @@ export const ES = ObjectAssign({}, ES2020, {
// then it'd risk an infinite loop.
isOverflow = (timeRemainderNs - dayLengthNs) * direction >= 0;
if (isOverflow) {
({ years, months, weeks, days } = ES.AdjustDayRelativeTo(
({ years, months, weeks, days } = ES.AddDuration(
years,
months,
weeks,
days,
0,
0,
0,
0,
0,
0,
0,
0,
0,
direction,
largestUnit,
start
0,
0,
0,
0,
0,
0,
dtStart
));
timeRemainderNs -= dayLengthNs;

Expand Down
11 changes: 8 additions & 3 deletions spec/abstractops.html
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,10 @@ <h1>Singular and plural units</h1>
<emu-clause id="sec-temporal-torelativetemporalobject" aoid="ToRelativeTemporalObject">
<h1>ToRelativeTemporalObject ( _options_ )</h1>
<p>
The abstract operation ToRelativeTemporalObject examines the value of the `relativeTo` property of its _options_ argument, and attempts to return a Temporal.ZonedDateTime instance <mark>(TODO)</mark>, Temporal.PlainDateTime instance, or *undefined*, in order of preference.
If none of those are possible, the operation throws a *RangeError*.
The abstract operation ToRelativeTemporalObject examines the value of the `relativeTo` property of its _options_ argument.
If this is not present, it returns *undefined*.
Otherwise, it attempts to return a Temporal.ZonedDateTime instance or Temporal.PlainDateTime instance, in order of preference, by converting the value.
If neither of those are possible, the operation throws a *RangeError*.
</p>
<emu-alg>
1. Assert: Type(_options_) is Object.
Expand All @@ -466,7 +468,10 @@ <h1>ToRelativeTemporalObject ( _options_ )</h1>
1. Let _dateFromFields_ be ? Get(_calendar_, *"dateFromFields"*).
1. Let _dateOptions_ be ! OrdinaryObjectCreate(%Object.prototype%).
1. Let _temporalDate_ be ? Call(_dateFromFields_, _calendar_, « _fields_, _dateOptions_, %Temporal.PlainDate% »).
1. Return ? CreateTemporalDateTime(_temporalDate_.[[ISOYear]], _temporalDate_.[[ISOMonth]], _temporalDate_.[[ISODay]], _fields_.[[Hour]], _fields_.[[Minute]], _fields_.[[Second]], _fields_.[[Millisecond]], _fields_.[[Microsecond]], _fields_.[[Nanosecond]], _calendar_).
1. Let _timeFromFields_ be ? Get(_calendar_, *"timeFromFields"*).
1. Let _timeOptions_ be ! OrdinaryObjectCreate(%Object.prototype%).
1. Let _temporalTime_ be ? Call(_timeFromFields_, _calendar_, « _fields_, _timeOptions_, %Temporal.PlainTime% »).
1. Return ? CreateTemporalDateTime(_temporalDate_.[[ISOYear]], _temporalDate_.[[ISOMonth]], _temporalDate_.[[ISODay]], _temporalTime_.[[ISOHour]], _temporalTime_.[[ISOMinute]], _temporalTime_.[[ISOSecond]], _temporalTime_.[[ISOMillisecond]], _temporalTime_.[[ISOMicrosecond]], _temporalTime_.[[ISONanosecond]]).
1. Let _string_ be ? ToString(_value_).
1. Let _result_ be ? ParseTemporalDateTimeString(_string_).
1. Let _calendar_ be _result_.[[Calendar]].
Expand Down
22 changes: 0 additions & 22 deletions spec/plaindate.html
Original file line number Diff line number Diff line change
Expand Up @@ -966,28 +966,6 @@ <h1>BalanceDate ( _year_, _month_, _day_ )</h1>
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal-balancedurationdate" aoid="BalanceDurationDate">
<h1>BalanceDurationDate ( _years_, _months_, _startYear_, _startMonth_, _startDay_ )</h1>
<emu-alg>
1. If _months_ &lt; 0, then
1. Set _years_ to _years_ − 1.
1. Set _months_ to _months_ + 12.
1. Let _balanceResult_ be ? BalanceYearMonth(_startYear_ + _years_, _startMonth_ + _months_).
1. Repeat, while _startDay_ &gt; ! DaysInMonth(_balanceResult_.[[Year]], _balanceResult_.[[Month]]),
1. Set _months_ to _months_ − 1.
1. If _months_ &lt; 0, then
1. Set _years_ to _years_ − 1.
1. Set _months_ to _months_ + 12.
1. Set _balanceResult_ to ? BalanceYearMonth(_startYear_ + _years_, _startMonth_ + _months_).
1. Return the new Record {
[[Year]]: _balanceResult_.[[Year]],
[[Month]]: _balanceResult_.[[Month]],
[[Years]]: _years_,
[[Months]]: _months_
}.
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal-padyear" aoid="PadYear">
<h1>PadYear ( _y_ )</h1>
<emu-alg>
Expand Down
Loading