pub struct Months(_);
Expand description
A duration in calendar months
Implementations
Trait Implementations
sourceimpl Add<Months> for NaiveDate
impl Add<Months> for NaiveDate
sourcefn add(self, months: Months) -> Self::Output
fn add(self, months: Months) -> Self::Output
An addition of months to NaiveDate
clamped to valid days in resulting month.
Panics
Panics if the resulting date would be out of range.
Example
use chrono::{NaiveDate, Months};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
assert_eq!(from_ymd(2014, 1, 1) + Months::new(1), from_ymd(2014, 2, 1));
assert_eq!(from_ymd(2014, 1, 1) + Months::new(11), from_ymd(2014, 12, 1));
assert_eq!(from_ymd(2014, 1, 1) + Months::new(12), from_ymd(2015, 1, 1));
assert_eq!(from_ymd(2014, 1, 1) + Months::new(13), from_ymd(2015, 2, 1));
assert_eq!(from_ymd(2014, 1, 31) + Months::new(1), from_ymd(2014, 2, 28));
assert_eq!(from_ymd(2020, 1, 31) + Months::new(1), from_ymd(2020, 2, 29));
sourceimpl Add<Months> for NaiveDateTime
impl Add<Months> for NaiveDateTime
sourcefn add(self, rhs: Months) -> Self::Output
fn add(self, rhs: Months) -> Self::Output
An addition of months to NaiveDateTime
clamped to valid days in resulting month.
Panics
Panics if the resulting date would be out of range.
Example
use chrono::{Months, NaiveDate};
assert_eq!(
NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(1, 0, 0).unwrap() + Months::new(1),
NaiveDate::from_ymd_opt(2014, 2, 1).unwrap().and_hms_opt(1, 0, 0).unwrap()
);
assert_eq!(
NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(0, 2, 0).unwrap() + Months::new(11),
NaiveDate::from_ymd_opt(2014, 12, 1).unwrap().and_hms_opt(0, 2, 0).unwrap()
);
assert_eq!(
NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(0, 0, 3).unwrap() + Months::new(12),
NaiveDate::from_ymd_opt(2015, 1, 1).unwrap().and_hms_opt(0, 0, 3).unwrap()
);
assert_eq!(
NaiveDate::from_ymd_opt(2014, 1, 1).unwrap().and_hms_opt(0, 0, 4).unwrap() + Months::new(13),
NaiveDate::from_ymd_opt(2015, 2, 1).unwrap().and_hms_opt(0, 0, 4).unwrap()
);
assert_eq!(
NaiveDate::from_ymd_opt(2014, 1, 31).unwrap().and_hms_opt(0, 5, 0).unwrap() + Months::new(1),
NaiveDate::from_ymd_opt(2014, 2, 28).unwrap().and_hms_opt(0, 5, 0).unwrap()
);
assert_eq!(
NaiveDate::from_ymd_opt(2020, 1, 31).unwrap().and_hms_opt(6, 0, 0).unwrap() + Months::new(1),
NaiveDate::from_ymd_opt(2020, 2, 29).unwrap().and_hms_opt(6, 0, 0).unwrap()
);
type Output = NaiveDateTime
type Output = NaiveDateTime
The resulting type after applying the
+
operator.sourceimpl Hash for Months
impl Hash for Months
sourcefn hash<__H: Hasher>(&self, state: &mut __H)
fn hash<__H: Hasher>(&self, state: &mut __H)
Feeds this value into the given [
Hasher
]. Read more1.3.0fn hash_slice<H>(data: &[Self], state: &mut H)where
H: Hasher,
fn hash_slice<H>(data: &[Self], state: &mut H)where
H: Hasher,
Feeds a slice of this type into the given [
Hasher
]. Read moresourceimpl PartialOrd<Months> for Months
impl PartialOrd<Months> for Months
sourceimpl Sub<Months> for NaiveDate
impl Sub<Months> for NaiveDate
sourcefn sub(self, months: Months) -> Self::Output
fn sub(self, months: Months) -> Self::Output
A subtraction of Months from NaiveDate
clamped to valid days in resulting month.
Panics
Panics if the resulting date would be out of range.
Example
use chrono::{NaiveDate, Months};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
assert_eq!(from_ymd(2014, 1, 1) - Months::new(11), from_ymd(2013, 2, 1));
assert_eq!(from_ymd(2014, 1, 1) - Months::new(12), from_ymd(2013, 1, 1));
assert_eq!(from_ymd(2014, 1, 1) - Months::new(13), from_ymd(2012, 12, 1));
sourceimpl Sub<Months> for NaiveDateTime
impl Sub<Months> for NaiveDateTime
A subtraction of Months from NaiveDateTime
clamped to valid days in resulting month.
Panics
Panics if the resulting date would be out of range.
Example
use chrono::{Months, NaiveDate};
assert_eq!(
NaiveDate::from_ymd_opt(2014, 01, 01).unwrap().and_hms_opt(01, 00, 00).unwrap() - Months::new(11),
NaiveDate::from_ymd_opt(2013, 02, 01).unwrap().and_hms_opt(01, 00, 00).unwrap()
);
assert_eq!(
NaiveDate::from_ymd_opt(2014, 01, 01).unwrap().and_hms_opt(00, 02, 00).unwrap() - Months::new(12),
NaiveDate::from_ymd_opt(2013, 01, 01).unwrap().and_hms_opt(00, 02, 00).unwrap()
);
assert_eq!(
NaiveDate::from_ymd_opt(2014, 01, 01).unwrap().and_hms_opt(00, 00, 03).unwrap() - Months::new(13),
NaiveDate::from_ymd_opt(2012, 12, 01).unwrap().and_hms_opt(00, 00, 03).unwrap()
);
impl Copy for Months
impl Eq for Months
impl StructuralEq for Months
impl StructuralPartialEq for Months
Auto Trait Implementations
impl RefUnwindSafe for Months
impl Send for Months
impl Sync for Months
impl Unpin for Months
impl UnwindSafe for Months
Blanket Implementations
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstablefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> ToOwned for Twhere
T: Clone,
impl<T> ToOwned for Twhere
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
fn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
Uses borrowed data to replace owned data, usually by cloning. Read more