Skip to content

Commit

Permalink
Improve docs of Timestamp::minus_* and use panicking_sub
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Apr 10, 2024
1 parent 547efed commit 242cc1f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ and this project adheres to
custom message type ([#2099])
- cosmwasm-std: Add `Uint{64,128,256,512}::panicking_sub` which is like the
`Sub` implementation but `const`.
- cosmwasm-std: Let `Timestamp::minus_nanos` use `Uint64::panicking_sub` and
document panicking behaviour.

[#1983]: https://github.com/CosmWasm/cosmwasm/pull/1983
[#2057]: https://github.com/CosmWasm/cosmwasm/pull/2057
Expand Down
23 changes: 21 additions & 2 deletions packages/std/src/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,52 @@ impl Timestamp {
Timestamp(nanos)
}

/// Subtracts the given amount of days from the timestamp and
/// returns the result. The original value remains unchanged.
///
/// Panics if the result is not >= 0. I.e. times before epoch cannot be represented.
#[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)
}

/// Subtracts the given amount of hours from the timestamp and
/// returns the result. The original value remains unchanged.
///
/// Panics if the result is not >= 0. I.e. times before epoch cannot be represented.
#[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)
}

/// Subtracts the given amount of minutes from the timestamp and
/// returns the result. The original value remains unchanged.
///
/// Panics if the result is not >= 0. I.e. times before epoch cannot be represented.
#[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)
}

/// Subtracts the given amount of seconds from the timestamp and
/// returns the result. The original value remains unchanged.
///
/// Panics if the result is not >= 0. I.e. times before epoch cannot be represented.
#[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)
}

/// Subtracts the given amount of nanoseconds from the timestamp and
/// returns the result. The original value remains unchanged.
///
/// Panics if the result is not >= 0. I.e. times before epoch cannot be represented.
#[must_use = "this returns the result of the operation, without modifying the original"]
pub const fn minus_nanos(&self, subtrahend: u64) -> Timestamp {
let nanos = Uint64::new(self.0.u64() - subtrahend);
Timestamp(nanos)
Timestamp(self.0.panicking_sub(Uint64::new(subtrahend)))
}

/// Returns nanoseconds since epoch
Expand Down

0 comments on commit 242cc1f

Please sign in to comment.