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