Skip to content

Commit

Permalink
Polyfill: Add missing brand checks in TimeZone.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger committed Jun 4, 2021
1 parent 191710e commit 2c8aa80
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 41 deletions.
2 changes: 2 additions & 0 deletions polyfill/lib/timezone.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class TimeZone {
return ES.GetIANATimeZoneOffsetNanoseconds(GetSlot(instant, EPOCHNANOSECONDS), id);
}
getOffsetStringFor(instant) {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
instant = ES.ToTemporalInstant(instant);
return ES.BuiltinTimeZoneGetOffsetStringFor(this, instant);
}
Expand All @@ -63,6 +64,7 @@ export class TimeZone {
return ES.BuiltinTimeZoneGetPlainDateTimeFor(this, instant, calendar);
}
getInstantFor(dateTime, options = undefined) {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
dateTime = ES.ToTemporalDateTime(dateTime);
options = ES.GetOptionsObject(options);
const disambiguation = ES.ToTemporalDisambiguation(options);
Expand Down
21 changes: 21 additions & 0 deletions polyfill/test/TimeZone/prototype/getInstantFor/branding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.timezone.prototype.getinstantfor
features: [Symbol]
---*/

const getInstantFor = Temporal.TimeZone.prototype.getInstantFor;

assert.sameValue(typeof getInstantFor, "function");

assert.throws(TypeError, () => getInstantFor.call(undefined), "undefined");
assert.throws(TypeError, () => getInstantFor.call(null), "null");
assert.throws(TypeError, () => getInstantFor.call(true), "true");
assert.throws(TypeError, () => getInstantFor.call(""), "empty string");
assert.throws(TypeError, () => getInstantFor.call(Symbol()), "symbol");
assert.throws(TypeError, () => getInstantFor.call(1), "1");
assert.throws(TypeError, () => getInstantFor.call({}), "plain object");
assert.throws(TypeError, () => getInstantFor.call(Temporal.TimeZone), "Temporal.TimeZone");
assert.throws(TypeError, () => getInstantFor.call(Temporal.TimeZone.prototype), "Temporal.TimeZone.prototype");
21 changes: 21 additions & 0 deletions polyfill/test/TimeZone/prototype/getOffsetStringFor/branding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.timezone.prototype.getoffsetstringfor
features: [Symbol]
---*/

const getOffsetStringFor = Temporal.TimeZone.prototype.getOffsetStringFor;

assert.sameValue(typeof getOffsetStringFor, "function");

assert.throws(TypeError, () => getOffsetStringFor.call(undefined), "undefined");
assert.throws(TypeError, () => getOffsetStringFor.call(null), "null");
assert.throws(TypeError, () => getOffsetStringFor.call(true), "true");
assert.throws(TypeError, () => getOffsetStringFor.call(""), "empty string");
assert.throws(TypeError, () => getOffsetStringFor.call(Symbol()), "symbol");
assert.throws(TypeError, () => getOffsetStringFor.call(1), "1");
assert.throws(TypeError, () => getOffsetStringFor.call({}), "plain object");
assert.throws(TypeError, () => getOffsetStringFor.call(Temporal.TimeZone), "Temporal.TimeZone");
assert.throws(TypeError, () => getOffsetStringFor.call(Temporal.TimeZone.prototype), "Temporal.TimeZone.prototype");

This file was deleted.

6 changes: 0 additions & 6 deletions polyfill/test/usertimezone.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,14 @@ describe('Userland time zone', () => {
};

const inst = Temporal.Instant.fromEpochNanoseconds(0n);
const dt = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 456, 789);

it('has offset string +00:00', () =>
equal(Temporal.TimeZone.prototype.getOffsetStringFor.call(obj, inst), '+00:00'));
it('converts to DateTime', () => {
equal(`${Temporal.TimeZone.prototype.getPlainDateTimeFor.call(obj, inst)}`, '1970-01-01T00:00:00');
equal(
`${Temporal.TimeZone.prototype.getPlainDateTimeFor.call(obj, inst, 'gregory')}`,
'1970-01-01T00:00:00[u-ca=gregory]'
);
});
it('converts to Instant', () => {
equal(`${Temporal.TimeZone.prototype.getInstantFor.call(obj, dt)}`, '1976-11-18T15:23:30.123456789Z');
});
it('offset prints in instant.toString', () => equal(inst.toString({ timeZone: obj }), '1970-01-01T00:00:00+00:00'));
it('prints in zdt.toString', () => {
const zdt = new Temporal.ZonedDateTime(0n, obj);
Expand Down

0 comments on commit 2c8aa80

Please sign in to comment.