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

Duration.getTimeInMillis() returns wrong values for long durations

XMLWordPrintable

    • 1.3
    • generic
    • generic

        Name: erR10175 Date: 10/29/2003


         
          The methods

        public final long getTimeInMillis(Calendar startInstant)
        public final long getTimeInMillis(Date startInstant)

        of the class javax.xml.datatype.Duration return unexpected values for big
        durations that are still in the range of the type long.
         
        The documentation of the methods do not specify an acceptable range of the duration
        object. So the whole range of the type long (from Long.MIN_VALUE up to Long.MAX_VALUE
        of milliseconds) is expected to be processed correctly. But results are incorrect
        for long durations greater than 185542587187199999 ms. Some more incorrectly processed
        values are shown in the sample below. All of those values measured in milliseconds are
        expected to be within the range of the type long.

        This bug affects new test in JCK 1.5 (not yet integrated)
           api/javax_xml/datatype/Duration/index.html#DurationTests[GetTimeInMillis002]
           api/javax_xml/datatype/Duration/index.html#DurationTests[GetTimeInMillis003]
           api/javax_xml/datatype/Duration/index.html#DurationTests[GetTimeInMillis006]
           api/javax_xml/datatype/Duration/index.html#DurationTests[GetTimeInMillis007]

        To reproduce the bug compile and run the following code as shown
        in the log below:
        ------------------------------ test.java
        import javax.xml.datatype.Duration;
        import java.util.Calendar;
        import java.util.GregorianCalendar;

        public class test {
            static final long [] ms = {
                0,
                185542587187199998L,
                185542587187199999L,
                185542587187200000L,
                185542587187200001L,
                9223372036854775807L,
            };
            static final String [] msS = {
                "-PT9223372036854775.808S",
                "-PT1S",
                "PT0S",
                "PT185542587187199.999S",
                "PT9223372036854775.807S",
            };
            public static void main(String [] args) {
                
                Calendar c = new GregorianCalendar();
                c.setTimeInMillis(0);

                for(int i = 0; i < ms.length; ++i) {
                    System.out.println("new Duration(" + ms[i]
                        + "L).getTimeInMillis(c) returns "
                        + new Duration(ms[i]).getTimeInMillis(c));
                }
                for(int i = 0; i < msS.length; ++i) {
                    System.out.println("new Duration(\"" + msS[i]
                        + "\").getTimeInMillis(c) returns "
                        + new Duration(msS[i]).getTimeInMillis(c));
                }
            }
        }
        ----------------------------------------------------

        ------------------------------------------------ log
        $javac test.java && java -cp . -showversion test
        java version "1.5.0-beta"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b25)
        Java HotSpot(TM) Client VM (build 1.5.0-beta-b25, mixed mode)

        new Duration(0L).getTimeInMillis(c) returns 0
        new Duration(185542587187199998L).getTimeInMillis(c) returns 185542587187199998
        new Duration(185542587187199999L).getTimeInMillis(c) returns 185542587187199999
        new Duration(185542587187200000L).getTimeInMillis(c) returns -185542587187200000
        new Duration(185542587187200001L).getTimeInMillis(c) returns -185542587187199999
        new Duration(9223372036854775807L).getTimeInMillis(c) returns -53757322505224193
        new Duration("-PT9223372036854775.808S").getTimeInMillis(c) returns 1511828488192
        new Duration("-PT1S").getTimeInMillis(c) returns -1000
        new Duration("PT0S").getTimeInMillis(c) returns 0
        new Duration("PT185542587187199.999S").getTimeInMillis(c) returns -1
        new Duration("PT9223372036854775.807S").getTimeInMillis(c) returns -1511828488193
        ----------------------------------------------------

        ======================================================================

              kkawagucsunw Kohsuke Kawaguchi (Inactive)
              reysunw Rey Rey (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: