Skip to content

Commit db3505e

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 2ae97e0 commit db3505e

File tree

1 file changed

+12
-0
lines changed
  • lightning-liquidity/src/lsps0

1 file changed

+12
-0
lines changed

lightning-liquidity/src/lsps0/ser.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ 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

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

@@ -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 {

0 commit comments

Comments
 (0)