diff --git a/.changelog/unreleased/improvements/1030-new-time-api.md b/.changelog/unreleased/improvements/1030-new-time-api.md index a9c269f0d..bb8edd459 100644 --- a/.changelog/unreleased/improvements/1030-new-time-api.md +++ b/.changelog/unreleased/improvements/1030-new-time-api.md @@ -1,11 +1,11 @@ - Remove dependencies on the `chrono` crate. ([#1030](https://github.com/informalsystems/tendermint-rs/issues/1030)) - `[tendermint]` Improve `tendermint::Time` - ([#1030](https://github.com/informalsystems/tendermint-rs/issues/1030)): + ([#1036](https://github.com/informalsystems/tendermint-rs/issues/1036), + revised by [#1048](https://github.com/informalsystems/tendermint-rs/pull/1048)): * Restrict the validity range of `Time` to dates with years in the range 1-9999, to match the specification of protobuf message `Timestamp`. Add an `ErrorDetail` variant `DateOutOfRange` to report when this restriction is not met. * Added a conversion to, and a fallible conversion from, `OffsetDateTime` of the `time` crate. - * Added `Time` methods `checked_add` and `checked_sub`. diff --git a/light-client/src/predicates.rs b/light-client/src/predicates.rs index 8b984c4dc..2002e3ffb 100644 --- a/light-client/src/predicates.rs +++ b/light-client/src/predicates.rs @@ -329,7 +329,7 @@ mod tests { assert!(result_ok.is_ok()); // 2. ensure it fails if header is from a future time - let now = now.checked_sub(one_second * 15).unwrap(); + let now = (now - one_second * 15).unwrap(); let result_err = vp.is_header_from_past(header.time, one_second, now); match result_err { diff --git a/pbt-gen/Cargo.toml b/pbt-gen/Cargo.toml index c22589d1e..7e901794c 100644 --- a/pbt-gen/Cargo.toml +++ b/pbt-gen/Cargo.toml @@ -19,8 +19,8 @@ description = """ default = ["time"] [dependencies] -time = { version = "0.3.5", default-features = false, optional = true } +time = { version = "0.3", default-features = false, optional = true } proptest = { version = "0.10.1", default-features = false, features = ["std"] } [dev-dependencies] -time = { version = "0.3.5", features = ["macros"] } +time = { version = "0.3", features = ["macros"] } diff --git a/tendermint/src/time.rs b/tendermint/src/time.rs index ac53be2c8..8578beae5 100644 --- a/tendermint/src/time.rs +++ b/tendermint/src/time.rs @@ -112,20 +112,6 @@ impl Time { pub fn to_rfc3339(&self) -> String { timestamp::to_rfc3339_nanos(self.0.assume_utc()) } - - /// Computes `self + duration`, returning `None` if an overflow occurred. - pub fn checked_add(self, duration: Duration) -> Option { - let duration = duration.try_into().ok()?; - let t = self.0.checked_add(duration)?; - Self::from_utc(t.assume_utc()).ok() - } - - /// Computes `self - duration`, returning `None` if an overflow occurred. - pub fn checked_sub(self, duration: Duration) -> Option { - let duration = duration.try_into().ok()?; - let t = self.0.checked_sub(duration)?; - Self::from_utc(t.assume_utc()).ok() - } } impl fmt::Display for Time { @@ -306,6 +292,7 @@ mod tests { } } + #[allow(dead_code)] fn duration_from_nanos(whole_nanos: u128) -> Duration { let secs: u64 = (whole_nanos / 1_000_000_000).try_into().unwrap(); let nanos = (whole_nanos % 1_000_000_000) as u32; @@ -368,33 +355,5 @@ mod tests { } } - proptest! { - #[test] - fn checked_add_regular((dt, d) in args_for_regular_add()) { - let t: Time = dt.try_into().unwrap(); - let t = t.checked_add(d).unwrap(); - let res: OffsetDateTime = t.into(); - assert_eq!(res, dt + d); - } - - #[test] - fn checked_sub_regular((dt, d) in args_for_regular_sub()) { - let t: Time = dt.try_into().unwrap(); - let t = t.checked_sub(d).unwrap(); - let res: OffsetDateTime = t.into(); - assert_eq!(res, dt - d); - } - - #[test] - fn checked_add_overflow((dt, d) in args_for_overflowed_add()) { - let t: Time = dt.try_into().unwrap(); - assert_eq!(t.checked_add(d), None); - } - - #[test] - fn checked_sub_overflow((dt, d) in args_for_overflowed_sub()) { - let t: Time = dt.try_into().unwrap(); - assert_eq!(t.checked_sub(d), None); - } - } + // TODO: add tests for additive ops using the strategies above }