go timestamppb 与 time互转

1,143 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

go proto 微服务接入进来的gogole timestamp格式

github.com/protocolbuf…

time转成pb

import "google.golang.org/protobuf/types/known/timestamppb"
timestamppb.New(message.sendTime) //返回成pb格式的

pb转成time

import time
timeStamp := req.GetSendTime()//请求进来的
time.Unix(timeStamp.GetSeconds(), 0)
func test() {
   now := time.Now() //获取当前时间
   fmt.Printf("current time:%v\n", now)
   timestamp1 := now.Unix()     //时间戳
   timestamp2 := now.UnixNano() //纳秒时间戳
   fmt.Printf("current timestamp1:%v\n", timestamp1)
   fmt.Printf("current timestamp2:%v\n", timestamp2)
   timeObj := time.Unix(timestamp1, 0) //将时间戳转为时间格式
   fmt.Println(timeObj)
}