diff --git a/Cargo.toml b/Cargo.toml index 7bdbf3d..a2ef6ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "init4-bin-base" description = "Internal utilities for binaries produced by the init4 team" keywords = ["init4", "bin", "base"] -version = "0.18.0-rc.0" +version = "0.18.1-rc.0" edition = "2021" rust-version = "1.85" authors = ["init4", "James Prestwich", "evalir"] diff --git a/src/utils/calc.rs b/src/utils/calc.rs index 0cb824f..97c1ba1 100644 --- a/src/utils/calc.rs +++ b/src/utils/calc.rs @@ -234,6 +234,21 @@ impl SlotCalculator { self.point_within_slot(chrono::Utc::now().timestamp() as u64) } + /// The current number of milliseconds into the slot. + /// + /// Truncates the millisecond timestamp to seconds, then uses that to + /// calculate the point within the slot, adding back the milliseconds + /// remainder to the final result to provide millisecond precision. + pub fn current_point_within_slot_ms(&self) -> Option { + // NB: Only fetch from now() object once and reuse + let timestamp_ms = chrono::Utc::now().timestamp_millis() as u64; + let timestamp_s = timestamp_ms / 1000; + let fractional = timestamp_ms % 1000; + + self.point_within_slot(timestamp_s) + .map(|point| point * 1000 + fractional) + } + /// Calculates the slot that starts at the given timestamp. /// Returns `None` if the timestamp is not a slot boundary. /// Returns `None` if the timestamp is before the chain's start timestamp.