lightning_liquidity/utils/
time.rs

1//! Utilities for time handling in LSPS5 service.
2
3use core::time::Duration;
4
5/// Trait defining a time provider for LSPS5 service.
6///
7/// This trait is used to provide the current time for LSPS5 service operations
8/// and to convert between timestamps and durations.
9pub trait TimeProvider {
10	/// Get the current time as a duration since the Unix epoch.
11	fn duration_since_epoch(&self) -> Duration;
12}
13
14/// Default time provider using the system clock.
15///
16/// You likely don't need to use this directly, it is used automatically with
17/// [`LiquidityManager::new`]
18///
19/// [`LiquidityManager::new`]: crate::manager::LiquidityManager::new
20#[derive(Clone, Debug)]
21#[cfg(feature = "time")]
22pub struct DefaultTimeProvider;
23
24#[cfg(feature = "time")]
25impl TimeProvider for DefaultTimeProvider {
26	fn duration_since_epoch(&self) -> Duration {
27		use std::time::{SystemTime, UNIX_EPOCH};
28		SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch")
29	}
30}
31#[cfg(feature = "time")]
32impl core::ops::Deref for DefaultTimeProvider {
33	type Target = Self;
34	fn deref(&self) -> &Self {
35		self
36	}
37}