You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Ewa Szwed <ew...@gmail.com> on 2013/12/06 13:16:11 UTC
operations on xsd:duration
Hello,
I would like to ask if it is doable to do something like this in Sparql on
Jena TDB.
I would like to calculate the difference between 2 dates.
What I have at the moment working is:
BIND(fn:year-from-dateTime(?date_of_birth) AS ?year_of_birth)
BIND(fn:year-from-dateTime(?date_of_death) AS ?year_of_death)
BIND(?year_of_death - ?year_of_birth AS ?age)
BIND(fn:concat(?age, " (", ?year_of_birth, "-", ?year_of_death,
")" ) AS ?age_at_death)
Unfortunately it is not perfect calculation because it just subtracts two
years from ech other and will now answer precisely how old was someone when
he died.
I would need to extract years from xsd:duration
Is it possible to do it?
I would appreciate any answer.
Re: operations on xsd:duration
Posted by Ewa Szwed <ew...@gmail.com>.
Hi, thanks for your answer.
I am aware of all of these.
Unfortunately fn:years-from-duration does not work but how can it since
"P25045DT0H0M0.000S"^^<http://www.w3.org/2001/XMLSchema#duration> does
not have years part :(
2013/12/6 Alexander Dutton <al...@it.ox.ac.uk>
> Hi Ewa,
>
> On 06/12/13 16:03, Ewa Szwed wrote:
> > Got it!
> > Done:
> >
> > * BIND(str(floor(fn:days-from-duration(?date_of_death -
> ?date_of_birth)
> > / 365)) as ?age_at_death)*
>
> Careful; that'll only get you the days component¹. Even if it did what
> you were hoping for, you'll end up with off-by-one errors if they die
> close to their birthday due to the cumulative effects of leap years.
>
> You probably want:
>
> BIND(fn:years-from-duration(?date_of_death - ?date_of_birth) AS
> ?age_at_death)
>
> Best regards,
>
> Alex
>
> ¹ <http://www.w3.org/TR/xpath-functions/#func-days-from-duration>
>
> --
> Alexander Dutton
> Linked Open Data Architect, Office of the CIO; data.ox.ac.uk, OxPoints
> IT Services, University of Oxford
>
>
>
Re: operations on xsd:duration
Posted by Andy Seaborne <an...@apache.org>.
On 06/12/13 16:09, Alexander Dutton wrote:
> Hi Ewa,
>
> On 06/12/13 16:03, Ewa Szwed wrote:
>> Got it!
>> Done:
>>
>> * BIND(str(floor(fn:days-from-duration(?date_of_death - ?date_of_birth)
>> / 365)) as ?age_at_death)*
>
> Careful; that'll only get you the days component¹. Even if it did what
> you were hoping for, you'll end up with off-by-one errors if they die
> close to their birthday due to the cumulative effects of leap years.
>
> You probably want:
>
> BIND(fn:years-from-duration(?date_of_death - ?date_of_birth) AS
> ?age_at_death)
You are right to worry.
There are two kinds of (totally order) durations:
xsd:yearMonthDuration
xsd:dayTimeDuration
you can't mix them (months have variable number of days)
The difference of two xsd:dates is a number of day so the duration is of
the dayTimeDuration which does not have a year component.
fn:years-from-duration is an accessor - it's not calculating the number
of years because it can't as without a timeframe, you don't know about
leap years.
So ?date_of_death - ?date_of_birth is a number of days and /365 is about
the best estimate you can do without grounds the calculation in a fixed
timeframe.
I may have got this wrong, but in implementation ARQ converts to
milliseconds and then uses Java's newDuration(long milliSeconds) which
returns a large day component xsd:Duration, no years or months.
Andy
>
> Best regards,
>
> Alex
>
> ¹ <http://www.w3.org/TR/xpath-functions/#func-days-from-duration>
>
Re: operations on xsd:duration
Posted by Alexander Dutton <al...@it.ox.ac.uk>.
Hi Ewa,
On 06/12/13 16:03, Ewa Szwed wrote:
> Got it!
> Done:
>
> * BIND(str(floor(fn:days-from-duration(?date_of_death - ?date_of_birth)
> / 365)) as ?age_at_death)*
Careful; that'll only get you the days component¹. Even if it did what
you were hoping for, you'll end up with off-by-one errors if they die
close to their birthday due to the cumulative effects of leap years.
You probably want:
BIND(fn:years-from-duration(?date_of_death - ?date_of_birth) AS
?age_at_death)
Best regards,
Alex
¹ <http://www.w3.org/TR/xpath-functions/#func-days-from-duration>
--
Alexander Dutton
Linked Open Data Architect, Office of the CIO; data.ox.ac.uk, OxPoints
IT Services, University of Oxford
Re: operations on xsd:duration
Posted by Ewa Szwed <ew...@gmail.com>.
Got it!
Done:
* BIND(str(floor(fn:days-from-duration(?date_of_death - ?date_of_birth)
/ 365)) as ?age_at_death)*
2013/12/6 Ewa Szwed <ew...@gmail.com>
> for:
>
> BIND(?date_of_death - ?date_of_birth as ?duration)
>
> ?duration is
>
> "P25045DT0H0M0.000S"^^<http://www.w3.org/2001/XMLSchema#duration>
>
> Is it possible to extract days/years value from it in Sparql?
>
>
>
> 2013/12/6 Andy Seaborne <an...@apache.org>
>
>> On 06/12/13 12:16, Ewa Szwed wrote:
>>
>>> Hello,
>>>
>>> I would like to ask if it is doable to do something like this in Sparql
>>> on
>>> Jena TDB.
>>>
>>> I would like to calculate the difference between 2 dates.
>>>
>>> What I have at the moment working is:
>>>
>>> BIND(fn:year-from-dateTime(?date_of_birth) AS ?year_of_birth)
>>>
>>
>> shorter:
>> YEAR(?date_of_birth)
>>
>>
>> BIND(fn:year-from-dateTime(?date_of_death) AS ?year_of_death)
>>> BIND(?year_of_death - ?year_of_birth AS ?age)
>>> BIND(fn:concat(?age, " (", ?year_of_birth, "-", ?year_of_death,
>>> ")" ) AS ?age_at_death)
>>>
>>
>> ARQ supports xsd:duration.
>>
>> ?date_of_death - ?date_of_birth -> xsd:duration
>>
>> assuming ?date_of_death and ?date_of_birth are well-formed xsd:dateTimes.
>>
>>
>>
>>> Unfortunately it is not perfect calculation because it just subtracts two
>>> years from ech other and will now answer precisely how old was someone
>>> when
>>> he died.
>>>
>>> I would need to extract years from xsd:duration
>>>
>>> Is it possible to do it?
>>>
>>> I would appreciate any answer.
>>>
>>>
>>
>
Re: operations on xsd:duration
Posted by Ewa Szwed <ew...@gmail.com>.
for:
BIND(?date_of_death - ?date_of_birth as ?duration)
?duration is
"P25045DT0H0M0.000S"^^<http://www.w3.org/2001/XMLSchema#duration>
Is it possible to extract days/years value from it in Sparql?
2013/12/6 Andy Seaborne <an...@apache.org>
> On 06/12/13 12:16, Ewa Szwed wrote:
>
>> Hello,
>>
>> I would like to ask if it is doable to do something like this in Sparql on
>> Jena TDB.
>>
>> I would like to calculate the difference between 2 dates.
>>
>> What I have at the moment working is:
>>
>> BIND(fn:year-from-dateTime(?date_of_birth) AS ?year_of_birth)
>>
>
> shorter:
> YEAR(?date_of_birth)
>
>
> BIND(fn:year-from-dateTime(?date_of_death) AS ?year_of_death)
>> BIND(?year_of_death - ?year_of_birth AS ?age)
>> BIND(fn:concat(?age, " (", ?year_of_birth, "-", ?year_of_death,
>> ")" ) AS ?age_at_death)
>>
>
> ARQ supports xsd:duration.
>
> ?date_of_death - ?date_of_birth -> xsd:duration
>
> assuming ?date_of_death and ?date_of_birth are well-formed xsd:dateTimes.
>
>
>
>> Unfortunately it is not perfect calculation because it just subtracts two
>> years from ech other and will now answer precisely how old was someone
>> when
>> he died.
>>
>> I would need to extract years from xsd:duration
>>
>> Is it possible to do it?
>>
>> I would appreciate any answer.
>>
>>
>
Re: operations on xsd:duration
Posted by Andy Seaborne <an...@apache.org>.
On 06/12/13 12:16, Ewa Szwed wrote:
> Hello,
>
> I would like to ask if it is doable to do something like this in Sparql on
> Jena TDB.
>
> I would like to calculate the difference between 2 dates.
>
> What I have at the moment working is:
>
> BIND(fn:year-from-dateTime(?date_of_birth) AS ?year_of_birth)
shorter:
YEAR(?date_of_birth)
> BIND(fn:year-from-dateTime(?date_of_death) AS ?year_of_death)
> BIND(?year_of_death - ?year_of_birth AS ?age)
> BIND(fn:concat(?age, " (", ?year_of_birth, "-", ?year_of_death,
> ")" ) AS ?age_at_death)
ARQ supports xsd:duration.
?date_of_death - ?date_of_birth -> xsd:duration
assuming ?date_of_death and ?date_of_birth are well-formed xsd:dateTimes.
>
> Unfortunately it is not perfect calculation because it just subtracts two
> years from ech other and will now answer precisely how old was someone when
> he died.
>
> I would need to extract years from xsd:duration
>
> Is it possible to do it?
>
> I would appreciate any answer.
>