1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// This is a part of relativedelta.
// See README.md and LICENSE.txt for details.

//! # relativedelta: Relative Date and Time for Rust
//!
//! ## Usage
//!
//! Put this in your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! relativedelta = "0.1"
//! ```

#[macro_use] extern crate impl_ops;

pub mod relativedelta;
pub use crate::relativedelta::RelativeDelta;

#[cfg(test)]
mod tests {
	use crate::relativedelta::RelativeDelta;
	use chrono::{TimeZone, Utc};

	#[test]
	fn test_add_self() {
		let lhs = RelativeDelta::with_years(1).new();
		let rhs = RelativeDelta::with_years(2).new();

		assert_eq!(lhs + rhs, RelativeDelta::with_years(3).new());
		assert_eq!(lhs + rhs, rhs + lhs);
	}

	#[test]
	fn test_add() {
		let year = 2020;
		let month = 4;
		//let month2 = 3;
		//let months = -11;
		let day = 28;
		//let days = 31;
		let hour = 12;
		let min = 35;
		let sec = 48;
		//let n_secs : i64 = -11_111_111_111;
		let dt = Utc.ymd(year, month, day).and_hms(hour, min, sec);
//		let ddt = RelativeDelta::years(1)
//				.with_month(month2)
//				.with_months(months)
//				.with_days(days)
//				.with_nanoseconds(n_secs)
//				.new();

		let add_1_year = RelativeDelta::with_years(1).new();
		assert_eq!(
			dt + add_1_year,
			Utc.ymd(2021, month, day).and_hms(hour, min, sec)
		);

		let sub_1_year = RelativeDelta::with_years(-1).new();
		assert_eq!(
			dt + sub_1_year,
			Utc.ymd(2019, month, day).and_hms(hour, min, sec)
		);

		let set_year = RelativeDelta::with_year(2010).new();
		assert_eq!(
			dt + set_year,
			Utc.ymd(2010, month, day).and_hms(hour, min, sec)
		);

		let set_year = RelativeDelta::with_year(-1).new();
		assert_eq!(
			dt + set_year,
			Utc.ymd(-1, month, day).and_hms(hour, min, sec)
		);

		let add_69_months = RelativeDelta::with_months(69).new();
		// Expected after fix
		assert_eq!(add_69_months.years(), 5);
		assert_eq!(add_69_months.months(), 9);
		assert_eq!(
			dt + add_69_months,
			Utc.ymd(2026, 1, day).and_hms(hour, min, sec)
		);

		let sub_6_months = RelativeDelta::with_months(-6).new();
		assert_eq!(
			dt + sub_6_months,
			Utc.ymd(2019, 10, day).and_hms(hour, min, sec)
		);

		let sub_47_months = RelativeDelta::with_months(-47).new();
		// Expected after fix
		assert_eq!(sub_47_months.years(), -3);
		assert_eq!(sub_47_months.months(), -11);
		assert_eq!(
			dt + sub_47_months,
			Utc.ymd(2016, 5, day).and_hms(hour, min, sec)
		);

		let add_400_days = RelativeDelta::with_days(400).new();
		assert_eq!(
			dt + add_400_days,
			Utc.ymd(2021, 6, 2).and_hms(hour, min, sec)
		);

		let sub_400_days = RelativeDelta::with_days(-400).new();
		assert_eq!(
			dt + sub_400_days,
			Utc.ymd(2019, 3, 25).and_hms(hour, min, sec)
		);

		let pay1 = RelativeDelta::with_day(1)
				.with_days(-1)
				.with_month(Some(3))
				.with_months(1)
				.new();
		assert_eq!(dt + pay1, Utc.ymd(2020, 3, 31).and_hms(hour, min, sec));

		let pay2 = RelativeDelta::with_day(1)
				.with_days(-1)
				.with_month(Some(6))
				.with_months(1)
				.new();
		assert_eq!(dt + pay2, Utc.ymd(2020, 6, 30).and_hms(hour, min, sec));

		let pay3 = RelativeDelta::with_day(1)
				.with_days(-1)
				.with_month(Some(9))
				.with_months(1)
				.new();
		assert_eq!(dt + pay3, Utc.ymd(2020, 9, 30).and_hms(hour, min, sec));

		let pay4 = RelativeDelta::with_day(1)
				.with_days(-1)
				.with_month(Some(12))
				.with_months(1)
				.new();
		assert_eq!(dt + pay4, Utc.ymd(2020, 12, 31).and_hms(hour, min, sec));

	}

	#[test]
	fn test_mul() {
		let ddt = RelativeDelta::with_years(10)
				.and_months(6)
				.and_days(-15)
				.and_hours(23)
				.new();
		let r = ddt * 0.5_f64;


		assert_eq!(r, RelativeDelta::yysmmsdds(None, 5, None, 3, None, -7).and_minutes(-30).new());

		let rhs = RelativeDelta::yysmmsdds(Some(2020), 1, Some(1), 42, None, 0).new();
		assert_eq!(rhs * 0.5, RelativeDelta::with_years(2).and_year(Some(2020)).and_months(3).and_month(Some(1)).new());
	}

	#[test]
	fn test_init_with_float() {
		let ddt = RelativeDelta::ysmsdshsmsssns_f(1.5, -18.0, 0.0, 0.0, 0.0, 0.0, 0).new();
		assert_eq!(
			ddt,
			RelativeDelta::yysmmsdds(None, 0, None, 0, None, 0)
					.and_hhsmmssss(None, 0, None, 0, None, 0)
					.new()
		);

		let ddt = RelativeDelta::ysmsdshsmsssns_f(-0.42, -15.7, -12.3, -5.32, 3.14, 0.15, 22232).new();
		let r = RelativeDelta::with_years(-1).and_months(-8).and_months_f( -0.7399999999999984).and_days(-12).and_hours(-12).and_minutes(-28).and_seconds(-3).and_nanoseconds(-449977768).new();
		assert_eq!(ddt, r);
	}
}