Wrong as.period computation with negative intervals #285
Comments
This one turned to be tough. Some more examples: start <- ymd('1992-03-01')
(per <- as.period(new_interval(start, end))) # "-2d 0H 0M 0S"
start + per # "1992-02-28 UTC"
start <- ymd('1992-04-01')
(per <- as.period(new_interval(start, end))) # "-1m -4d 0H 0M 0S"
start + per # "1992-02-26 UTC"
start <- ymd('1992-05-01')
(per <- as.period(new_interval(start, end))) # "-2m -3d 0H 0M 0S"
start + per # "1992-02-27 UTC" The main issue is that the computation of In principle there are two ways out. Either make I feel that the first option is cleaner, but will likely result in a lot of existing code. The second option might have less severe consequences, but will result in asymmetric arithmetics which is hard to reason about. More-over, lubridate relies on internal |
I have implemented asymmetric arithmetics of positive/negative periods as suggested above. With this fix the following behaves as expected: (per <- as.period(new_interval(start, end))) # "-1d 0H 0M 0S"
start + per # "1992-02-29 UTC"
start <- ymd('1992-04-01')
(per <- as.period(new_interval(start, end))) # "-1m -1d 0H 0M 0S"
start + per # "1992-02-29 UTC"
start <- ymd('1992-05-01')
(per <- as.period(new_interval(start, end))) # "-2m -1d 0H 0M 0S"
start + per #"1992-02-29 UTC" For the new semantics of the negative periods see these tests. |
The result is exactly one day smaller. I expect it's a wrong computing with days in
.int_to_period
.The text was updated successfully, but these errors were encountered: