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

28 lines
661 B
Go
Raw Normal View History

2021-10-12 09:12:54 -04:00
package utils
2022-12-08 23:08:04 -04:00
import (
"time"
"google.golang.org/protobuf/proto"
2022-12-08 23:08:04 -04:00
)
2021-10-12 09:12:54 -04: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 15:48:04 +02:00
}
2022-12-08 23:08:04 -04:00
type Timesource interface {
Now() time.Time
}
// GetUnixEpoch returns the current time in unix timestamp with the integer part
2022-12-08 23:08:04 -04: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-08 23:08:04 -04:00
if len(timesource) != 0 {
return GetUnixEpochFrom(timesource[0].Now())
}
return GetUnixEpochFrom(time.Now())
2021-10-12 09:12:54 -04:00
}