Skip to content

Commit

Permalink
Convert calendar arguments in SytemDateTime.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger committed Aug 18, 2020
1 parent 454c5a9 commit b5700a8
Show file tree
Hide file tree
Showing 22 changed files with 429 additions and 15 deletions.
3 changes: 2 additions & 1 deletion polyfill/lib/calendar.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global __debug__ */

import { ES } from './ecmascript.mjs';
import { GetIntrinsic, MakeIntrinsicClass } from './intrinsicclass.mjs';
import { GetIntrinsic, MakeIntrinsicClass, DefineIntrinsic } from './intrinsicclass.mjs';
import { CALENDAR_ID, ISO_YEAR, ISO_MONTH, ISO_DAY, CreateSlots, GetSlot, SetSlot } from './slots.mjs';

export class Calendar {
Expand Down Expand Up @@ -112,6 +112,7 @@ export class Calendar {
}

MakeIntrinsicClass(Calendar, 'Temporal.Calendar');
DefineIntrinsic('Temporal.Calendar.from', Calendar.from);

class ISO8601 extends Calendar {
constructor(id = 'iso8601') {
Expand Down
15 changes: 15 additions & 0 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,21 @@ export const ES = ObjectAssign({}, ES2019, {
ToTemporalYearMonthRecord: (bag) => {
return ES.ToRecord(bag, [['era', undefined], ['month'], ['year']]);
},
CalendarFrom: (calendarLike) => {
const TemporalCalendar = GetIntrinsic('%Temporal.Calendar%');
let from = TemporalCalendar.from;
if (from === undefined) {
from = GetIntrinsic('%Temporal.Calendar.from%');
}
return ES.Call(from, TemporalCalendar, [calendarLike]);
},
ToTemporalCalendar: (calendarLike) => {
if (typeof calendarLike === 'object' && calendarLike) {
return calendarLike;
}
const identifier = ES.ToString(calendarLike);
return ES.CalendarFrom(identifier);
},
TimeZoneFrom: (temporalTimeZoneLike) => {
const TemporalTimeZone = GetIntrinsic('%Temporal.TimeZone%');
let from = TemporalTimeZone.from;
Expand Down
8 changes: 5 additions & 3 deletions polyfill/lib/now.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GetDefaultCalendar } from './calendar.mjs';
import { ES } from './ecmascript.mjs';
import { GetIntrinsic } from './intrinsicclass.mjs';

Expand All @@ -13,13 +14,14 @@ function absolute() {
const Absolute = GetIntrinsic('%Temporal.Absolute%');
return new Absolute(ES.SystemUTCEpochNanoSeconds());
}
function dateTime(temporalTimeZoneLike = timeZone(), calendar = undefined) {
function dateTime(temporalTimeZoneLike = timeZone(), calendarLike = GetDefaultCalendar()) {
const timeZone = ES.ToTemporalTimeZone(temporalTimeZoneLike);
const calendar = ES.ToTemporalCalendar(calendarLike);
const abs = absolute();
return ES.GetTemporalDateTimeFor(timeZone, abs, calendar);
}
function date(temporalTimeZoneLike, calendar = undefined) {
return ES.TemporalDateTimeToDate(dateTime(temporalTimeZoneLike, calendar));
function date(temporalTimeZoneLike, calendarLike = undefined) {
return ES.TemporalDateTimeToDate(dateTime(temporalTimeZoneLike, calendarLike));
}
function time(temporalTimeZoneLike) {
return ES.TemporalDateTimeToTime(dateTime(temporalTimeZoneLike));
Expand Down
33 changes: 33 additions & 0 deletions polyfill/test/now/date/calendar-convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.now.date
---*/

const values = [
[null, "null"],
[true, "true"],
["iso8601", "iso8601"],
[2020, "2020"],
[2n, "2"],
];

const calendar = Temporal.Calendar.from("iso8601");
const original = Temporal.Calendar.from;
for (const [input, output] of values) {
Temporal.Calendar.from = function(argument) {
assert.sameValue(argument, output);
// TODO: Remove.
Temporal.Calendar.from = original;
return calendar;
};

Temporal.now.date("UTC", input);
}

Temporal.Calendar.from = function() {
throw new Test262Error("Should not call Calendar.from");
};

assert.throws(TypeError, () => Temporal.now.date("UTC", Symbol()));
49 changes: 49 additions & 0 deletions polyfill/test/now/date/calendar-from-undefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.now.date
includes: [compareArray.js]
---*/

const actual = [];
const expected = [
"get Temporal.Calendar.from",
"get timeZone.getDateTimeFor",
"call timeZone.getDateTimeFor",
];
const dateTime = Temporal.DateTime.from("1963-07-02T12:34:56.987654321");

const timeZone = new Proxy({
getDateTimeFor(absolute, calendar) {
actual.push("call timeZone.getDateTimeFor");
assert.sameValue(absolute instanceof Temporal.Absolute, true, "Absolute");
assert.sameValue(calendar instanceof Temporal.Calendar, true, "Calendar");
assert.sameValue(calendar.id, "japanese");
return dateTime;
},
}, {
has(target, property) {
actual.push(`has timeZone.${property}`);
return property in target;
},
get(target, property) {
actual.push(`get timeZone.${property}`);
return target[property];
},
});

Object.defineProperty(Temporal.Calendar, "from", {
get() {
actual.push("get Temporal.Calendar.from");
return undefined;
},
});

const result = Temporal.now.date(timeZone, "japanese");
assert.sameValue(result instanceof Temporal.Date, true);
for (const property of ["year", "month", "day"]) {
assert.sameValue(result[property], dateTime[property], property);
}

assert.compareArray(actual, expected);
68 changes: 68 additions & 0 deletions polyfill/test/now/date/calendar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.now.date
includes: [compareArray.js]
---*/

const actual = [];
const expected = [
"get Temporal.TimeZone.from",
"call Temporal.TimeZone.from",
"get Temporal.Calendar.from",
"call Temporal.Calendar.from",
"get timeZone.getDateTimeFor",
"call timeZone.getDateTimeFor",
];
const dateTime = Temporal.DateTime.from("1963-07-02T12:34:56.987654321");

const calendar = {};

const timeZone = new Proxy({
getDateTimeFor(absolute, calendarArg) {
actual.push("call timeZone.getDateTimeFor");
assert.sameValue(absolute instanceof Temporal.Absolute, true, "Absolute");
assert.sameValue(calendarArg, calendar);
return dateTime;
},
}, {
has(target, property) {
actual.push(`has timeZone.${property}`);
return property in target;
},
get(target, property) {
actual.push(`get timeZone.${property}`);
return target[property];
},
});

Object.defineProperty(Temporal.TimeZone, "from", {
get() {
actual.push("get Temporal.TimeZone.from");
return function(argument) {
actual.push("call Temporal.TimeZone.from");
assert.sameValue(argument, "UTC");
return timeZone;
};
},
});

Object.defineProperty(Temporal.Calendar, "from", {
get() {
actual.push("get Temporal.Calendar.from");
return function(argument) {
actual.push("call Temporal.Calendar.from");
assert.sameValue(argument, "iso8601");
return calendar;
};
},
});

const result = Temporal.now.date("UTC", "iso8601");
assert.sameValue(result instanceof Temporal.Date, true);
for (const property of ["year", "month", "day"]) {
assert.sameValue(result[property], dateTime[property], property);
}

assert.compareArray(actual, expected);
4 changes: 3 additions & 1 deletion polyfill/test/now/date/timezone-invalid-result.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ const invalidValues = [

for (const dateTime of invalidValues) {
const timeZone = {
getDateTimeFor(absolute) {
getDateTimeFor(absolute, calendar) {
assert.sameValue(absolute instanceof Temporal.Absolute, true, "Absolute");
assert.sameValue(calendar instanceof Temporal.Calendar, true, "Calendar");
assert.sameValue(calendar.id, "iso8601");
return dateTime;
},
};
Expand Down
4 changes: 3 additions & 1 deletion polyfill/test/now/date/timezone-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ const expected = [
const dateTime = Temporal.DateTime.from("1963-07-02T12:34:56.987654321");

const timeZone = new Proxy({
getDateTimeFor(absolute) {
getDateTimeFor(absolute, calendar) {
actual.push("call timeZone.getDateTimeFor");
assert.sameValue(absolute instanceof Temporal.Absolute, true, "Absolute");
assert.sameValue(calendar instanceof Temporal.Calendar, true, "Calendar");
assert.sameValue(calendar.id, "iso8601");
return dateTime;
},
}, {
Expand Down
11 changes: 10 additions & 1 deletion polyfill/test/now/date/timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ const expected = [
const dateTime = Temporal.DateTime.from("1963-07-02T12:34:56.987654321");

const timeZone = new Proxy({
getDateTimeFor(absolute) {
getDateTimeFor(absolute, calendar) {
actual.push("call timeZone.getDateTimeFor");
assert.sameValue(absolute instanceof Temporal.Absolute, true, "Absolute");
assert.sameValue(calendar instanceof Temporal.Calendar, true, "Calendar");
assert.sameValue(calendar.id, "iso8601");
return dateTime;
},
}, {
Expand All @@ -43,6 +45,13 @@ Object.defineProperty(Temporal.TimeZone, "from", {
},
});

Object.defineProperty(Temporal.Calendar, "from", {
get() {
actual.push("get Temporal.Calendar.from");
return undefined;
},
});

const result = Temporal.now.date("UTC");
assert.sameValue(result instanceof Temporal.Date, true);
for (const property of ["year", "month", "day"]) {
Expand Down
33 changes: 33 additions & 0 deletions polyfill/test/now/dateTime/calendar-convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.now.datetime
---*/

const values = [
[null, "null"],
[true, "true"],
["iso8601", "iso8601"],
[2020, "2020"],
[2n, "2"],
];

const calendar = Temporal.Calendar.from("iso8601");
const original = Temporal.Calendar.from;
for (const [input, output] of values) {
Temporal.Calendar.from = function(argument) {
assert.sameValue(argument, output);
// TODO: Remove.
Temporal.Calendar.from = original;
return calendar;
};

Temporal.now.dateTime("UTC", input);
}

Temporal.Calendar.from = function() {
throw new Test262Error("Should not call Calendar.from");
};

assert.throws(TypeError, () => Temporal.now.dateTime("UTC", Symbol()));
46 changes: 46 additions & 0 deletions polyfill/test/now/dateTime/calendar-from-undefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.now.datetime
includes: [compareArray.js]
---*/

const actual = [];
const expected = [
"get Temporal.Calendar.from",
"get timeZone.getDateTimeFor",
"call timeZone.getDateTimeFor",
];
const dateTime = Temporal.DateTime.from("1963-07-02T12:34:56.987654321");

const timeZone = new Proxy({
getDateTimeFor(absolute, calendar) {
actual.push("call timeZone.getDateTimeFor");
assert.sameValue(absolute instanceof Temporal.Absolute, true, "Absolute");
assert.sameValue(calendar instanceof Temporal.Calendar, true, "Calendar");
assert.sameValue(calendar.id, "japanese");
return dateTime;
},
}, {
has(target, property) {
actual.push(`has timeZone.${property}`);
return property in target;
},
get(target, property) {
actual.push(`get timeZone.${property}`);
return target[property];
},
});

Object.defineProperty(Temporal.Calendar, "from", {
get() {
actual.push("get Temporal.Calendar.from");
return undefined;
},
});

const result = Temporal.now.dateTime(timeZone, "japanese");
assert.sameValue(result, dateTime);

assert.compareArray(actual, expected);
Loading

0 comments on commit b5700a8

Please sign in to comment.