diff --git a/prdoc/pr_5316.prdoc b/prdoc/pr_5316.prdoc new file mode 100644 index 000000000000..75b431c941d5 --- /dev/null +++ b/prdoc/pr_5316.prdoc @@ -0,0 +1,11 @@ +title: add timestamp function to sp-consensus-slots + +doc: + - audience: Node Dev + description: | + Added timestamp function to sp-consensus-slots to get the first timestamp + of the given slot. + +crates: + - name: sp-consensus-slots + bump: minor diff --git a/substrate/primitives/consensus/slots/src/lib.rs b/substrate/primitives/consensus/slots/src/lib.rs index eb3b3d3a449f..dfa46fcf2571 100644 --- a/substrate/primitives/consensus/slots/src/lib.rs +++ b/substrate/primitives/consensus/slots/src/lib.rs @@ -91,6 +91,13 @@ impl Slot { Slot(timestamp.as_millis() / slot_duration.as_millis()) } + /// Timestamp of the start of the slot. + /// + /// Returns `None` if would overflow for given `SlotDuration`. + pub fn timestamp(&self, slot_duration: SlotDuration) -> Option { + slot_duration.as_millis().checked_mul(self.0).map(Timestamp::new) + } + /// Saturating addition. pub fn saturating_add>(self, rhs: T) -> Self { Self(self.0.saturating_add(rhs.into()))