由于来自 Bigquery 数据库的查询响应,我必须使用 Civil.Datetime。
如何从 Golang 中的 Civil.Datetime 获取 time.Time 日期?
就像是:
var date time.Time = civil.ToTime(myCivilTime)
Run Code Online (Sandbox Code Playgroud)
我检查了民用时间文档:https : //godoc.org/cloud.google.com/go/civil并且有一个功能似乎可以满足我的要求。
func (d Date) In(loc *time.Location) time.Time
Run Code Online (Sandbox Code Playgroud)
但我不确定我应该如何设置 *time.Location 参数。
使用time.Location对您的应用程序有意义的任何内容。例如,
package main
import (
"fmt"
"time"
"cloud.google.com/go/civil"
)
func main() {
now := time.Now().Round(0)
fmt.Println(now, " : time Now")
d := civil.DateTimeOf(now)
fmt.Println(d, " : civil DateTime")
t := d.In(time.UTC)
fmt.Println(t, " : time UTC")
t = d.In(time.Local)
fmt.Println(t, " : time Local")
pacific, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
fmt.Println(err)
return
}
t = d.In(pacific)
fmt.Println(t, " : time Pacific")
}
Run Code Online (Sandbox Code Playgroud)
输出:
2018-07-03 12:46:15.728611385 -0400 EDT : time Now
2018-07-03T12:46:15.728611385 : civil DateTime
2018-07-03 12:46:15.728611385 +0000 UTC : time UTC
2018-07-03 12:46:15.728611385 -0400 EDT : time Local
2018-07-03 12:46:15.728611385 -0700 PDT : time Pacific
Run Code Online (Sandbox Code Playgroud)