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

Make methods on NaiveDate const #1205

Merged
merged 6 commits into from
Jul 29, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl NaiveWeek {
// Do not construct an intermediate date beyond `self.date`, because that may be out of
// range if `date` is close to `NaiveDate::MAX`.
let days = start - ref_day - if start > ref_day { 7 } else { 0 };
self.date.diff_days(days as i64).unwrap()
self.date.add_days(days).unwrap()
}

/// Returns a date representing the last day of the week.
Expand Down Expand Up @@ -95,7 +95,7 @@ impl NaiveWeek {
// Do not construct an intermediate date before `self.date` (like with `first_day()`),
// because that may be out of range if `date` is close to `NaiveDate::MIN`.
let days = end - ref_day + if end < ref_day { 7 } else { 0 };
self.date.diff_days(days as i64).unwrap()
self.date.add_days(days).unwrap()
}

/// Returns a [`RangeInclusive<T>`] representing the whole week bounded by
Expand Down Expand Up @@ -740,7 +740,7 @@ impl NaiveDate {
return Some(self);
}

i64::try_from(days.0).ok().and_then(|d| self.diff_days(d))
i32::try_from(days.0).ok().and_then(|d| self.add_days(d))
}

/// Subtract a duration in [`Days`] from the date
Expand Down Expand Up @@ -768,11 +768,11 @@ impl NaiveDate {
return Some(self);
}

i64::try_from(days.0).ok().and_then(|d| self.diff_days(-d))
i32::try_from(days.0).ok().and_then(|d| self.add_days(-d))
}

fn diff_days(self, days: i64) -> Option<Self> {
let secs = days.checked_mul(86400)?; // 86400 seconds in one day
fn add_days(self, days: i32) -> Option<Self> {
djc marked this conversation as resolved.
Show resolved Hide resolved
let secs = (days as i64).checked_mul(86400)?; // 86400 seconds in one day
if secs >= core::i64::MAX / 1000 || secs <= core::i64::MIN / 1000 {
return None; // See the `time` 0.1 crate. Outside these bounds, `Duration::seconds` will panic
}
Expand Down