github.com/prysmaticlabs/prysm@v1.4.4/shared/slotutil/slottime.go (about) 1 package slotutil 2 3 import ( 4 "time" 5 6 types "github.com/prysmaticlabs/eth2-types" 7 "github.com/prysmaticlabs/prysm/shared/params" 8 "github.com/prysmaticlabs/prysm/shared/timeutils" 9 ) 10 11 // SlotStartTime returns the start time in terms of its unix epoch 12 // value. 13 func SlotStartTime(genesis uint64, slot types.Slot) time.Time { 14 duration := time.Second * time.Duration(slot.Mul(params.BeaconConfig().SecondsPerSlot)) 15 startTime := time.Unix(int64(genesis), 0).Add(duration) 16 return startTime 17 } 18 19 // SlotsSinceGenesis returns the number of slots since 20 // the provided genesis time. 21 func SlotsSinceGenesis(genesis time.Time) types.Slot { 22 if genesis.After(timeutils.Now()) { // Genesis has not occurred yet. 23 return 0 24 } 25 return types.Slot(uint64(timeutils.Since(genesis).Seconds()) / params.BeaconConfig().SecondsPerSlot) 26 } 27 28 // EpochsSinceGenesis returns the number of slots since 29 // the provided genesis time. 30 func EpochsSinceGenesis(genesis time.Time) types.Epoch { 31 return types.Epoch(SlotsSinceGenesis(genesis) / params.BeaconConfig().SlotsPerEpoch) 32 } 33 34 // DivideSlotBy divides the SECONDS_PER_SLOT configuration 35 // parameter by a specified number. It returns a value of time.Duration 36 // in milliseconds, useful for dividing values such as 1 second into 37 // millisecond-based durations. 38 func DivideSlotBy(timesPerSlot int64) time.Duration { 39 return time.Duration(int64(params.BeaconConfig().SecondsPerSlot*1000)/timesPerSlot) * time.Millisecond 40 } 41 42 // MultiplySlotBy multiplies the SECONDS_PER_SLOT configuration 43 // parameter by a specified number. It returns a value of time.Duration 44 // in millisecond-based durations. 45 func MultiplySlotBy(times int64) time.Duration { 46 return time.Duration(int64(params.BeaconConfig().SecondsPerSlot)*times) * time.Second 47 } 48 49 // AbsoluteValueSlotDifference between two slots. 50 func AbsoluteValueSlotDifference(x, y types.Slot) uint64 { 51 if x > y { 52 return uint64(x.SubSlot(y)) 53 } 54 return uint64(y.SubSlot(x)) 55 }