SPARQL计算两个日期之间的差异

时间:2013-09-03 22:04:50

标签: date sparql

计算两个SPARQL xsd:dateTime?

之间差异的函数是什么?

我找到了对datediff的引用,但这不起作用。 SPARQL中是否存在这样的内置函数,就像大多数其他查询语言一样?

2 个答案:

答案 0 :(得分:9)

某些SPARQL引擎可能直接支持日期算法。例如,如this answers.semanticweb.com question and answer中所述,Jena支持日期算术,您可以从另一个日期中减去一个日期并获得xsd:Duration。其他实现可以支持日期功能。例如,this Virtuoso example包括使用bif:datediff。但是,这些是扩展,并不保证在任何特定的SPARQL实现中都存在。

对于更便携,但效率更低的解决方案,您可以使用year从日期时间中提取年份部分。例如,这可以让你做到

select ?age where { 
  bind( "1799-12-14"^^<http://www.w3.org/2001/XMLSchema#date> as ?death )
  bind( "1732-02-22"^^<http://www.w3.org/2001/XMLSchema#date> as ?birth )
  bind( year(?death)-year(?birth) as ?age )
}

并获得结果

-------
| age |
=======
| 67  |
-------

根据两个日期的月份和日期,这可能会被一个人关闭,但您可以使用monthday函数同样解决此问题。 This thread描述了一些此类实现。

答案 1 :(得分:9)

当我在搜索如何计算SPARQL中的年龄时,我发现了这篇文章。感谢Joshua Taylor提供了正确的提示。然而,我通过添加调整来改进代码段,因此如果该人在他们去世的那一年没有过生日,那么它不会被禁用一年。我认为对于那些在搜索主题时找到这篇文章的人可能会有所帮助,就像我做的那样。:

select ?age where { 
  bind( "1799-12-14"^^<http://www.w3.org/2001/XMLSchema#date> as ?death )
  bind( "1732-02-22"^^<http://www.w3.org/2001/XMLSchema#date> as ?birth )
  bind( year(?death) - year(?birth) - if(month(?death)<month(?birth) || (month(?death)=month(?birth) && day(?death)<day(?birth)),1,0) as ?age )
}
相关问题