Skip to content

Commit

Permalink
libtime: Fix adding negative duration to Timespec.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruuda committed Sep 3, 2014
1 parent 8a89867 commit 0b4912b
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/libtime/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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() {
Expand Down

5 comments on commit 0b4912b

@bors
Copy link
Contributor

@bors bors commented on 0b4912b Sep 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at ruuda@0b4912b

@bors
Copy link
Contributor

@bors bors commented on 0b4912b Sep 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging ruud-v-a/rust/timespec-arithmetic = 0b4912b into auto

@bors
Copy link
Contributor

@bors bors commented on 0b4912b Sep 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ruud-v-a/rust/timespec-arithmetic = 0b4912b merged ok, testing candidate = c95aa99

@bors
Copy link
Contributor

@bors bors commented on 0b4912b Sep 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = c95aa99

Please sign in to comment.