@@ -1225,6 +1225,12 @@ impl Invoice {
1225
1225
self . signed_invoice . recover_payee_pub_key ( ) . expect ( "was checked by constructor" ) . 0
1226
1226
}
1227
1227
1228
+ /// Returns the Duration since the Unix epoch at which the invoice expires.
1229
+ /// Returning None if overflow occurred.
1230
+ pub fn expires_at ( & self ) -> Option < Duration > {
1231
+ self . duration_since_epoch ( ) . checked_add ( self . expiry_time ( ) )
1232
+ }
1233
+
1228
1234
/// Returns the invoice's expiry time, if present, otherwise [`DEFAULT_EXPIRY_TIME`].
1229
1235
pub fn expiry_time ( & self ) -> Duration {
1230
1236
self . signed_invoice . expiry_time ( )
@@ -1247,6 +1253,20 @@ impl Invoice {
1247
1253
}
1248
1254
}
1249
1255
1256
+ /// Returns the Duration remaining until the invoice expires.
1257
+ #[ cfg( feature = "std" ) ]
1258
+ pub fn duration_until_expiry ( & self ) -> Duration {
1259
+ SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH )
1260
+ . map ( |now| self . expiration_remaining_from_epoch ( now) )
1261
+ . unwrap_or ( Duration :: from_nanos ( 0 ) )
1262
+ }
1263
+
1264
+ /// Returns the Duration remaining until the invoice expires given the current time.
1265
+ /// `time` is the timestamp as a duration since the Unix epoch.
1266
+ pub fn expiration_remaining_from_epoch ( & self , time : Duration ) -> Duration {
1267
+ self . expires_at ( ) . map ( |x| x. checked_sub ( time) ) . flatten ( ) . unwrap_or ( Duration :: from_nanos ( 0 ) )
1268
+ }
1269
+
1250
1270
/// Returns whether the expiry time would pass at the given point in time.
1251
1271
/// `at_time` is the timestamp as a duration since the Unix epoch.
1252
1272
pub fn would_expire ( & self , at_time : Duration ) -> bool {
0 commit comments