Skip to content

Instantly share code, notes, and snippets.

@ydm
Last active May 1, 2024 17:17
Show Gist options
  • Save ydm/ecd0def3f538965413972fb241615dc3 to your computer and use it in GitHub Desktop.
Save ydm/ecd0def3f538965413972fb241615dc3 to your computer and use it in GitHub Desktop.
import datetime
GENESIS_SLOT = 0
GENESIS_TIME = 1606824023
SECONDS_PER_SLOT = 12
def compute_slot_at_timestamp(ts):
time_since_genesis = ts - GENESIS_TIME
return GENESIS_SLOT + time_since_genesis//SECONDS_PER_SLOT
def compute_timestamp_at_slot(slot):
slots_since_genesis = slot - GENESIS_SLOT
return GENESIS_TIME + slots_since_genesis*SECONDS_PER_SLOT
def compute_slot_at_time(dt):
ts = int(dt.timestamp())
return compute_slot_at_timestamp(ts)
def compute_time_at_slot(slot):
ts = compute_timestamp_at_slot(slot)
return datetime.datetime.utcfromtimestamp(ts).replace(tzinfo=datetime.timezone.utc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment