File tree Expand file tree Collapse file tree 1 file changed +25
-3
lines changed
rust/cardano-chain-follower/src Expand file tree Collapse file tree 1 file changed +25
-3
lines changed Original file line number Diff line number Diff line change @@ -170,9 +170,31 @@ impl Network {
170170 ///
171171 /// The Slot does not have to be a valid slot present in the blockchain.
172172 #[ must_use]
173- pub fn time_to_slot ( & self , _time : DateTime < Utc > ) -> Option < u64 > {
174- // TODO: Implement this, for now just return None.
175- None
173+ pub fn time_to_slot ( & self , time : DateTime < Utc > ) -> Option < u64 > {
174+ let genesis = self . genesis_values ( ) ;
175+
176+ let byron_start_time = DateTime :: < Utc > :: from_timestamp ( genesis. byron_known_time as i64 , 0 ) ?;
177+ let byron_slot_length = genesis. byron_slot_length as i64 ;
178+
179+ let shelley_start_time = DateTime :: < Utc > :: from_timestamp ( genesis. shelley_known_time as i64 , 0 ) ?;
180+ let shelley_slot_length = genesis. shelley_slot_length as i64 ;
181+
182+ // determine if the given time is in Byron or Shelley era.
183+ if time < byron_start_time {
184+ return None ;
185+ }
186+
187+ if time < shelley_start_time {
188+ // Byron era
189+ let time_diff = time - byron_start_time;
190+ let elapsed_slots = time_diff. num_seconds ( ) / byron_slot_length;
191+ Some ( genesis. byron_known_slot + elapsed_slots as u64 )
192+ } else {
193+ // Shelley era
194+ let time_diff = time - shelley_start_time;
195+ let elapsed_slots = time_diff. num_seconds ( ) / shelley_slot_length;
196+ Some ( genesis. shelley_known_slot + elapsed_slots as u64 )
197+ }
176198 }
177199}
178200
You can’t perform that action at this time.
0 commit comments