Xsd:time from xsd:dateTime

Are there any SPARQL methods to extract an xsd:time value from a xsd:dateTime?

I want to write queries that find dateTimes that occur before or after a given time of day. For example:

select * where {    
 # this doesn't work.  Is there another way?
 #bind (xsd:time(now()) as ?timeOfDay)

 bind ("07:00:00Z"^^xsd:time as ?timeOfDay )
 bind (?timeOfDay > "08:00:00Z"^^xsd:time as ?testResult)
}

Hi Peter,

Afaict the only way is to extract the components manually:

select * where {
bind(now() as ?aDateTime)
bind(hours(?aDateTime) as ?h)
bind(minutes(?aDateTime) as ?m)
bind(seconds(?aDateTime) as ?s)
bind(<http://www.w3.org/2003/11/swrlb#time>(?h, ?m, ?s) as ?aTime)
}
+-----------------------------------------------+-------+-------+--------+--------------------------+
|                   aDateTime                   |   h   |   m   |   s    |          aTime           |
+-----------------------------------------------+-------+-------+--------+--------------------------+
| "2017-08-31T17:01:11.778-05:00"^^xsd:dateTime | 17    | 1     | 11.778 | "17:01:11.778"^^xsd:time |
+-----------------------------------------------+-------+-------+--------+--------------------------+
2 Likes

Thanks Jess, That’s definitely a lot more verbose than what I was hoping for, but it does solves my problem. :slight_smile:

You could always write a custom function that would be less verbose although it wouldn’t be portable.

Looks like xsd:dateTime to xsd:time is a valid XPath cast. We’ll get this added along with any other missing casts.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.