metomi / isodatetime

:date: :watch: Python ISO 8601 date time parser and data model/manipulation utilities
GNU Lesser General Public License v3.0
38 stars 20 forks source link

Addition of "conjugate" Duration to TimePoint - order of operations #214

Open MetRonnie opened 2 years ago

MetRonnie commented 2 years ago

From https://github.com/cylc/cylc-flow/issues/4908

When you add a conjugate Duration such as P1M1D to a TimePoint, it seems isodatetime does + P1D + P1M internally, which I think is confusing.

This can be argued either way (I don't think ISO 8601 specifies what order is supposed to happen), but I think the order should be as is written from left to right, i.e. + P1M + P1D.

Why does the order matter, you ask? For nominal (inexact) units, addition is not associative:

>>> 2021-02-28 + P1M + P1D
2021-03-29
>>> 2021-02-28 + P1D + P1M
2021-04-01
>>> 2021-02-28 + P1M1D
2021-04-01

(I've used shorthand to represent the metomi.isodatetime.data objects)

The other side of the argument is that we should stick with the existing behaviour to avoid a breaking, possibly-painful change.

Note: If users want the addition to take place in a specific order, they should use separate Duration objects and add them to the TimePoint object in the desired order, not add Duration objects together.