Skip to content

Commit 623dd37

Browse files
authored
feat: adds support for millisecond slot timepoints (#94)
feat: adds support for millisecond slot timepoints
1 parent fa220b8 commit 623dd37

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "init4-bin-base"
33

44
description = "Internal utilities for binaries produced by the init4 team"
55
keywords = ["init4", "bin", "base"]
6-
version = "0.18.0-rc.0"
6+
version = "0.18.1-rc.0"
77
edition = "2021"
88
rust-version = "1.85"
99
authors = ["init4", "James Prestwich", "evalir"]

src/utils/calc.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,21 @@ impl SlotCalculator {
234234
self.point_within_slot(chrono::Utc::now().timestamp() as u64)
235235
}
236236

237+
/// The current number of milliseconds into the slot.
238+
///
239+
/// Truncates the millisecond timestamp to seconds, then uses that to
240+
/// calculate the point within the slot, adding back the milliseconds
241+
/// remainder to the final result to provide millisecond precision.
242+
pub fn current_point_within_slot_ms(&self) -> Option<u64> {
243+
// NB: Only fetch from now() object once and reuse
244+
let timestamp_ms = chrono::Utc::now().timestamp_millis() as u64;
245+
let timestamp_s = timestamp_ms / 1000;
246+
let fractional = timestamp_ms % 1000;
247+
248+
self.point_within_slot(timestamp_s)
249+
.map(|point| point * 1000 + fractional)
250+
}
251+
237252
/// Calculates the slot that starts at the given timestamp.
238253
/// Returns `None` if the timestamp is not a slot boundary.
239254
/// Returns `None` if the timestamp is before the chain's start timestamp.

0 commit comments

Comments
 (0)