【问题标题】:Parsing time offset in Go using time package使用时间包在 Go 中解析时间偏移
【发布时间】:2021-12-15 22:53:25
【问题描述】:

我正在尝试在 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

这里是playground的链接

【问题讨论】:

    标签: go time


    【解决方案1】:

    func(Time) Zone 返回第二个参数offset,它是UTC 以东的秒数。因此,-0700 的偏移量返回为 -25200,即 - (7 * 60 * 60)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-10
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-22
      • 1970-01-01
      相关资源
      最近更新 更多