[ 
https://issues.apache.org/jira/browse/JENA-1402?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16211520#comment-16211520
 ] 

Greg Albiston commented on JENA-1402:
-------------------------------------

Hi Andy,

It does look like a bug in javax.xml.datatype.Duration.

Can Jena be switched over to the new JDK8 Time API?

This gives correct results for the two incorrect examples given previously.
The new Java Time should overcome issues in the old API and is compliant with 
ISO-8601 used by the XSD dateTime, time and duration datatypes.
[http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html#close]

Thanks,

Greg

{code:java}
import java.time.Duration;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;

public class ExampleApp {

    public static void main(String[] args) {

        //Jena result: PT883M0.020S
        //Java result: PT53S
        Duration dur1 = Duration.parse("PT2M3.123S");
        Duration dur2 = Duration.parse("PT1M10.123S");
        Duration result = dur1.minus(dur2);
        System.out.println(dur1 + " - " + dur2 + " = " + result);

        //Jena result: PT1H3883M0.020S
        //Java result: PT1H3M53S
        dur1 = Duration.parse("PT1H4M3.123S");
        dur2 = Duration.parse("PT0M10.123S");
        result = dur1.minus(dur2);   //gives "PT53S" - correct
        System.out.println(dur1 + " - " + dur2 + " = " + result);

        //Jena result: PT883M0.020S
        //Java result: PT883M0.020S
        DatatypeFactory factory = DatatypeFactory.newInstance();
        javax.xml.datatype.Duration dt1 = factory.newDuration("PT2M3.123S");
        javax.xml.datatype.Duration dt2 = factory.newDuration("PT1M10.123S");

        javax.xml.datatype.Duration res1 = dt1.subtract(dt2);
        System.out.println(dt1 + " - " + dt2 + " = " + res1);
    }
}
{code}


> Subtracting two xsd:Duration gives incorrect results in SPARQL query
> --------------------------------------------------------------------
>
>                 Key: JENA-1402
>                 URL: https://issues.apache.org/jira/browse/JENA-1402
>             Project: Apache Jena
>          Issue Type: Bug
>          Components: ARQ
>    Affects Versions: Jena 3.4.0
>            Reporter: Greg Albiston
>
> There is an issue when subtracting two xsd:durations that include:
> * decimal seconds
> * non-zero minutes
> * second operand has a greater number of seconds than the first operand, i.e. 
> the minutes are reduced. 
> The result is a large number of minutes and incorrect seconds.
> For example:
> Integer, Larger: "PT2M3S" - "PT1M10S"  = "PT0M53S" CORRECT
> Decimal, Smaller: "PT2M3.123S" - "PT1M1.123S" = "PT1M2.000S" CORRECT
> Decimal, Larger, Seconds: "PT0M3.123S" - "PT1M10.123S"  = "-PT1M7.000S" 
> CORRECT
> Decimal, Larger, Minutes: "PT2M3.123S" - "PT1M10.123S"  = "PT883M0.020S" 
> INCORRECT
> Decimal, Larger, Hours: "PT1H4M3.123S" - "PT0M10.123S" = "PT1H3883M0.020S" 
> INCORRECT
> Example SPARQL:
> {code:sparql}
> SELECT ?res ?op1 ?op2
> WHERE{
>    VALUES (?op1 ?op2) {
>         ("PT2M3S"^^<http://www.w3.org/2001/XMLSchema#duration> 
> "PT1M10S"^^<http://www.w3.org/2001/XMLSchema#duration>)
>         ("PT2M3.123S"^^<http://www.w3.org/2001/XMLSchema#duration> 
> "PT1M1.123S"^^<http://www.w3.org/2001/XMLSchema#duration>)
>         ("PT0M3.123S"^^<http://www.w3.org/2001/XMLSchema#duration> 
> "PT1M10.123S"^^<http://www.w3.org/2001/XMLSchema#duration>)
>         ("PT2M3.123S"^^<http://www.w3.org/2001/XMLSchema#duration> 
> "PT1M10.123S"^^<http://www.w3.org/2001/XMLSchema#duration>)
>         ("PT1H4M3.123S"^^<http://www.w3.org/2001/XMLSchema#duration> 
> "PT0M10.123S"^^<http://www.w3.org/2001/XMLSchema#duration>)
>     }
>     BIND(?op1 - ?op2 AS ?res)
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to