使用时间包在 Go 中解析时间偏移

我正在尝试在 Go 中使用 Time 包来提取时间详细信息,如下所示。我已经能够成功解析年、月、日、小时、分钟和秒等值。不幸的是,当我尝试使用 Zone 来提取偏移量时,我似乎得到了不正确的偏移量。


当我尝试查看我的时间对象时,我看到两个偏移条目,不确定我做错了什么。


 serverTime := "2021-10-31T22:17:03.996-0700"

    fmt.Println("Server time is: ", serverTime)

    t, _ := time.Parse("2006-01-02T15:04:05.999-0700", serverTime)

    zone, offset := t.Zone()

    fmt.Println("Printing time object: ",t)

    fmt.Println("Year", t.Year())

    fmt.Println("Month", t.Month().String())

    fmt.Println("Date", t.Day())

    fmt.Println("Hour", t.Hour())

    fmt.Println("Minutes", t.Minute())

    fmt.Println("Seconds",t.Second())


    fmt.Println("Zone:", zone)

    fmt.Println("Offset", offset)

偏移量的输出是:偏移量-25200,我希望它是-0700

这是游乐场的链接


缥缈止盈
浏览 113回答 1
1回答

长风秋雁

func(Time) Zone返回第二个参数,offset它是 UTC 以东的秒数。所以你的偏移量-0700返回为-25200- (7 * 60 * 60)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go