go-waku/waku/v2/utils/time.go

28 lines
661 B
Go
Raw Normal View History

2021-10-12 13:12:54 +00:00
package utils
2022-12-09 03:08:04 +00:00
import (
"time"
"google.golang.org/protobuf/proto"
2022-12-09 03:08:04 +00:00
)
2021-10-12 13:12:54 +00:00
// GetUnixEpochFrom converts a time into a unix timestamp with nanoseconds
func GetUnixEpochFrom(now time.Time) *int64 {
return proto.Int64(now.UnixNano())
2021-10-18 13:48:04 +00:00
}
2022-12-09 03:08:04 +00:00
type Timesource interface {
Now() time.Time
}
// GetUnixEpoch returns the current time in unix timestamp with the integer part
2022-12-09 03:08:04 +00:00
// representing seconds and the decimal part representing subseconds.
// Optionally receives a timesource to obtain the time from
func GetUnixEpoch(timesource ...Timesource) *int64 {
2022-12-09 03:08:04 +00:00
if len(timesource) != 0 {
return GetUnixEpochFrom(timesource[0].Now())
}
return GetUnixEpochFrom(time.Now())
2021-10-12 13:12:54 +00:00
}