tidyverse / lubridate

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

years(1) fails on Leap days on R 4.2.2 #1087

Closed lkoppers closed 1 year ago

lkoppers commented 1 year ago

In older versions of R as.Date("2020-02-29") - years(1) results to NA what can be expected. In R 4.2.2 the result is "1970-01-29"

library(lubridate)
as.Date("2020-02-29") - years(1)
vspinu commented 1 year ago

The expected result is NA

> as.Date("2020-02-29") - years(1)
[1] NA

It's because 2019-02-29 does not exist. If you want a different behavior use either %m+% or add_with_rallback() function

> as.Date("2020-02-29") %m-% years(1)
[1] "2019-02-28"
> add_with_rollback(as.Date("2020-02-29"), -years(1))
[1] "2019-02-28"