@@ -244,6 +244,28 @@ impl SlotCalculator {
244
244
pub fn current_point_within_slot ( & self ) -> Option < u64 > {
245
245
self . point_within_slot ( chrono:: Utc :: now ( ) . timestamp ( ) as u64 )
246
246
}
247
+
248
+ /// Calculates the slot that starts at the given timestamp.
249
+ /// If given a timestamp that is NOT a slot boundary, it will return
250
+ /// `None`.
251
+ pub const fn slot_starting_at ( & self , timestamp : u64 ) -> Option < usize > {
252
+ if timestamp % self . slot_duration != 0 {
253
+ return None ;
254
+ }
255
+ self . slot_containing ( timestamp)
256
+ }
257
+
258
+ /// Calculates the slot that ends at the given timestamp.
259
+ /// If given a timestamp that is NOT a slot boundary, it will return
260
+ /// `None`.
261
+ pub fn slot_ending_at ( & self , timestamp : u64 ) -> Option < usize > {
262
+ if timestamp % self . slot_duration != 0 {
263
+ return None ;
264
+ }
265
+ self . slot_containing ( timestamp)
266
+ . and_then ( |slot| slot. checked_sub ( 1 ) )
267
+ . and_then ( |slot| if slot == 0 { None } else { Some ( slot) } )
268
+ }
247
269
}
248
270
249
271
#[ cfg( test) ]
@@ -265,10 +287,16 @@ mod tests {
265
287
assert_eq ! ( calculator. slot_containing( 1 ) , None ) ;
266
288
assert_eq ! ( calculator. slot_containing( 11 ) , None ) ;
267
289
290
+ assert_eq ! ( calculator. slot_ending_at( 12 ) , None ) ;
291
+ assert_eq ! ( calculator. slot_starting_at( 12 ) , Some ( 1 ) ) ;
268
292
assert_eq ! ( calculator. slot_containing( 12 ) , Some ( 1 ) ) ;
269
293
assert_eq ! ( calculator. slot_containing( 13 ) , Some ( 1 ) ) ;
294
+ assert_eq ! ( calculator. slot_starting_at( 13 ) , None ) ;
270
295
assert_eq ! ( calculator. slot_containing( 23 ) , Some ( 1 ) ) ;
296
+ assert_eq ! ( calculator. slot_ending_at( 23 ) , None ) ;
271
297
298
+ assert_eq ! ( calculator. slot_ending_at( 24 ) , Some ( 1 ) ) ;
299
+ assert_eq ! ( calculator. slot_starting_at( 24 ) , Some ( 2 ) ) ;
272
300
assert_eq ! ( calculator. slot_containing( 24 ) , Some ( 2 ) ) ;
273
301
assert_eq ! ( calculator. slot_containing( 25 ) , Some ( 2 ) ) ;
274
302
assert_eq ! ( calculator. slot_containing( 35 ) , Some ( 2 ) ) ;
0 commit comments