使用 google.protobuf.Timestamp 在 Go 中解析带有时区偏移的日期时间戳
Parsing datetimestamps with timezone offset in Go using google.protobuf.Timestamp
我正在创建一个将使用 GRPC 和 protobuf 的 Go 应用程序。我的 RPC 服务应获取包含类型 google.protobuf.Timestamp
的消息,对其进行解析并最终将其保存在数据库中或对其执行更多操作。
我对什么被认为是类型 google.protobuf.Timestamp
的有效输入感到困惑。我希望对具有时区偏移的日期时间戳使用以下格式。
2019-02-15T13:00:00+01:00
这是我正在使用的原型文件。
syntax = "proto3"
package example;
import "google/protobuf/timestamp.proto"
service Tester {
rpc ParseDateTimeStamp(TSRequest) returns (TSReply) {}
}
message TSRequest {
google.protobuf.Timestamp dts = 1;
}
message TSReply {
string message = 1;
}
问题是当我向包含日期时间戳的 GRPC 服务器发送消息时。我希望给定的 2019-02-15T13:00:00+01:00
日期时间戳的类型 *tsbp.Timestamp
是有效的,并给我从纪元开始的适当秒数。 (从 timestamp.go 调用 GetSeconds()
后)
为上面的示例输入调用 ptypes.TimestampString(ts *tspb.Timestamp)
returns 1970-01-01T00:00:00Z
。
google.protobuf.Timestamp
是否接受带 +- 偏移量的日期时间戳?
或者我是否必须输入字符串类型,然后使用 time.Format
解析为 time.Time
,而不是使用 protobuf 中的时间戳变量类型?如果是这样,你能提供一个例子吗?
google.protobuf.Timestamp 的 gRPC 消息类型在内部只有两个 int64
的
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;
}
所以在这种格式类型中,没有什么可以解析。
一个人通常需要:
- 像您的
2019-02-15T13:00:00+01:00
一样的字符串格式,并使用 time.Parse
转换为 time.Time
- 然后使用 ptypes.TimestampProto()
将 time.Time
转换为 *tspb.Timestamp
仅供参考,在您引用的输出中,您看到了一个 zero
时间戳(即秒和纳秒均为零)- 因此 "1970-01-01T00:00:00Z"
输出。
执行上面的流程:
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"
注意: ptype.Timestamp
去除了任何时区 - Z
所以 UTC
时间。因此,如果您需要保留 time.Time
的时区,除了 google.protobuf.Timestamp
消息之外,还需要在 gRPC 消息中发送偏移量。
我正在创建一个将使用 GRPC 和 protobuf 的 Go 应用程序。我的 RPC 服务应获取包含类型 google.protobuf.Timestamp
的消息,对其进行解析并最终将其保存在数据库中或对其执行更多操作。
我对什么被认为是类型 google.protobuf.Timestamp
的有效输入感到困惑。我希望对具有时区偏移的日期时间戳使用以下格式。
2019-02-15T13:00:00+01:00
这是我正在使用的原型文件。
syntax = "proto3"
package example;
import "google/protobuf/timestamp.proto"
service Tester {
rpc ParseDateTimeStamp(TSRequest) returns (TSReply) {}
}
message TSRequest {
google.protobuf.Timestamp dts = 1;
}
message TSReply {
string message = 1;
}
问题是当我向包含日期时间戳的 GRPC 服务器发送消息时。我希望给定的 2019-02-15T13:00:00+01:00
日期时间戳的类型 *tsbp.Timestamp
是有效的,并给我从纪元开始的适当秒数。 (从 timestamp.go 调用 GetSeconds()
后)
为上面的示例输入调用 ptypes.TimestampString(ts *tspb.Timestamp)
returns 1970-01-01T00:00:00Z
。
google.protobuf.Timestamp
是否接受带 +- 偏移量的日期时间戳?
或者我是否必须输入字符串类型,然后使用 time.Format
解析为 time.Time
,而不是使用 protobuf 中的时间戳变量类型?如果是这样,你能提供一个例子吗?
google.protobuf.Timestamp 的 gRPC 消息类型在内部只有两个 int64
的
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;
}
所以在这种格式类型中,没有什么可以解析。
一个人通常需要:
- 像您的
2019-02-15T13:00:00+01:00
一样的字符串格式,并使用time.Parse
转换为 - 然后使用 ptypes.TimestampProto() 将
time.Time
time.Time
转换为 *tspb.Timestamp
仅供参考,在您引用的输出中,您看到了一个 zero
时间戳(即秒和纳秒均为零)- 因此 "1970-01-01T00:00:00Z"
输出。
执行上面的流程:
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"
注意: ptype.Timestamp
去除了任何时区 - Z
所以 UTC
时间。因此,如果您需要保留 time.Time
的时区,除了 google.protobuf.Timestamp
消息之外,还需要在 gRPC 消息中发送偏移量。