From 0b4912b9afdb75c11d1b44f1b8c3df8b9839ca67 Mon Sep 17 00:00:00 2001 From: Ruud van Asseldonk Date: Wed, 3 Sep 2014 19:27:55 +0200 Subject: [PATCH] libtime: Fix adding negative duration to Timespec. --- src/libtime/lib.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libtime/lib.rs b/src/libtime/lib.rs index 34402d01c865d..848cd5e17b1cf 100644 --- a/src/libtime/lib.rs +++ b/src/libtime/lib.rs @@ -103,6 +103,9 @@ impl Add for Timespec { if nsec >= NSEC_PER_SEC { nsec -= NSEC_PER_SEC; sec += 1; + } else if nsec < 0 { + nsec += NSEC_PER_SEC; + sec -= 1; } Timespec::new(sec, nsec) } @@ -1533,6 +1536,12 @@ mod tests { let w = u + v; assert_eq!(w.sec, 4); assert_eq!(w.nsec, 1); + + let k = Timespec::new(1, 0); + let l = Duration::nanoseconds(-1); + let m = k + l; + assert_eq!(m.sec, 0); + assert_eq!(m.nsec, 999_999_999); } fn test_timespec_sub() {