rluiten / elm-date-extra

Elm Date Extra library add/subtract/diff/format etc dates
http://package.elm-lang.org/packages/rluiten/elm-date-extra/latest
BSD 3-Clause "New" or "Revised" License
75 stars 36 forks source link

Months subtraction #47

Closed pro100filipp closed 7 years ago

pro100filipp commented 7 years ago

There's an issue with month subtraction. Repro steps:

import Date exposing (fromTime)
import Date.Extra.Duration exposing (Duration(..), add)
today = fromTime 1499529702655
add Month -7 today -- results in Dec 08 2016, which is right
add Month -19 today -- results in Dec 08 2014, which is one year to the past wrong
add Month -20 today -- results in Nov 08 2015, which is right again

Seems that problem is with the yearOffset in the addMonth function, but I'm not sure. I'll probably do some work on this.

Update

Further investigation showed that the root problem is in the Internal.daysFromCivil. When targetMonthInt equals zero resulting year is reduced by one (which is logical and right behavior). But when newMonthInt is simultaneously less than zero (in case of subtracting 12 + currentMonthNumber months or more) it leads to additional year subtraction from yearOffset.

I'll be making a PR now.

rluiten commented 7 years ago

Nice catch.

rluiten commented 7 years ago

Ok merged, added your test cases and published as 9.0.1. Thanks for the report and fix.