Inconsistent time_length #1192
Copy link
Copy link
Open
Description
Hi,
In lubridate versions 1.9.3 and 1.9.4, I get the following inconsistencies:
time_length(interval("2023-12-05", "2023-12-31"), "years") # 0.07103825
time_length(interval("2023-12-05", "2023-12-31"), "days") / 365 # 0.07123288
I guess it has something to do with inferring leap years incorrectly, because:
time_length(interval("2023-12-05", "2023-12-31"), "days") / 366 # 0.07103825
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
ErdaradunGaztea commentedon Mar 29, 2026
It depends on what the authors of the package intended; right now, the way
time_length()works with intervals when counting months/years is by considering "anniversaries". In other words, it counts months and years starting from the interval start. Suppose we calculate length of a slightly longerinterval("2021-12-05", "2023-12-30"). The "anniversaries" take place on December 5th each year. We know this interval is 2 years and a bit more, so to calculate that "bit more" we take the last anniversary (i.e. December 5th 2023) and calculate the fraction of time falling within the interval (Dec 5 - Dec 31) to the whole next "year" (Dec 5, 2023 - Dec 4, 2024). As you can see, the last part is what covers the leap day of February 2024.Now, I don't know about package authors, but I agree that - intuitively - time length should add fractions of years/months on both sides of the interval. In the case I mentioned - ~0.07 of year 2021 + ~0.99 of year 2023 (and the whole year 2022, of course). I'd be willing to make a PR if this interpretation is deemed more correct :)