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

26 lines
619 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"
)
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 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 {
if len(timesource) != 0 {
return GetUnixEpochFrom(timesource[0].Now())
} else {
return GetUnixEpochFrom(time.Now())
}
2021-10-12 13:12:54 +00:00
}