From 978b64ff3342c460060a710c69556144f65ae1f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Ill=C3=A9s?= Date: Sun, 2 Oct 2016 15:38:06 +0200 Subject: [PATCH] support Add/Sub assignment operators for Naive types. Requires Rust 1.8 or higher. --- src/naive/date.rs | 34 +++++++++++++++++++++++++++++++++- src/naive/datetime.rs | 34 +++++++++++++++++++++++++++++++++- src/naive/time.rs | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 99 insertions(+), 3 deletions(-) diff --git a/src/naive/date.rs b/src/naive/date.rs index 85cde84..7f06d32 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -48,7 +48,7 @@ //! This is currently the internal format of Chrono's date types. use std::{str, fmt, hash}; -use std::ops::{Add, Sub}; +use std::ops::{Add, Sub, AddAssign, SubAssign}; use num::traits::ToPrimitive; use {Weekday, Datelike}; @@ -1318,6 +1318,12 @@ impl Add for NaiveDate { } } +impl AddAssign 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. /// /// This does not overflow or underflow at all, @@ -1384,6 +1390,12 @@ impl Sub for NaiveDate { } } +impl SubAssign for NaiveDate { + fn sub_assign(&mut self, rhs: Duration) { + *self = self.sub(rhs); + } +} + /// The `Debug` output of the naive date `d` is same to /// [`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)); } + #[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] fn test_date_fmt() { assert_eq!(format!("{:?}", NaiveDate::from_ymd(2012, 3, 4)), "2012-03-04"); diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index c611f40..e734fd4 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -5,7 +5,7 @@ //! ISO 8601 date and time without timezone. use std::{str, fmt, hash}; -use std::ops::{Add, Sub}; +use std::ops::{Add, Sub, AddAssign, SubAssign}; use num::traits::ToPrimitive; use {Weekday, Timelike, Datelike}; @@ -1101,6 +1101,12 @@ impl Add for NaiveDateTime { } } +impl AddAssign for NaiveDateTime { + fn add_assign(&mut self, rhs: Duration) { + *self = self.add(rhs); + } +} + /// A subtraction of `NaiveDateTime` from `NaiveDateTime` yields a `Duration`. /// This does not overflow or underflow at all. /// @@ -1204,6 +1210,12 @@ impl Sub for NaiveDateTime { } } +impl SubAssign 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 /// [`dt.format("%Y-%m-%dT%H:%M:%S%.f")`](../../format/strftime/index.html). /// @@ -1615,6 +1627,26 @@ mod tests { 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] 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(); diff --git a/src/naive/time.rs b/src/naive/time.rs index ec8f564..76ab6e4 100644 --- a/src/naive/time.rs +++ b/src/naive/time.rs @@ -158,7 +158,7 @@ //! **there is absolutely no guarantee that the leap second read has actually happened**. use std::{str, fmt, hash}; -use std::ops::{Add, Sub}; +use std::ops::{Add, Sub, AddAssign, SubAssign}; use Timelike; use div::div_mod_floor; @@ -972,6 +972,12 @@ impl Add for NaiveTime { } } +impl AddAssign 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. /// This does not overflow or underflow at all. /// @@ -1095,6 +1101,12 @@ impl Sub for NaiveTime { } } +impl SubAssign for NaiveTime { + fn sub_assign(&mut self, rhs: Duration) { + *self = self.sub(rhs); + } +} + /// The `Debug` output of the naive time `t` is same to /// [`t.format("%H:%M:%S%.f")`](../../format/strftime/index.html). /// @@ -1537,6 +1549,26 @@ mod tests { (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] fn test_time_sub() { macro_rules! check {