Skip to content

Commit 015ace8

Browse files
fixup: Extend LSPSDateTime to avoid using std::SystemTime
1 parent c9d39e8 commit 015ace8

File tree

1 file changed

+27
-10
lines changed
  • lightning-liquidity/src/lsps0

1 file changed

+27
-10
lines changed

lightning-liquidity/src/lsps0/ser.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ use bitcoin::secp256k1::PublicKey;
2727
use core::fmt::{self, Display};
2828
use core::str::FromStr;
2929

30-
#[cfg(feature = "std")]
31-
use std::time::{SystemTime, UNIX_EPOCH};
32-
3330
use serde::de::{self, MapAccess, Visitor};
3431
use serde::ser::SerializeStruct;
3532
use serde::{Deserialize, Deserializer, Serialize};
@@ -201,15 +198,35 @@ impl LSPSDateTime {
201198
}
202199

203200
/// Returns if the given time is in the past.
204-
#[cfg(feature = "std")]
205201
pub fn is_past(&self) -> bool {
206-
let now_seconds_since_epoch = SystemTime::now()
207-
.duration_since(UNIX_EPOCH)
208-
.expect("system clock to be ahead of the unix epoch")
209-
.as_secs();
210202
let datetime_seconds_since_epoch =
211-
self.0.timestamp().try_into().expect("expiration to be ahead of unix epoch");
212-
now_seconds_since_epoch > datetime_seconds_since_epoch
203+
self.timestamp().try_into().expect("expiration to be ahead of unix epoch");
204+
LSPSDateTime::now().timestamp() > datetime_seconds_since_epoch
205+
}
206+
207+
/// Returns the timestamp as seconds since the Unix epoch.
208+
pub fn timestamp(&self) -> i64 {
209+
self.0.timestamp()
210+
}
211+
212+
/// Returns the current time as an LSPSDateTime.
213+
pub fn now() -> Self {
214+
Self(chrono::Utc::now())
215+
}
216+
217+
/// Returns the duration between the current time and the given time.
218+
pub fn duration_since(&self, other: &Self) -> chrono::Duration {
219+
self.0.signed_duration_since(other.0)
220+
}
221+
222+
/// Returns the substracted duration from the given time.
223+
pub fn checked_sub_signed(&self, duration: chrono::Duration) -> Option<Self> {
224+
self.0.checked_sub_signed(duration).map(Self)
225+
}
226+
227+
/// Returns the added duration to the given time.
228+
pub fn checked_add_signed(&self, duration: chrono::Duration) -> Option<Self> {
229+
self.0.checked_add_signed(duration).map(Self)
213230
}
214231
}
215232

0 commit comments

Comments
 (0)