From 93c9d08902c4855e59435846d529db235ba1ae31 Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Fri, 16 Jun 2023 14:28:23 +0200 Subject: [PATCH 1/2] Add Timestamp::{plus,minus}_{minutes, hours, days} --- packages/std/src/timestamp.rs | 86 +++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/packages/std/src/timestamp.rs b/packages/std/src/timestamp.rs index a8e4ea8585..61c363e5d7 100644 --- a/packages/std/src/timestamp.rs +++ b/packages/std/src/timestamp.rs @@ -38,6 +38,24 @@ impl Timestamp { Timestamp(Uint64::new(seconds_since_epoch * 1_000_000_000)) } + #[must_use = "this returns the result of the operation, without modifying the original"] + #[inline] + pub const fn plus_days(&self, addition: u64) -> Timestamp { + self.plus_hours(addition * 24) + } + + #[must_use = "this returns the result of the operation, without modifying the original"] + #[inline] + pub const fn plus_hours(&self, addition: u64) -> Timestamp { + self.plus_minutes(addition * 60) + } + + #[must_use = "this returns the result of the operation, without modifying the original"] + #[inline] + pub const fn plus_minutes(&self, addition: u64) -> Timestamp { + self.plus_seconds(addition * 60) + } + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn plus_seconds(&self, addition: u64) -> Timestamp { self.plus_nanos(addition * 1_000_000_000) @@ -49,6 +67,24 @@ impl Timestamp { Timestamp(nanos) } + #[must_use = "this returns the result of the operation, without modifying the original"] + #[inline] + pub const fn minus_days(&self, subtrahend: u64) -> Timestamp { + self.minus_hours(subtrahend * 24) + } + + #[must_use = "this returns the result of the operation, without modifying the original"] + #[inline] + pub const fn minus_hours(&self, subtrahend: u64) -> Timestamp { + self.minus_minutes(subtrahend * 60) + } + + #[must_use = "this returns the result of the operation, without modifying the original"] + #[inline] + pub const fn minus_minutes(&self, subtrahend: u64) -> Timestamp { + self.minus_seconds(subtrahend * 60) + } + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn minus_seconds(&self, subtrahend: u64) -> Timestamp { self.minus_nanos(subtrahend * 1_000_000_000) @@ -156,6 +192,56 @@ mod tests { let _earlier = Timestamp::from_nanos(100).minus_nanos(101); } + #[test] + fn timestamp_plus_days() { + let ts = Timestamp::from_seconds(123).plus_days(0); + assert_eq!(ts.0.u64(), 123_000_000_000); + let ts = Timestamp::from_seconds(123).plus_days(10); + assert_eq!(ts.0.u64(), 864_123_000_000_000); + } + + #[test] + fn timestamp_minus_days() { + let ts = Timestamp::from_seconds(123).minus_days(0); + assert_eq!(ts.0.u64(), 123_000_000_000); + let ts = Timestamp::from_seconds(2 * 86400 + 123).minus_days(1); + assert_eq!(ts.0.u64(), 86_523_000_000_000); + let ts = Timestamp::from_seconds(86400).minus_days(1); + assert_eq!(ts.0.u64(), 0); + } + + #[test] + fn timestamp_plus_hours() { + let ts = Timestamp::from_seconds(123).plus_hours(0); + assert_eq!(ts.0.u64(), 123_000_000_000); + let ts = Timestamp::from_seconds(123).plus_hours(2); + assert_eq!(ts.0.u64(), 123_000_000_000 + 60 * 60 * 2 * 1_000_000_000); + } + + #[test] + fn timestamp_minus_hours() { + let ts = Timestamp::from_seconds(2 * 60 * 60).minus_hours(0); + assert_eq!(ts.0.u64(), 2 * 60 * 60 * 1_000_000_000); + let ts = Timestamp::from_seconds(2 * 60 * 60 + 123).minus_hours(1); + assert_eq!(ts.0.u64(), 60 * 60 * 1_000_000_000 + 123_000_000_000); + } + + #[test] + fn timestamp_plus_minutes() { + let ts = Timestamp::from_seconds(123).plus_minutes(0); + assert_eq!(ts.0.u64(), 123_000_000_000); + let ts = Timestamp::from_seconds(123).plus_minutes(2); + assert_eq!(ts.0.u64(), 123_000_000_000 + 60 * 2 * 1_000_000_000); + } + + #[test] + fn timestamp_minus_minutes() { + let ts = Timestamp::from_seconds(5 * 60).minus_minutes(0); + assert_eq!(ts.0.u64(), 5 * 60 * 1_000_000_000); + let ts = Timestamp::from_seconds(5 * 60 + 123).minus_minutes(1); + assert_eq!(ts.0.u64(), 4 * 60 * 1_000_000_000 + 123_000_000_000); + } + #[test] fn timestamp_nanos() { let sum = Timestamp::from_nanos(123); From 075194533405e7787740c95f92f29d8a3c763bb8 Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Fri, 16 Jun 2023 14:36:51 +0200 Subject: [PATCH 2/2] Add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81552226bf..cc3ab23b83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,10 +10,12 @@ and this project adheres to - cosmwasm-std: Add `<<` and `<<=` implementation for `Uint{64,128,256,512}` types. ([#1723]) +- cosmwasm-std: Add `Timestamp::{plus,minus}_{minutes, hours, days}`. ([#1729]) - cosmwasm-std: Add `Decimal::bps` and `Decimal256::bps` to create a decimal from a basis point value ([#1715]). [#1723]: https://github.com/CosmWasm/cosmwasm/pull/1723 +[#1729]: https://github.com/CosmWasm/cosmwasm/pull/1729 [#1715]: https://github.com/CosmWasm/cosmwasm/pull/1715 ### Changed