Subject: | month+day duration disobeys arithmetic identity |
Date: | Sun, 11 Aug 2013 17:03:33 +0100 |
To: | bug-DateTime [...] rt.cpan.org |
From: | Zefram <zefram [...] fysh.org> |
With DateTime-1.03:
$dt0 = DateTime->new(year => 2013, month => 6, day => 30);
$dt1 = DateTime->new(year => 2013, month => 8, day => 31);
$dur = $dt1 - $dt0;
# $dur is 2 months + 1 day
print $dt0 + $dur;
# => 2013-09-01
$dur is representing the interval between these dates in a sensible way.
Applying the interval to $dt0 has to be done by adding the 2 months
first and then the 1 day, but ->add_duration actually does it the other
way round. It adds 1 day to 2013-06-30 to get 2013-07-01, and then adds
on 2 months to get 2013-09-01, not matching the original $dt1.
DateTime's additive arithmetic fundamentally cannot obey the usual
arithmetic identities. It'll inevitably get into trouble with things
like ($dt + $dur0 + $dur1 - $dur0 - $dur1). But the above should be
an easy case, as it's applying a duration to represent exactly the same
calendar interval from which it was generated. The problem is that the
addition operation is not the converse of the subtraction, and that's
occurring because it's internally turning the easy case into the kind
of case that's inherent trouble.
-zefram