Skip to content

Commit 66e1ba2

Browse files
committed
Account for subsecond values when adding to Time
Fixes #252
1 parent 20e9414 commit 66e1ba2

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ Versioning].
77

88
---
99

10+
## 0.2.14 [2020-05-02]
11+
12+
### Fixed
13+
14+
Adding/subtracting a `core::time::Duration` now correctly takes subsecond
15+
values into account. This also affects `PrimitiveDateTime` and `OffsetDateTime`.
16+
1017
## 0.2.13 [2020-05-01]
1118

1219
### Fixed

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.13"
3+
version = "0.2.14"
44
authors = ["Jacob Pratt <[email protected]>"]
55
edition = "2018"
66
repository = "https://github.com/time-rs/time"

src/time_mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,8 @@ impl Add<StdDuration> for Time {
686686
/// ```
687687
#[inline(always)]
688688
fn add(self, duration: StdDuration) -> Self::Output {
689-
self + Duration::seconds((duration.as_secs() % 86_400) as i64)
689+
self + Duration::try_from(duration)
690+
.expect("overflow converting `core::time::Duration` to `time::Duration`")
690691
}
691692
}
692693

@@ -770,7 +771,8 @@ impl Sub<StdDuration> for Time {
770771
/// ```
771772
#[inline(always)]
772773
fn sub(self, duration: StdDuration) -> Self::Output {
773-
self - Duration::seconds((duration.as_secs() % 86_400) as i64)
774+
self - Duration::try_from(duration)
775+
.expect("overflow converting `core::time::Duration` to `time::Duration`")
774776
}
775777
}
776778

@@ -1234,6 +1236,10 @@ mod test {
12341236

12351237
#[test]
12361238
fn add_std_duration() -> crate::Result<()> {
1239+
assert_eq!(
1240+
time!(0:00) + 1.std_milliseconds(),
1241+
time!(0:00:00:001_000_000)
1242+
);
12371243
assert_eq!(time!(0:00) + 1.std_seconds(), time!(0:00:01));
12381244
assert_eq!(time!(0:00) + 1.std_minutes(), time!(0:01));
12391245
assert_eq!(time!(0:00) + 1.std_hours(), time!(1:00));
@@ -1264,6 +1270,10 @@ mod test {
12641270
assert_eq!(time!(12:00) - 1.std_hours(), time!(11:00));
12651271

12661272
// Underflow
1273+
assert_eq!(
1274+
time!(0:00) - 1.std_milliseconds(),
1275+
time!(23:59:59:999_000_000)
1276+
);
12671277
assert_eq!(time!(0:00) - 1.std_seconds(), time!(23:59:59));
12681278
assert_eq!(time!(0:00) - 1.std_minutes(), time!(23:59));
12691279
assert_eq!(time!(0:00) - 1.std_hours(), time!(23:00));

0 commit comments

Comments
 (0)