Skip to content

Commit afe320b

Browse files
fixup: Improvements on LSPSDateTime
- Implement From<Duration> for LSPSDateTime - Avoid doing ambiguous timestamp types - Add abs_diff function to use on client / service
1 parent 6e43a5f commit afe320b

File tree

1 file changed

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

1 file changed

+13
-0
lines changed

lightning-liquidity/src/lsps0/ser.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::lsps2::msgs::{
1818
};
1919
use crate::prelude::{HashMap, String};
2020

21+
use chrono::DateTime;
2122
use lightning::ln::msgs::{DecodeError, LightningError};
2223
use lightning::ln::wire;
2324
use lightning::util::ser::{LengthLimitedRead, LengthReadable, WithoutLength};
@@ -27,6 +28,7 @@ use bitcoin::secp256k1::PublicKey;
2728
use core::fmt::{self, Display};
2829
use core::str::FromStr;
2930

31+
use core::time::Duration;
3032
#[cfg(feature = "std")]
3133
use std::time::{SystemTime, UNIX_EPOCH};
3234

@@ -212,6 +214,17 @@ impl LSPSDateTime {
212214
self.0.timestamp().try_into().expect("expiration to be ahead of unix epoch");
213215
now_seconds_since_epoch > datetime_seconds_since_epoch
214216
}
217+
218+
/// Returns the time in seconds since the unix epoch.
219+
pub fn abs_diff(&self, other: &Self) -> u64 {
220+
self.0.timestamp().abs_diff(other.0.timestamp())
221+
}
222+
}
223+
224+
impl From<Duration> for LSPSDateTime {
225+
fn from(duration: Duration) -> Self {
226+
Self(DateTime::UNIX_EPOCH + duration)
227+
}
215228
}
216229

217230
impl FromStr for LSPSDateTime {

0 commit comments

Comments
 (0)