How can I make Timestamp Protobuff back to my timezone since the github version of timestamp is deprecated?

Viewed 298

Alright so I just realized something when I generate new protobuff file using:

protoc my_file.proto --go_out=./

My .proto struct is somehow looks like:

message MyRequest {
 google.protobuf.Timestamp my_time = 1;
}

If I use the protoc command somewhere around in 2021 I would get:

import timestamp "github.com/golang/protobuf/ptypes/timestamp"

type MyRequest struct {
    MyTime *timestamp.Timestamp `protobuf:"bytes,1,opt,name=my_time,json=myTime,proto3" json:"my_time,omitempty"`
}

But now I 'll get:

import timestamppb "google.golang.org/protobuf/types/known/timestamppb"
    
type MyRequest struct {
    MyTime *timestamppb.Timestamp `protobuf:"bytes,1,opt,name=my_time,json=myTime,proto3" json:"my_time,omitempty"`
}

The main problem is that the old struct presents MyTime in my timezone, but the new one converts it to UTC+0. Anyone know how to solve this problem?

1 Answers

You can see this method is call .UTC() method.

Looking from that method, you can create custom function to return time for your time zone.

this is the example :

func ForMyTimeZone(x *timestamppb.Timestamp) time.Time {
    return time.Unix(int64(x.GetSeconds()), int64(x.GetNanos()))
}
Related