Skip to content

Commit b105bc5

Browse files
Prefactor - Improvements on LSPSDateTime
Introduce new_from_duration_since_epoch constructor from Duration. Also add abs_diff function to use on client / service. Also do feature='time' instead of feature='std' on time related functionality
1 parent f7c9d04 commit b105bc5

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

lightning-liquidity/src/lsps0/ser.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ use lightning::util::ser::{LengthLimitedRead, LengthReadable, WithoutLength};
2929

3030
use bitcoin::secp256k1::PublicKey;
3131

32-
#[cfg(feature = "std")]
32+
use core::time::Duration;
33+
#[cfg(feature = "time")]
3334
use std::time::{SystemTime, UNIX_EPOCH};
3435

3536
use serde::de::{self, MapAccess, Visitor};
@@ -204,7 +205,7 @@ impl LSPSDateTime {
204205
}
205206

206207
/// Returns if the given time is in the past.
207-
#[cfg(feature = "std")]
208+
#[cfg(feature = "time")]
208209
pub fn is_past(&self) -> bool {
209210
let now_seconds_since_epoch = SystemTime::now()
210211
.duration_since(UNIX_EPOCH)
@@ -214,6 +215,16 @@ impl LSPSDateTime {
214215
self.0.timestamp().try_into().expect("expiration to be ahead of unix epoch");
215216
now_seconds_since_epoch > datetime_seconds_since_epoch
216217
}
218+
219+
/// Returns the time in seconds since the unix epoch.
220+
pub fn abs_diff(&self, other: &Self) -> u64 {
221+
self.0.timestamp().abs_diff(other.0.timestamp())
222+
}
223+
224+
/// Returns the time in seconds since the unix epoch.
225+
pub fn new_from_duration_since_epoch(duration: Duration) -> Self {
226+
Self(chrono::DateTime::UNIX_EPOCH + duration)
227+
}
217228
}
218229

219230
impl FromStr for LSPSDateTime {

lightning-liquidity/src/lsps2/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ pub fn is_valid_opening_fee_params(
2828
}
2929

3030
/// Determines if the given parameters are expired, or still valid.
31-
#[cfg_attr(not(feature = "std"), allow(unused_variables))]
31+
#[cfg_attr(not(feature = "time"), allow(unused_variables))]
3232
pub fn is_expired_opening_fee_params(fee_params: &LSPS2OpeningFeeParams) -> bool {
33-
#[cfg(feature = "std")]
33+
#[cfg(feature = "time")]
3434
{
3535
fee_params.valid_until.is_past()
3636
}
37-
#[cfg(not(feature = "std"))]
37+
#[cfg(not(feature = "time"))]
3838
{
3939
// TODO: We need to find a way to check expiry times in no-std builds.
4040
false

0 commit comments

Comments
 (0)