Skip to content

Commit c95aa99

Browse files
committed
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.
2 parents d59d97c + 0b4912b commit c95aa99

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/libtime/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ impl Add<Duration, Timespec> for Timespec {
103103
if nsec >= NSEC_PER_SEC {
104104
nsec -= NSEC_PER_SEC;
105105
sec += 1;
106+
} else if nsec < 0 {
107+
nsec += NSEC_PER_SEC;
108+
sec -= 1;
106109
}
107110
Timespec::new(sec, nsec)
108111
}
@@ -1533,6 +1536,12 @@ mod tests {
15331536
let w = u + v;
15341537
assert_eq!(w.sec, 4);
15351538
assert_eq!(w.nsec, 1);
1539+
1540+
let k = Timespec::new(1, 0);
1541+
let l = Duration::nanoseconds(-1);
1542+
let m = k + l;
1543+
assert_eq!(m.sec, 0);
1544+
assert_eq!(m.nsec, 999_999_999);
15361545
}
15371546

15381547
fn test_timespec_sub() {

0 commit comments

Comments
 (0)