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-years
[1] http://docs.basex.org/wiki/Conversion_Module#convert:dayTime-to-integer
On Fri, Dec 6, 2019 at 11:52 AM France Baril france.baril@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@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@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@architextus.com