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
Sync IsValidDuration with Temporal, including precision range limits #199
Comments
I believe that the range check may be able to be performed using float math. I'll run some experiments. Either way, the Temporal spec confirms that this can be done using |
See also #157. |
We have TC39-TG1 consensus from the November 2023 meeting for duplicating the limits from Temporal. #173 (comment) |
Looking at the formula:
It is easy to tell if milliseconds, microseconds, or nanoseconds alone cause us to reach the limit. We can simply find the value that causes us to start exceeding 2^53. It happens to be the case that We get problems when we start adding multiple fields together, because the intermediate value might lose decimal places. For example, However, it seems that this is a general problem, not specific to unsafe integers as the spec says. f64 will always be subject to loss of precision in a particular arithmetic operation. I don't remember why Temporal decided to check bounds on the result of this weird formula instead of just checking each field individually, especially fractional seconds. If all the fractional seconds were just capped at MAX_SAFE_INTEGER, then it would never be possible to exceed 2^53 seconds when rounded to the nearest integer. |
This formula is used to be able to represent all valid instant duration spans: let minInstant = new Temporal.Instant(-864n * 10n**19n);
let maxInstant = new Temporal.Instant(864n * 10n**19n);
let duration = minInstant.until(maxInstant, {largestUnit: "milliseconds"});
console.log(duration.milliseconds); This prints |
Temporal was changed to add limits to duration field values:
tc39/proposal-temporal@a9abc12
Relevant Temporal discussions:
tc39/proposal-temporal#1700
tc39/proposal-temporal#2195
We should implement this change in Intl.DurationFormat in order to make it more consistent and also allow calculations without using BigInt / mathematical values.
The following test case will need to be updated:
https://github.com/tc39/test262/blob/main/test/intl402/DurationFormat/prototype/format/precision-exact-mathematical-values.js
The text was updated successfully, but these errors were encountered: