Last active
May 1, 2024 17:17
-
-
Save ydm/ecd0def3f538965413972fb241615dc3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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