libtime: Fix adding negative duration to Timespec.

This commit is contained in:
Ruud van Asseldonk 2014-09-03 19:27:55 +02:00
parent 8a8986776d
commit 0b4912b9af
1 changed files with 9 additions and 0 deletions

View File

@ -103,6 +103,9 @@ impl Add<Duration, Timespec> for Timespec {
if nsec >= NSEC_PER_SEC { if nsec >= NSEC_PER_SEC {
nsec -= NSEC_PER_SEC; nsec -= NSEC_PER_SEC;
sec += 1; sec += 1;
} else if nsec < 0 {
nsec += NSEC_PER_SEC;
sec -= 1;
} }
Timespec::new(sec, nsec) Timespec::new(sec, nsec)
} }
@ -1533,6 +1536,12 @@ mod tests {
let w = u + v; let w = u + v;
assert_eq!(w.sec, 4); assert_eq!(w.sec, 4);
assert_eq!(w.nsec, 1); 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() { fn test_timespec_sub() {