Skip to content

Commit cca6fa0

Browse files
Create a new trait TimeProvider
- Provides a single method duration_since_epoch() -> Duration - Also a DefaultTimeProvider implementation is provided that uses std::time
1 parent 8c51013 commit cca6fa0

File tree

1 file changed

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

1 file changed

+22
-0
lines changed

lightning-liquidity/src/lsps0/ser.rs

+22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use alloc::string::String;
88

99
use core::fmt::{self, Display};
1010
use core::str::FromStr;
11+
use core::time::Duration;
1112

1213
use crate::lsps0::msgs::{
1314
LSPS0ListProtocolsRequest, LSPS0Message, LSPS0Request, LSPS0Response,
@@ -784,3 +785,24 @@ pub(crate) mod u32_fee_rate {
784785
Ok(FeeRate::from_sat_per_kwu(fee_rate_sat_kwu as u64))
785786
}
786787
}
788+
789+
/// Trait defining a time provider
790+
///
791+
/// This trait is used to provide the current time service operations and to convert between timestamps and durations.
792+
pub trait TimeProvider {
793+
/// Get the current time as a duration since the Unix epoch.
794+
fn duration_since_epoch(&self) -> Duration;
795+
}
796+
797+
/// Default time provider using the system clock.
798+
#[derive(Clone, Debug)]
799+
#[cfg(feature = "time")]
800+
pub struct DefaultTimeProvider;
801+
802+
#[cfg(feature = "time")]
803+
impl TimeProvider for DefaultTimeProvider {
804+
fn duration_since_epoch(&self) -> Duration {
805+
use std::time::{SystemTime, UNIX_EPOCH};
806+
SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch")
807+
}
808+
}

0 commit comments

Comments
 (0)