/
duration.go
39 lines (32 loc) · 988 Bytes
/
duration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package types
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func RegulatedUnixTime(timestamp uint64) uint64 {
// divide and round down to align to regulated periods
return timestamp / RegulatedPeriod * RegulatedPeriod
}
func RegulatedUnixTimeFromNow(ctx sdk.Context, seconds uint64) uint64 {
timestamp := ctx.BlockTime().Add(time.Duration(seconds) * time.Second).Unix()
return RegulatedUnixTime(uint64(timestamp))
}
func NextRegulatedUnixTime(timestamp uint64) uint64 {
CheckRegulatedUnixTime(timestamp)
if timestamp > MaxUnixTime-RegulatedPeriod {
panic("too large unix time")
}
return timestamp + RegulatedPeriod
}
func PreviousRegulatedUnixTime(timestamp uint64) uint64 {
CheckRegulatedUnixTime(timestamp)
if timestamp < RegulatedPeriod {
panic("too small unix time")
}
return timestamp - RegulatedPeriod
}
func CheckRegulatedUnixTime(timestamp uint64) {
if RegulatedUnixTime(timestamp) != timestamp {
panic("invalid regulated unix time")
}
}