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

Update Instant.toZonedDateTime{,ISO}. #1115

Merged
merged 1 commit into from
Nov 4, 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/nextWeeklyOccurrence.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function nextWeeklyOccurrence(now, weekday, eventTime, eventTimeZone) {
nextOccurrence = nextOccurrence.add({ days: 7 });
}

return eventTimeZone.getInstantFor(nextOccurrence).toZonedDateTime(now.timeZone, now.calendar).toDateTime();
return eventTimeZone.getInstantFor(nextOccurrence).toZonedDateTime(now).toDateTime();
}

// "Weekly on Thursdays at 08:45 California time":
Expand Down
25 changes: 21 additions & 4 deletions polyfill/lib/instant.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,33 @@ export class Instant {
valueOf() {
throw new TypeError('use compare() or equals() to compare Temporal.Instant');
}
toZonedDateTime(temporalTimeZoneLike, calendarLike) {
toZonedDateTime(item) {
if (!ES.IsTemporalInstant(this)) throw new TypeError('invalid receiver');
const timeZone = ES.ToTemporalTimeZone(temporalTimeZoneLike);
if (ES.Type(item) !== 'Object') {
throw new TypeError('invalid argument in toZonedDateTime');
}
const calendarLike = item.calendar;
if (calendarLike === undefined) {
throw new TypeError('missing calendar property in toZonedDateTime');
}
const calendar = ES.ToTemporalCalendar(calendarLike);
const temporalTimeZoneLike = item.timeZone;
if (temporalTimeZoneLike === undefined) {
throw new TypeError('missing timeZone property in toZonedDateTime');
}
const timeZone = ES.ToTemporalTimeZone(temporalTimeZoneLike);
const TemporalZonedDateTime = GetIntrinsic('%Temporal.ZonedDateTime%');
return new TemporalZonedDateTime(GetSlot(this, EPOCHNANOSECONDS), timeZone, calendar);
}
toZonedDateTimeISO(temporalTimeZoneLike) {
toZonedDateTimeISO(item) {
if (!ES.IsTemporalInstant(this)) throw new TypeError('invalid receiver');
const timeZone = ES.ToTemporalTimeZone(temporalTimeZoneLike);
if (ES.Type(item) === 'Object') {
const timeZoneProperty = item.timeZone;
if (timeZoneProperty !== undefined) {
item = timeZoneProperty;
}
}
const timeZone = ES.ToTemporalTimeZone(item);
const calendar = GetISO8601Calendar();
const TemporalZonedDateTime = GetIntrinsic('%Temporal.ZonedDateTime%');
return new TemporalZonedDateTime(GetSlot(this, EPOCHNANOSECONDS), timeZone, calendar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ for (const [input, output] of values) {
return calendar;
};

const zdt = instant.toZonedDateTime("UTC", input);
const zdt = instant.toZonedDateTime({ timeZone: "UTC", calendar: input });
assert.sameValue(called, 1);
assert.sameValue(zdt.calendar, calendar);
}
Expand All @@ -33,4 +33,4 @@ Temporal.Calendar.from = function() {
throw new Test262Error("Should not call Calendar.from");
};

assert.throws(TypeError, () => instant.toZonedDateTime("UTC", Symbol()));
assert.throws(TypeError, () => instant.toZonedDateTime({ timeZone: "UTC", calendar: Symbol() }));
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ for (const [value, description] of values) {
return value;
};

assert.throws(TypeError, () => instant.toZonedDateTime(timeZone, "test"), description);
assert.throws(TypeError, () => instant.toZonedDateTime({ timeZone, calendar: "test" }), description);
assert.sameValue(called, 1);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Object.defineProperty(Temporal.Calendar, "from", {
},
});

const result = instant.toZonedDateTime(timeZone, "japanese");
const result = instant.toZonedDateTime({ timeZone, calendar: "japanese" });
assert.sameValue(result.epochNanoseconds, instant.epochNanoseconds);
assert.sameValue(result.calendar instanceof Temporal.Calendar, true);
assert.sameValue(result.calendar.id, "japanese");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Object.defineProperty(Temporal.Calendar, "from", {
},
});

const result = instant.toZonedDateTime(timeZone, calendar);
const result = instant.toZonedDateTime({ timeZone, calendar });
assert.sameValue(result.epochNanoseconds, instant.epochNanoseconds);
assert.sameValue(result.calendar, calendar);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Object.defineProperty(Temporal.Calendar, "from", {
},
});

const result = instant.toZonedDateTime(timeZone, calendar);
const result = instant.toZonedDateTime({ timeZone, calendar });
assert.sameValue(result.epochNanoseconds, instant.epochNanoseconds);
assert.sameValue(result.calendar, calendar);

Expand Down
6 changes: 3 additions & 3 deletions polyfill/test/Instant/prototype/toZonedDateTime/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"get Temporal.TimeZone.from",
"call Temporal.TimeZone.from",
"get Temporal.Calendar.from",
"call Temporal.Calendar.from",
"get Temporal.TimeZone.from",
"call Temporal.TimeZone.from",
];

const instant = Temporal.Instant.from("1975-02-02T14:25:36.123456789Z");
Expand Down Expand Up @@ -57,7 +57,7 @@ Object.defineProperty(Temporal.Calendar, "from", {
},
});

const result = instant.toZonedDateTime("UTC", "iso8601");
const result = instant.toZonedDateTime({ timeZone: "UTC", calendar: "iso8601" });
assert.sameValue(result.epochNanoseconds, instant.epochNanoseconds);
assert.sameValue(result.calendar, calendar);

Expand Down
2 changes: 1 addition & 1 deletion polyfill/test/Instant/prototype/toZonedDateTime/length.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ includes: [propertyHelper.js]
---*/

verifyProperty(Temporal.Instant.prototype.toZonedDateTime, "length", {
value: 2,
value: 1,
writable: false,
enumerable: false,
configurable: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const timeZone = new Proxy({
},
});

const result = instant.toZonedDateTime(timeZone, calendar);
const result = instant.toZonedDateTime({ timeZone, calendar });
assert.sameValue(result.epochNanoseconds, instant.epochNanoseconds);

assert.compareArray(actual, expected);
14 changes: 7 additions & 7 deletions polyfill/test/instant.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1307,20 +1307,20 @@ describe('Instant', () => {
describe('Instant.toZonedDateTime() works', () => {
const inst = Instant.from('1976-11-18T14:23:30.123456789Z');
it('throws without parameter', () => {
throws(() => inst.toZonedDateTime(), RangeError);
throws(() => inst.toZonedDateTime(), TypeError);
});
it('throws with only one parameter', () => {
throws(() => inst.toZonedDateTime('Asia/Singapore'));
it('throws with a string parameter', () => {
throws(() => inst.toZonedDateTime('Asia/Singapore'), TypeError);
});
it('time zone parameter UTC', () => {
const tz = Temporal.TimeZone.from('UTC');
const zdt = inst.toZonedDateTime(tz, 'gregory');
const timeZone = Temporal.TimeZone.from('UTC');
const zdt = inst.toZonedDateTime({ timeZone, calendar: 'gregory' });
equal(inst.epochNanoseconds, zdt.epochNanoseconds);
equal(`${zdt}`, '1976-11-18T14:23:30.123456789+00:00[UTC][c=gregory]');
});
it('time zone parameter non-UTC', () => {
const tz = Temporal.TimeZone.from('America/New_York');
const zdt = inst.toZonedDateTime(tz, 'gregory');
const timeZone = Temporal.TimeZone.from('America/New_York');
const zdt = inst.toZonedDateTime({ timeZone, calendar: 'gregory' });
equal(inst.epochNanoseconds, zdt.epochNanoseconds);
equal(`${zdt}`, '1976-11-18T09:23:30.123456789-05:00[America/New_York][c=gregory]');
});
Expand Down
15 changes: 12 additions & 3 deletions polyfill/test/zoneddatetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,10 @@ describe('ZonedDateTime', () => {
equal(`${zdt.toDate()}`, '2019-10-29');
});
it('preserves the calendar', () => {
const zdt = Temporal.Instant.from('2019-10-29T09:46:38.271986102Z').toZonedDateTime(tz, 'gregory');
const zdt = Temporal.Instant.from('2019-10-29T09:46:38.271986102Z').toZonedDateTime({
timeZone: tz,
calendar: 'gregory'
});
equal(zdt.toDate().calendar.id, 'gregory');
});
});
Expand All @@ -1302,7 +1305,10 @@ describe('ZonedDateTime', () => {
equal(`${zdt.toYearMonth()}`, '2019-10');
});
it('preserves the calendar', () => {
const zdt = Temporal.Instant.from('2019-10-29T09:46:38.271986102Z').toZonedDateTime(tz, 'gregory');
const zdt = Temporal.Instant.from('2019-10-29T09:46:38.271986102Z').toZonedDateTime({
timeZone: tz,
calendar: 'gregory'
});
equal(zdt.toYearMonth().calendar.id, 'gregory');
});
});
Expand All @@ -1312,7 +1318,10 @@ describe('ZonedDateTime', () => {
equal(`${zdt.toMonthDay()}`, '10-29');
});
it('preserves the calendar', () => {
const zdt = Temporal.Instant.from('2019-10-29T09:46:38.271986102Z').toZonedDateTime(tz, 'gregory');
const zdt = Temporal.Instant.from('2019-10-29T09:46:38.271986102Z').toZonedDateTime({
timeZone: tz,
calendar: 'gregory'
});
equal(zdt.toMonthDay().calendar.id, 'gregory');
});
});
Expand Down
24 changes: 18 additions & 6 deletions spec/instant.html
Original file line number Diff line number Diff line change
Expand Up @@ -446,30 +446,42 @@ <h1>Temporal.Instant.prototype.valueOf ( )</h1>
</emu-clause>

<emu-clause id="sec-temporal.instant.prototype.tozoneddatetime">
<h1>Temporal.Instant.prototype.toZonedDateTime ( _temporalTimeZoneLike_, _calendarLike_ )</h1>
<h1>Temporal.Instant.prototype.toZonedDateTime ( _item_ )</h1>
<p>
The `toZonedDateTime` method takes two arguments, _temporalTimeZoneLike_ and _calendarLike_.
The `toZonedDateTime` method takes one argument _item_.
The following steps are taken:
</p>
<emu-alg>
1. Let _instant_ be the *this* value.
1. Perform ? RequireInternalSlot(_instant_, [[InitializedTemporalInstant]]).
1. Let _timeZone_ be ? ToTemporalTimeZone(_temporalTimeZoneLike_).
1. If Type(_item_) is not Object, then
1. Throw a *TypeError* exception.
1. Let _calendarLike_ be ? Get(_item_, *"calendar"*).
1. If _calendarLike_ is *undefined*, then
1. Throw a *TypeError* exception.
1. Let _calendar_ be ? ToTemporalCalendar(_calendarLike_).
1. Let _temporalTimeZoneLike_ be ? Get(_item_, *"timeZone"*).
1. If _temporalTimeZoneLike_ is *undefined*, then
1. Throw a *TypeError* exception.
1. Let _timeZone_ be ? ToTemporalTimeZone(_temporalTimeZoneLike_).
1. Return ? CreateTemporalZonedDateTime(_instant_.[[Nanoseconds]], _timeZone_, _calendar_).
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal.instant.prototype.tozoneddatetimeiso">
<h1>Temporal.Instant.prototype.toZonedDateTimeISO ( _temporalTimeZoneLike_ )</h1>
<h1>Temporal.Instant.prototype.toZonedDateTimeISO ( _item_ )</h1>
<p>
The `toZonedDateTimeISO` method takes one argument _temporalTimeZoneLike_.
The `toZonedDateTimeISO` method takes one argument _item_.
The following steps are taken:
</p>
<emu-alg>
1. Let _instant_ be the *this* value.
1. Perform ? RequireInternalSlot(_instant_, [[InitializedTemporalInstant]]).
1. Let _timeZone_ be ? ToTemporalTimeZone(_temporalTimeZoneLike_).
1. If Type(_item_) is Object, then
1. Let _timeZoneProperty_ be ? Get(_item_, *"timeZone"*).
1. If _timeZoneProperty_ is not *undefined*, then
1. Set _item_ to _timeZoneProperty_.
1. Let _timeZone_ be ? ToTemporalTimeZone(_item_).
1. Let _calendar_ be ? GetISO8601Calendar().
1. Return ? CreateTemporalZonedDateTime(_instant_.[[Nanoseconds]], _timeZone_, _calendar_).
</emu-alg>
Expand Down