gRPC 消息类型google.protobuf.时间戳 https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/timestamp.proto,内部只有两个int64
's
message Timestamp {
// Represents seconds of UTC time since Unix epoch
// 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
// 9999-12-31T23:59:59Z inclusive.
int64 seconds = 1;
// Non-negative fractions of a second at nanosecond resolution. Negative
// second values with fractions must still have non-negative nanos values
// that count forward in time. Must be from 0 to 999,999,999
// inclusive.
int32 nanos = 2;
}
所以当采用这种格式类型时,没有什么可以parse.
通常需要:
- 像你这样的字符串格式
2019-02-15T13:00:00+01:00
并转换为time.Time
using time.Parse
- 然后转换
time.Time
to a *tspb.Timestamp
using ptypes.TimestampProto() https://godoc.org/github.com/golang/protobuf/ptypes#TimestampProto
仅供参考,在您引用的输出中,您会看到zero
时间戳(即秒和纳秒都为零) - 因此"1970-01-01T00:00:00Z"
output.
实现上面的流程:
ts, err := time.Parse(time.RFC3339, "2019-02-15T13:00:00+01:00")
pbts, err := ptypes.TimestampProto(ts) // ptypes.Timestamp:"seconds:1550232000 "
fmt.Println(ptypes.TimestampString(pbts)) // "2019-02-15T12:00:00Z"
操场 https://play.golang.org/p/UABbRDSYb_y
Note: the ptype.Timestamp
被剥夺了任何时区 -Z
so UTC
时间。因此,如果您需要保留时区time.Time
,除了您的 gRPC 消息之外,还需要在您的 gRPC 消息中发送偏移量google.protobuf.Timestamp
信息。