Issue with years-from-duration!?
Hi,
The following function returns P4511D: 0 but I expect P4511D: 12 Bug or bad usage?
let $d1 := xs:date('2007-07-31')Quoted text · 4 lines
> let $d2 := xs:date('2019-12-06') > let $duration := $d2 - $d1 > let $years := years-from-duration($duration) > return $duration || ': ' || $years
Quoted text · France Baril · 13 lines
Am 06.12.2019 um 11:23 schrieb France Baril: > > > The following function returns P4511D: 0 but I expect P4511D: 12 > Bug or bad usage? > > let $d1 := xs:date('2007-07-31') > let $d2 := xs:date('2019-12-06') > let $duration := $d2 - $d1 > let $years := years-from-duration($duration) > return $duration || ': ' || $years > >
The spec https://www.w3.org/TR/xpath-functions/#func-years-from-duration says "If|$arg|is an|xs:dayTimeDuration|the function returns 0.".
Quoted text · Martin Honnen · 20 lines
Am 06.12.2019 um 11:32 schrieb Martin Honnen: > Am 06.12.2019 um 11:23 schrieb France Baril: >> >> >> The following function returns P4511D: 0 but I expect P4511D: 12 >> Bug or bad usage? >> >> let $d1 := xs:date('2007-07-31') >> let $d2 := xs:date('2019-12-06') >> let $duration := $d2 - $d1 >> let $years := years-from-duration($duration) >> return $duration || ': ' || $years >> >> > > The spec > https://www.w3.org/TR/xpath-functions/#func-years-from-duration says > "If|$arg|is an|xs:dayTimeDuration|the function returns 0.". > >
As a workaround, see https://www.oxygenxml.com/archives/xsl-list/200601/msg00446.html which tries to compute the years from the dayTimeDuration.
Hi France,
The function fn:years-from-duration is fairly basic; it only returns the year component of your duration argument (as xs:dayTimeDuration has none, in contrast to xs:yearMonthDuration). The background: A year may have 365 or 366 days, and the duration is not sufficient to compute the number of years.
You can use the average number of years for your computation if that’s sufficient for your use case (see [1] for a variety of approximations to choose from):
let $d1 := xs:date('2007-07-31')
let $d2 := xs:date('2019-12-06')
let $duration := $d2 - $d1
let $days := days-from-duration($duration)
return $days div 365.2425Best, Christian
Quoted text · France Baril · 13 lines
On Fri, Dec 6, 2019 at 11:24 AM France Baril <france.baril at architextus.com> wrote: > > Hi, > > The following function returns P4511D: 0 but I expect P4511D: 12 > Bug or bad usage? > >> let $d1 := xs:date('2007-07-31') >> let $d2 := xs:date('2019-12-06') >> let $duration := $d2 - $d1 >> let $years := years-from-duration($duration) >> return $duration || ': ' || $years
I was going to ask if I could count on the difference always being in a PxxxD format. I am happy to see that days-from-duration doesn't return 0. I tried casting the xs:duration to many formats and kept getting 0.
Thanks for the tips
Quoted text · Christian Grün · 41 lines
On Fri, Dec 6, 2019 at 11:38 AM Christian Grün <christian.gruen at gmail.com> wrote: > Hi France, > > The function fn:years-from-duration is fairly basic; it only returns > the year component of your duration argument (as xs:dayTimeDuration > has none, in contrast to xs:yearMonthDuration). The background: A year > may have 365 or 366 days, and the duration is not sufficient to > compute the number of years. > > You can use the average number of years for your computation if that’s > sufficient for your use case (see [1] for a variety of approximations > to choose from): > > let $d1 := xs:date('2007-07-31') > let $d2 := xs:date('2019-12-06') > let $duration := $d2 - $d1 > let $days := days-from-duration($duration) > return $days div 365.2425 > > Best, > Christian > > [1] https://en.wikipedia.org/wiki/Year > > > > On Fri, Dec 6, 2019 at 11:24 AM France Baril > <france.baril at architextus.com> wrote: > > > > Hi, > > > > The following function returns P4511D: 0 but I expect P4511D: 12 > > Bug or bad usage? > > > >> let $d1 := xs:date('2007-07-31') > >> let $d2 := xs:date('2019-12-06') > >> let $duration := $d2 - $d1 > >> let $years := years-from-duration($duration) > >> return $duration || ': ' || $years >
For advanced computations, it’s sometimes easier to convert duration items to milliseconds with our custom functions [1]:
let $ms-years := 1000 * 60 * 60 * 24 * 365
let $dur := xs:dayTimeDuration('P12345DT2M3S')
let $ms := convert:dayTime-to-integer($dur)
return $ms idiv $ms-yearsQuoted text · France Baril · 54 lines
On Fri, Dec 6, 2019 at 11:52 AM France Baril <france.baril at architextus.com> wrote: > > I was going to ask if I could count on the difference always being in a PxxxD format. I am happy to see that days-from-duration doesn't return 0. I tried casting the xs:duration to many formats and kept getting 0. > > Thanks for the tips > > On Fri, Dec 6, 2019 at 11:38 AM Christian Grün <christian.gruen at gmail.com> wrote: >> >> Hi France, >> >> The function fn:years-from-duration is fairly basic; it only returns >> the year component of your duration argument (as xs:dayTimeDuration >> has none, in contrast to xs:yearMonthDuration). The background: A year >> may have 365 or 366 days, and the duration is not sufficient to >> compute the number of years. >> >> You can use the average number of years for your computation if that’s >> sufficient for your use case (see [1] for a variety of approximations >> to choose from): >> >> let $d1 := xs:date('2007-07-31') >> let $d2 := xs:date('2019-12-06') >> let $duration := $d2 - $d1 >> let $days := days-from-duration($duration) >> return $days div 365.2425 >> >> Best, >> Christian >> >> [1] https://en.wikipedia.org/wiki/Year >> >> >> >> On Fri, Dec 6, 2019 at 11:24 AM France Baril >> <france.baril at architextus.com> wrote: >> > >> > Hi, >> > >> > The following function returns P4511D: 0 but I expect P4511D: 12 >> > Bug or bad usage? >> > >> >> let $d1 := xs:date('2007-07-31') >> >> let $d2 := xs:date('2019-12-06') >> >> let $duration := $d2 - $d1 >> >> let $years := years-from-duration($duration) >> >> return $duration || ': ' || $years > > > > -- > France Baril > Architecte documentaire / Documentation architect > france.baril at architextus.com
Written for BaseX 9.3. BaseX may have changed since: please check the current documentation.