|
Hi, Is it possible to get the day of the week from a dateTime literal using SPARQL? I'm trying to infer whether a certain dateTime literal (retrieved from the 'now()' function) is on a certain day of the week. I'm aware of the year, month, day, hours, minutes, seconds and timezone functions, but not of a similar function returning the day of the week. If there's no such function, is there another way to infer the day of the week? Regards, Jacco |
|
Okay, SPARQL 1.1... (Note SPARQL 1.1 has floor. You can calculate a % b (modulo) using a - (floor(a/b) * b).) Using the Gaussian function in @harschware's link, this should give an ID for the current day of the week: (EDIT: formula now (i) adjusts for January, February, (ii) ensures positive result, (iii) doesn't assume a four digit year.)
You can test this here, with todays "day" here. If you wanted to return names of the days, one quick trick would be to add triples of the following form to the store (assuming you have write access):
and then add:
to your query to retrieve the day name. If you don't have write access, you could encode the day names into the query:
Which gives the answer here. (Thanks to @database_animal for pointing me to the if–then–else ladder pattern.) Nice, thanks for this! Really helpful. I didn't know there was a floor function in SPARQL 1.1. But your query doesn't substract the year with 1 if the month is january or february @Jacco, good point: I missed that, but it's now fixed in the formula. With conditional branching it would be easier ( I also added something to make sure the result is positive, and now calculate the century/year using UDPATE: Used conditions to clean up the formula a bit. |
|
Not a sure if there is a function that will do it for you that is available in SPARQL. I'll let others more familiar with the spec help you with that, but if there isn't I think it would be a great exercise to see if you could roll your own using basic math functions (if enough of those are available). The formula for doing that can be found here Thanks, I'll look into that formula. Edit: Nice formula, will be useful to remember! Sadly SPARQL doesn't support modulus or floor calculations. You were right about being a great exercise. My SPARQL foo has increased by three bar, particularly regarding some of the new functions and casting (which I think is poorly explained in the doc if @lee sees this; would be great to have an example since it's not clear that the cast function name is the datatype URI; I thought it was I think I mighta gotten stuck with the modulus operator being missing so thanks for proving it could be done. And, since we're hoping @lee is listening... modulus operator, case function and of course, dayOfWeek function would also be great. :-) |

