Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8307578

Error computing the amount of milli and micro seconds between java.time.Instants

    XMLWordPrintable

Details

    • CSR
    • Resolution: Approved
    • P3
    • 20-pool, 21
    • core-libs
    • None
    • behavioral
    • low
    • The difference in milliseconds between Instants may be higher than expected by no more than 999 milliseconds. Errors in the difference of microseconds are unlikely, typical Instant values from system clocks have only microsecond precision.
    • Java API
    • Implementation

    Description

      Summary

      When computing the time between java.time.Instants using MILLIS.between(t1, t2) or MICROS.between(t1, t2) or t1.until(t2, MILLIS) or t1.until(t2, MICROS) the difference may be incorrect.

      Problem

      The implementation of java.time.Instant.until(Instant t, TemporalUnit unit) is specialized for computing the number of milliseconds or microseconds between two Instants. The representation of an Instant is the number of seconds and the number of nanoseconds. The implementation computing the number of units between, as of JDK 18, did not propagate carry and borrow between seconds and nanoseconds when computing milliseconds and microseconds between Instants.

      The computations are correct in cases where the difference in nanoseconds is whole milliseconds or microseconds respectively.

      Solution

      In the cases where the difference in the number of seconds is negative and the difference in the difference of nano-seconds is positive and the case where the difference in seconds is positive and the difference in nano-seconds is negative the difference in the number of seconds is updated.

      Specification

      There is no change to the specification, the implementation is updated to correctly return the number of milliseconds and microseconds between the instants.

      Attachments

        Issue Links

          Activity

            People

              rriggs Roger Riggs
              rriggs Roger Riggs
              Naoto Sato
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: