英文:
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)"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论