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

remove observable calls to "from" #1419

Merged
merged 4 commits into from
Mar 8, 2021
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
29 changes: 9 additions & 20 deletions docs/cookbook/stockExchangeTimeZone.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,6 @@ class NYSETimeZone extends Temporal.TimeZone {

const tzNYSE = Object.freeze(new NYSETimeZone());

// Monkeypatch Temporal.TimeZone.from to handle our custom time zone
let oldTzFrom;
const replacement = (item) => {
if (item === 'NYSE') return tzNYSE;
return oldTzFrom.call(Temporal.TimeZone, item);
};
if (Temporal.TimeZone.from !== replacement) {
oldTzFrom = Temporal.TimeZone.from;
Temporal.TimeZone.from = replacement;
}

let zdt;
let isOpen;
let date;
Expand All @@ -165,16 +154,16 @@ assert.equal(date.toString(), '2020-11-12');

// 2. Is the stock market open on a particular date?
date = Temporal.PlainDate.from('2020-11-12');
isOpen = date.toZonedDateTime('NYSE').toPlainDate().equals(date);
isOpen = date.toZonedDateTime(tzNYSE).toPlainDate().equals(date);
assert.equal(isOpen, true);
date = Temporal.PlainDate.from('2020-11-14');
isOpen = date.toZonedDateTime('NYSE').toPlainDate().equals(date);
isOpen = date.toZonedDateTime(tzNYSE).toPlainDate().equals(date);
assert.equal(isOpen, false);

// 3. For a particular date, when is the next market day?
const getNextMarketDay = (date) => {
date = Temporal.PlainDate.from(date);
const zdt = date.toZonedDateTime('NYSE');
const zdt = date.toZonedDateTime(tzNYSE);
if (zdt.toPlainDate().equals(date)) {
// It's a market day, so find the next one
return zdt.add({ days: 1 }).toPlainDate();
Expand All @@ -195,22 +184,22 @@ assert.equal(newDate.equals('2020-11-16'), true);
// If it's closed, then when will it open next?
// Return a result in the local time zone, not NYC's time zone.
zdt = Temporal.ZonedDateTime.from('2020-11-12T18:50-08:00[America/Los_Angeles]');
inNYSE = zdt.withTimeZone('NYSE');
isOpen = inNYSE.toPlainDateTime().toZonedDateTime('NYSE').equals(inNYSE);
inNYSE = zdt.withTimeZone(tzNYSE);
isOpen = inNYSE.toPlainDateTime().toZonedDateTime(tzNYSE).equals(inNYSE);
assert.equal(isOpen, false);
nextOpen = inNYSE.timeZone.getNextTransition(zdt.toInstant()).toZonedDateTimeISO(zdt.timeZone);
assert.equal(nextOpen.toString(), '2020-11-13T06:30:00-08:00[America/Los_Angeles]');

zdt = Temporal.ZonedDateTime.from('2020-11-12T12:50-08:00[America/Los_Angeles]');
inNYSE = zdt.withTimeZone('NYSE');
isOpen = inNYSE.toPlainDateTime().toZonedDateTime('NYSE').equals(inNYSE);
inNYSE = zdt.withTimeZone(tzNYSE);
isOpen = inNYSE.toPlainDateTime().toZonedDateTime(tzNYSE).equals(inNYSE);
assert.equal(isOpen, true);
todayClose = inNYSE.timeZone.getNextTransition(zdt.toInstant()).toZonedDateTimeISO(zdt.timeZone);
assert.equal(todayClose.toString(), '2020-11-12T13:00:00-08:00[America/Los_Angeles]');

// 5. For any particular market date, what were the opening and closing clock times in NYC?
date = Temporal.PlainDate.from('2020-11-09');
openInstant = date.toZonedDateTime('NYSE').toInstant();
closeInstant = date.toZonedDateTime('NYSE').timeZone.getNextTransition(openInstant);
openInstant = date.toZonedDateTime(tzNYSE).toInstant();
closeInstant = date.toZonedDateTime(tzNYSE).timeZone.getNextTransition(openInstant);
assert.equal(openInstant.toZonedDateTimeISO('America/New_York').toPlainTime().toString(), '09:30:00');
assert.equal(closeInstant.toZonedDateTimeISO('America/New_York').toPlainTime().toString(), '16:00:00');
18 changes: 2 additions & 16 deletions polyfill/lib/calendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,7 @@ export class Calendar {
return ES.ToString(this);
}
static from(item) {
if (ES.Type(item) === 'Object') {
if (!('calendar' in item)) return item;
item = item.calendar;
if (ES.Type(item) === 'Object' && !('calendar' in item)) return item;
}
const stringIdent = ES.ToString(item);
if (IsBuiltinCalendar(stringIdent)) return new Calendar(stringIdent);
let calendar;
try {
({ calendar } = ES.ParseISODateTime(stringIdent, { zoneRequired: false }));
} catch {
throw new RangeError(`Invalid calendar: ${stringIdent}`);
}
if (!calendar) calendar = 'iso8601';
return new Calendar(calendar);
return ES.CalendarFrom(item, this);
}
}

Expand Down Expand Up @@ -2000,6 +1986,6 @@ impl['gregory'] = ObjectAssign({}, nonIsoGeneralImpl, { helper: helperGregory })

const BUILTIN_CALENDAR_IDS = Object.keys(impl);

function IsBuiltinCalendar(id) {
export function IsBuiltinCalendar(id) {
return ArrayIncludes.call(BUILTIN_CALENDAR_IDS, id);
}
45 changes: 27 additions & 18 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
MICROSECONDS,
NANOSECONDS
} from './slots.mjs';
import { IsBuiltinCalendar } from './calendar.mjs';

const DAYMILLIS = 86400000;
const NS_MIN = bigInt(-86400).multiply(1e17);
Expand Down Expand Up @@ -1481,7 +1482,7 @@ export const ES = ObjectAssign({}, ES2020, {
calendar
} = ES.ParseTemporalZonedDateTimeString(ES.ToString(item)));
if (!ianaName) throw new RangeError('time zone ID required in brackets');
timeZone = ES.TimeZoneFrom(ianaName);
timeZone = ES.TimeZoneFrom(ianaName, GetIntrinsic('%Temporal.TimeZone%'));
if (!calendar) calendar = ES.GetISO8601Calendar();
calendar = ES.ToTemporalCalendar(calendar);
}
Expand Down Expand Up @@ -1513,17 +1514,22 @@ export const ES = ObjectAssign({}, ES2020, {
const TemporalCalendar = GetIntrinsic('%Temporal.Calendar%');
return new TemporalCalendar('iso8601');
},
CalendarFrom: (calendarLike) => {
const TemporalCalendar = GetIntrinsic('%Temporal.Calendar%');
let from = TemporalCalendar.from;
if (from === undefined) {
from = GetIntrinsic('%Temporal.Calendar.from%');
CalendarFrom: (item, constructor) => {
if (ES.Type(item) === 'Object') {
if (!('calendar' in item)) return item;
item = item.calendar;
if (ES.Type(item) === 'Object' && !('calendar' in item)) return item;
}
const calendar = ES.Call(from, TemporalCalendar, [calendarLike]);
if (ES.Type(calendar) !== 'Object') {
throw new TypeError('Temporal.Calendar.from should return an object');
const stringIdent = ES.ToString(item);
if (IsBuiltinCalendar(stringIdent)) return new constructor(stringIdent);
let calendar;
try {
({ calendar } = ES.ParseISODateTime(stringIdent, { zoneRequired: false }));
} catch {
throw new RangeError(`Invalid calendar: ${stringIdent}`);
}
return calendar;
if (!calendar) calendar = 'iso8601';
return new constructor(calendar);
},
CalendarFields: (calendar, fieldNames) => {
const fields = ES.GetMethod(calendar, 'fields');
Expand Down Expand Up @@ -1633,7 +1639,7 @@ export const ES = ObjectAssign({}, ES2020, {
return calendarLike;
}
const identifier = ES.ToString(calendarLike);
return ES.CalendarFrom(identifier);
return ES.CalendarFrom(identifier, GetIntrinsic('%Temporal.Calendar%'));
},
CalendarCompare: (one, two) => {
const cal1 = ES.ToString(one);
Expand Down Expand Up @@ -1674,20 +1680,23 @@ export const ES = ObjectAssign({}, ES2020, {
if (!ES.IsTemporalMonthDay(result)) throw new TypeError('invalid result');
return result;
},
TimeZoneFrom: (temporalTimeZoneLike) => {
const TemporalTimeZone = GetIntrinsic('%Temporal.TimeZone%');
let from = TemporalTimeZone.from;
if (from === undefined) {
from = GetIntrinsic('%Temporal.TimeZone.from%');
TimeZoneFrom: (item, constructor) => {
if (ES.Type(item) === 'Object') {
if (!('timeZone' in item)) return item;
item = item.timeZone;
if (ES.Type(item) === 'Object' && !('timeZone' in item)) return item;
}
return ES.Call(from, TemporalTimeZone, [temporalTimeZoneLike]);
const timeZone = ES.TemporalTimeZoneFromString(ES.ToString(item));
const result = new constructor(timeZone);
if (!ES.IsTemporalTimeZone(result)) throw new TypeError('invalid result');
return result;
},
ToTemporalTimeZone: (temporalTimeZoneLike) => {
if (ES.Type(temporalTimeZoneLike) === 'Object') {
return temporalTimeZoneLike;
}
const identifier = ES.ToString(temporalTimeZoneLike);
return ES.TimeZoneFrom(identifier);
return ES.TimeZoneFrom(identifier, GetIntrinsic('%Temporal.TimeZone%'));
},
TimeZoneCompare: (one, two) => {
const tz1 = ES.ToString(one);
Expand Down
11 changes: 1 addition & 10 deletions polyfill/lib/timezone.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,9 @@ export class TimeZone {
return ES.ToString(this);
}
static from(item) {
if (ES.Type(item) === 'Object') {
if (!('timeZone' in item)) return item;
item = item.timeZone;
if (ES.Type(item) === 'Object' && !('timeZone' in item)) return item;
}
const timeZone = ES.TemporalTimeZoneFromString(ES.ToString(item));
const result = new this(timeZone);
if (!ES.IsTemporalTimeZone(result)) throw new TypeError('invalid result');
return result;
return ES.TimeZoneFrom(item, this);
}
}

MakeIntrinsicClass(TimeZone, 'Temporal.TimeZone');
DefineIntrinsic('Temporal.TimeZone.from', TimeZone.from);
DefineIntrinsic('Temporal.TimeZone.prototype.getOffsetNanosecondsFor', TimeZone.prototype.getOffsetNanosecondsFor);

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ includes: [compareArray.js]
---*/

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

const instant = Temporal.Instant.from("1975-02-02T14:25:36.123456789Z");
const dateTime = Temporal.PlainDateTime.from("1963-07-02T12:34:56.987654321");
Expand All @@ -30,13 +28,6 @@ const timeZone = new Proxy({
},
});

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

const result = instant.toZonedDateTime({ timeZone, calendar: "japanese" });
assert.sameValue(result.epochNanoseconds, instant.epochNanoseconds);
assert.sameValue(result.calendar instanceof Temporal.Calendar, true);
Expand Down
64 changes: 0 additions & 64 deletions polyfill/test/Instant/prototype/toZonedDateTime/calendar.js

This file was deleted.

Loading