2

I need some help to calculate the difference between two dates with SPARQL in Ontotext GraphDB. I know that SPARQL protocol does not support arithmetic operation on dates, however some SPARQL engines support it.

Just as an example in Fuseki I could do .

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?duration
WHERE {
  BIND (("2011-02-04T14:45:13.815-05:00"^^xsd:dateTime - "2011-02- 
  02T14:45:13.815-05:00"^^xsd:dateTime) AS ?duration)
}

the result is duration: "P2DT0H0M0.000S"^^xsd:duration. Then I can get 2 days diff, or Virtuoso provides a built-in function bif:datediff.

My question is, if is there something similar on GraphDB to solve this easy problem without a big workaround.

Thanks in advance.

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
valdo2627
  • 23
  • 2
  • [On FactForge](http://factforge.net/sparql?name=&infer=false&sameAs=false&query=PREFIX%20dbo:%20%3Chttp:%2F%2Fdbpedia.org%2Fontology%2F%3E%0APREFIX%20dbr:%20%3Chttp:%2F%2Fdbpedia.org%2Fresource%2F%3E%0APREFIX%20xsd:%20%3Chttp:%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%3E%0Aselect%20*%20%7B%20%0Aservice%20%3Chttps:%2F%2Fquery.wikidata.org%2Fsparql%3E%0A%20%20%20%20%7B%0A%20%20%20%20BIND%20((%222011-02-04T14:45:13%22%5E%5Exsd:dateTime%20-%20%222011-02-02T14:45:14%22%5E%5Exsd:dateTime)%20AS%20%3Fduration)%20%20%20%20%0A%20%20%20%20%7D%0A%7D) – Stanislav Kralin Sep 07 '18 at 15:15
  • For datetimes within the same month, you could [extract](https://www.w3.org/TR/sparql11-query/#func-date-time) days, hours, minutes etc. and perform calculations manually. – Stanislav Kralin Sep 07 '18 at 15:20
  • Hi Stanislav Kralin, thanks for your help. I will mark your first response as a solution. However, I still have the same problem, because is a close network, no internet access from the VM where the server is running. No way I may use external service. And unfortunately the dates are not within the same month. So, for now, I will request the data with sparql and finish the calculation and response on a backend function. Thanks. – valdo2627 Sep 10 '18 at 13:48
  • So what about accepting the answer :)? – Stanislav Kralin Nov 03 '18 at 16:00

1 Answers1

0

That was solved in 8.7.0 (released 28 September):

GDB-2887 As a GraphDB user I need support of “+” / “-” operations between xsd:dateTime and xsd:duration with SPARQL

Now your query should return "P2DT0H0M0.000S"^^xsd:dateTimeDuration.


Alternatively, one could use federated queries to public endpoints based on Virtuoso, Blazegraph, Fuseki etc.:

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX bif: <bif:>
SELECT ?duration1 ?duration2 ?duration3 ?duration4 { 
    VALUES (?start ?end)
        {("2011-02-02T14:45:14"^^xsd:dateTime "2011-02-04T14:45:13"^^xsd:dateTime)}
    SERVICE <http://dbpedia.org/sparql>           #-- Virtuoso
        { BIND ((?end - ?start)/86400.0 AS ?duration1) }
    SERVICE <http://dbpedia.org/sparql>           #-- Virtuoso
        { BIND (bif:datediff("day", ?start, ?end) AS ?duration2) }
    SERVICE <https://query.wikidata.org/sparql>   #-- Blazegraph
        { BIND ((?end - ?start) AS ?duration3) }
    SERVICE <http://zbw.eu/beta/sparql/stw/query> #-- Fuseki
        { BIND (day(?end - ?start) AS ?duration4) }
}

Try on FactForge!

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58