Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang: How to Convert time.Time to a Protobuf Timestamp?

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?

like image 665
FestiveHydra235 Avatar asked Sep 13 '25 17:09

FestiveHydra235


2 Answers

See New and Timestamp.AsTime in timestamppb

These support conversion to/from time.Time and Timestamp

like image 161
DazWilkin Avatar answered Sep 15 '25 21:09

DazWilkin


Try using the timestamppb.New

like this:

newTimeStamp:=timestamppb.New(your_time)
like image 45
yashwanth krishna Avatar answered Sep 15 '25 19:09

yashwanth krishna