support Add/Sub assignment operators for Naive types.

Requires Rust 1.8 or higher.
This commit is contained in:
János Illés 2016-10-02 15:38:06 +02:00
parent e308e8e929
commit 978b64ff33
3 changed files with 99 additions and 3 deletions

View File

@ -48,7 +48,7 @@
//! This is currently the internal format of Chrono's date types. //! This is currently the internal format of Chrono's date types.
use std::{str, fmt, hash}; use std::{str, fmt, hash};
use std::ops::{Add, Sub}; use std::ops::{Add, Sub, AddAssign, SubAssign};
use num::traits::ToPrimitive; use num::traits::ToPrimitive;
use {Weekday, Datelike}; use {Weekday, Datelike};
@ -1318,6 +1318,12 @@ impl Add<Duration> for NaiveDate {
} }
} }
impl AddAssign<Duration> for NaiveDate {
fn add_assign(&mut self, rhs: Duration) {
*self = self.add(rhs);
}
}
/// A subtraction of `NaiveDate` from `NaiveDate` yields a `Duration` of integral numbers. /// A subtraction of `NaiveDate` from `NaiveDate` yields a `Duration` of integral numbers.
/// ///
/// This does not overflow or underflow at all, /// This does not overflow or underflow at all,
@ -1384,6 +1390,12 @@ impl Sub<Duration> for NaiveDate {
} }
} }
impl SubAssign<Duration> for NaiveDate {
fn sub_assign(&mut self, rhs: Duration) {
*self = self.sub(rhs);
}
}
/// The `Debug` output of the naive date `d` is same to /// The `Debug` output of the naive date `d` is same to
/// [`d.format("%Y-%m-%d")`](../../format/strftime/index.html). /// [`d.format("%Y-%m-%d")`](../../format/strftime/index.html).
/// ///
@ -1978,6 +1990,26 @@ mod tests {
check((MIN_YEAR, 1, 1), (0, 1, 1), Duration::days(MIN_DAYS_FROM_YEAR_0 as i64)); check((MIN_YEAR, 1, 1), (0, 1, 1), Duration::days(MIN_DAYS_FROM_YEAR_0 as i64));
} }
#[test]
fn test_date_addassignment() {
let ymd = NaiveDate::from_ymd;
let mut date = ymd(2016, 10, 1);
date += Duration::days(10);
assert_eq!(date, ymd(2016, 10, 11));
date += Duration::days(30);
assert_eq!(date, ymd(2016, 11, 10));
}
#[test]
fn test_date_subassignment() {
let ymd = NaiveDate::from_ymd;
let mut date = ymd(2016, 10, 11);
date -= Duration::days(10);
assert_eq!(date, ymd(2016, 10, 1));
date -= Duration::days(2);
assert_eq!(date, ymd(2016, 9, 29));
}
#[test] #[test]
fn test_date_fmt() { fn test_date_fmt() {
assert_eq!(format!("{:?}", NaiveDate::from_ymd(2012, 3, 4)), "2012-03-04"); assert_eq!(format!("{:?}", NaiveDate::from_ymd(2012, 3, 4)), "2012-03-04");

View File

@ -5,7 +5,7 @@
//! ISO 8601 date and time without timezone. //! ISO 8601 date and time without timezone.
use std::{str, fmt, hash}; use std::{str, fmt, hash};
use std::ops::{Add, Sub}; use std::ops::{Add, Sub, AddAssign, SubAssign};
use num::traits::ToPrimitive; use num::traits::ToPrimitive;
use {Weekday, Timelike, Datelike}; use {Weekday, Timelike, Datelike};
@ -1101,6 +1101,12 @@ impl Add<Duration> for NaiveDateTime {
} }
} }
impl AddAssign<Duration> for NaiveDateTime {
fn add_assign(&mut self, rhs: Duration) {
*self = self.add(rhs);
}
}
/// A subtraction of `NaiveDateTime` from `NaiveDateTime` yields a `Duration`. /// A subtraction of `NaiveDateTime` from `NaiveDateTime` yields a `Duration`.
/// This does not overflow or underflow at all. /// This does not overflow or underflow at all.
/// ///
@ -1204,6 +1210,12 @@ impl Sub<Duration> for NaiveDateTime {
} }
} }
impl SubAssign<Duration> for NaiveDateTime {
fn sub_assign(&mut self, rhs: Duration) {
*self = self.sub(rhs);
}
}
/// The `Debug` output of the naive date and time `dt` is same to /// The `Debug` output of the naive date and time `dt` is same to
/// [`dt.format("%Y-%m-%dT%H:%M:%S%.f")`](../../format/strftime/index.html). /// [`dt.format("%Y-%m-%dT%H:%M:%S%.f")`](../../format/strftime/index.html).
/// ///
@ -1615,6 +1627,26 @@ mod tests {
Duration::seconds(999_999_999)); Duration::seconds(999_999_999));
} }
#[test]
fn test_datetime_addassignment() {
let ymdhms = |y,m,d,h,n,s| NaiveDate::from_ymd(y,m,d).and_hms(h,n,s);
let mut date = ymdhms(2016, 10, 1, 10, 10, 10);
date += Duration::minutes(10_000_000);
assert_eq!(date, ymdhms(2035, 10, 6, 20, 50, 10));
date += Duration::days(10);
assert_eq!(date, ymdhms(2035, 10, 16, 20, 50, 10));
}
#[test]
fn test_datetime_subassignment() {
let ymdhms = |y,m,d,h,n,s| NaiveDate::from_ymd(y,m,d).and_hms(h,n,s);
let mut date = ymdhms(2016, 10, 1, 10, 10, 10);
date -= Duration::minutes(10_000_000);
assert_eq!(date, ymdhms(1997, 9, 26, 23, 30, 10));
date -= Duration::days(10);
assert_eq!(date, ymdhms(1997, 9, 16, 23, 30, 10));
}
#[test] #[test]
fn test_datetime_timestamp() { fn test_datetime_timestamp() {
let to_timestamp = |y,m,d,h,n,s| NaiveDate::from_ymd(y,m,d).and_hms(h,n,s).timestamp(); let to_timestamp = |y,m,d,h,n,s| NaiveDate::from_ymd(y,m,d).and_hms(h,n,s).timestamp();

View File

@ -158,7 +158,7 @@
//! **there is absolutely no guarantee that the leap second read has actually happened**. //! **there is absolutely no guarantee that the leap second read has actually happened**.
use std::{str, fmt, hash}; use std::{str, fmt, hash};
use std::ops::{Add, Sub}; use std::ops::{Add, Sub, AddAssign, SubAssign};
use Timelike; use Timelike;
use div::div_mod_floor; use div::div_mod_floor;
@ -972,6 +972,12 @@ impl Add<Duration> for NaiveTime {
} }
} }
impl AddAssign<Duration> for NaiveTime {
fn add_assign(&mut self, rhs: Duration) {
*self = self.add(rhs);
}
}
/// A subtraction of `NaiveTime` from `NaiveTime` yields a `Duration` within +/- 1 day. /// A subtraction of `NaiveTime` from `NaiveTime` yields a `Duration` within +/- 1 day.
/// This does not overflow or underflow at all. /// This does not overflow or underflow at all.
/// ///
@ -1095,6 +1101,12 @@ impl Sub<Duration> for NaiveTime {
} }
} }
impl SubAssign<Duration> for NaiveTime {
fn sub_assign(&mut self, rhs: Duration) {
*self = self.sub(rhs);
}
}
/// The `Debug` output of the naive time `t` is same to /// The `Debug` output of the naive time `t` is same to
/// [`t.format("%H:%M:%S%.f")`](../../format/strftime/index.html). /// [`t.format("%H:%M:%S%.f")`](../../format/strftime/index.html).
/// ///
@ -1537,6 +1549,26 @@ mod tests {
(hmsm(3, 4, 6, 678), -86400)); (hmsm(3, 4, 6, 678), -86400));
} }
#[test]
fn test_time_addassignment() {
let hms = NaiveTime::from_hms;
let mut time = hms(12, 12, 12);
time += Duration::hours(10);
assert_eq!(time, hms(22, 12, 12));
time += Duration::hours(10);
assert_eq!(time, hms(8, 12, 12));
}
#[test]
fn test_time_subassignment() {
let hms = NaiveTime::from_hms;
let mut time = hms(12, 12, 12);
time -= Duration::hours(10);
assert_eq!(time, hms(2, 12, 12));
time -= Duration::hours(10);
assert_eq!(time, hms(16, 12, 12));
}
#[test] #[test]
fn test_time_sub() { fn test_time_sub() {
macro_rules! check { macro_rules! check {