Go时间解析返回不同的值

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

Go time parse returning different values

问题

我遇到了一个问题,Go的时间解析在同一个时区中返回了两个不同的值。

func timeParse() {
    layout := "Mon, 2 Jan 2006 03:04:05 -0700 (MST)"

    value1 := "Mon, 18 Jan 2016 01:48:52 -0800 (PST)"
    value2 :=  "Tue, 19 Jan 2016 17:49:33 -0800 (PST)"

    t1, _ := time.Parse(layout, value1)
    fmt.Println(t1)

    t2, _ := time.Parse(layout, value2)
    fmt.Println(t2)
}

输出结果:

2016-01-18 01:48:52 -0800 PST
0001-01-01 00:00:00 +0000 UTC

注意第二个时间没有正确解析。

英文:

I'm facing an issue where Go time parse is returning different values for two times in the same timezone.

func timeParse() {
    layout := "Mon, 2 Jan 2006 03:04:05 -0700 (MST)"

    value1 := "Mon, 18 Jan 2016 01:48:52 -0800 (PST)"
    value2 :=  "Tue, 19 Jan 2016 17:49:33 -0800 (PST)"

	t1, _ := time.Parse(layout, value1)
    fmt.Println(t1)

    t2, _ := time.Parse(layout, value2)
    fmt.Println(t2)
}

Output:

2016-01-18 01:48:52 -0800 PST
0001-01-01 00:00:00 +0000 UTC

Notice that the second one didn't parse properly.

答案1

得分: 1

发现了我的错误。布局需要一个24小时制的时间。
修正为:

layout := "Mon, 2 Jan 2006 15:04:05 -0700 (MST)"
英文:

Found my mistake. The layout expects a 24 hour times.
Fixed with:

layout := "Mon, 2 Jan 2006 15:04:05 -0700 (MST)"

huangapple
  • 本文由 发表于 2016年2月2日 14:31:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/35146579.html
匿名

发表评论

匿名网友

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

确定