Is there a way to get the current time in SPARQL as a duration in seconds or milliseconds from some epoch? The standard includes now(), which returns an xsd:dateTime, and a variety of functions for extracting parts of xsd:dateTimes, but nothing for converting the whole dateTime to seconds. Am I missing something?
Asked
Active
Viewed 3,921 times
4
-
What sort of conversion to seconds/milliseconds are you envisioning? A date is a particular point in time. n seconds is a duration between two points in time. It doesn't make sense to convert a date into a duration. In some contexts, it's common to measure time as the number of seconds from some well-known "epoch". E.g., in Common Lisp, a [universal time](http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_u.htm#universal_time) is measured in seconds since 1900. In Unix systems, [time(2)](http://linux.die.net/man/2/time) returns the seconds since 1970-01-01 00:00:00 +0000 (UTC). – Joshua Taylor Jun 28 '14 at 12:35
-
@JoshuaTaylor Answer Updated. This is what I meant, sorry for not mentioning that. – Nick Louloudakis Jun 28 '14 at 12:43
-
So you just want to compute the *duration* between two timepoints? Is it OK if you specify one yourself? E.g., if you specify the epoch, since there's no universally accepted one? – Joshua Taylor Jun 28 '14 at 12:57
-
If that's the case, then you might be interested in [SPARQL Calculating the difference between two date](http://stackoverflow.com/q/18602243/1281433), and [How to perform arithmetic operations in Sparql with python?](http://stackoverflow.com/q/8201012/1281433) Unfortunately, those [point out](http://stackoverflow.com/a/8204515/1281433) that "Even SPARQL 1.1 does not support arithmetic operations on dates by default. See the section on SPARQL operator mapping: arithmetic operations are only defined on numeric datatypes." There are some workarounds in those questions though. – Joshua Taylor Jun 28 '14 at 13:00
-
Ok, finally, a workaround will be used through Java, by putting the seconds/milliseconds hardcoded in the query forming before submitting it. Please put your comment as an answer, in order so that I can say "thank you for your time and effort" by voting it as best. – Nick Louloudakis Jun 28 '14 at 13:05
1 Answers
5
Unfortunately, SPARQL 1.1 isn't required to support date arithmetic, although some implementations may. For instance, on the DBpedia endpoint, you can execute a query like this:
select (NOW() - ?epoch as ?time ) where {
values ?epoch { "1970-01-01T00:00:00"^^xsd:dateTime }
}
More details about the fact that SPARQL 1.1 isn't required to support this, along with some more portable workarounds (e.g., extracting components of dates and doing arithmetic with those) can be found in the answers to some related questions:

Community
- 1
- 1

Joshua Taylor
- 84,998
- 9
- 154
- 353
-
1Well, it's a workaround; implementations aren't required to support date arithmetic like this, so it's not portable. In implementations that don't support it, you'll just get an error value. – Joshua Taylor Jun 28 '14 at 13:08