Skip to content

Instantly share code, notes, and snippets.

@caike
Created April 15, 2023 14:13
Show Gist options
  • Save caike/b4c37696f700f7aa5d32c0b87c7a9142 to your computer and use it in GitHub Desktop.
Save caike/b4c37696f700f7aa5d32c0b87c7a9142 to your computer and use it in GitHub Desktop.
Script for calculating time left for current Cardano epoch
#!/bin/bash
getEpoch(){
CCLI=$(which cardano-cli)
"$CCLI" query tip --mainnet | sed -n '3 p'| sed 's/[^0-9]*//g'
}
# Description : Calculation of days, hours, minutes and seconds from time in seconds
timeLeft() {
local T=$1
local D=$((T/60/60/24))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
(( D > 0 )) && printf '%dd ' $D
printf '%02d:%02d:%02d' $H $M $S
}
calculateTimeLeft() {
###################
# Preview Network #
###################
#SHELLEY_TRANS_EPOCH=0;
#BYRON_SLOT_LENGTH=20000;
#BYRON_EPOCH_LENGTH=4320;
#SLOT_LENGTH=1;
#EPOCH_LENGTH=86400;
#BYRON_GENESIS_START_SEC=1666656000;
###########
# Mainnet #
###########
SHELLEY_TRANS_EPOCH=208
BYRON_SLOT_LENGTH=20000
BYRON_EPOCH_LENGTH=21600
SLOT_LENGTH=1
EPOCH_LENGTH=432000
BYRON_GENESIS_START_SEC=1506203091
current_time_sec=$(printf '%(%s)T\n' -1)
echo $(( ((SHELLEY_TRANS_EPOCH * BYRON_SLOT_LENGTH * BYRON_EPOCH_LENGTH) / 1000) + (($(getEpoch) + 1 - SHELLEY_TRANS_EPOCH) * SLOT_LENGTH * EPOCH_LENGTH) - current_time_sec + BYRON_GENESIS_START_SEC ))
}
formattedTimeLeft=$(timeLeft $(calculateTimeLeft))
echo $formattedTimeLeft
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment