Skip to content

Commit

Permalink
Add skipped tests for more ZonedDateTime relativeTo functionality
Browse files Browse the repository at this point in the history
These tests don't work yet, but it's nonetheless good to have them
reviewed and landed in the test suite.

See: #1171
  • Loading branch information
ptomato committed Nov 12, 2020
1 parent ee8d095 commit 768a17f
Showing 1 changed file with 165 additions and 10 deletions.
175 changes: 165 additions & 10 deletions polyfill/test/duration.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -479,15 +479,44 @@ describe('Duration', () => {
const relativeTo = Temporal.ZonedDateTime.from('2017-01-01T00:00[America/Montevideo]');
equal(`${oneDay.add(hours24, { relativeTo })}`, 'P2D');
});
it.skip('relativeTo does affect days if ZonedDateTime, and duration encompasses DST change', () => {
const relativeTo = Temporal.ZonedDateTime.from('2019-11-02T00:00[America/Vancouver]');
equal(`${oneDay.add(hours24, { relativeTo })}`, 'P1D24H');
const skippedHourDay = Temporal.ZonedDateTime.from('2019-03-10T00:00[America/Vancouver]');
const repeatedHourDay = Temporal.ZonedDateTime.from('2019-11-03T00:00[America/Vancouver]');
const inRepeatedHour = Temporal.ZonedDateTime.from('2019-11-03T01:00-07:00[America/Vancouver]');
const hours12 = new Duration(0, 0, 0, 0, 12);
const hours25 = new Duration(0, 0, 0, 0, 25);
describe.skip('relativeTo affects days if ZonedDateTime, and duration encompasses DST change', () => {
it('start inside repeated hour, end after', () => {
equal(`${oneDay.add(hours25, { relativeTo: inRepeatedHour })}`, 'P2D');
});
it('start inside repeated hour, end in skipped hour', () => {
equal(`${oneDay.add(Duration.from({ days: 125, hours: 1 }), { relativeTo: inRepeatedHour })}`, 'P126D');
});
it('start in normal hour, end in skipped hour', () => {
const relativeTo = Temporal.ZonedDateTime.from('2019-03-08T01:30[America/Vancouver]');
equal(`${oneDay.add(hours25, { relativeTo })}`, 'P2DT1H');
});
it('start before skipped hour, end >1 day after', () => {
equal(`${oneDay.add(hours25, { relativeTo: skippedHourDay })}`, 'P2DT2H');
});
it('start before skipped hour, end <1 day after', () => {
equal(`${oneDay.add(hours12, { relativeTo: skippedHourDay })}`, 'P1DT13H');
});
it('start before repeated hour, end >1 day after', () => {
equal(`${oneDay.add(hours25, { relativeTo: repeatedHourDay })}`, 'P2D');
});
it('start before repeated hour, end <1 day after', () => {
equal(`${oneDay.add(hours12, { relativeTo: repeatedHourDay })}`, 'P1DT11H');
});
it('Samoa skipped 24 hours', () => {
const relativeTo = Temporal.ZonedDateTime.from('2011-12-28T12:00-10:00[Pacific/Apia]');
equal(`${oneDay.add(hours25, { relativeTo })}`, 'P3DT1H');
});
});
it.skip('casts relativeTo to ZonedDateTime if possible', () => {
equal(`${oneDay.add(hours24, { relativeTo: '2019-11-02T00:00[America/Vancouver]' })}`, 'P1D24H');
equal(`${oneDay.add(hours24, { relativeTo: '2019-11-02T00:00[America/Vancouver]' })}`, 'P1DT24H');
equal(
`${oneDay.add(hours24, { relativeTo: { year: 2019, month: 11, day: 3, timeZone: 'America/Vancouver' } })}`,
'P1D24H'
`${oneDay.add(hours24, { relativeTo: { year: 2019, month: 11, day: 2, timeZone: 'America/Vancouver' } })}`,
'P1DT24H'
);
});
it('casts relativeTo to PlainDateTime if possible', () => {
Expand Down Expand Up @@ -628,12 +657,41 @@ describe('Duration', () => {
const relativeTo = Temporal.ZonedDateTime.from('2017-01-01T00:00[America/Montevideo]');
equal(`${oneDay.subtract(hours24, { relativeTo })}`, 'PT0S');
});
it.skip('relativeTo does affect days if ZonedDateTime, and duration encompasses DST change', () => {
const relativeTo = Temporal.ZonedDateTime.from('2019-11-03T00:00[America/Vancouver]');
equal(`${oneDay.subtract(hours24, { relativeTo })}`, 'PT1H');
const skippedHourDay = Temporal.ZonedDateTime.from('2019-03-10T00:00[America/Vancouver]');
const repeatedHourDay = Temporal.ZonedDateTime.from('2019-11-03T00:00[America/Vancouver]');
const inRepeatedHour = Temporal.ZonedDateTime.from('2019-11-03T01:00-07:00[America/Vancouver]');
const twoDays = new Duration(0, 0, 0, 2);
const threeDays = new Duration(0, 0, 0, 3);
describe.skip('relativeTo affects days if ZonedDateTime, and duration encompasses DST change', () => {
it('start inside repeated hour, end after', () => {
equal(`${oneDay.subtract(hours24, { relativeTo: inRepeatedHour })}`, 'PT1H');
});
it('start inside repeated hour, end in skipped hour', () => {
equal(`${Duration.from({ days: 127, hours: 1 }).subtract(oneDay, { relativeTo: inRepeatedHour })}`, 'P126D');
});
it('start in normal hour, end in skipped hour', () => {
const relativeTo = Temporal.ZonedDateTime.from('2019-03-10T01:30[America/Vancouver]');
equal(`${oneDay.subtract(hours24, { relativeTo })}`, '-PT1H');
});
it('start before skipped hour, end >1 day after', () => {
equal(`${threeDays.subtract(hours24, { relativeTo: skippedHourDay })}`, 'P2DT1H');
});
it('start before skipped hour, end <1 day after', () => {
equal(`${twoDays.subtract(hours24, { relativeTo: skippedHourDay })}`, 'P1DT1H');
});
it('start before repeated hour, end >1 day after', () => {
equal(`${threeDays.subtract(hours24, { relativeTo: repeatedHourDay })}`, 'P1DT23H');
});
it('start before repeated hour, end <1 day after', () => {
equal(`${twoDays.subtract(hours24, { relativeTo: repeatedHourDay })}`, 'PT24H');
});
it('Samoa skipped 24 hours', () => {
const relativeTo = Temporal.ZonedDateTime.from('2011-12-29T12:00-10:00[Pacific/Apia]');
equal(`${threeDays.subtract(hours24, { relativeTo })}`, 'P1D');
});
});
it.skip('casts relativeTo to ZonedDateTime if possible', () => {
equal(`${oneDay.subtract(hours24, { relativeTo: '2019-11-02T00:00[America/Vancouver]' })}`, 'PT1H');
equal(`${oneDay.subtract(hours24, { relativeTo: '2019-11-03T00:00[America/Vancouver]' })}`, 'PT1H');
equal(
`${oneDay.subtract(hours24, { relativeTo: { year: 2019, month: 11, day: 3, timeZone: 'America/Vancouver' } })}`,
'PT1H'
Expand Down Expand Up @@ -869,6 +927,53 @@ describe('Duration', () => {
const relativeTo = Temporal.ZonedDateTime.from('2017-01-01T00:00[America/Montevideo]');
equal(`${hours25.round({ largestUnit: 'days', relativeTo })}`, 'P1DT1H');
});
const skippedHourDay = Temporal.ZonedDateTime.from('2019-03-10T00:00[America/Vancouver]');
const repeatedHourDay = Temporal.ZonedDateTime.from('2019-11-03T00:00[America/Vancouver]');
const inRepeatedHour = Temporal.ZonedDateTime.from('2019-11-03T01:00-07:00[America/Vancouver]');
const hours12 = new Duration(0, 0, 0, 0, 12);
describe.skip('relativeTo affects days if ZonedDateTime, and duration encompasses DST change', () => {
it('start inside repeated hour, end after', () => {
equal(`${hours25.round({ largestUnit: 'days', relativeTo: inRepeatedHour })}`, 'P1D');
});
it('start inside repeated hour, end in skipped hour', () => {
// gain one hour at the beginning, lose one at the end, days average 24h
equal(
Duration.from({ days: 126, hours: 1 }).round({ largestUnit: 'hours', relativeTo: inRepeatedHour }).hours,
126 * 24 + 1
);
});
it('start in normal hour, end in skipped hour', () => {
const relativeTo = Temporal.ZonedDateTime.from('2019-03-09T01:30[America/Vancouver]');
equal(`${hours25.round({ largestUnit: 'days', relativeTo })}`, 'P1DT2H');
});
it('start before skipped hour, end >1 day after', () => {
equal(`${hours25.round({ largestUnit: 'days', relativeTo: skippedHourDay })}`, 'P1DT2H');
});
it('start before skipped hour, end <1 day after', () => {
equal(`${hours12.round({ largestUnit: 'days', relativeTo: skippedHourDay })}`, 'PT12H');
});
it('start before repeated hour, end >1 day after', () => {
equal(`${hours25.round({ largestUnit: 'days', relativeTo: repeatedHourDay })}`, 'P1D');
});
it('start before repeated hour, end <1 day after', () => {
equal(`${hours12.round({ largestUnit: 'days', relativeTo: repeatedHourDay })}`, 'PT12H');
});
it('Samoa skipped 24 hours', () => {
const relativeTo = Temporal.ZonedDateTime.from('2011-12-29T12:00-10:00[Pacific/Apia]');
equal(`${hours25.round({ largestUnit: 'days', relativeTo })}`, 'P2DT1H');
equal(`${Duration.from({ hours: 48 }).round({ largestUnit: 'days', relativeTo })}`, 'P3D');
});
});
it.skip('casts relativeTo to ZonedDateTime if possible', () => {
equal(`${hours25.round({ largestUnit: 'days', relativeTo: '2019-11-03T00:00[America/Vancouver]' })}`, 'P1D');
equal(
`${hours25.round({
largestUnit: 'days',
relativeTo: { year: 2019, month: 11, day: 3, timeZone: 'America/Vancouver' }
})}`,
'P1D'
);
});
it('casts relativeTo to PlainDateTime if possible', () => {
equal(`${hours25.round({ largestUnit: 'days', relativeTo: '2019-11-02T00:00' })}`, 'P1DT1H');
equal(`${hours25.round({ largestUnit: 'days', relativeTo: { year: 2019, month: 11, day: 2 } })}`, 'P1DT1H');
Expand Down Expand Up @@ -1354,6 +1459,56 @@ describe('Duration', () => {
const relativeTo = Temporal.ZonedDateTime.from('2017-01-01T00:00[America/Montevideo]');
equal(oneDay.total({ unit: 'hours', relativeTo }), 24);
});
const skippedHourDay = Temporal.ZonedDateTime.from('2019-03-10T00:00[America/Vancouver]');
const repeatedHourDay = Temporal.ZonedDateTime.from('2019-11-03T00:00[America/Vancouver]');
const inRepeatedHour = Temporal.ZonedDateTime.from('2019-11-03T01:00-07:00[America/Vancouver]');
const hours12 = new Duration(0, 0, 0, 0, 12);
describe.skip('relativeTo affects days if ZonedDateTime, and duration encompasses DST change', () => {
it('start inside repeated hour, end after', () => {
equal(oneDay.total({ unit: 'hours', relativeTo: inRepeatedHour }), 25);
equal(oneDay.total({ unit: 'hours', relativeTo: inRepeatedHour.add({ hours: 1 }) }), 24);
});
it('start inside repeated hour, end in skipped hour', () => {
// gain one hour at the beginning, lose one at the end, days average 24h
equal(
Duration.from({ days: 126, hours: 1 }).total({ unit: 'hours', relativeTo: inRepeatedHour }),
126 * 24 + 1
);
});
it('start in normal hour, end in skipped hour', () => {
const relativeTo = Temporal.ZonedDateTime.from('2019-03-09T01:30[America/Vancouver]');
equal(Duration.from({ days: 1, hours: 1 }).total({ unit: 'hours', relativeTo }), 24);
});
it('start before skipped hour, end >1 day after', () => {
equal(oneDay.total({ unit: 'hours', relativeTo: skippedHourDay }), 23);
});
it('start before skipped hour, end <1 day after', () => {
equal(hours12.total({ unit: 'hours', relativeTo: skippedHourDay }), 12);
});
it('start before repeated hour, end >1 day after', () => {
equal(oneDay.total({ unit: 'hours', relativeTo: repeatedHourDay }), 25);
});
it('start before repeated hour, end <1 day after', () => {
equal(hours12.total({ unit: 'hours', relativeTo: repeatedHourDay }), 12);
});
it('Samoa skipped 24 hours', () => {
const relativeTo = Temporal.ZonedDateTime.from('2011-12-29T12:00-10:00[Pacific/Apia]');
equal(Duration.from({ days: 2 }).total({ unit: 'hours', relativeTo }), 24);
equal(Duration.from({ days: 3 }).total({ unit: 'hours', relativeTo }), 48);
});
});
it.skip('totaling back up to days', () => {
const relativeTo = Temporal.ZonedDateTime.from('2019-11-02T00:00[America/Vancouver]');
equal(Duration.from({ hours: 48 }).total({ unit: 'days' }), 2);
equal(Duration.from({ hours: 48 }).total({ unit: 'days', relativeTo }), 1 + 24 / 25);
});
it.skip('casts relativeTo to ZonedDateTime if possible', () => {
equal(oneDay.total({ unit: 'hours', relativeTo: '2019-11-03T00:00[America/Vancouver]' }), 25);
equal(
oneDay.total({ unit: 'hours', relativeTo: { year: 2019, month: 11, day: 3, timeZone: 'America/Vancouver' } }),
25
);
});
it('balances up to the next unit after rounding', () => {
const almostWeek = Duration.from({ days: 6, hours: 20 });
equal(almostWeek.total({ unit: 'weeks', relativeTo: '2020-01-01' }), (6 + 20 / 24) / 7);
Expand Down

0 comments on commit 768a17f

Please sign in to comment.