-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow difference() to return negative durations
This undoes the work in commit d84653e although it's not exactly a revert, because the situation before that was that durations were always positive. We also don't reinstate the behaviour of never returning a duration larger than 12 hours. Now, for all types, calling smaller.difference(larger) will result in a negative duration. See: #782
- Loading branch information
Showing
24 changed files
with
155 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,18 @@ | ||
/** | ||
* @typedef {Object} ElapsedDuration | ||
* @property {string} return.sign - "+" or "-" | ||
* @property {Temporal.Duration} return.duration - Elapsed duration | ||
*/ | ||
/** | ||
* Compute the difference between two instants, suitable for use in a countdown, | ||
* for example. | ||
* | ||
* @param {Temporal.Absolute} then - Instant since when to measure the duration | ||
* @param {Temporal.Absolute} now - Instant until when to measure the duration | ||
* @param {string} [largestUnit=days] - Largest time unit to have in the result | ||
* @returns {ElapsedDuration} Time between `then` and `now` | ||
*/ | ||
function getElapsedDurationSinceInstant(then, now, largestUnit = 'days') { | ||
const sign = Temporal.Absolute.compare(now, then) < 0 ? '-' : '+'; | ||
const duration = sign === '-' ? then.difference(now, { largestUnit }) : now.difference(then, { largestUnit }); | ||
return { sign, duration }; | ||
} | ||
const result = Temporal.Absolute.from('2020-01-09T04:00Z').difference(Temporal.Absolute.from('2020-01-09T00:00Z'), { | ||
largestUnit: 'hours' | ||
}); | ||
assert.equal(`${result}`, 'PT4H'); | ||
|
||
const result = getElapsedDurationSinceInstant( | ||
Temporal.Absolute.from('2020-01-09T00:00Z'), | ||
Temporal.Absolute.from('2020-01-09T04:00Z') | ||
); | ||
assert.equal(`${result.sign}${result.duration}`, '+PT4H'); | ||
|
||
const result2 = getElapsedDurationSinceInstant( | ||
Temporal.Absolute.from('2020-01-09T04:00Z'), | ||
Temporal.Absolute.from('2020-01-09T00:00Z'), | ||
'minutes' | ||
); | ||
assert.equal(`${result2.sign}${result2.duration}`, '-PT240M'); | ||
const result2 = Temporal.Absolute.from('2020-01-09T00:00Z').difference(Temporal.Absolute.from('2020-01-09T04:00Z'), { | ||
largestUnit: 'minutes' | ||
}); | ||
assert.equal(`${result2}`, '-PT240M'); | ||
|
||
// Example of using it in a countdown: | ||
|
||
const { sign, duration } = getElapsedDurationSinceInstant( | ||
Temporal.Absolute.from('2020-04-01T13:00-07:00[America/Los_Angeles]'), | ||
const duration = Temporal.Absolute.from('2020-04-01T13:00-07:00[America/Los_Angeles]').difference( | ||
Temporal.now.absolute() | ||
); | ||
// Note that this does not work unless you have Intl.DurationFormat, which is | ||
// still an early-stage proposal. | ||
`It's ${duration.toLocaleString()} ${sign < 0 ? 'until' : 'since'} the TC39 Temporal presentation`; | ||
`It's ${duration.toLocaleString()} ${duration.sign < 0 ? 'until' : 'since'} the TC39 Temporal presentation`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.