Golang:如何将 time.Time 转换为 Protobuf 的 Timestamp?

huangapple go评论76阅读模式
英文:

Golang: How to Convert time.Time to a Protobuf Timestamp?

问题

我可以帮你翻译这段内容。以下是翻译的结果:

我在 Golang 中有一个 time.Time 变量 10-30 00:49:07.1236,需要将其转换为 Go 的 Protobuf timestamp.Timestamp。你知道有哪些函数可以实现这个转换吗?或者我是从错误的角度来看待这个问题吗?

英文:

I have a time.Time variable in Golang 10-30 00:49:07.1236 that needs to be converted to a Go Protobuf timestamp.Timestamp. Any idea on what functions can be used to accomplish this? Or am I looking at this from the wrong angle?

答案1

得分: 18

请参考timestamppb中的NewTimestamp.AsTime函数。它们支持time.TimeTimestamp之间的转换。

英文:

See New and Timestamp.AsTime in timestamppb

These support conversion to/from time.Time and Timestamp

答案2

得分: -2

以下代码将以timestamppb格式返回当前时间:

import "google.golang.org/protobuf/types/known/timestamppb"
timeNow := timestamppb.Now()

以下代码将以time.Time格式返回时间:

timeNow.GetCurrentTime().AsTime()
英文:

Below code will give current time in timestamppb format

import "google.golang.org/protobuf/types/known/timestamppb"
timeNow := timestamppb.Now()

Below will return time in time.Time format

timeNow.GetCurrentTime().AsTime()

huangapple
  • 本文由 发表于 2021年11月3日 10:11:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/69818905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定