eclipse-rdf4j / rdf4j

Eclipse RDF4J: scalable RDF for Java
https://rdf4j.org/
BSD 3-Clause "New" or "Revised" License
367 stars 164 forks source link

Date/date-time subtraction #5171

Open frensjan opened 2 weeks ago

frensjan commented 2 weeks ago

Problem description

Some SPARQL implementation support subtracting dates and date-times. RDF4J does not. I've whipped up a basic implementation in our RDF4J based implementation. Would such an implementation be okay for a PR?

See also https://github.com/w3c/sparql-dev/issues/32.

I've hacked something like the following in XMLDatatypeMathUtil:

private static Literal operationsBetweenCalendars(Literal leftLit, Literal rightLit, MathOp op, ValueFactory vf) {
  XMLGregorianCalendar left = (XMLGregorianCalendar) leftLit.calendarValue().clone();
  XMLGregorianCalendar right = (XMLGregorianCalendar) rightLit.calendarValue().clone();

  if (op == MathOp.MINUS) {
    var difference = java.time.Duration.between(
      toZonedDateTime(right),
      toZonedDateTime(left)
    );
    var string = ...
    return buildLiteral(dtFactory.newDuration(string), vf);
  }

  ...
}

There are some finicky bits though here due to the conversion between java.time and javax.xml :( So I'm not guaranteeing 100% compatibility with xpath (op:subtract-dateTimes and friends).

Preferred solution

I think it would be good if RDF4J follows other implementations and supports the minus operator here. A function could also work, but is not ideal I think from a usage perspective. From an extension perspective it is of course.

Are you interested in contributing a solution yourself?

Yes

Alternatives you've considered

No response

Anything else?

No response