[Bug 4856] [F&O] arithmetic on durations

http://www.w3.org/Bugs/Public/show_bug.cgi?id=4856





--- Comment #12 from Benjamin Nguyen <benjie.nguyen@gmail.com>  2008-09-16 14:21:17 ---
Problem 1:

* xs:dateTime + xs:duration *

It is impossible to define addition and subtraction on the general type
xs:duration (without allowing negative values, which means changing the
definition of the type in XML Schema) due to the fact we can not convert days
into years or months. However, if an anchor xs:dateTime is provided, then it
seems possible to compute addition and subtraction of any sort of xs:duration
to this xs:dateTime, although this is not possible for the moment for the
current spec

Basically said, we would like to include an op:add-duration-to-dateTime and
op:subtract-duration-from-dateTime, we think this is part of the enhancement. 

let $date1 := xs:dateTime("2008-09-15T00:00:00Z")
let $duration1 := xs:duration("P1Y")
let $duration2 := xs:duration("-P1D")
return $date1 + $duration1 + $duration2

->  2009-09-14T00:00:00Z

Here we are able to define non ambiguously the resulting date due to the
priority of operators (i.e. we first compute $date + $duration1, which returns
a xs:dateTime then we add "-P1D" to this xs:dateTime).

In order to be able to add an arbitrary xs:duration, we need to define the
priority between the "subparts" of the xs:duration when a "year" changes length
according to the intermediate result. Remember that 2000 was a leap year in the
following example

let $date2 := xs:dateTime("2000-01-01T00:00:00Z")
let $duration3 := xs:duration("P1Y60D")

return $date2 + $duration3

The result is undefined, since it depends on the order of the addition:
case 1 : 2000-01-01T00:00:00Z + P1Y +P60D = 2001-03-02T00:00:00Z -> here P1Y ==
P366D
case 2 : 2000-01-01T00:00:00Z + P60D +P1Y = 2001-03-01T00:00:00Z -> here P1Y ==
P365D

We are not sure either what the result of 2000-02-29 + P1Y should return. There
are surely some other cases that we have overlooked and are ambiguous also.

Problem 2 :

* xs:duration +/- xs:duration *

It should also be possible to add and subtract durations, and return overflow
errors if the partial values  (i.e. any one of Y, M, D, etc. components)
evaluate to something negative. i.e. P1Y - P1D == error but P1Y1D - P1D = P1Y

Note that in this case addition does not cause any problem from the arithmetic
point of view, but it is stated in F&O : 

"Operations on durations (including equality comparison, casting to string, and
extraction of components) all treat the duration as normalized. This means that
the seconds and minutes components will always be less than 60, the hours
component less than 24, and the months component less than 12. Thus, for
example, a duration of 120 seconds always gives the same result as a duration
of two minutes." 

so maybe a overflow should also be generated if the result of the operation is
beyond these boundaries.

Problem 3 :

* xs:dateTime – xs:dateTime = xs:duration *

The previous requirement causes a different problem, since xs:dayTimeDurations
can have D components greater than 30.

Consider for instance op:subtract-dateTimes(    $arg1    as xs:dateTime,$arg2  
 as xs:dateTime) as xs:dayTimeDuration? Given proposition 1, this operator
should return a normalized (positive or negative) xs:duration + an anchor start
value (for instance $arg1) 

i.e., the current example is :
‘op:subtract-dateTimes(xs:dateTime("2000-10-30T06:12:00"),
xs:dateTime("1999-11-28T09:00:00Z")) returns an xs:dayTimeDuration value
corresponding to 337 days, 2 hours and 12 minutes’
It should return a xs:duration("P11M1D2H12M") (since in the example the dynamic
context provides a time zone value of -05:00) and the anchor value
xs:dateTime("2000-10-30T06:12:00")

Our opinion:

All this takes a close looking into, but it seems possible to make some sound
arithmetic out of all this, without introducing subtypes such as
xs:yearMonthDuration and xs:dayTimeDuration.


Benjamin Nguyen & Tristan Allard
UVSQ


-- 
Configure bugmail: http://www.w3.org/Bugs/Public/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.

Received on Tuesday, 16 September 2008 14:21:52 UTC