如何在GoLang中解析ISO 8601时间戳?

Jon*_*ant 10 time timestamp go

我知道我需要在GoLang中使用时间布局(如https://golang.org/src/time/format.go所示),但我找不到ISO 8601时间戳的布局.

如果有帮助,我会从Facebook API获取时间戳.这是一个示例时间戳:2016-07-25T02:22:33+0000

Jon*_*ant 14

我发现这个布局有效:"2006-01-02T15:04:05-0700"

  • `time.RFC3339`不适用于ISO 8601.示例:https://play.golang.org/p/gIi7D5KuWa (3认同)
  • 这是不正确的;它是 ISO 8601 的 RFC 3339 子集。值得注意的是,它无法解析 20060102T150405Z。 (3认同)

And*_*eKR 8

The problem here is that RFC3339 requires the zone offset to be given as "+00:00" (or "Z" in case of UTC) while ISO8601 allows it to be "+0000".

From RFC3339:

[...]

time-numoffset  = ("+" / "-") time-hour ":" time-minute
time-offset     = "Z" / time-numoffset

[...]

full-time       = partial-time time-offset
date-time       = full-date "T" full-time
Run Code Online (Sandbox Code Playgroud)

So instead of the time.RFC3339 layout

"2006-01-02T15:04:05Z07:00"
Run Code Online (Sandbox Code Playgroud)

you have to use:

"2006-01-02T15:04:05Z0700"
Run Code Online (Sandbox Code Playgroud)

  • @thibpat 在时间戳中你不能,但在 *layout string* 中你可以。在这种情况下的“Z” [意味着](https://golang.org/pkg/time/#pkg-constants) 当时区是 UTC 时,应该打印“Z”**而不是**偏移量. (2认同)