10

Is it possible to calculate a timestamp from a slot no? Since one slot is one second it should be pretty easy. But...

The current slot now (Tue 18 May 2021 05:46:04 PM CEST) is 29786450.

In mainnet-shelley-genesis.json i can see systemStart "2017-09-23T21:44:51Z"

Which is acually 1506203091 as unix timestamp.

If i calculate 1506203091 + 29786450 i get 1535989541 which is Sep 3, 2018, 5:45:41 PM something in 2018.

Is there a simple formular? It seems that the actual start date must be something like June 7, 2020 but i cannot find anything abount this date.

3 Answers 3

10

You did not account for Byron, where one slot was 20 seconds.

To make it easier, take the start time of epoch 209 which is the Shelley hardfork and started at 1596491091 unix time on slot 4924800.

Therefore to calculate the time of the current slot, you might try something like: 1596491091 + (current_slot - 4924800).

6
  • thank you, is this timestamp documented anywhere?
    – wutzebaer
    May 20, 2021 at 6:44
  • Not directly documented, it's on chain, I used blockfrost to retrieve it: cardano-mainnet.blockfrost.io/api/v0/epochs/209 (need blockfrost.io token). May 21, 2021 at 12:49
  • Hi i cannot accept your answer, because if i calculate 1596491091+29786450=1626277541 => 14.07.2021 - 17:45:41 which is far away from Tue 18 May what am i doing wrong?
    – wutzebaer
    May 28, 2021 at 20:28
  • 1
    Oh right, the epoch 209 started on slot 4924800. So you need to do: 1596491091+(current_slot - 4924800), I have edited my answer to mention this. May 28, 2021 at 20:36
  • looks good, thank you
    – wutzebaer
    May 28, 2021 at 20:52
2

Note this answer could change if a hard fork changes parameters.

The Shelly unix time is the start time of the Shelly era and the Shelly slot is the corresponding slot. We use these values to ignore the Bryon era (with odd slot value).

Calculate a slot: (returns the Cardano slot)

UNIX_TARGET_TIME - 1596491091+ 4924800

Check a slot: (returns unix timestamp)

1596491091 + (SLOT - 4924800)

Python example:

from datetime import datetime

SHELLY_UNIX = 1596491091
SHELLY_SLOT = 4924800

def getSlotFromUnixTime(ut):
    return ut - SHELLY_UNIX + SHELLY_SLOT

def getUTCEstimateFromSlot(slot):
    ts = SHELLY_UNIX + (slot - SHELLY_SLOT)
    print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))

source code: https://github.com/pixel-pool/PythonCardanoSlotsScript

1

Calculating a timestamp from a slot number is risky!
Theoretically, the slot duration could be changed by a hard fork for example because of performance reasons (as has already happened). So it's probably best to use an oracle for now.

But because time is used so frequently, it would probably be best if Cardano would support a more reliable native solution that avoids oracle fees.

3
  • ok thank you i thought slotnumbers were a reliable timestamp
    – wutzebaer
    May 22, 2021 at 8:44
  • 3
    That is very unlikely. This is the reason that the coefficient number was introduced. This parameter will be used to tune the network pace, not the slot number. The reason for the change in Shelley was this exact bad design, that is now fixed. May 23, 2021 at 8:46
  • Also, Cardano will likely lean into having to use an oracle, as evidenced by their explicit relying on a random global oracle for Ouroboros Chronos, and their overall trend towards distributed systems, not just distributed payment channels.
    – zhekson
    Nov 24, 2021 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.