Skip to content

Commit

Permalink
std: Fix the return value of Duration::span
Browse files Browse the repository at this point in the history
The subtraction was erroneously backwards, returning negative durations!

Closes #18925
  • Loading branch information
alexcrichton committed Nov 13, 2014
1 parent 37ea270 commit 8771394
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/librustc/util/common.rs
Expand Up @@ -35,7 +35,7 @@ pub fn time<T, U>(do_it: bool, what: &str, u: U, f: |U| -> T) -> T {
let rv = rv.unwrap();

println!("{}time: {}.{:03} \t{}", " ".repeat(old),
dur.num_seconds(), dur.num_milliseconds(), what);
dur.num_seconds(), dur.num_milliseconds() % 1000, what);
depth.replace(Some(old));

rv
Expand Down
9 changes: 8 additions & 1 deletion src/libstd/time/duration.rs
Expand Up @@ -140,7 +140,7 @@ impl Duration {
pub fn span(f: ||) -> Duration {
let before = super::precise_time_ns();
f();
Duration::nanoseconds((before - super::precise_time_ns()) as i64)
Duration::nanoseconds((super::precise_time_ns() - before) as i64)
}

/// Returns the total number of whole weeks in the duration.
Expand Down Expand Up @@ -565,4 +565,11 @@ mod tests {
assert_eq!(format!("{:30}", Duration::days(1) + Duration::milliseconds(2345)),
"P1DT2.345S".to_string());
}

#[test]
fn span() {
use io::timer::sleep;
let dur = Duration::span(|| sleep(Duration::milliseconds(5)));
assert!(dur > Duration::milliseconds(1));
}
}

5 comments on commit 8771394

@bors
Copy link
Contributor

@bors bors commented on 8771394 Nov 13, 2014

Choose a reason for hiding this comment

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

saw approval from huonw
at alexcrichton@8771394

@bors
Copy link
Contributor

@bors bors commented on 8771394 Nov 13, 2014

Choose a reason for hiding this comment

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

merging alexcrichton/rust/issue-18925 = 8771394 into auto

@bors
Copy link
Contributor

@bors bors commented on 8771394 Nov 13, 2014

Choose a reason for hiding this comment

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

alexcrichton/rust/issue-18925 = 8771394 merged ok, testing candidate = b1c84d6

@bors
Copy link
Contributor

@bors bors commented on 8771394 Nov 14, 2014

Choose a reason for hiding this comment

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

@bors
Copy link
Contributor

@bors bors commented on 8771394 Nov 14, 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 = b1c84d6

Please sign in to comment.