pub struct Months(/* private fields */);
Expand description
A duration in calendar months
Implementations§
Trait Implementations§
Source§impl<Tz: TimeZone> Add<Months> for DateTime<Tz>
Add Months
to DateTime
.
impl<Tz: TimeZone> Add<Months> for DateTime<Tz>
Add Months
to DateTime
.
The result will be clamped to valid days in the resulting month, see checked_add_months
for
details.
§Panics
Panics if:
- The resulting date would be out of range.
- The local time at the resulting date does not exist or is ambiguous, for example during a daylight saving time transition.
Strongly consider using DateTime<Tz>::checked_add_months
to get an Option
instead.
Source§impl Add<Months> for NaiveDate
Add Months
to NaiveDate
.
impl Add<Months> for NaiveDate
Add Months
to NaiveDate
.
The result will be clamped to valid days in the resulting month, see checked_add_months
for
details.
§Panics
Panics if the resulting date would be out of range.
Consider using NaiveDate::checked_add_months
to get an Option
instead.
§Example
use chrono::{Months, NaiveDate};
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));
Source§impl Add<Months> for NaiveDateTime
Add Months
to NaiveDateTime
.
impl Add<Months> for NaiveDateTime
Add Months
to NaiveDateTime
.
The result will be clamped to valid days in the resulting month, see checked_add_months
for
details.
§Panics
Panics if the resulting date would be out of range.
Consider using checked_add_months
to get an Option
instead.
§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()
);
Source§impl Ord for Months
impl Ord for Months
Source§impl PartialOrd for Months
impl PartialOrd for Months
Source§impl<Tz: TimeZone> Sub<Months> for DateTime<Tz>
Subtract Months
from DateTime
.
impl<Tz: TimeZone> Sub<Months> for DateTime<Tz>
Subtract Months
from DateTime
.
The result will be clamped to valid days in the resulting month, see
DateTime<Tz>::checked_sub_months
for details.
§Panics
Panics if:
- The resulting date would be out of range.
- The local time at the resulting date does not exist or is ambiguous, for example during a daylight saving time transition.
Strongly consider using DateTime<Tz>::checked_sub_months
to get an Option
instead.
Source§impl Sub<Months> for NaiveDate
Subtract Months
from NaiveDate
.
impl Sub<Months> for NaiveDate
Subtract Months
from NaiveDate
.
The result will be clamped to valid days in the resulting month, see checked_sub_months
for
details.
§Panics
Panics if the resulting date would be out of range.
Consider using NaiveDate::checked_sub_months
to get an Option
instead.
§Example
use chrono::{Months, NaiveDate};
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));
Source§impl Sub<Months> for NaiveDateTime
Subtract Months
from NaiveDateTime
.
impl Sub<Months> for NaiveDateTime
Subtract Months
from NaiveDateTime
.
The result will be clamped to valid days in the resulting month, see
NaiveDateTime::checked_sub_months
for details.
§Panics
Panics if the resulting date would be out of range.
Consider using NaiveDateTime::checked_sub_months
to get an Option
instead.
§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()
);