Skip to content
Draft
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
10 changes: 10 additions & 0 deletions src/utils/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ impl SlotCalculator {
self.point_within_slot(chrono::Utc::now().timestamp() as u64)
}

/// The current number of milliseconds into the slot.
pub fn current_point_within_slot_ms(&self) -> Option<u64> {
let now = chrono::Utc::now();
let timestamp = now.timestamp() as u64;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let millis = now.timestamp_subsec_millis() as u64;

self.point_within_slot(timestamp)
.map(|point| std::time::Duration::from_secs(point).as_millis() as u64 + millis)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than operating on seconds, and then converting to ms and adding, just do math on the u64s

add

fn point_within_slot_ms
fn slot_utc_offset_ms

etc.

do not concert to Duration as that may mess up the math

}

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