Skip to content

Commit

Permalink
Merge pull request #1729 from CosmWasm/1717-timestamp-helper-functions
Browse files Browse the repository at this point in the history
Add `Timestamp::{plus,minus}_{minutes, hours, days}`
  • Loading branch information
chipshort authored Jun 16, 2023
2 parents c1336a7 + 0751945 commit 87359df
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
86 changes: 86 additions & 0 deletions packages/std/src/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 87359df

Please sign in to comment.