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

Added length function to Month so that I don't have to use time crate #1643

Closed
wants to merge 2 commits into from

Conversation

DanielPoprawski
Copy link

Thanks for contributing to chrono!

If your feature is semver-compatible, please target the main branch;
for semver-incompatible changes, please target the 0.5.x branch.

Please consider adding a test to ensure your bug fix/feature will not break in the future.

src/month.rs Outdated
| Month::October
| Month::December => 31,
Month::April | Month::June | Month::September | Month::November => 30,
Month::February if year % 4 == 0 && (year % 25 != 0 || year % 16 == 0) => 29,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for February isn't fully accurate, and I think we have API for this on Date that you could repurpose.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the functions in question under NaiveDate and then under internals.rs but it seems to be meant for testing purposes only and it's unreachable from the month.rs file

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to expose whatever this needs as pub(crate), I don't want to duplicate this logic here.

src/month.rs Outdated
@@ -161,6 +161,22 @@ impl Month {
Month::December => "December",
}
}

/// Get the length of the month in a given year
pub const fn length(&self, year: i32) -> u8 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this name. len() would be more idiomatic in Rust but maybe it should be a little more specific like num_days()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think length is a pretty straightforward explanation of what the function does. I didn't like how num_days() sounds

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree -- len()/length() is not very specific and not fully intuitive on Month IMO -- for example, it could yield a Duration instead. We already use the num_days() for a number of methods in chrono and it would be a good fit here.

Copy link

codecov bot commented Dec 20, 2024

Codecov Report

Attention: Patch coverage is 0% with 7 lines in your changes missing coverage. Please review.

Project coverage is 90.88%. Comparing base (8b86349) to head (66b709e).

Files with missing lines Patch % Lines
src/month.rs 0.00% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1643      +/-   ##
==========================================
- Coverage   91.11%   90.88%   -0.24%     
==========================================
  Files          37       37              
  Lines       17137    17128       -9     
==========================================
- Hits        15614    15566      -48     
- Misses       1523     1562      +39     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

…s seems to be for testing purposes so I left it alone but updated the equation. Every 4th year is a leap year, except for if it is the first year of a century, except-except every 400 years
Copy link
Author

@DanielPoprawski DanielPoprawski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the month logic and changed the function name to len()

@djc djc closed this Dec 28, 2024
@djc
Copy link
Member

djc commented Dec 28, 2024

I'll do it myself.

@djc djc mentioned this pull request Jan 6, 2025
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

Successfully merging this pull request may close these issues.

2 participants