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

Computing micros between two instants unexpectedly overflows for some cases

XMLWordPrintable

    • b15
    • generic
    • generic
    • Verified

      A DESCRIPTION OF THE PROBLEM :
      When the distance between two instants is larger that Long.MAX_VALUE nanos, but less than Long.MAX_VALUE micros, ChronoUnit.MICROS.between() fails. This is due to the computation performed, which in OpenJDK 11 is in java.time.Instant, line 1148: nanosUntil(end) / 1000. nanosUntil overflows and throws an arithmetic exception.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Instant gregorianEpoch = Instant.EPOCH.minusSeconds(12219292800l);
      Instant now = Instant.now();
      long micros = ChronoUnit.MICROS.between(gregorianEpoch, now); -> Math.addExact throws ArithmeticException

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Correct long value returned
      ACTUAL -
      ArithmeticException

      FREQUENCY : always


            naoto Naoto Sato
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: