parsing - 为什么 go 使用本地位置而不是 UTC 解析我的时间戳

标签 parsing go time timestamp-with-timezone

我不明白这个行为(或文档): 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/

相关文章:

javascript - 使用 Javascript 从字符串中解析 '1/2'

go - "selective"goroutines 互斥

go - 我应该如何优化 golang 中的文件轮换?

ruby-on-rails - 如何查找范围是否包含在范围数组中?

parsing - 综合和继承属性

c - 使用 strsep 解析 C 中的字符串(替代方法)

Python HTML 解析与漂亮的汤和过滤停用词

JSON 写入创世 block 失败 : invalid character '\\' looking for beginning of object key string

java - 如何统计线程池中线程的有效执行时间,而不是线程的生命周期?

mysql - 在一个查询中获取每周、每月、每年的数据