如何解析“2021-01-19T16:20:04+0000"作为时间.时间 [英] How to parse "2021-01-19T16:20:04+0000" as time.Time

查看:19
本文介绍了如何解析“2021-01-19T16:20:04+0000"作为时间.时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下格式,当我阅读 +0000 时区偏移量rel="nofollow noreferrer">https://golang.org/pkg/time/#pkg-constants:

I've tried the following format which should catch the +0000 timezone offset as I read https://golang.org/pkg/time/#pkg-constants:

ts, err := time.Parse("2006-01-02T15:04:05-0700", "2021-01-19T16:20:04+0000")

但是打印结果时看起来很奇怪(没有错误):

But that looks rather strange when the result ist printed (no error):

2021-01-19 16:47:00 +0000 +0000

更新

在本地运行 https://play.golang.org/p/wHBYz7iKnLT (OSX11.1) 给出了奇怪的结果:

Running https://play.golang.org/p/wHBYz7iKnLT locally (OSX 11.1) gives the strange result:

❯ gor time.go 
2021-01-19 16:20:04 +0000 +0000 <nil>

推荐答案

你看到的是默认"格式化,例如当像 fmt.Println(ts) 一样打印时,这是 Time.String().引用自 Time.String():

What you see is the "default" formatting, e.g. when printed like fmt.Println(ts), which is the result of Time.String(). Quoting from Time.String():

String 返回使用格式字符串格式化的时间

String returns the time formatted using the format string

"2006-01-02 15:04:05.999999999 -0700 MST"

如您所见,它最后包含区域偏移量和区域名称.

As you can see, it contains the zone offset and the zone name in the end.

另请注意,您使用了 time.Parse() 哪些文件:

Also note that you used time.Parse() which documents that:

在没有时区指示器的情况下,Parse 返回一个 UTC 时间.

In the absence of a time zone indicator, Parse returns a time in UTC.

解析带有 -0700 等时区偏移量的时间时,如果偏移量对应于当前位置(本地)使用的时区,则 Parse 在返回的时间中使用该位置和时区.否则,它会将时间记录为处于制造位置,时间固定在给定的区域偏移量.

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.

由于在 Go Playground 上本地时间设置为 UTC,其偏移量与您解析的时间的偏移量相匹配,因此将使用该区域,因此将其打印在 Go Playground 上您会看到UTC.

Since on the Go Playground the local time is set to UTC whose offset matches the offset of the time you parse, that zone will be used, so printing it on the Go Playground you'll see UTC.

当您在本地(在您的计算机上)解析它并且本地时区不是 UTC 或另一个偏移量为 0 的区域时,根据文档,该位置将被记录为名称带有偏移量的虚构位置.

When you parse it locally (on your computer) and the local time zone is not UTC or another zone that has 0 offset, as per the doc, that location is recorded as a fabricated location with the name having the offset.

因此,当您在本地解析和打印该时间时,由于默认格式包括区域偏移量和区域名称,并且由于两者都是 +0000,您将看到 +0000 打印了两次.

So when you parse and print that time locally, since the default format includes both the zone offset and the zone name, and since both are +0000, you'll see +0000 printed twice.

不用担心,解析的时间当然是正确的:

Worry not, the parsed time is of course correct:

ts, err := time.Parse("2006-01-02T15:04:05-0700", "2021-01-19T16:20:04+0000")
if err != nil {
    panic(err)
}
fmt.Println(ts)

fmt.Println(ts.Format("2006-01-02 16:04:05 -0700"))
fmt.Println(ts.Format("2006-01-02 16:04:05 MST"))

此输出在 Go Playground:

2021-01-19 16:20:04 +0000 UTC
2021-01-19 16:20:04 +0000
2021-01-19 16:20:04 UTC

本地(有 CET 区域)它输出:

Locally (having CET zone) it outputs:

2021-01-19 16:20:04 +0000 +0000
2021-01-19 16:20:04 +0000
2021-01-19 16:20:04 +0000

请注意,如果您要解析具有不同区域偏移量(不是 0)的时间,您也会在 Go Playground 上看到相同的情况.例如:

Note that if you'd parse a time with a different zone offset (not being 0), you'll see the same on the Go Playground too. For example:

ts, err := time.Parse("2006-01-02T15:04:05-0700", "2021-01-19T16:20:04+1100")
if err != nil {
    panic(err)
}
fmt.Println(ts)

fmt.Println(ts.Format("2006-01-02 16:04:05 -0700"))
fmt.Println(ts.Format("2006-01-02 16:04:05 MST"))

这在 Go Playground 和我本地输出(使用 CET 区):

This outputs both on the Go Playground and locally for me (with CET zone):

2021-01-19 16:20:04 +1100 +1100
2021-01-19 16:20:04 +1100
2021-01-19 16:20:04 +1100

这篇关于如何解析“2021-01-19T16:20:04+0000"作为时间.时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆