-
Notifications
You must be signed in to change notification settings - Fork 593
convenience functions to create NaiveDates #893
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
Conversation
bbe5709
to
bff51bd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These look convenient!
Please add #[must_use]
to these functions.
I am not super convinced of the usefulness of all these methods. My reason is that there are all kinds of operations someone may want to do on dates. Where do you draw the line?
But then you would also want Would similar methods on |
Self::from_ymd_opt(year.into(), month.number_from_month(), 1).unwrap() | ||
} | ||
|
||
/// Create a `NaiveDate` of the ;ast day of the given year and month |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Create a `NaiveDate` of the ;ast day of the given year and month | |
/// Create a `NaiveDate` of the last day of the given year and month |
/// | ||
/// This takes an `i16` rather than `i32` to ensure it is not | ||
/// out of the allowed range | ||
pub fn start_year(year: i16) -> NaiveDate { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using an i16
ties into your work in #882. But maybe it is best to accept an i32
until the decision is made to restrict the range of allowed years?
/// out of the allowed range | ||
pub fn end_year(year: i16) -> NaiveDate { | ||
// ideally this unwrap can be later removed | ||
Self::from_ymd_opt(i32::from(year) + 1, 1, 1).unwrap().pred_opt().unwrap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might fail if year == MAX_YEAR
.
// ideally this unwrap can be later removed | ||
let start = Self::start_month(year, month); | ||
|
||
(start + Months::new(1)).pred_opt().unwrap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might fail if year == MAX_YEAR && month == 12
.
Closing. I would be interested to have a method to easily create a |
These functions are to reduce the number of unwraps needed when creating NaiveDates