【问题标题】:How to convert .net Datetime object to google.protobuf.Timestamp如何将 .net Datetime 对象转换为 google.protobuf.Timestamp
【发布时间】:2020-12-15 06:27:16
【问题描述】:

我正在处理的项目中使用 gRPC 连接,但在将选定的 Datetime 对象转换为 google.protobuf.Timestamp 时遇到问题 每次我将 Datetime 映射到 google.protobuf.Timestamp 时,映射的结果都是 google.protobuf.Timestamp 的最小值,即 (1970-01-01T00:00:00Z.)

这是我正在使用的映射

CreateMap<DateTime, Google.Protobuf.WellKnownTypes.Timestamp>().ConvertUsing(x => Timestamp.FromDateTime(DateTime.SpecifyKind(x, DateTimeKind.Utc)));
CreateMap<Google.Protobuf.WellKnownTypes.Timestamp, DateTime>().ConvertUsing(x => x.ToDateTime());

【问题讨论】:

  • 问题:您使用的是 Google protobuf/grpc 实现,还是 protobuf-net?他们都将定义这种类型,但方式略有不同
  • 导入“google/protobuf/timestamp.proto”;我在我的 proto 文件中使用它
  • 两者都适用;让我换个说法 - 你这里有没有人可以运行的最小再现,显示你看到的结果?

标签: asp.net-core protocol-buffers grpc


【解决方案1】:

使用Google.Protobuf.WellKnownTypes

Timestamp ts = Timestamp.FromDateTime(datetime)

【讨论】:

    猜你喜欢
    • 2017-05-31
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 2021-05-23
    相关资源
    最近更新 更多