@@ -540,7 +540,8 @@ impl<D: tb::Bool, H: tb::Bool, T: tb::Bool, C: tb::Bool, S: tb::Bool> InvoiceBui
540
540
self
541
541
}
542
542
543
- /// Sets the expiry time
543
+ /// Sets the expiry time, dropping the subsecond part (which is not representable in BOLT 11
544
+ /// invoices).
544
545
pub fn expiry_time ( mut self , expiry_time : Duration ) -> Self {
545
546
self . tagged_fields . push ( TaggedField :: ExpiryTime ( ExpiryTime :: from_duration ( expiry_time) ) ) ;
546
547
self
@@ -632,7 +633,8 @@ impl<D: tb::Bool, H: tb::Bool, C: tb::Bool, S: tb::Bool> InvoiceBuilder<D, H, tb
632
633
self . set_flags ( )
633
634
}
634
635
635
- /// Sets the timestamp to a duration since the Unix epoch.
636
+ /// Sets the timestamp to a duration since the Unix epoch, dropping the subsecond part (which
637
+ /// is not representable in BOLT 11 invoices).
636
638
pub fn duration_since_epoch ( mut self , time : Duration ) -> InvoiceBuilder < D , H , tb:: True , C , S > {
637
639
match PositiveTimestamp :: from_duration_since_epoch ( time) {
638
640
Ok ( t) => self . timestamp = Some ( t) ,
@@ -960,12 +962,18 @@ impl PositiveTimestamp {
960
962
///
961
963
/// Otherwise, returns a [`CreationError::TimestampOutOfBounds`].
962
964
pub fn from_unix_timestamp ( unix_seconds : u64 ) -> Result < Self , CreationError > {
963
- Self :: from_duration_since_epoch ( Duration :: from_secs ( unix_seconds) )
965
+ if unix_seconds <= MAX_TIMESTAMP {
966
+ Ok ( Self ( Duration :: from_secs ( unix_seconds) ) )
967
+ } else {
968
+ Err ( CreationError :: TimestampOutOfBounds )
969
+ }
964
970
}
965
971
966
972
/// Creates a `PositiveTimestamp` from a [`SystemTime`] with a corresponding Unix timestamp in
967
973
/// the range `0..=MAX_TIMESTAMP`.
968
974
///
975
+ /// Note that the subsecond part is dropped as it is not representable in BOLT 11 invoices.
976
+ ///
969
977
/// Otherwise, returns a [`CreationError::TimestampOutOfBounds`].
970
978
#[ cfg( feature = "std" ) ]
971
979
pub fn from_system_time ( time : SystemTime ) -> Result < Self , CreationError > {
@@ -977,13 +985,11 @@ impl PositiveTimestamp {
977
985
/// Creates a `PositiveTimestamp` from a [`Duration`] since the Unix epoch in the range
978
986
/// `0..=MAX_TIMESTAMP`.
979
987
///
988
+ /// Note that the subsecond part is dropped as it is not representable in BOLT 11 invoices.
989
+ ///
980
990
/// Otherwise, returns a [`CreationError::TimestampOutOfBounds`].
981
991
pub fn from_duration_since_epoch ( duration : Duration ) -> Result < Self , CreationError > {
982
- if duration. as_secs ( ) <= MAX_TIMESTAMP {
983
- Ok ( PositiveTimestamp ( duration) )
984
- } else {
985
- Err ( CreationError :: TimestampOutOfBounds )
986
- }
992
+ Self :: from_unix_timestamp ( duration. as_secs ( ) )
987
993
}
988
994
989
995
/// Returns the Unix timestamp representing the stored time
@@ -1356,9 +1362,9 @@ impl ExpiryTime {
1356
1362
ExpiryTime ( Duration :: from_secs ( seconds) )
1357
1363
}
1358
1364
1359
- /// Construct an `ExpiryTime` from a `Duration`.
1365
+ /// Construct an `ExpiryTime` from a `Duration`, dropping the sub-second part .
1360
1366
pub fn from_duration ( duration : Duration ) -> ExpiryTime {
1361
- ExpiryTime ( duration)
1367
+ Self :: from_seconds ( duration. as_secs ( ) )
1362
1368
}
1363
1369
1364
1370
/// Returns the expiry time in seconds
0 commit comments