@@ -27,9 +27,6 @@ use bitcoin::secp256k1::PublicKey;
2727use core:: fmt:: { self , Display } ;
2828use core:: str:: FromStr ;
2929
30- #[ cfg( feature = "std" ) ]
31- use std:: time:: { SystemTime , UNIX_EPOCH } ;
32-
3330use serde:: de:: { self , MapAccess , Visitor } ;
3431use serde:: ser:: SerializeStruct ;
3532use 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