Skip to content

NaiveDate::years_since wrong documentation #1577

@Taxalo

Description

@Taxalo

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.

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