Nicer date and time handling in SPARQL 1.2

Add, subtract, and ADJUST() dates and times.

[SPARQL logo and calendar]

SPARQL 1.1 has been with us for about ten years. Work on SPARQL 1.2 is currently underway, and one nice set of improvements will let us do much more with date and time values.

I didn’t realize how minimal SPARQL 1.1’s ability to handle these was until I saw the introductory material in the Add Support Durations, Dates, and Times issue recently added to the SPARQL 1.2 development discussion. I had never noticed how the SPARQL 1.1 Recommendation explicitly says that it supports the xsd:dateTime data type without mentioning xsd:date or any of the other related date and time data types.

With this support, SPARQL 1.1 lets you compare xsd:dateTime values so that you can filter for events that occur before or after a particular point in time (see this example from my book Learning SPARQL) or between two points in time. SPARQL 1.2 will add many related options, including the ability to do date arithmetic.

My experiments with subtracting one date or date-time value from another had different levels of success with different SPARQL processors because some of these processors have added degrees of support just because it was useful to have, even though it wasn’t explicitly required by the standard. One example is Ontotext’s added support for date and time manipulation. Another is the way that Wikidata’s SPARQL endpoint can subtract xsd:date values, although it returns a decimal number; with Ontotext and the proposed 1.2 standard it returns a duration value. Having more extensive support for working with dates and times right in the SPARQL 1.2 standard will ensure that all the SPARQL processors support it and that they all use a consistent syntax and return consistent data types. This is why we use standards!

The proposed 1.2 additions will also let us add and subtract durations from xsd:time and xsd:dateTime values. And, it will give us a new function that builds on these: ADJUST(), which adjusts xsd:dateTime values based on their time zones.

The latest release of Apache Jena supports this new function, so I tried it out. The following Turtle data shows the start and end time of a meeting that takes place in the New York City time zone:

@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix d:   <http://learningsparql.com/ns/data#> .
@prefix t:   <http://purl.org/tio/ns#> . 

d:meeting1 t:starts "2023-10-14T12:30:00-05:00"^^xsd:dateTime ;
           t:ends   "2023-10-14T15:00:00-05:00"^^xsd:dateTime . 

The query below uses the ADJUST() function to calculate the Los Angeles start and end time of the same meeting. The time zone of the New York meeting was indicated with -05:00, so the LA equivalent is -08:00:

PREFIX t: <http://purl.org/tio/ns#> 
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?NYCStartTime ?LAStartTime
WHERE
{
  ?mtg t:starts ?NYCStartTime . 
  BIND (ADJUST(?NYCStartTime, xsd:dayTimeDuration("-PT8H")) AS ?LAStartTime)
}

The result:

-----------------------------------------------------------------------------------------
| NYCStartTime                              | LAStartTime                               |
=========================================================================================
| "2023-10-14T12:30:00-05:00"^^xsd:dateTime | "2023-10-14T09:30:00-08:00"^^xsd:dateTime |
-----------------------------------------------------------------------------------------

Read the Add Support Durations, Dates, and Times issue mentioned above for more details about the expanded support for manipulation of date and time data. You’ll see some great new things that we’ll be able to do with a lot of existing data, especially with all those date values in Wikidata.


Comments? Reply to my tweet (or even better, my Mastodon message) announcing this blog entry.