Skip to content

Commit 242cc1f

Browse files
committed
Improve docs of Timestamp::minus_* and use panicking_sub
1 parent 547efed commit 242cc1f

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ and this project adheres to
2525
custom message type ([#2099])
2626
- cosmwasm-std: Add `Uint{64,128,256,512}::panicking_sub` which is like the
2727
`Sub` implementation but `const`.
28+
- cosmwasm-std: Let `Timestamp::minus_nanos` use `Uint64::panicking_sub` and
29+
document panicking behaviour.
2830

2931
[#1983]: https://github.com/CosmWasm/cosmwasm/pull/1983
3032
[#2057]: https://github.com/CosmWasm/cosmwasm/pull/2057

packages/std/src/timestamp.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,52 @@ impl Timestamp {
6666
Timestamp(nanos)
6767
}
6868

69+
/// Subtracts the given amount of days from the timestamp and
70+
/// returns the result. The original value remains unchanged.
71+
///
72+
/// Panics if the result is not >= 0. I.e. times before epoch cannot be represented.
6973
#[must_use = "this returns the result of the operation, without modifying the original"]
7074
#[inline]
7175
pub const fn minus_days(&self, subtrahend: u64) -> Timestamp {
7276
self.minus_hours(subtrahend * 24)
7377
}
7478

79+
/// Subtracts the given amount of hours from the timestamp and
80+
/// returns the result. The original value remains unchanged.
81+
///
82+
/// Panics if the result is not >= 0. I.e. times before epoch cannot be represented.
7583
#[must_use = "this returns the result of the operation, without modifying the original"]
7684
#[inline]
7785
pub const fn minus_hours(&self, subtrahend: u64) -> Timestamp {
7886
self.minus_minutes(subtrahend * 60)
7987
}
8088

89+
/// Subtracts the given amount of minutes from the timestamp and
90+
/// returns the result. The original value remains unchanged.
91+
///
92+
/// Panics if the result is not >= 0. I.e. times before epoch cannot be represented.
8193
#[must_use = "this returns the result of the operation, without modifying the original"]
8294
#[inline]
8395
pub const fn minus_minutes(&self, subtrahend: u64) -> Timestamp {
8496
self.minus_seconds(subtrahend * 60)
8597
}
8698

99+
/// Subtracts the given amount of seconds from the timestamp and
100+
/// returns the result. The original value remains unchanged.
101+
///
102+
/// Panics if the result is not >= 0. I.e. times before epoch cannot be represented.
87103
#[must_use = "this returns the result of the operation, without modifying the original"]
88104
pub const fn minus_seconds(&self, subtrahend: u64) -> Timestamp {
89105
self.minus_nanos(subtrahend * 1_000_000_000)
90106
}
91107

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

98117
/// Returns nanoseconds since epoch

0 commit comments

Comments
 (0)