Skip to content

Commit

Permalink
Fix Time.from when parameter is another Time
Browse files Browse the repository at this point in the history
- Fixes #735.
- Adds tests for `Time.from` when the source is `Time` or `DateTime`.
- Also corrects an inaccurate comment.
  • Loading branch information
justingrant authored and ptomato committed Jul 7, 2020
1 parent 15601b1 commit 2161891
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
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
({ 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

0 comments on commit 2161891

Please sign in to comment.