<p>I share this code snippet with the converter.</p>
<pre><code>module Week03.Converter ( posixTimeToSlotTestnetConverter ) where 

import Ledger          ( POSIXTime(POSIXTime), Slot(Slot) )
import Ledger.TimeSlot ( SlotConfig(SlotConfig), posixTimeToEnclosingSlot )

posixTimeToSlotTestnetConverter :: POSIXTime -&gt; Slot
posixTimeToSlotTestnetConverter time = slotWhenSlotChangedTo1Sec + posixTimeToEnclosingSlot testnetConf time

timeWhenSlotChangedTo1Sec :: POSIXTime
timeWhenSlotChangedTo1Sec = POSIXTime 1595967616000  -- 2020/07/28 20:20:16 - epoch:74 - slot:1598400 - block:1597133  

slotWhenSlotChangedTo1Sec :: Slot
slotWhenSlotChangedTo1Sec = Slot 1598400

testnetConf :: SlotConfig
testnetConf = SlotConfig 1000 timeWhenSlotChangedTo1Sec

</code></pre>
<p>Kudos to @Travis for <strong>timeWhenSlotChangedTo1Sec</strong> value. See:
<a href="https://cardano.stackexchange.com/questions/6955/when-was-the-slot-duration-set-to-1-sec-on-cardano-testnet">When was the slot duration set to 1 sec on Cardano Testnet?</a></p>

CC BY-SA 4.0
2022-02-02T19:21:56.447Z
2022-02-02T23:52:55.623Z
2022-02-02T23:52:55.623Z
kindofdev
<p>The accepted answer already explains how the calculation must be made and provides a sample code in Haskell; however, I will also leave here a simple snippet of code in Java, removing all the Haskell boilerplate (and can be easily ported to any other language).</p>
<pre><code>/**
 * Converts from POSIX time to slot.
 *
 * @param time The POSIX time to be converted (in seconds).
 *
 * @return The matching slot.
 */
static long posixTimeToSlot(long time)
{
    long slotWhenSlotChangedTo1Sec = 1598400;
    long timeWhenSlotChangedTo1Sec = 1595967616L;

    long remainingSeconds = time - timeWhenSlotChangedTo1Sec;

    return slotWhenSlotChangedTo1Sec + remainingSeconds;
}
</code></pre>

CC BY-SA 4.0
2022-03-12T18:05:40.420Z
2022-03-12T18:05:40.420Z
AngelCastillo
<p>I'd like to obtain the slot number corresponding to a posixtime on Cardano Testnet.</p>
<p>For example, this might be useful if you need to set a slot number in cardano-cli, say building a transaction: <code>cardano-cli transaction build --invalid-before slotNumber</code></p>

<plutus><plutus-pioneer-program><cardano-cli><testnet><slottime>
How to convert posixTime to slot number on Cardano Testnet
# How to convert posixTime to slot number on Cardano Testnet

(Asked by: username_0 on 2022-02-02)

I'd like to obtain the slot number corresponding to a posixtime on Cardano Testnet.

For example, this might be useful if you need to set a slot number in cardano-cli, say building a transaction: `cardano-cli transaction build --invalid-before slotNumber`

**Tags:** plutus, plutus-pioneer-program, cardano-cli, testnet, slottime

---

## 2 answers

### Answer by username_0 on 2022-02-02. Score: 12 | Accepted answer

I share this code snippet with the converter.
module Week03.Converter ( posixTimeToSlotTestnetConverter ) where 

import Ledger          ( POSIXTime(POSIXTime), Slot(Slot) )
import Ledger.TimeSlot ( SlotConfig(SlotConfig), posixTimeToEnclosingSlot )

posixTimeToSlotTestnetConverter :: POSIXTime -> Slot
posixTimeToSlotTestnetConverter time = slotWhenSlotChangedTo1Sec + posixTimeToEnclosingSlot testnetConf time

timeWhenSlotChangedTo1Sec :: POSIXTime
timeWhenSlotChangedTo1Sec = POSIXTime 1595967616000  -- 2020/07/28 20:20:16 - epoch:74 - slot:1598400 - block:1597133  

slotWhenSlotChangedTo1Sec :: Slot
slotWhenSlotChangedTo1Sec = Slot 1598400

testnetConf :: SlotConfig
testnetConf = SlotConfig 1000 timeWhenSlotChangedTo1Sec


Kudos to @Travis for timeWhenSlotChangedTo1Sec value. See:
When was the slot duration set to 1 sec on Cardano Testnet?


---

### Answer by username_1 on 2022-03-12. Score: 4

The accepted answer already explains how the calculation must be made and provides a sample code in Haskell; however, I will also leave here a simple snippet of code in Java, removing all the Haskell boilerplate (and can be easily ported to any other language).
/**
 * Converts from POSIX time to slot.
 *
 * @param time The POSIX time to be converted (in seconds).
 *
 * @return The matching slot.
 */
static long posixTimeToSlot(long time)
{
    long slotWhenSlotChangedTo1Sec = 1598400;
    long timeWhenSlotChangedTo1Sec = 1595967616L;

    long remainingSeconds = time - timeWhenSlotChangedTo1Sec;

    return slotWhenSlotChangedTo1Sec + remainingSeconds;
}


---

