File tree Expand file tree Collapse file tree 1 file changed +10
-5
lines changed
Expand file tree Collapse file tree 1 file changed +10
-5
lines changed Original file line number Diff line number Diff line change @@ -235,13 +235,18 @@ impl SlotCalculator {
235235 }
236236
237237 /// 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.
238242 pub fn current_point_within_slot_ms ( & self ) -> Option < u64 > {
239- let now = chrono:: Utc :: now ( ) ;
240- let timestamp = now. timestamp ( ) as u64 ;
241- let millis = now. timestamp_subsec_millis ( ) as 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 remainder = timestamp_ms - timestamp_s;
242247
243- self . point_within_slot ( timestamp )
244- . map ( |point| std:: time:: Duration :: from_secs ( point) . as_millis ( ) as u64 + millis )
248+ self . point_within_slot ( timestamp_s )
249+ . map ( |point| std:: time:: Duration :: from_secs ( point) . as_millis ( ) as u64 + remainder )
245250 }
246251
247252 /// Calculates the slot that starts at the given timestamp.
You can’t perform that action at this time.
0 commit comments