Skip to content

Commit 55410ef

Browse files
Prefactor - Improvements on LSPSDateTime
- do new_from_duration_since_epoch (instead of From<Duration>) - Avoid doing ambiguous timestamp types - Add abs_diff function to use on client / service
1 parent a84286a commit 55410ef

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

lightning-liquidity/src/lsps0/ser.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ use crate::lsps2::msgs::{
2323
};
2424
use crate::prelude::HashMap;
2525

26+
use chrono::DateTime;
2627
use lightning::ln::msgs::{DecodeError, LightningError};
2728
use lightning::ln::wire;
2829
use lightning::util::ser::{LengthLimitedRead, LengthReadable, WithoutLength};
2930

3031
use bitcoin::secp256k1::PublicKey;
3132

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

3537
use serde::de::{self, MapAccess, Visitor};
@@ -204,7 +206,7 @@ impl LSPSDateTime {
204206
}
205207

206208
/// Returns if the given time is in the past.
207-
#[cfg(feature = "std")]
209+
#[cfg(feature = "time")]
208210
pub fn is_past(&self) -> bool {
209211
let now_seconds_since_epoch = SystemTime::now()
210212
.duration_since(UNIX_EPOCH)
@@ -214,6 +216,16 @@ impl LSPSDateTime {
214216
self.0.timestamp().try_into().expect("expiration to be ahead of unix epoch");
215217
now_seconds_since_epoch > datetime_seconds_since_epoch
216218
}
219+
220+
/// Returns the time in seconds since the unix epoch.
221+
pub fn abs_diff(&self, other: &Self) -> u64 {
222+
self.0.timestamp().abs_diff(other.0.timestamp())
223+
}
224+
225+
/// Returns the time in seconds since the unix epoch.
226+
pub fn new_from_duration_since_epoch(duration: Duration) -> Self {
227+
Self(DateTime::UNIX_EPOCH + duration)
228+
}
217229
}
218230

219231
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)