Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NaiveDate::years_since wrong documentation #1577

Closed
Taxalo opened this issue Apr 29, 2024 · 0 comments
Closed

NaiveDate::years_since wrong documentation #1577

Taxalo opened this issue Apr 29, 2024 · 0 comments

Comments

@Taxalo
Copy link
Contributor

Taxalo commented Apr 29, 2024

The documentation on the NaiveDate::years_since says it Returns None if base < self.. But this is not correct. It is actually Returns None if base > self.. As the implementation is:

pub const fn years_since(&self, base: Self) -> Option<u32> {
        let mut years = self.year() - base.year(); 
        // Comparing tuples is not (yet) possible in const context. Instead we combine month and
        // day into one `u32` for easy comparison.
        if (self.month() << 5 | self.day()) < (base.month() << 5 | base.day()) {
            years -= 1;
        }

        match years >= 0 {
            true => Some(years as u32),
            false => None,
        }
    }

Base must be bigger than self to return None, take the example of 2022-01-01 and 1998-01-01. If base was 2022 and 1998 self it will be 1998 - 2022 < 0. Which will return None from being negative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant