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

Duration.toString violates specification

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 8, 9
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b101
    • CPU:
      generic
    • OS:
      generic

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_66"
      Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
      Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      In the JavaDoc, for Duration.toString, it is specified that the "hours, minutes and seconds will all have the same sign". However, this is violated in certain cases. For instance, while Duration.ofSeconds(-59, 100000000) correctly prints as "PT-58.9S", Duration.ofSeconds(-60, 100000000) prints as "PT-1M0.1S" with a negative minute, but a positive second, when "PT-59.9S" would be the expected result.


      REPRODUCIBILITY :
      This bug can be reproduced always.

        Activity

        Hide
        psonal Pallavi Sonal added a comment - - edited
        Following are the results of executing attached test case:
        JDK 8u66 - Fail
        JDK 8u72 - Fail
        JDK 9EA b93 - Fail

        Here is the output:
        PT-58.9S
        PT-1M0.1S

        Following is the link of the Java API for Duration.toString():
        https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#toString--
        Show
        psonal Pallavi Sonal added a comment - - edited Following are the results of executing attached test case: JDK 8u66 - Fail JDK 8u72 - Fail JDK 9EA b93 - Fail Here is the output: PT-58.9S PT-1M0.1S Following is the link of the Java API for Duration.toString(): https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#toString--
        Hide
        ntv Nadeesh Tv (Inactive) added a comment - - edited
        For Duration.ofSeconds(-60, 100000000).toString(). Expected result is "PT-1M0.1S" not "PT-59.9S"
        From the java doc of Duration.toString()
        The format of the returned string will be {@code PTnHnMnS}, where n is
             * the relevant hours, minutes or seconds part of the duration.
              * If a section has a zero value, it is omitted.
        In this case 60 second can be express as 1 Minute therefore Second part is zero. Therefore Second is zero, not "positive or negative"

        '1_000_000_00' passing to Duration.ofSeconds() is the nanoSecondadjuster. If 'nanoSecondadjuster (incase of +ve sign)' is less than 1_000_000_000 then it will represent as the fractional part of second
          From java doc - "Any fractional seconds are placed after a decimal point i the seconds section."
        Therefore the behavior seen is as expected not an issue.

        But there may be a minor typo in the java doc.
        I think 'i' should replace with 'in' : "decimal point i the seconds section" -> "decimal point in the seconds section"

        Roger, Please confirm
        Show
        ntv Nadeesh Tv (Inactive) added a comment - - edited For Duration.ofSeconds(-60, 100000000).toString(). Expected result is "PT-1M0.1S" not "PT-59.9S" From the java doc of Duration.toString() The format of the returned string will be {@code PTnHnMnS}, where n is      * the relevant hours, minutes or seconds part of the duration.       * If a section has a zero value, it is omitted. In this case 60 second can be express as 1 Minute therefore Second part is zero. Therefore Second is zero, not "positive or negative" '1_000_000_00' passing to Duration.ofSeconds() is the nanoSecondadjuster. If 'nanoSecondadjuster (incase of +ve sign)' is less than 1_000_000_000 then it will represent as the fractional part of second   From java doc - "Any fractional seconds are placed after a decimal point i the seconds section." Therefore the behavior seen is as expected not an issue. But there may be a minor typo in the java doc. I think 'i' should replace with 'in' : "decimal point i the seconds section" -> "decimal point in the seconds section" Roger, Please confirm
        Hide
        rriggs Roger Riggs added a comment -
        The statement "The hours, minutes and seconds will all have the same sign." is easy to challenge.
        Duration.ofSeconds(-60, 00000001); => "PT-1M0.000000001S"

        The minutes and seconds have different signs.
        I don't have a good way to explain it yet.

        Yes, to the typo.


        Show
        rriggs Roger Riggs added a comment - The statement "The hours, minutes and seconds will all have the same sign." is easy to challenge. Duration.ofSeconds(-60, 00000001); => "PT-1M0.000000001S" The minutes and seconds have different signs. I don't have a good way to explain it yet. Yes, to the typo.
        Hide
        scolebourne Stephen Colebourne added a comment -
        Yes, this is a bug. It affects situations where the amount is between -59 seconds and -60 seconds. The same problem afflicts between -119 and -120 seconds. And no doubt more.

        These both represent the same amount, and the toString works OK:
        Duration.ofSeconds(-58, -900_000_000);
        Duration.ofSeconds(-59, 100_000_000);
        PT-58.9S

        These both represent the same amount, but the toString is broken:
        Duration.ofSeconds(-59, -900_000_000);
        Duration.ofSeconds(-60, 100_000_000);
        PT-1M0.1S (should be PT-59.9S)

        These both represent the same amount, and the toString works OK:
        Duration.ofSeconds(-60, -900_000_000);
        Duration.ofSeconds(-61, 100_000_000);
        PT-1M-0.9S

        Show
        scolebourne Stephen Colebourne added a comment - Yes, this is a bug. It affects situations where the amount is between -59 seconds and -60 seconds. The same problem afflicts between -119 and -120 seconds. And no doubt more. These both represent the same amount, and the toString works OK: Duration.ofSeconds(-58, -900_000_000); Duration.ofSeconds(-59, 100_000_000); PT-58.9S These both represent the same amount, but the toString is broken: Duration.ofSeconds(-59, -900_000_000); Duration.ofSeconds(-60, 100_000_000); PT-1M0.1S (should be PT-59.9S) These both represent the same amount, and the toString works OK: Duration.ofSeconds(-60, -900_000_000); Duration.ofSeconds(-61, 100_000_000); PT-1M-0.9S
        Hide
        hgupdate HG Updates added a comment -
        URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/c65bb5305208
        User: rriggs
        Date: 2016-01-05 15:16:53 +0000
        Show
        hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/c65bb5305208 User: rriggs Date: 2016-01-05 15:16:53 +0000
        Hide
        hgupdate HG Updates added a comment -
        URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/c65bb5305208
        User: lana
        Date: 2016-01-13 21:05:17 +0000
        Show
        hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/c65bb5305208 User: lana Date: 2016-01-13 21:05:17 +0000

          People

          • Assignee:
            ntv Nadeesh Tv (Inactive)
            Reporter:
            webbuggrp Webbug Group
          • Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: