I have a time.Time variable in Golang 10-30 00:49:07.1236
that needs to be converted to a Go Protobuf timestamp.Timestamp. Any idea on what functions can be used to accomplish this? Or am I looking at this from the wrong angle?
Asked
Active
Viewed 1.3k times
13

FestiveHydra235
- 473
- 1
- 7
- 23
-
Recommend using `godoc` or going to pkg.go.dev to read the documentation for the libraries you are using. For example, go to https://pkg.go.dev/google.golang.org/protobuf/types/known/timestamppb#Timestamp – Dietrich Epp Nov 03 '21 at 02:57
2 Answers
18
See New
and Timestamp.AsTime
in timestamppb
These support conversion to/from time.Time
and Timestamp

DazWilkin
- 32,823
- 5
- 47
- 88
-2
Below code will give current time in timestamppb format
import "google.golang.org/protobuf/types/known/timestamppb"
timeNow := timestamppb.Now()
Below will return time in time.Time format
timeNow.GetCurrentTime().AsTime()

Sharath BJ
- 1,393
- 1
- 12
- 16