fixed a bug on `DateTimeZ::add` with the negative fractional `Duration`.

This commit is contained in:
Kang Seonghoon 2014-07-29 15:38:24 +09:00
parent c7c3052892
commit 07d98709df
2 changed files with 6 additions and 1 deletions

View File

@ -832,6 +832,7 @@ mod tests {
check((2014, 1, 1), Duration::zero(), (2014, 1, 1)); check((2014, 1, 1), Duration::zero(), (2014, 1, 1));
check((2014, 1, 1), Duration::seconds(86399), (2014, 1, 1)); check((2014, 1, 1), Duration::seconds(86399), (2014, 1, 1));
check((2014, 1, 1), Duration::seconds(-86399), (2014, 1, 1)); // always round towards zero
check((2014, 1, 1), Duration::days(1), (2014, 1, 2)); check((2014, 1, 1), Duration::days(1), (2014, 1, 2));
check((2014, 1, 1), Duration::days(-1), (2013, 12, 31)); check((2014, 1, 1), Duration::days(-1), (2013, 12, 31));
check((2014, 1, 1), Duration::days(364), (2014, 12, 31)); check((2014, 1, 1), Duration::days(364), (2014, 12, 31));

View File

@ -191,7 +191,9 @@ impl Timelike for DateTimeZ {
impl Add<Duration,DateTimeZ> for DateTimeZ { impl Add<Duration,DateTimeZ> for DateTimeZ {
fn add(&self, rhs: &Duration) -> DateTimeZ { fn add(&self, rhs: &Duration) -> DateTimeZ {
let mut date = self.date + *rhs; // we want `(DateZ + days in Duration) + (TimeZ + secs/nanos in Duration)`
// to be equal to `DateTimeZ + Duration`, but `DateZ + Duration` rounds towards zero.
let mut date = self.date + Duration::days(rhs.to_tuple().val0());
let time = self.time + *rhs; let time = self.time + *rhs;
if time < self.time { if time < self.time {
// since the time portion of the duration is always positive and bounded, // since the time portion of the duration is always positive and bounded,
@ -232,6 +234,8 @@ mod tests {
let ymdhms = |y,m,d,h,n,s| DateTimeZ::from_ymdhms(y,m,d,h,n,s); let ymdhms = |y,m,d,h,n,s| DateTimeZ::from_ymdhms(y,m,d,h,n,s);
assert_eq!(ymdhms(2014, 5, 6, 7, 8, 9) + Duration::seconds(3600 + 60 + 1), assert_eq!(ymdhms(2014, 5, 6, 7, 8, 9) + Duration::seconds(3600 + 60 + 1),
ymdhms(2014, 5, 6, 8, 9, 10)); ymdhms(2014, 5, 6, 8, 9, 10));
assert_eq!(ymdhms(2014, 5, 6, 7, 8, 9) + Duration::seconds(-(3600 + 60 + 1)),
ymdhms(2014, 5, 6, 6, 7, 8));
assert_eq!(ymdhms(2014, 5, 6, 7, 8, 9) + Duration::seconds(86399), assert_eq!(ymdhms(2014, 5, 6, 7, 8, 9) + Duration::seconds(86399),
ymdhms(2014, 5, 7, 7, 8, 8)); ymdhms(2014, 5, 7, 7, 8, 8));
assert_eq!(ymdhms(2014, 5, 6, 7, 8, 9) + Duration::seconds(86400 * 10), assert_eq!(ymdhms(2014, 5, 6, 7, 8, 9) + Duration::seconds(86400 * 10),