Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
15 changes: 15 additions & 0 deletions src/utils/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> {
// 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.
Expand Down