我不明白这个行为(或文档): https://play.golang.org/p/vz2UTz-3Yy
在 Playground 上,它返回预期的结果:
t = 2015-06-01 00:00:00 +0000 UTC
t.Location() = UTC
parsed = 2015-06-01 00:00:00 +0000 UTC
parsed.Location() = UTC
在我的 mac 上,我得到:
t = 2015-06-01 00:00:00 +0000 +0000
t.Location() =
parsed = 2015-06-01 00:00:00 +0000 +0000
parsed.Location() =
问题是,如果我创建一个日期
time.Date(2015, time.June, 01, 00, 0, 0, 0, time.UTC)
这两个时间是不同的,因为一个有位置(“UTC”),另一个没有(或“本地”)。我有点迷路了。
谢谢
最佳答案
When parsing a time with a zone offset like -0700, if the offset corresponds to a time zone used by the current location (Local), then Parse uses that location and zone in the returned time. Otherwise it records the time as being in a fabricated location with time fixed at the given zone offset. [time.Parse]
t.Location
(名称)仅在本地偏移量与解析日期字符串中的偏移量匹配时设置。您可能设置了不同的时区。
因此记录了偏移量但没有查找位置。
关于parsing - 为什么 go 使用本地位置而不是 UTC 解析我的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39539210/