Skip to content

Commit

Permalink
Account for subsecond values when adding to Time
Browse files Browse the repository at this point in the history
Fixes #252
  • Loading branch information
jhpratt committed May 2, 2020
1 parent 20e9414 commit 66e1ba2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ Versioning].

---

## 0.2.14 [2020-05-02]

### Fixed

Adding/subtracting a `core::time::Duration` now correctly takes subsecond
values into account. This also affects `PrimitiveDateTime` and `OffsetDateTime`.

## 0.2.13 [2020-05-01]

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "time"
version = "0.2.13"
version = "0.2.14"
authors = ["Jacob Pratt <the.z.cuber@gmail.com>"]
edition = "2018"
repository = "https://github.com/time-rs/time"
Expand Down
14 changes: 12 additions & 2 deletions src/time_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,8 @@ impl Add<StdDuration> for Time {
/// ```
#[inline(always)]
fn add(self, duration: StdDuration) -> Self::Output {
self + Duration::seconds((duration.as_secs() % 86_400) as i64)
self + Duration::try_from(duration)
.expect("overflow converting `core::time::Duration` to `time::Duration`")
}
}

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

Expand Down Expand Up @@ -1234,6 +1236,10 @@ mod test {

#[test]
fn add_std_duration() -> crate::Result<()> {
assert_eq!(
time!(0:00) + 1.std_milliseconds(),
time!(0:00:00:001_000_000)
);
assert_eq!(time!(0:00) + 1.std_seconds(), time!(0:00:01));
assert_eq!(time!(0:00) + 1.std_minutes(), time!(0:01));
assert_eq!(time!(0:00) + 1.std_hours(), time!(1:00));
Expand Down Expand Up @@ -1264,6 +1270,10 @@ mod test {
assert_eq!(time!(12:00) - 1.std_hours(), time!(11:00));

// Underflow
assert_eq!(
time!(0:00) - 1.std_milliseconds(),
time!(23:59:59:999_000_000)
);
assert_eq!(time!(0:00) - 1.std_seconds(), time!(23:59:59));
assert_eq!(time!(0:00) - 1.std_minutes(), time!(23:59));
assert_eq!(time!(0:00) - 1.std_hours(), time!(23:00));
Expand Down

0 comments on commit 66e1ba2

Please sign in to comment.