diff options
| author | bors <bors@rust-lang.org> | 2014-09-04 03:01:03 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-09-04 03:01:03 +0000 |
| commit | c95aa9950fdbab2788b211e31e77d05ff3be7bc2 (patch) | |
| tree | 1f7a3e326d37defc8260e9f585885f942ac11e11 /src | |
| parent | d59d97cbec6807166bc237d6f3f9bd1eeb5288d7 (diff) | |
| parent | 0b4912b9afdb75c11d1b44f1b8c3df8b9839ca67 (diff) | |
| download | rust-c95aa9950fdbab2788b211e31e77d05ff3be7bc2.tar.gz rust-c95aa9950fdbab2788b211e31e77d05ff3be7bc2.zip | |
auto merge of #16972 : ruud-v-a/rust/timespec-arithmetic, r=alexcrichton
Changing from `Timespec` to `Duration` introduced a bug for negative durations. This fixes that.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libtime/lib.rs | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/src/libtime/lib.rs b/src/libtime/lib.rs index 34402d01c86..848cd5e17b1 100644 --- a/src/libtime/lib.rs +++ b/src/libtime/lib.rs @@ -103,6 +103,9 @@ impl Add<Duration, Timespec> 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() { |
