Skip to content

Commit 5a19e53

Browse files
committed
Fix subtraction of Instants
1 parent 0dabad7 commit 5a19e53

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ Versioning].
77

88
---
99

10-
## Unreleased
10+
## 0.2.12 [2020-04-30]
11+
12+
### Fixed
13+
14+
Subtracting `Instant`s can correctly result in a negative duration, rather than
15+
resulting in the absolute value of it.
16+
17+
## 0.2.11 [2020-04-27]
1118

1219
### Added
1320

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "time"
3-
version = "0.2.11"
3+
version = "0.2.12"
44
authors = ["Jacob Pratt <[email protected]>"]
55
edition = "2018"
66
repository = "https://github.com/time-rs/time"

src/instant.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ impl Sub for Instant {
154154
Ordering::Greater => (self.inner - other.inner)
155155
.try_into()
156156
.expect("overflow converting `std::time::Duration` to `time::Duration`"),
157-
Ordering::Less => (other.inner - self.inner)
158-
.try_into()
157+
Ordering::Less => -Duration::try_from(other.inner - self.inner)
159158
.expect("overflow converting `std::time::Duration` to `time::Duration`"),
160159
}
161160
}
@@ -496,4 +495,19 @@ mod test {
496495
let now_std = StdInstant::from(now_time) - 1.seconds();
497496
assert!(now_std < now_time);
498497
}
498+
499+
#[test]
500+
fn sub_regression() {
501+
let now = Instant::now();
502+
let future = now + Duration::seconds(5);
503+
let past = now - Duration::seconds(5);
504+
505+
assert_eq!(future - now, Duration::seconds(5));
506+
assert_eq!(now - past, Duration::seconds(5));
507+
assert_eq!(future - past, Duration::seconds(10));
508+
509+
assert_eq!(now - future, Duration::seconds(-5));
510+
assert_eq!(past - now, Duration::seconds(-5));
511+
assert_eq!(past - future, Duration::seconds(-10));
512+
}
499513
}

0 commit comments

Comments
 (0)