Skip to content
Open
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
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 remainder = timestamp_ms - timestamp_s;

self.point_within_slot(timestamp_s)
.map(|point| std::time::Duration::from_secs(point).as_millis() as u64 + remainder)
}

/// 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