Re: [basex-talk] Issue with years-from-duration!?
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 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 > 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 >> 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.ba...@architextus.com
Re: [basex-talk] Issue with years-from-duration!?
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 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 > 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.ba...@architextus.com
Re: [basex-talk] Issue with years-from-duration!?
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 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
Re: [basex-talk] Issue with years-from-duration!?
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.
Re: [basex-talk] Issue with years-from-duration!?
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.".
[basex-talk] 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') > 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.ba...@architextus.com