2021-10-12 13:12:54 +00:00
|
|
|
package utils
|
|
|
|
|
2022-12-09 03:08:04 +00:00
|
|
|
import (
|
|
|
|
"time"
|
2023-11-07 19:48:43 +00:00
|
|
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
2022-12-09 03:08:04 +00:00
|
|
|
)
|
2021-10-12 13:12:54 +00:00
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// GetUnixEpochFrom converts a time into a unix timestamp with nanoseconds
|
2023-11-07 19:48:43 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:09:48 +00:00
|
|
|
// 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
|
2023-11-07 19:48:43 +00:00
|
|
|
func GetUnixEpoch(timesource ...Timesource) *int64 {
|
2022-12-09 03:08:04 +00:00
|
|
|
if len(timesource) != 0 {
|
|
|
|
return GetUnixEpochFrom(timesource[0].Now())
|
|
|
|
}
|
2023-09-11 14:24:05 +00:00
|
|
|
|
|
|
|
return GetUnixEpochFrom(time.Now())
|
2021-10-12 13:12:54 +00:00
|
|
|
}
|