-
Notifications
You must be signed in to change notification settings - Fork 527
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
Years type implementation #900
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,8 @@ use crate::naive::{IsoWeek, NaiveDateTime, NaiveTime}; | |
use crate::oldtime::Duration as OldDuration; | ||
use crate::{Datelike, Duration, Weekday}; | ||
|
||
use crate::Years; | ||
|
||
use super::internals::{self, DateImpl, Mdf, Of, YearFlags}; | ||
use super::isoweek; | ||
|
||
|
@@ -1248,6 +1250,23 @@ impl NaiveDate { | |
NaiveWeek { date: *self, start } | ||
} | ||
|
||
// I think self here is a copy, but I still have to make sure it's | ||
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. It is a copy, you only want to take |
||
pub fn checked_add_years(self, years: Years) -> Option<NaiveDate> { | ||
if years.0 == 0 { | ||
return Some(self); | ||
} | ||
let num_years = i32::try_from(years.0).ok()?; | ||
self.with_year(self.year() + num_years) | ||
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. This will fail if you add 1 year to February 29th. This is different from 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. How do you handle integer overflow in for example |
||
} | ||
|
||
pub fn checked_sub_years(self, years: Years) -> Option<NaiveDate> { | ||
if years.0 == 0 { | ||
return Some(self); | ||
} | ||
let num_years = i32::try_from(years.0).ok()?; | ||
self.with_year(self.year() - num_years) | ||
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. As above. |
||
} | ||
|
||
/// The minimum possible `NaiveDate` (January 1, 262145 BCE). | ||
pub const MIN: NaiveDate = NaiveDate { ymdf: (MIN_YEAR << 13) | (1 << 4) | 0o07 /*FE*/ }; | ||
/// The maximum possible `NaiveDate` (December 31, 262143 CE). | ||
|
@@ -1687,6 +1706,22 @@ impl Sub<Months> for NaiveDate { | |
} | ||
} | ||
|
||
impl Add<Years> for NaiveDate { | ||
type Output = NaiveDate; | ||
|
||
fn add(self, years: Years) -> Self::Output { | ||
self.checked_add_years(years).unwrap() | ||
} | ||
} | ||
|
||
impl Sub<Years> for NaiveDate { | ||
type Output = NaiveDate; | ||
|
||
fn sub(self, years: Years) -> Self::Output { | ||
self.checked_sub_years(years).unwrap() | ||
} | ||
} | ||
|
||
impl Add<Days> for NaiveDate { | ||
type Output = NaiveDate; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ use crate::oldtime::Duration as OldDuration; | |
use crate::{DateTime, Datelike, LocalResult, Months, TimeZone, Timelike, Weekday}; | ||
use core::cmp::Ordering; | ||
|
||
use crate::Years; | ||
|
||
#[cfg(feature = "rustc-serialize")] | ||
pub(super) mod rustc_serialize; | ||
|
||
|
@@ -927,6 +929,16 @@ impl NaiveDateTime { | |
} | ||
} | ||
} | ||
|
||
pub fn checked_add_years(mut self, num_years: Years) -> Option<NaiveDateTime> { | ||
self.date = self.date.checked_add_years(num_years)?; | ||
Some(self) | ||
} | ||
|
||
pub fn checked_sub_years(mut self, num_years: Years) -> Option<NaiveDateTime> { | ||
self.date = self.date.checked_sub_years(num_years)?; | ||
Some(self) | ||
} | ||
} | ||
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.
|
||
|
||
impl Datelike for NaiveDateTime { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// I usually develop with this clippy flags | ||
// but I can remove once the this module is done | ||
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. 👍 when done. |
||
#![warn(clippy::all, | ||
/*#![warn(*/clippy::pedantic, | ||
clippy::perf, | ||
clippy::nursery, | ||
// clippy::cargo, | ||
clippy::unwrap_used, | ||
clippy::expect_used)] | ||
|
||
/// TODO | ||
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. Needs documentation. |
||
#[derive(Debug)] | ||
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. Can you make this match the derives and attributes of |
||
pub struct Years(pub(crate) u32); // I believe the fields of the struct should be private | ||
|
||
impl Years { | ||
/// TODO | ||
pub fn new(num_years: u32) -> Years { | ||
Self(num_years) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::NaiveDate; | ||
use crate::Years; | ||
|
||
#[test] | ||
fn checked_adding_years() { | ||
assert_eq!( | ||
NaiveDate::from_ymd_opt(2023, 2, 28).unwrap(), | ||
NaiveDate::from_ymd_opt(2020, 2, 28).unwrap().checked_add_years(Years::new(3)).unwrap() | ||
); | ||
} | ||
|
||
#[test] | ||
fn using_add_operator() { | ||
assert_eq!( | ||
NaiveDate::from_ymd_opt(2023, 2, 28).unwrap(), | ||
NaiveDate::from_ymd_opt(2020, 2, 28).unwrap() + Years::new(3) | ||
); | ||
} | ||
|
||
#[test] | ||
fn checked_subtracting_years() { | ||
assert_eq!( | ||
NaiveDate::from_ymd_opt(2017, 2, 28).unwrap(), | ||
NaiveDate::from_ymd_opt(2020, 2, 28).unwrap().checked_sub_years(Years::new(3)).unwrap() | ||
); | ||
} | ||
|
||
#[test] | ||
fn using_sub_operator() { | ||
assert_eq!( | ||
NaiveDate::from_ymd_opt(2017, 2, 28).unwrap(), | ||
NaiveDate::from_ymd_opt(2020, 2, 28).unwrap() - Years::new(3) | ||
); | ||
} | ||
} |
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.
The
Date
type is deprecated, better to not add new methods to it.