-
Notifications
You must be signed in to change notification settings - Fork 593
Convert NaiveDate/NaiveDateTime::checked_(add/sub)_days
to return Result
#1475
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -557,89 +557,83 @@ impl NaiveDate { | |
/// | ||
/// # Errors | ||
/// | ||
/// Returns `None` if the resulting date would be out of range. | ||
/// Returns [`Error::OutOfRange`] if the resulting date would be out of range. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// # use chrono::{NaiveDate, Days}; | ||
/// assert_eq!( | ||
/// NaiveDate::from_ymd(2022, 2, 20).unwrap().checked_add_days(Days::new(9)), | ||
/// Some(NaiveDate::from_ymd(2022, 3, 1).unwrap()) | ||
/// ); | ||
/// # use chrono::{NaiveDate, Days, Error}; | ||
/// assert_eq!( | ||
/// NaiveDate::from_ymd(2022, 7, 31).unwrap().checked_add_days(Days::new(2)), | ||
/// Some(NaiveDate::from_ymd(2022, 8, 2).unwrap()) | ||
/// NaiveDate::from_ymd(2022, 2, 20)?.checked_add_days(Days::new(9)), | ||
/// NaiveDate::from_ymd(2022, 3, 1) | ||
/// ); | ||
/// assert_eq!( | ||
/// NaiveDate::from_ymd(2022, 7, 31).unwrap().checked_add_days(Days::new(1000000000000)), | ||
/// None | ||
/// NaiveDate::from_ymd(2022, 7, 31)?.checked_add_days(Days::new(1000000000000)), | ||
/// Err(Error::OutOfRange) | ||
/// ); | ||
/// # Ok::<(), Error>(()) | ||
/// ``` | ||
#[must_use] | ||
pub const fn checked_add_days(self, days: Days) -> Option<Self> { | ||
pub const fn checked_add_days(self, days: Days) -> Result<Self, Error> { | ||
match days.0 <= i32::MAX as u64 { | ||
true => self.add_days(days.0 as i32), | ||
false => None, | ||
false => Err(Error::OutOfRange), | ||
} | ||
} | ||
|
||
/// Subtract a duration in [`Days`] from the date | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns `None` if the resulting date would be out of range. | ||
/// Returns [`Error::OutOfRange`] if the resulting date would be out of range. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// # use chrono::{NaiveDate, Days}; | ||
/// # use chrono::{NaiveDate, Days, Error}; | ||
/// assert_eq!( | ||
/// NaiveDate::from_ymd(2022, 2, 20).unwrap().checked_sub_days(Days::new(6)), | ||
/// Some(NaiveDate::from_ymd(2022, 2, 14).unwrap()) | ||
/// NaiveDate::from_ymd(2022, 2, 20)?.checked_sub_days(Days::new(6)), | ||
Zomtir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// NaiveDate::from_ymd(2022, 2, 14) | ||
/// ); | ||
/// assert_eq!( | ||
/// NaiveDate::from_ymd(2022, 2, 20).unwrap().checked_sub_days(Days::new(1000000000000)), | ||
/// None | ||
/// NaiveDate::from_ymd(2022, 2, 20)?.checked_sub_days(Days::new(1000000000000)), | ||
/// Err(Error::OutOfRange) | ||
/// ); | ||
/// # Ok::<(), Error>(()) | ||
/// ``` | ||
#[must_use] | ||
pub const fn checked_sub_days(self, days: Days) -> Option<Self> { | ||
pub const fn checked_sub_days(self, days: Days) -> Result<Self, Error> { | ||
match days.0 <= i32::MAX as u64 { | ||
true => self.add_days(-(days.0 as i32)), | ||
false => None, | ||
false => Err(Error::OutOfRange), | ||
} | ||
} | ||
|
||
/// Add a duration of `i32` days to the date. | ||
pub(crate) const fn add_days(self, days: i32) -> Option<Self> { | ||
pub(crate) const fn add_days(self, days: i32) -> Result<Self, Error> { | ||
// Fast path if the result is within the same year. | ||
// Also `DateTime::checked_(add|sub)_days` relies on this path, because if the value remains | ||
// within the year it doesn't do a check if the year is in range. | ||
// This way `DateTime:checked_(add|sub)_days(Days::new(0))` can be a no-op on dates were the | ||
// local datetime is beyond `NaiveDate::{MIN, MAX}. | ||
const ORDINAL_MASK: i32 = 0b1_1111_1111_0000; | ||
if let Some(ordinal) = ((self.yof() & ORDINAL_MASK) >> 4).checked_add(days) { | ||
let ordinal = | ||
try_ok_or!(((self.yof() & ORDINAL_MASK) >> 4).checked_add(days), Error::OutOfRange); | ||
{ | ||
if ordinal > 0 && ordinal <= (365 + self.leap_year() as i32) { | ||
let year_and_flags = self.yof() & !ORDINAL_MASK; | ||
return Some(NaiveDate::from_yof(year_and_flags | (ordinal << 4))); | ||
return Ok(NaiveDate::from_yof(year_and_flags | (ordinal << 4))); | ||
} | ||
} | ||
// do the full check | ||
let year = self.year(); | ||
let (mut year_div_400, year_mod_400) = div_mod_floor(year, 400); | ||
let cycle = yo_to_cycle(year_mod_400 as u32, self.ordinal()); | ||
let cycle = try_opt!((cycle as i32).checked_add(days)); | ||
let cycle = try_ok_or!((cycle as i32).checked_add(days), Error::OutOfRange); | ||
let (cycle_div_400y, cycle) = div_mod_floor(cycle, 146_097); | ||
year_div_400 += cycle_div_400y; | ||
|
||
let (year_mod_400, ordinal) = cycle_to_yo(cycle as u32); | ||
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32); | ||
ok!(NaiveDate::from_ordinal_and_flags( | ||
year_div_400 * 400 + year_mod_400 as i32, | ||
ordinal, | ||
flags | ||
)) | ||
NaiveDate::from_ordinal_and_flags(year_div_400 * 400 + year_mod_400 as i32, ordinal, flags) | ||
} | ||
|
||
/// Makes a new `NaiveDateTime` from the current date and given `NaiveTime`. | ||
|
@@ -903,7 +897,7 @@ impl NaiveDate { | |
if days < i32::MIN as i64 || days > i32::MAX as i64 { | ||
return None; | ||
} | ||
self.add_days(days as i32) | ||
ok!(self.add_days(days as i32)) | ||
} | ||
|
||
/// Subtracts the number of whole days in the given `TimeDelta` from the current date. | ||
|
@@ -936,7 +930,7 @@ impl NaiveDate { | |
if days < i32::MIN as i64 || days > i32::MAX as i64 { | ||
return None; | ||
} | ||
self.add_days(days as i32) | ||
ok!(self.add_days(days as i32)) | ||
} | ||
|
||
/// Subtracts another `NaiveDate` from the current date. | ||
|
@@ -1995,7 +1989,7 @@ impl Iterator for NaiveDateWeeksIterator { | |
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let current = self.value; | ||
self.value = current.checked_add_days(Days::new(7))?; | ||
self.value = current.checked_add_days(Days::new(7)).ok()?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated to this PR, so just ignore this comment. |
||
Some(current) | ||
} | ||
|
||
|
@@ -2010,7 +2004,7 @@ impl ExactSizeIterator for NaiveDateWeeksIterator {} | |
impl DoubleEndedIterator for NaiveDateWeeksIterator { | ||
fn next_back(&mut self) -> Option<Self::Item> { | ||
let current = self.value; | ||
self.value = current.checked_sub_days(Days::new(7))?; | ||
self.value = current.checked_sub_days(Days::new(7)).ok()?; | ||
Some(current) | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Seems like a more interesting example 👍.