首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将google.protobuf.Timestamp设置为空?

如何将google.protobuf.Timestamp设置为空?
EN

Stack Overflow用户
提问于 2022-05-12 03:35:20
回答 2查看 2.7K关注 0票数 0

如何在Google ProtoBuf中表示空时间戳?

我正在读取来自postgreSQL数据库的DateTime(其中一些是空的)到protobuf TimeStamps中。

代码语言:javascript
运行
复制
message test {
  google.protobuf.TimestampValue TransactionTime =1;
}

不幸的是,没有像google.protobuf.TimestampValue这样的动物。

任何帮助都是非常感谢的。

提亚

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-05-18 14:51:09

正如@JayantSeth所指出的,您应该使用google.protobuf.Timestamp

由于Protobuf禁止将字段设置为null,所以可以使用默认值在应用程序中表示nullmessage Timestamp中有两个字段

代码语言:javascript
运行
复制
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;
}

int64int32类型的默认值都是0。因此,如果从Postgres读取的值为null,则可以将其设置为0。

代码语言:javascript
运行
复制
Timestamp timestamp;
timestamp.set_seconds(0);
timestamp.set_nanos(0);

在您的应用程序中,您可以将timestamp(0)视为null

票数 3
EN

Stack Overflow用户

发布于 2022-11-16 23:32:29

我同意拉姆齐的回答。在Python中,检查null的一个好方法是检查这样的秒或纳秒字段:

代码语言:javascript
运行
复制
from google.protobuf import timestamp_pb2
timestamp = timestamp_pb2.Timestamp()
 
if timestamp.seconds:
    # A valid non-null date was passed
else:
    # The date field was left empty; act accordingly.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72209861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档