lubridate icon indicating copy to clipboard operation
lubridate copied to clipboard

duration() incorrectly parses strings with fractional numbers

Open GegznaV opened this issue 4 years ago • 3 comments

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)

GegznaV avatar Jan 28 '21 16:01 GegznaV

it's, sort of, by design as the unit parser was primarily designed for periods. Should be reconsidered for durations, I agree.

vspinu avatar Feb 24 '21 12:02 vspinu

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!

nicholasdavies avatar Dec 09 '22 14:12 nicholasdavies

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.

pank avatar Oct 10 '23 12:10 pank