Skip to content

Clarity on maximum Duration nanoseconds value #3106

@nekevss

Description

@nekevss

Mentioned briefly during the champions call.

I was hoping to get some clarity on the max time duration for nanoseconds, specifically when creating a Duration.

Background

Currently, the maximum valid Duration for a normalized nanosecond is 9,007,199,254,740,991,999,999,999 (2^53 * 10^9 - 1). This number is both VERY large, and also not valid when provided to IsValidDuration, which, due to floating arithmetic appears to have a maximum duration value of 9,007,199,254,740,991,463,129,087 (beyond this point floating point rounds up to 2^53, below it appears to round to 2^53 - 1).

Examples

The test262 test that the below tests are based off is: Duration/prototype/round/out-of-range-when-converting-from-normalized-duration.js.

But the below example better highlights the boundary mentioned in the above background.

import { Temporal } from "@js-temporal/polyfill"

const d = new Temporal.Duration(0, 0, 0, 0, 0, 0, /* s = */ Number.MAX_SAFE_INTEGER, 0, 0, /* ns = */ 463_129_087);
let result = d.round({ largestUnit: "nanoseconds", roundingIncrement: 1})
console.log(result.toString()) // PT9007199254740990.926258176S

try {
    const e = new Temporal.Duration(0, 0, 0, 0, 0, 0, /* s = */ Number.MAX_SAFE_INTEGER, 0, 0, /* ns = */ 463_129_088);
    e.round({ largestUnit: "nanoseconds", roundingIncrement: 1})
} catch (e) {
    console.log(e) // Throws range error
}

Looking for general thoughts on the maximum size of the Duration, whether this behavior is intended or could/should be updated.

FWIW, also, the day range of Duration a duration with only day + time fields is very large as defined by IsValidDuration. (2^53 - 1) / 86,400 = 104,249,991,374 days, where the maximum epoch day range is 100,000,000 day.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions