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

Fix Time.from when param is another Time #736

Merged
merged 1 commit into from
Jul 7, 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
3 changes: 2 additions & 1 deletion polyfill/lib/time.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,14 @@ export class Time {
let hour, minute, second, millisecond, microsecond, nanosecond;
if (typeof item === 'object' && item) {
if (ES.IsTemporalTime(item)) {
hour = GetSlot(item, HOUR);
minute = GetSlot(item, MINUTE);
second = GetSlot(item, SECOND);
millisecond = GetSlot(item, MILLISECOND);
microsecond = GetSlot(item, MICROSECOND);
nanosecond = GetSlot(item, NANOSECOND);
} else {
// Intentionally alphabetical
// Intentionally largest to smallest units
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future reference, we can just delete this comment, but no need to block the PR on it. (Calling the property getters on a passed-in object in alphabetical order is mandated by the spec, which is now implemented in ES.ToTemporalTimeRecord. Here, they can be in any old order.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling the property getters on a passed-in object in alphabetical order is mandated by the spec

OK, got it. Out of curiosity, why is alpha order required?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alphabetical doesn't really matter, but some order is required to be specified, since the calls are observable by client code if you pass in a Proxy, for example. I'm not sure why alphabetical was chosen originally as the arbitrary order, but I'd guess it's because there's some precedent elsewhere.

({ hour, minute, second, millisecond, microsecond, nanosecond } = ES.ToTemporalTimeRecord(item));
}
} else {
Expand Down
12 changes: 10 additions & 2 deletions polyfill/test/time.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import Pretty from '@pipobscure/demitasse-pretty';
const { reporter } = Pretty;

import { strict as assert } from 'assert';
const { equal, notEqual, throws } = assert;
const { equal, notEqual, throws, deepEqual } = assert;

import * as Temporal from 'proposal-temporal';
const { Time } = Temporal;
const { Time, DateTime } = Temporal;

describe('Time', () => {
describe('Structure', () => {
Expand Down Expand Up @@ -367,6 +367,14 @@ describe('Time', () => {
equal(`${Time.from('23:59:60', { disambiguation: 'reject' })}`, '23:59:59');
});
it('Time.from(number) is converted to string', () => equal(`${Time.from(1523)}`, `${Time.from('1523')}`));
it('Time.from(time) returns the same properties', () => {
const t = Time.from('2020-02-12T11:42+01:00[Europe/Amsterdam]');
deepEqual(Time.from(t).getFields(), t.getFields());
});
it('Time.from(dateTime) returns the same time properties', () => {
const dt = DateTime.from('2020-02-12T11:42+01:00[Europe/Amsterdam]');
deepEqual(Time.from(dt).getFields(), dt.toTime().getFields());
});
it('Time.from(time) is not the same object', () => {
const t = Time.from('2020-02-12T11:42+01:00[Europe/Amsterdam]');
notEqual(Time.from(t), t);
Expand Down