Skip to content

Commit b1c84d6

Browse files
committed
auto merge of #18926 : alexcrichton/rust/issue-18925, r=huonw
The subtraction was erroneously backwards, returning negative durations! Closes #18925
2 parents 15ba87f + 8771394 commit b1c84d6

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/librustc/util/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn time<T, U>(do_it: bool, what: &str, u: U, f: |U| -> T) -> T {
3535
let rv = rv.unwrap();
3636

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

4141
rv

src/libstd/time/duration.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl Duration {
140140
pub fn span(f: ||) -> Duration {
141141
let before = super::precise_time_ns();
142142
f();
143-
Duration::nanoseconds((before - super::precise_time_ns()) as i64)
143+
Duration::nanoseconds((super::precise_time_ns() - before) as i64)
144144
}
145145

146146
/// Returns the total number of whole weeks in the duration.
@@ -565,4 +565,11 @@ mod tests {
565565
assert_eq!(format!("{:30}", Duration::days(1) + Duration::milliseconds(2345)),
566566
"P1DT2.345S".to_string());
567567
}
568+
569+
#[test]
570+
fn span() {
571+
use io::timer::sleep;
572+
let dur = Duration::span(|| sleep(Duration::milliseconds(5)));
573+
assert!(dur > Duration::milliseconds(1));
574+
}
568575
}

0 commit comments

Comments
 (0)