计算两个日期之间的差异 [英] SPARQL Calculating the difference between two date

查看:126
本文介绍了计算两个日期之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是计算两个SPARQL之间的差异的功能xsd:dateTime?



我已经找到对datediff的引用,但这不工作。这样的内置函数是否存在于SPARQL中,与大多数其他查询语言一样?

解决方案

某些SPARQL引擎可能支持日期算术直。例如,在 this answers.semanticweb.com问题和答案中提到,耶拿支持日期算术,您可以从另一个减去一个日期并获得xsd:持续时间。其他实现可以支持datediff函数。例如,这个Virtuoso示例包括使用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)
}
pre>

并获得结果

  ------- 
|年龄|
=======
| 67 |
-------

这有可能被一个人关闭,取决于两个日期的月份和日期,但您也可以使用月份功能。 这个线程描述了一些这样的实现。


What is the function to calculate difference between two SPARQL xsd:dateTime ?

I have found a reference to datediff, but this is not working. Does such a built in function exist in SPARQL as it does with most other query languages?

解决方案

Some SPARQL engines may support date arithmetic directly. For instance, as mention in this answers.semanticweb.com question and answer, Jena supports date arithmetic, and you can subtract one date from another and get an xsd:Duration. Other implementations may support a datediff function. For instance, this Virtuoso example includes the use of bif:datediff. However, these are extensions and not guaranteed to be present in any particular SPARQL implementation.

For a more portable, though possibly less efficient solution, you can use the year to extract the year parts from a datetime. This lets you do, for instance,

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 )
}

and get results

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

This has the potential to be off by one, depending on the months and days of the two dates, but you can similarly work around this by using the month and day functions. This thread describes some of that sort of implementation.

这篇关于计算两个日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆