Skip to content

Inconsistent time_length #1192

Open
Open

Description

@ViThanh

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

Activity

  1. ErdaradunGaztea commented on Mar 29, 2026

    @ErdaradunGaztea

    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 longer interval("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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions