Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

month+day duration disobeys arithmetic identity #22

Open
autarch opened this issue Nov 12, 2016 · 0 comments
Open

month+day duration disobeys arithmetic identity #22

autarch opened this issue Nov 12, 2016 · 0 comments

Comments

@autarch
Copy link
Member

autarch commented Nov 12, 2016

Migrated from rt.cpan.org #87770 (status was 'new')

Requestors:

From zefram@fysh.org on 2013-08-11 16:03:45:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant