lubridate
lubridate copied to clipboard
duration() incorrectly parses strings with fractional numbers
Function duration() incorrectly parses strings with units when fractional numbers are used, e.g.:
lubridate::duration("1y") # OK
#> [1] "31557600s (~1 years)"
lubridate::duration("1.5y") # Incorrect
#> [1] "31557600s (~1 years)"
Created on 2021-01-28 by the reprex package (v0.3.0)
In the second example, I expect either a clear failure, or a warning, or (better) a correct result which is the same as in:
lubridate::duration(year = 1.5)
#> [1] "47336400s (~1.5 years)"
Created on 2021-01-28 by the reprex package (v0.3.0)
it's, sort of, by design as the unit parser was primarily designed for periods. Should be reconsidered for durations, I agree.
Seems like more of a bug to me. duration works for fractional days, so it's really unexpected that it doesn't work for fractional years.
lubridate::duration("1 day")
# [1] "86400s (~1 days)"
lubridate::duration("1.5 days")
# [1] "129600s (~1.5 days)"
lubridate::duration("1 year")
# [1] "31557600s (~1 years)"
lubridate::duration("1.5 years")
# [1] "31557600s (~1 years)"
Thanks!
Maybe related:
lubridate::as.duration("P1M") # "2629800s (~4.35 weeks)"
lubridate::as.duration("P1.5M") # "18408600s (~30.44 weeks)"
Although lubridate::month(1.5) throws an error. (I guess the nicer output in both cases would be 3944700s). Ah, but lubridate::duration(month=1.5) works as expected.