tidyverse / lubridate

Make working with dates in R just that little bit easier
https://lubridate.tidyverse.org
GNU General Public License v3.0
724 stars 207 forks source link

Equal to == operator doesn't work as expected with lubridate intervals #1135

Open jakemanger opened 10 months ago

jakemanger commented 10 months ago

If you attempt to use an == check with lubridate intervals, it uses the interval duration, rather than the interval object itself.

Reading the docs for lubridate::interval, this doesn't seem like this should be the default behaviour. Shouldn't this act like comparing two time ranges?

library(lubridate)
> interval(start=ymd('2018-01-01'), end=ymd('2018-01-02')) == interval(start=ymd('2018-01-02'), end=ymd('2018-01-02')) 
[1] FALSE
> interval(start=ymd('2018-01-01'), end=ymd('2018-01-02')) == interval(start=ymd('2018-01-03'), end=ymd('2018-01-04')) 
[1] TRUE

Note, I am using lubridate 1.9.2

vspinu commented 10 months ago

Agreed. Intervals are not exactly comparable, so comparison operations don't make sense and == is a special case of those.

Two options, either implement comparison operations as a partial order or, alternatively, error on all comparisons.

jakemanger commented 10 months ago

@vspinu I believe the comparison as a partial order approach would be much more useful (at least for me). I've implemented a proposed solution here #1136