Skip to content

Bug in Instant.plus(Duration) on JS for large Instants and small Durations #263

Closed
@lukellmann

Description

@lukellmann

Running this code on JS produces an unexpected result

val instant = Instant.fromEpochMilliseconds(Long.MAX_VALUE) + 999_999.nanoseconds
println(instant) // expected some positive (clamped) value, got -1000000-01-01T00:00:00Z

The reason for this seems to be this line of code.

  1. Instant.fromEpochMilliseconds(Long.MAX_VALUE) gives the max instant on JS (+1000000-12-31T23:59:59.999999999Z)
  2. plusFix overflows and throws a DateTimeException
  3. here is the actual bug: because the added amount is smaller than one second, seconds is 0, resulting in Instant.MIN being returned

The line should probably be something like this instead:

if (duration.isPositive()) MAX else MIN

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions