Search code examples
sparqlrdfsemantic-webowlrdfs

sparql how to filter according to the date range


I have the following filters that filters my data to the item in the last week.

#just consider the likes in the last one week.
  filter (?ratingDate >= "2017-03-01T00:00:00"^^xsd:dateTime )

as you see i set the dateTime of the last week in my hand (hard coded), is there a way to set this date automatially?

I'm looking for something like now-7day


Solution

  • (consolidating the comments)

    now - 7 days requires a number of things:

    There is a function in SPARQL for the current point in time: now()

    "7 days" is an xsd:duration, "P7D"^^xsd:duration

    "-" is arithmetic involving an xsd:dateTime and an xsd:duration -- the operation is op:subtract-yearMonthDuration-from-dateTime (from "XPath and XQuery Functions and Operators") -- and also overloading the "-" operation to dispatch to that function.

    You need to check with the SPARQL engine you are using as to whether the extension of xsd:dateTime and an xsd:duration arithmetic is supported.

    Once you have the calculated xsd:dateTime for "now - 7 days", the ">=" comparison of a part of standard SPARQL:

    FILTER ( ?ratingDate >= (now()-"P7D"^^xsd:duration) )